diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp --- a/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp @@ -72,25 +72,34 @@ return stackStore.Allocated() + useCounts.MemoryUsage(); } -static void CompressStackStore() { +static void *CompressStackStore(void *) { u64 start = MonotonicNanoTime(); uptr diff = stackStore.Pack(static_cast( - common_flags()->compress_stack_depot)); + Abs(common_flags()->compress_stack_depot))); if (!diff) - return; + return nullptr; u64 finish = MonotonicNanoTime(); uptr total_before = stackStore.Allocated() + diff; VPrintf(1, "%s: StackDepot released %zu KiB out of %zu KiB in %llu ms\n", SanitizerToolName, diff >> 10, total_before >> 10, (finish - start) / 1000000); + return nullptr; } void StackDepotNode::store(u32 id, const args_type &args, hash_type hash) { stack_hash = hash; uptr pack = 0; store_id = stackStore.Store(args, &pack); - if (pack && common_flags()->compress_stack_depot) - CompressStackStore(); + if (LIKELY(!pack)) + return; + if (!common_flags()->compress_stack_depot) + return; + constexpr u32 kFallbackToNoThread = 1u << 22; + if (id % kFallbackToNoThread != 0 && /* never rely on thread only */ + common_flags()->compress_stack_depot > 0 /* for testing */ && + internal_start_thread(&CompressStackStore, nullptr)) + return; + CompressStackStore(nullptr); } StackDepotNode::args_type StackDepotNode::load(u32 id) const { diff --git a/compiler-rt/test/sanitizer_common/TestCases/compress_stack_depot.cpp b/compiler-rt/test/sanitizer_common/TestCases/compress_stack_depot.cpp --- a/compiler-rt/test/sanitizer_common/TestCases/compress_stack_depot.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/compress_stack_depot.cpp @@ -1,7 +1,7 @@ // RUN: %clangxx %s -fsanitize-memory-track-origins=1 -o %t // RUN: %env_tool_opts="compress_stack_depot=0:malloc_context_size=128:verbosity=1" %run %t 2>&1 | FileCheck %s --implicit-check-not="StackDepot released" -// RUN: %env_tool_opts="compress_stack_depot=1:malloc_context_size=128:verbosity=1" %run %t 2>&1 | FileCheck %s --check-prefixes=COMPRESS -// RUN: %env_tool_opts="compress_stack_depot=2:malloc_context_size=128:verbosity=1" %run %t 2>&1 | FileCheck %s --check-prefixes=COMPRESS +// RUN: %env_tool_opts="compress_stack_depot=-1:malloc_context_size=128:verbosity=1" %run %t 2>&1 | FileCheck %s --check-prefixes=COMPRESS +// RUN: %env_tool_opts="compress_stack_depot=-2:malloc_context_size=128:verbosity=1" %run %t 2>&1 | FileCheck %s --check-prefixes=COMPRESS // Ubsan does not store stacks. // UNSUPPORTED: ubsan