Files
TheRock/compiler/amd-llvm/compiler-rt/test/tsan/Darwin/dyld-insert-libraries.c
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

113 lines
3.8 KiB
C

// Test that dyld interposition works in the presence of DYLD_INSERT_LIBRARIES.
// Additionally, the injected library also has a pthread introspection hook that
// calls intercepted APIs before and after calling through to the TSan hook.
// This mirrors what libBacktraceRecording.dylib (Xcode 'Queue Debugging'
// feature) does.
// RUN: %clang_tsan %s -o %t
// RUN: %clang_tsan %s -o %t.dylib -fno-sanitize=thread -dynamiclib -DSHARED_LIB
//
// RUN: env DYLD_INSERT_LIBRARIES=%t.dylib %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
//
// XFAIL: darwin-remote
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#if defined(SHARED_LIB)
enum {
PTHREAD_INTROSPECTION_THREAD_CREATE = 1,
PTHREAD_INTROSPECTION_THREAD_START,
PTHREAD_INTROSPECTION_THREAD_TERMINATE,
PTHREAD_INTROSPECTION_THREAD_DESTROY,
};
typedef void (*pthread_introspection_hook_t)(unsigned int event,
pthread_t thread, void *addr,
size_t size);
extern pthread_introspection_hook_t pthread_introspection_hook_install(
pthread_introspection_hook_t hook);
static pthread_introspection_hook_t previous_pthread_hook;
static void pthread_introspection_hook(unsigned int event, pthread_t thread, void *addr, size_t size) {
pthread_t self;
const unsigned k_max_thread_name_size = 64;
char name[k_max_thread_name_size];
// Use some intercepted APIs *before* TSan hook runs.
{
self = pthread_self();
pthread_getname_np(self, name, k_max_thread_name_size);
if (strlen(name) == 0) {
strlcpy(name, "n/a", 4);
}
}
// This calls through to the TSan-installed hook, because the injected library
// constructor (see __library_initializer() below) runs after the TSan
// initializer. It replaces and forward to the previously-installed TSan
// introspection hook (very similar to what libBacktraceRecording.dylib does).
assert(previous_pthread_hook);
previous_pthread_hook(event, thread, addr, size);
// Use some intercepted APIs *after* TSan hook runs.
{
assert(self == pthread_self());
char name2[k_max_thread_name_size];
pthread_getname_np(self, name2, k_max_thread_name_size);
if (strlen(name2) == 0) {
strlcpy(name2, "n/a", 4);
}
assert(strcmp(name, name2) == 0);
}
switch (event) {
case PTHREAD_INTROSPECTION_THREAD_CREATE:
fprintf(stderr, "THREAD_CREATE %p, self: %p, name: %s\n", thread, self, name);
break;
case PTHREAD_INTROSPECTION_THREAD_START:
fprintf(stderr, "THREAD_START %p, self: %p, name: %s\n", thread, self, name);
break;
case PTHREAD_INTROSPECTION_THREAD_TERMINATE:
fprintf(stderr, "THREAD_TERMINATE %p, self: %p, name: %s\n", thread, self, name);
break;
case PTHREAD_INTROSPECTION_THREAD_DESTROY:
fprintf(stderr, "THREAD_DESTROY %p, self: %p, name: %s\n", thread, self, name);
break;
}
}
__attribute__((constructor))
static void __library_initializer(void) {
fprintf(stderr, "__library_initializer\n");
previous_pthread_hook = pthread_introspection_hook_install(pthread_introspection_hook);
}
#else // defined(SHARED_LIB)
void *Thread(void *a) {
pthread_setname_np("child thread");
fprintf(stderr, "Hello from pthread\n");
return NULL;
}
int main() {
fprintf(stderr, "main\n");
pthread_t t;
pthread_create(&t, NULL, Thread, NULL);
pthread_join(t, NULL);
fprintf(stderr, "Done.\n");
}
#endif // defined(SHARED_LIB)
// CHECK: __library_initializer
// CHECK: main
// Ignore TSan background thread.
// CHECK: THREAD_CREATE
// CHECK: THREAD_CREATE [[CHILD:0x[0-9a-f]+]]
// CHECK: THREAD_START [[CHILD]], self: [[CHILD]], name: n/a
// CHECK: Hello from pthread
// CHECK: THREAD_TERMINATE [[CHILD]], self: [[CHILD]], name: child thread
// CHECK: THREAD_DESTROY [[CHILD]]