Index: lib/asan/asan_rtl.cc =================================================================== --- lib/asan/asan_rtl.cc +++ lib/asan/asan_rtl.cc @@ -383,6 +383,13 @@ kHighShadowBeg > kMidMemEnd); } +static bool UNUSED __local_asan_dyninit = [] { + MaybeStartBackgroudThread(); + SetSoftRssLimitExceededCallback(AsanSoftRssLimitExceededCallback); + + return false; +}(); + static void AsanInitInternal() { if (LIKELY(asan_inited)) return; SanitizerToolName = "AddressSanitizer"; @@ -457,9 +464,6 @@ allocator_options.SetFrom(flags(), common_flags()); InitializeAllocator(allocator_options); - MaybeStartBackgroudThread(); - SetSoftRssLimitExceededCallback(AsanSoftRssLimitExceededCallback); - // On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited // should be set to 1 prior to initializing the threads. asan_inited = 1; Index: lib/sanitizer_common/sanitizer_common_libcdep.cc =================================================================== --- lib/sanitizer_common/sanitizer_common_libcdep.cc +++ lib/sanitizer_common/sanitizer_common_libcdep.cc @@ -25,7 +25,7 @@ SoftRssLimitExceededCallback = Callback; } -#if SANITIZER_LINUX && !SANITIZER_GO +#if (SANITIZER_LINUX || SANITIZER_NETBSD) && !SANITIZER_GO // Weak default implementation for when sanitizer_stackdepot is not linked in. SANITIZER_WEAK_ATTRIBUTE StackDepotStats *StackDepotGetStats() { return nullptr; @@ -114,7 +114,7 @@ } void MaybeStartBackgroudThread() { -#if SANITIZER_LINUX && \ +#if (SANITIZER_LINUX || SANITIZER_NETBSD) && \ !SANITIZER_GO // Need to implement/test on other platforms. // Start the background thread if one of the rss limits is given. if (!common_flags()->hard_rss_limit_mb && Index: test/sanitizer_common/TestCases/Linux/hard_rss_limit_mb_test.cc =================================================================== --- test/sanitizer_common/TestCases/Linux/hard_rss_limit_mb_test.cc +++ test/sanitizer_common/TestCases/Linux/hard_rss_limit_mb_test.cc @@ -1,44 +0,0 @@ -// Check hard_rss_limit_mb. Not all sanitizers implement it yet. -// RUN: %clangxx -O2 %s -o %t -// -// Run with limit should fail: -// RUN: %env_tool_opts=hard_rss_limit_mb=100 not %run %t 2>&1 | FileCheck %s -// This run uses getrusage: -// RUN: %env_tool_opts=hard_rss_limit_mb=100:can_use_proc_maps_statm=0 not %run %t 2>&1 | FileCheck %s -// -// Run w/o limit or with a large enough limit should pass: -// RUN: %env_tool_opts=hard_rss_limit_mb=1000 %run %t -// RUN: %run %t -// -// FIXME: make it work for other sanitizers. -// XFAIL: lsan -// XFAIL: tsan -// XFAIL: msan -// XFAIL: ubsan - -// https://github.com/google/sanitizers/issues/981 -// UNSUPPORTED: android-26 - -#include -#include -#include - -const int kNumAllocs = 200 * 1000; -const int kAllocSize = 1000; -volatile char *sink[kNumAllocs]; - -int main(int argc, char **argv) { - for (int i = 0; i < kNumAllocs; i++) { - if ((i % 1000) == 0) { - // Don't write to stderr! Doing that triggers a kernel race condition - // between this thread and the rss-limit thread, and may lose part of the - // output. See https://lkml.org/lkml/2014/2/17/324. - printf("[%d]\n", i); - } - char *x = new char[kAllocSize]; - memset(x, 0, kAllocSize); - sink[i] = x; - } - sleep(1); // Make sure the background thread has time to kill the process. -// CHECK: hard rss limit exhausted -} Index: test/sanitizer_common/TestCases/hard_rss_limit_mb_test.cc =================================================================== --- test/sanitizer_common/TestCases/hard_rss_limit_mb_test.cc +++ test/sanitizer_common/TestCases/hard_rss_limit_mb_test.cc @@ -0,0 +1,46 @@ +// Check hard_rss_limit_mb. Not all sanitizers implement it yet. +// RUN: %clangxx -O2 %s -o %t +// +// Run with limit should fail: +// RUN: %env_tool_opts=hard_rss_limit_mb=100 not %run %t 2>&1 | FileCheck %s +// This run uses getrusage: +// RUN: %env_tool_opts=hard_rss_limit_mb=100:can_use_proc_maps_statm=0 not %run %t 2>&1 | FileCheck %s +// +// Run w/o limit or with a large enough limit should pass: +// RUN: %env_tool_opts=hard_rss_limit_mb=1000 %run %t +// RUN: %run %t +// +// FIXME: make it work for other sanitizers. +// XFAIL: lsan +// XFAIL: tsan +// XFAIL: msan +// XFAIL: ubsan + +// https://github.com/google/sanitizers/issues/981 +// UNSUPPORTED: android-26 + +// UNSUPPORTED: freebsd, solaris, darwin + +#include +#include +#include + +const int kNumAllocs = 200 * 1000; +const int kAllocSize = 1000; +volatile char *sink[kNumAllocs]; + +int main(int argc, char **argv) { + for (int i = 0; i < kNumAllocs; i++) { + if ((i % 1000) == 0) { + // Don't write to stderr! Doing that triggers a kernel race condition + // between this thread and the rss-limit thread, and may lose part of the + // output. See https://lkml.org/lkml/2014/2/17/324. + printf("[%d]\n", i); + } + char *x = new char[kAllocSize]; + memset(x, 0, kAllocSize); + sink[i] = x; + } + sleep(1); // Make sure the background thread has time to kill the process. +// CHECK: hard rss limit exhausted +}