Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler-rt/lib/asan/asan_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_placement_new.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
#include "sanitizer_common/sanitizer_thread_history.h"
#include "sanitizer_common/sanitizer_tls_get_addr.h"

namespace __asan {
Expand Down Expand Up @@ -555,6 +556,12 @@ void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {
threads);
}

void PrintThreads() {
InternalScopedString out;
PrintThreadHistory(__asan::asanThreadRegistry(), out);
Report("%s\n", out.data());
}

} // namespace __lsan

// ---------------------- Interface ---------------- {{{1
Expand Down
7 changes: 6 additions & 1 deletion compiler-rt/lib/hwasan/hwasan_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {
__hwasan::hwasanThreadArgRetval().GetAllPtrsLocked(ptrs);
}

void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {}
void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {
// TODO: implement.
}
void PrintThreads() {
// TODO: implement.
}

} // namespace __lsan
17 changes: 12 additions & 5 deletions compiler-rt/lib/lsan/lsan_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,14 +771,15 @@ static bool PrintResults(LeakReport &report) {
}
if (common_flags()->print_suppressions)
GetSuppressionContext()->PrintMatchedSuppressions();
if (unsuppressed_count > 0) {
if (unsuppressed_count)
report.PrintSummary();
return true;
}
return false;
if ((unsuppressed_count && common_flags()->verbosity >= 2) ||
flags()->log_threads)
PrintThreads();
return unsuppressed_count;
}

static bool CheckForLeaks() {
static bool CheckForLeaksOnce() {
if (&__lsan_is_turned_off && __lsan_is_turned_off()) {
VReport(1, "LeakSanitizer is disabled\n");
return false;
Expand Down Expand Up @@ -830,6 +831,12 @@ static bool CheckForLeaks() {
}
}

static bool CheckForLeaks() {
int leaking_tries = 0;
for (int i = 0; i < flags()->tries; ++i) leaking_tries += CheckForLeaksOnce();
return leaking_tries == flags()->tries;
}

static bool has_reported_leaks = false;
bool HasReportedLeaks() { return has_reported_leaks; }

Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/lsan/lsan_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void GetThreadExtraStackRangesLocked(tid_t os_id,
InternalMmapVector<Range> *ranges);
void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs);
void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads);
void PrintThreads();

//// --------------------------------------------------------------------------
//// Allocator prototypes.
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/lsan/lsan_flags.inc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ LSAN_FLAG(bool, use_poisoned, false,
"Consider pointers found in poisoned memory to be valid.")
LSAN_FLAG(bool, log_pointers, false, "Debug logging")
LSAN_FLAG(bool, log_threads, false, "Debug logging")
LSAN_FLAG(int, tries, 1, "Debug option to repeat leak checking multiple times")
LSAN_FLAG(const char *, suppressions, "", "Suppressions file name.")
LSAN_FLAG(int, thread_suspend_fail, 1,
"Behaviour if thread suspendion all thread (0 - "
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/lsan/lsan_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {
threads);
}

void PrintThreads() {
// TODO: implement.
}

