diff --git a/.jenkins/pytorch/test.sh b/.jenkins/pytorch/test.sh index 5154f5a3f3b3..2c77c1e516bb 100755 --- a/.jenkins/pytorch/test.sh +++ b/.jenkins/pytorch/test.sh @@ -135,9 +135,8 @@ fi # if you're not careful. Check this if you made some changes and the # ASAN test is not working if [[ "$BUILD_ENVIRONMENT" == *asan* ]]; then - # Suppress vptr violations arising from multiple copies of pybind11 export ASAN_OPTIONS=detect_leaks=0:symbolize=1:detect_stack_use_after_return=1:strict_init_order=true:detect_odr_violation=0 - export UBSAN_OPTIONS=print_stacktrace=1:suppressions=$PWD/ubsan.supp + export UBSAN_OPTIONS=print_stacktrace=1 export PYTORCH_TEST_WITH_ASAN=1 export PYTORCH_TEST_WITH_UBSAN=1 # TODO: Figure out how to avoid hard-coding these paths @@ -180,9 +179,10 @@ if [[ "$BUILD_ENVIRONMENT" == *asan* ]]; then ulimit -s 81920 (cd test && python -c "import torch; print(torch.__version__, torch.version.git_version)") - echo "The next three invocations are expected to crash; if they don't that means ASAN/UBSAN is misconfigured" + echo "The next four invocations are expected to crash; if they don't that means ASAN/UBSAN is misconfigured" (cd test && ! get_exit_code python -c "import torch; torch._C._crash_if_csrc_asan(3)") (cd test && ! get_exit_code python -c "import torch; torch._C._crash_if_csrc_ubsan(0)") + (cd test && ! get_exit_code python -c "import torch; torch._C._crash_if_vptr_ubsan()") (cd test && ! get_exit_code python -c "import torch; torch._C._crash_if_aten_asan(3)") fi diff --git a/CMakeLists.txt b/CMakeLists.txt index baf97a455863..b1e0f517fafa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,7 +184,7 @@ cmake_dependent_option( "BUILD_TEST" OFF) option(USE_CPP_CODE_COVERAGE "Compile C/C++ with code coverage flags" OFF) option(USE_COLORIZE_OUTPUT "Colorize output during compilation" ON) -option(USE_ASAN "Use Address Sanitizer" OFF) +option(USE_ASAN "Use Address+Undefined Sanitizers" OFF) option(USE_TSAN "Use Thread Sanitizer" OFF) option(USE_CUDA "Use CUDA" ON) cmake_dependent_option( @@ -928,8 +928,8 @@ if(NOT MSVC) endif() if(USE_ASAN) - string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fsanitize=address") - string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fsanitize=address") + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fsanitize=address -fsanitize=undefined") + string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fsanitize=address -fsanitize=undefined") endif() if(USE_TSAN) diff --git a/test/cpp/jit/test_misc.cpp b/test/cpp/jit/test_misc.cpp index 2aac6cacdffc..3be0b8598b73 100644 --- a/test/cpp/jit/test_misc.cpp +++ b/test/cpp/jit/test_misc.cpp @@ -491,10 +491,20 @@ TEST(ControlFlowTest, Basic) { ASSERT_EQ(256, run_binary("while_test", 2, 0)); } +#if defined(__has_feature) +#if __has_feature(address_sanitizer) +#define HAS_ASANUBSAN 1 +#endif +#endif + +#ifndef HAS_ASANUBSAN +// This test fails vptr UBSAN checks + TEST(ProtoTest, Basic) { ::ONNX_NAMESPACE::ModelProto proto; proto.set_producer_name("foo"); } +#endif // test a few features that are not directly used in schemas yet TEST(SchemaParserTest, NestedArrays) { diff --git a/torch/csrc/Module.cpp b/torch/csrc/Module.cpp index 9c6f2ed4177a..b17d8ae07de7 100644 --- a/torch/csrc/Module.cpp +++ b/torch/csrc/Module.cpp @@ -184,6 +184,25 @@ static PyObject* THPModule_crashIfCsrcUBSAN(PyObject* module, PyObject* arg) { return THPUtils_packInt32((int)y); } +static PyObject* THPModule_crashIfvptrUBSAN(PyObject* module, PyObject* noarg) { + // This code shoud work perfectly fine, as vtables are idential for Foo and + // Baz unless rtti and ubsan are enabled + struct Foo { + virtual int bar() = 0; + virtual ~Foo() = default; + }; + struct Baz { + virtual int bar() { + return 17; + } + virtual ~Baz() = default; + }; + Baz x{}; + auto y = static_cast(static_cast(&x)); + auto rc = y->bar(); + return THPUtils_packInt32(rc); +} + static PyObject* THPModule_crashIfATenASAN(PyObject* module, PyObject* arg) { THPUtils_assert( THPUtils_checkLong(arg), @@ -933,6 +952,7 @@ static PyMethodDef TorchMethods[] = { {"_infer_size", THPModule_inferSize, METH_VARARGS, nullptr}, {"_crash_if_csrc_asan", THPModule_crashIfCsrcASAN, METH_O, nullptr}, {"_crash_if_csrc_ubsan", THPModule_crashIfCsrcUBSAN, METH_O, nullptr}, + {"_crash_if_vptr_ubsan", THPModule_crashIfvptrUBSAN, METH_NOARGS, nullptr}, {"_crash_if_aten_asan", THPModule_crashIfATenASAN, METH_O, nullptr}, {"_show_config", THPModule_showConfig, METH_NOARGS, nullptr}, {"_cxx_flags", THPModule_cxxFlags, METH_NOARGS, nullptr}, diff --git a/ubsan.supp b/ubsan.supp deleted file mode 100644 index 395f5208c843..000000000000 --- a/ubsan.supp +++ /dev/null @@ -1,2 +0,0 @@ -vptr:libtorch_python.so -vptr:test_jit