From 31bdedfb654bfde41e119394cf05fb5ba4576078 Mon Sep 17 00:00:00 2001 From: cyy Date: Tue, 7 Mar 2023 22:40:33 +0800 Subject: [PATCH 1/3] Enable Leak Sanitizer --- .ci/pytorch/test.sh | 4 ++- lsan.supp | 30 ++++++++++++++++++++++ torch/csrc/Module.cpp | 2 +- torch/csrc/autograd/VariableTypeManual.cpp | 4 +-- torch/csrc/autograd/variable.h | 4 +-- 5 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 lsan.supp diff --git a/.ci/pytorch/test.sh b/.ci/pytorch/test.sh index daa258d283fa..5243778f9fb1 100755 --- a/.ci/pytorch/test.sh +++ b/.ci/pytorch/test.sh @@ -216,11 +216,13 @@ 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 - export ASAN_OPTIONS=detect_leaks=0:symbolize=1:detect_stack_use_after_return=true:strict_init_order=true:detect_odr_violation=1:detect_container_overflow=0:check_initialization_order=true:debug=true + export ASAN_OPTIONS=detect_leaks=1:symbolize=1:detect_stack_use_after_return=true:strict_init_order=true:detect_odr_violation=1:detect_container_overflow=0:check_initialization_order=true:debug=true:fast_unwind_on_malloc=1 if [[ "$BUILD_ENVIRONMENT" == *cuda* ]]; then export ASAN_OPTIONS="${ASAN_OPTIONS}:protect_shadow_gap=0" fi export UBSAN_OPTIONS=print_stacktrace=1:suppressions=$PWD/ubsan.supp + # Suppress some hard to solve indirect leaks + export LSAN_OPTIONS="suppressions=$PWD/lsan.supp" export PYTORCH_TEST_WITH_ASAN=1 export PYTORCH_TEST_WITH_UBSAN=1 # TODO: Figure out how to avoid hard-coding these paths diff --git a/lsan.supp b/lsan.supp new file mode 100644 index 000000000000..ff8d109e901c --- /dev/null +++ b/lsan.supp @@ -0,0 +1,30 @@ +leak:pybind11::cpp_function +leak:PyMem_RawMalloc +leak:unicode_resize +leak:PyObject_Malloc +leak:PyByteArray_Resize +leak:numpy +leak:list_append +leak:unicodeobject +leak:obmalloc +leak:gcmodule +leak:listobject +leak:bytesobject +leak:PyThread_allocate_lock +leak:sccache +leak:rustc-1.61.0 +leak:gcc/x86_64-linux-gnu/11 +leak:x86_64-linux-gnu-gcc-11 +leak:libbfd +leak:x86_64-linux-gnu-ld.bfd +leak:git +leak:libio +leak:unknown module +leak:g++ +leak:conda-linux-gnu-ld +leak:crypto +leak:torch::detail::(anonymous namespace)::get_set_cached_attr +leak:torch::jit::tensorexpr::TensorExprKernel::preAllocIntermediateBufs +leak:optree +leak:python +leak:torch::tensors::initialize_aten_types diff --git a/torch/csrc/Module.cpp b/torch/csrc/Module.cpp index 8a8bd79653c8..373ff981a877 100644 --- a/torch/csrc/Module.cpp +++ b/torch/csrc/Module.cpp @@ -237,7 +237,7 @@ static PyObject* THPModule_initExtension( END_HANDLE_TH_ERRORS } -// The idea behind these two functions is to make it easy to test if we are +// The idea behind these functions is to make it easy to test if we are // built with ASAN: they're designed not to crash if ASAN is not enabled, but // to trigger ASAN if it is enabled. This lets us run a "canary" tests which // checks if our build environment is misconfigured. diff --git a/torch/csrc/autograd/VariableTypeManual.cpp b/torch/csrc/autograd/VariableTypeManual.cpp index e270df51221b..4414891fe39c 100644 --- a/torch/csrc/autograd/VariableTypeManual.cpp +++ b/torch/csrc/autograd/VariableTypeManual.cpp @@ -456,7 +456,7 @@ static Tensor detach(c10::DispatchKeySet ks, const Tensor& self) { // NB: we can't make detach() a normal view operator because the codegen // generates allow_tensor_metadata_change = True for them. In the future we // should have an option for this in the codegen. - auto result = as_view( + return as_view( /* base */ self, /* output */ out, /* is_bw_differentiable */ false, @@ -465,8 +465,6 @@ static Tensor detach(c10::DispatchKeySet ks, const Tensor& self) { /* rev_view_func */ nullptr, /* creation_meta */ CreationMeta::DEFAULT, /*allow_tensor_metadata_change=*/false); - - return result; } static Tensor _fw_primal( diff --git a/torch/csrc/autograd/variable.h b/torch/csrc/autograd/variable.h index dfffd3d97095..78baee428af9 100644 --- a/torch/csrc/autograd/variable.h +++ b/torch/csrc/autograd/variable.h @@ -844,7 +844,7 @@ inline Variable make_variable_non_differentiable_view( /*version_counter=*/impl::version_counter(base), /*allow_tensor_metadata_change=*/allow_tensor_metadata_change); data_impl_copy->set_autograd_meta(nullptr); - return Variable(data_impl_copy); + return Variable(std::move(data_impl_copy)); } return Variable(); } @@ -903,7 +903,7 @@ inline Variable make_variable( /*allow_tensor_metadata_change=*/allow_tensor_metadata_change); data_impl_copy->set_autograd_meta(std::make_unique( data_impl_copy.get(), false, std::move(gradient_edge))); - return Variable(data_impl_copy); + return Variable(std::move(data_impl_copy)); } return Variable(); } From b0ba8dd9cdab36053f767203734b53b6559881bb Mon Sep 17 00:00:00 2001 From: cyy Date: Wed, 18 Jun 2025 16:50:29 +0800 Subject: [PATCH 2/3] Add suppression --- lsan.supp | 1 + 1 file changed, 1 insertion(+) diff --git a/lsan.supp b/lsan.supp index ff8d109e901c..dff7d63a2fd5 100644 --- a/lsan.supp +++ b/lsan.supp @@ -28,3 +28,4 @@ leak:torch::jit::tensorexpr::TensorExprKernel::preAllocIntermediateBufs leak:optree leak:python leak:torch::tensors::initialize_aten_types +leak:libclang_rt From 12a702e3fc17dae4fcae1dc195776ece488eaf94 Mon Sep 17 00:00:00 2001 From: cyy Date: Wed, 25 Jun 2025 15:34:09 +0800 Subject: [PATCH 3/3] Don't print suppression --- .ci/pytorch/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/pytorch/test.sh b/.ci/pytorch/test.sh index 5243778f9fb1..869436ac548f 100755 --- a/.ci/pytorch/test.sh +++ b/.ci/pytorch/test.sh @@ -216,7 +216,7 @@ 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 - export ASAN_OPTIONS=detect_leaks=1:symbolize=1:detect_stack_use_after_return=true:strict_init_order=true:detect_odr_violation=1:detect_container_overflow=0:check_initialization_order=true:debug=true:fast_unwind_on_malloc=1 + export ASAN_OPTIONS=detect_leaks=1:symbolize=1:detect_stack_use_after_return=true:strict_init_order=true:detect_odr_violation=1:detect_container_overflow=0:check_initialization_order=true:debug=true:fast_unwind_on_malloc=1:print_suppressions=0 if [[ "$BUILD_ENVIRONMENT" == *cuda* ]]; then export ASAN_OPTIONS="${ASAN_OPTIONS}:protect_shadow_gap=0" fi