void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {
GetThreadArgRetval().GetAllPtrsLocked(ptrs);
}
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/sanitizer_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ set(SANITIZER_SYMBOLIZER_SOURCES
sanitizer_symbolizer_report.cpp
sanitizer_symbolizer_report_fuchsia.cpp
sanitizer_symbolizer_win.cpp
sanitizer_thread_history.cpp
sanitizer_unwind_linux_libcdep.cpp
sanitizer_unwind_fuchsia.cpp
sanitizer_unwind_win.cpp
Expand Down
72 changes: 72 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_thread_history.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===-- sanitizer_thread_history.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "sanitizer_thread_history.h"

#include "sanitizer_stackdepot.h"
namespace __sanitizer {

void PrintThreadHistory(ThreadRegistry &registry, InternalScopedString &out) {
ThreadRegistryLock l(&registry);
// Stack traces are largest part of printout and they often the same for
// multiple threads, so we will deduplicate them.
InternalMmapVector<const ThreadContextBase *> stacks;

registry.RunCallbackForEachThreadLocked(
[](ThreadContextBase *context, void *arg) {
static_cast<decltype(&stacks)>(arg)->push_back(context);
},
&stacks);

Sort(stacks.data(), stacks.size(),
[](const ThreadContextBase *a, const ThreadContextBase *b) {
if (a->stack_id < b->stack_id)
return true;
if (a->stack_id > b->stack_id)
return false;
return a->unique_id < b->unique_id;
});

auto describe_thread = [&](const ThreadContextBase *context) {
if (!context) {
out.Append("T-1");
return;
}
out.AppendF("T%llu/%llu", context->unique_id, context->os_id);
if (internal_strlen(context->name))
out.AppendF(" (%s)", context->name);
};

auto get_parent =
[&](const ThreadContextBase *context) -> const ThreadContextBase * {
if (!context)
return nullptr;
ThreadContextBase *parent = registry.GetThreadLocked(context->parent_tid);
if (!parent)
return nullptr;
if (parent->unique_id >= context->unique_id)
return nullptr;
return parent;
};

const ThreadContextBase *prev = nullptr;
for (const ThreadContextBase *context : stacks) {
if (prev && prev->stack_id != context->stack_id)
StackDepotGet(prev->stack_id).PrintTo(&out);
prev = context;
out.Append("Thread ");
describe_thread(context);
out.Append(" was created by ");
describe_thread(get_parent(context));
out.Append("\n");
}
if (prev)
StackDepotGet(prev->stack_id).PrintTo(&out);
}

} // namespace __sanitizer
24 changes: 24 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_thread_history.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- sanitizer_thread_history.h ------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Utility to print thread histroy from ThreadRegistry.
//
//===----------------------------------------------------------------------===//

#ifndef SANITIZER_THREAD_HISTORY_H
#define SANITIZER_THREAD_HISTORY_H

#include "sanitizer_thread_registry.h"

namespace __sanitizer {

void PrintThreadHistory(ThreadRegistry& registry, InternalScopedString& out);

} // namespace __sanitizer

#endif // SANITIZER_THREAD_HISTORY_H
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_thread_registry.h"

#include <iostream>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_stackdepot.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_thread_history.h"
#include "sanitizer_pthread_wrappers.h"

using testing::HasSubstr;

namespace __sanitizer {

static Mutex tctx_allocator_lock;
Expand Down Expand Up @@ -239,4 +247,56 @@ TEST(SanitizerCommon, ThreadRegistryThreadedTest) {
ThreadedTestRegistry(&registry);
}

TEST(SanitizerCommon, PrintThreadHistory) {
ThreadRegistry registry(GetThreadContext<TestThreadContext>,
kThreadsPerShard * kNumShards + 1, 10, 0);

UNINITIALIZED BufferedStackTrace stack1;
stack1.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, false,
/*max_depth=*/1);

UNINITIALIZED BufferedStackTrace stack2;
stack2.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, false,
/*max_depth=*/1);

EXPECT_EQ(0U, registry.CreateThread(0, true, -1, 0, nullptr));
for (int i = 0; i < 5; i++) {
registry.CreateThread(0, true, 0, StackDepotPut(stack1), nullptr);
registry.CreateThread(0, true, 0, StackDepotPut(stack2), nullptr);
}

InternalScopedString out;
PrintThreadHistory(registry, out);

std::string substrings[] = {
"Thread T0/0 was created by T-1",
"<empty stack>",
"",
"Thread T1/0 was created by T0/0",
"Thread T3/0 was created by T0/0",
"Thread T5/0 was created by T0/0",
"Thread T7/0 was created by T0/0",
"Thread T9/0 was created by T0/0",
"#0 0x",
"",
"Thread T2/0 was created by T0/0",
"Thread T4/0 was created by T0/0",
"Thread T6/0 was created by T0/0",
"Thread T8/0 was created by T0/0",
"Thread T10/0 was created by T0/0",
"#0 0x",
"",
};

std::stringstream ss(out.data());
std::string line;

for (auto substr : substrings) {
std::getline(ss, line);
EXPECT_THAT(line, HasSubstr(substr)) << line;
}

EXPECT_FALSE(std::getline(ss, line)) << "Unmatched line: " << line;
}

} // namespace __sanitizer
23 changes: 23 additions & 0 deletions compiler-rt/test/lsan/TestCases/flag_tries.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Test retries option of lsan.
// RUN: %clang_lsan %s -o %t
// RUN: %env_lsan_opts=use_stacks=0:use_registers=0:symbolize=0 %run %t foo 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK1
// RUN: %env_lsan_opts=use_stacks=0:use_registers=0:symbolize=0:tries=12 %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK,CHECK12

#include <assert.h>
#include <sanitizer/lsan_interface.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void *p;

int main(int argc, char *argv[]) {
fprintf(stderr, "Test alloc: %p.\n", malloc(1337));
// CHECK: Test alloc:

assert(__lsan_do_recoverable_leak_check() == 1);
// CHECK1-COUNT-1: SUMMARY: {{.*}}Sanitizer: 1337 byte
// CHECK12-COUNT-12: SUMMARY: {{.*}}Sanitizer: 1337 byte

_exit(0);
}
Loading