mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Add UBSAN to ASAN (#88055)
Add undefined behavior sanitizer to `USE_ASAN` option. Added `torch._C._crash_if_vptr_ubsan()` that only fails if vptr belongs to a wrong class after typecast Deleted all ubsan supressions, but disabled `ProtoTest::Basic` as it fails above-mentioned vptr check. Fixes https://github.com/pytorch/pytorch/issues/88042 Pull Request resolved: https://github.com/pytorch/pytorch/pull/88055 Approved by: https://github.com/ezyang
This commit is contained in:
committed by
PyTorch MergeBot
parent
81f74eed75
commit
e1c123d29a
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -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<Foo*>(static_cast<void*>(&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},
|
||||
|
@ -1,2 +0,0 @@
|
||||
vptr:libtorch_python.so
|
||||
vptr:test_jit
|
Reference in New Issue
Block a user