Skip to content

[tsan] Allow unloading of ignored libraries #105660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 15 additions & 13 deletions compiler-rt/lib/sanitizer_common/sanitizer_libignore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void LibIgnore::AddIgnoredLibrary(const char *name_templ) {
lib->templ = internal_strdup(name_templ);
lib->name = nullptr;
lib->real_name = nullptr;
lib->loaded = false;
lib->range_id = kInvalidCodeRangeId;
}

void LibIgnore::OnLibraryLoaded(const char *name) {
Expand All @@ -43,7 +43,7 @@ void LibIgnore::OnLibraryLoaded(const char *name) {
buf[0]) {
for (uptr i = 0; i < count_; i++) {
Lib *lib = &libs_[i];
if (!lib->loaded && (!lib->real_name) &&
if (!lib->loaded() && (!lib->real_name) &&
TemplateMatch(lib->templ, name))
lib->real_name = internal_strdup(buf.data());
}
Expand All @@ -70,28 +70,31 @@ void LibIgnore::OnLibraryLoaded(const char *name) {
Die();
}
loaded = true;
if (lib->loaded)
if (lib->loaded())
continue;
VReport(1,
"Matched called_from_lib suppression '%s' against library"
" '%s'\n",
lib->templ, mod.full_name());
lib->loaded = true;
lib->name = internal_strdup(mod.full_name());
const uptr idx =
atomic_load(&ignored_ranges_count_, memory_order_relaxed);
CHECK_LT(idx, ARRAY_SIZE(ignored_code_ranges_));
ignored_code_ranges_[idx].begin = range.beg;
ignored_code_ranges_[idx].end = range.end;
ignored_code_ranges_[idx].OnLoad(range.beg, range.end);
// Record the index of the ignored range.
lib->range_id = idx;
atomic_store(&ignored_ranges_count_, idx + 1, memory_order_release);
break;
}
}
if (lib->loaded && !loaded) {
Report("%s: library '%s' that was matched against called_from_lib"
" suppression '%s' is unloaded\n",
SanitizerToolName, lib->name, lib->templ);
Die();
if (lib->loaded() && !loaded) {
VReport(1,
"%s: library '%s' that was matched against called_from_lib"
" suppression '%s' is unloaded\n",
SanitizerToolName, lib->name, lib->templ);
// The library is unloaded so mark the ignored code range as unloaded.
ignored_code_ranges_[lib->range_id].OnUnload();
lib->range_id = kInvalidCodeRangeId;
}
}

Expand All @@ -110,8 +113,7 @@ void LibIgnore::OnLibraryLoaded(const char *name) {
const uptr idx =
atomic_load(&instrumented_ranges_count_, memory_order_relaxed);
CHECK_LT(idx, ARRAY_SIZE(instrumented_code_ranges_));
instrumented_code_ranges_[idx].begin = range.beg;
instrumented_code_ranges_[idx].end = range.end;
instrumented_code_ranges_[idx].OnLoad(range.beg, range.end);
atomic_store(&instrumented_ranges_count_, idx + 1,
memory_order_release);
}
Expand Down
35 changes: 23 additions & 12 deletions compiler-rt/lib/sanitizer_common/sanitizer_libignore.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,36 @@ class LibIgnore {
bool IsPcInstrumented(uptr pc) const;

private:
static const uptr kMaxIgnoredRanges = 128;
static const uptr kMaxInstrumentedRanges = 1024;
static const uptr kMaxLibs = 1024;
static const uptr kInvalidCodeRangeId = -1;

struct Lib {
char *templ;
char *name;
char *real_name; // target of symlink
bool loaded;
uptr range_id;
bool loaded() const { return range_id != kInvalidCodeRangeId; };
};

struct LibCodeRange {
uptr begin;
uptr end;
};
bool IsInRange(uptr pc) const {
return (pc >= begin && pc < atomic_load(&end, memory_order_acquire));
}

inline bool IsInRange(uptr pc, const LibCodeRange &range) const {
return (pc >= range.begin && pc < range.end);
}
void OnLoad(uptr b, uptr e) {
begin = b;
atomic_store(&end, e, memory_order_release);
}

static const uptr kMaxIgnoredRanges = 128;
static const uptr kMaxInstrumentedRanges = 1024;
static const uptr kMaxLibs = 1024;
void OnUnload() { atomic_store(&end, 0, memory_order_release); }

private:
uptr begin;
// A value of 0 means the associated module was unloaded.
atomic_uintptr_t end;
};

// Hot part:
atomic_uintptr_t ignored_ranges_count_;
Expand All @@ -90,7 +101,7 @@ class LibIgnore {
inline bool LibIgnore::IsIgnored(uptr pc, bool *pc_in_ignored_lib) const {
const uptr n = atomic_load(&ignored_ranges_count_, memory_order_acquire);
for (uptr i = 0; i < n; i++) {
if (IsInRange(pc, ignored_code_ranges_[i])) {
if (ignored_code_ranges_[i].IsInRange(pc)) {
*pc_in_ignored_lib = true;
return true;
}
Expand All @@ -104,7 +115,7 @@ inline bool LibIgnore::IsIgnored(uptr pc, bool *pc_in_ignored_lib) const {
inline bool LibIgnore::IsPcInstrumented(uptr pc) const {
const uptr n = atomic_load(&instrumented_ranges_count_, memory_order_acquire);
for (uptr i = 0; i < n; i++) {
if (IsInRange(pc, instrumented_code_ranges_[i]))
if (instrumented_code_ranges_[i].IsInRange(pc))
return true;
}
return false;
Expand Down
30 changes: 21 additions & 9 deletions compiler-rt/test/tsan/ignore_lib3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

// RUN: %clangxx_tsan -O1 %s -DLIB -fPIC -fno-sanitize=thread -shared -o %t-dir/libignore_lib3.so
// RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t-dir/executable
// RUN: %env_tsan_opts=suppressions='%s.supp' %deflake %run %t-dir/executable | FileCheck %s
// RUN: %env_tsan_opts=suppressions='%s.supp':verbosity=1 %run %t-dir/executable 2>&1 | FileCheck %s

// Tests that unloading of a library matched against called_from_lib suppression
// causes program crash (this is not supported).
// is supported.

// Some aarch64 kernels do not support non executable write pages
// REQUIRES: stable-runtime
Expand All @@ -22,18 +22,30 @@

int main(int argc, char **argv) {
std::string lib = std::string(dirname(argv[0])) + "/libignore_lib3.so";
void *h = dlopen(lib.c_str(), RTLD_GLOBAL | RTLD_NOW);
dlclose(h);
void *h;
void (*f)();
// Try opening, closing and reopening the ignored lib.
for (unsigned int k = 0; k < 2; k++) {
h = dlopen(lib.c_str(), RTLD_GLOBAL | RTLD_NOW);
if (h == 0)
exit(printf("failed to load the library (%d)\n", errno));
f = (void (*)())dlsym(h, "libfunc");
if (f == 0)
exit(printf("failed to find the func (%d)\n", errno));
f();
dlclose(h);
}
fprintf(stderr, "OK\n");
}

#else // #ifdef LIB

extern "C" void libfunc() {
}
# include "ignore_lib_lib.h"

#endif // #ifdef LIB

// CHECK: ThreadSanitizer: library {{.*}} that was matched against called_from_lib suppression 'ignore_lib3.so' is unloaded
// CHECK-NOT: OK

// CHECK: Matched called_from_lib suppression 'ignore_lib3.so'
// CHECK: library '{{.*}}ignore_lib3.so' that was matched against called_from_lib suppression 'ignore_lib3.so' is unloaded
// CHECK: Matched called_from_lib suppression 'ignore_lib3.so'
// CHECK: library '{{.*}}ignore_lib3.so' that was matched against called_from_lib suppression 'ignore_lib3.so' is unloaded
// CHECK: OK
Loading