Skip to content

Commit

Permalink
Fixed crashes on Alpine when profiling native apps
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Oct 10, 2024
1 parent 4208d5c commit 3992d6a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/asprof.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# define DLLEXPORT __attribute__((visibility("default"),externally_visible))
#endif

#define WEAK __attribute__((weak))

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/chk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "asprof.h"


// libgcc refers to __sprintf_chk, but there is no such symbol in musl libc.
// Export a weak symbol in order to make profiler library work both with glibc and musl.
#define WEAK_EXPORT __attribute__((visibility("default"),externally_visible,weak))

extern "C" WEAK_EXPORT
extern "C" WEAK DLLEXPORT
int __sprintf_chk(char* s, int flag, size_t slen, const char* format, ...) {
va_list args;
va_start(args, format);
Expand Down
23 changes: 14 additions & 9 deletions src/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include "profiler.h"


#define ADDRESS_OF(sym) ({ \
void* addr = dlsym(RTLD_NEXT, #sym); \
addr != NULL ? (sym##_t)addr : sym; \
})

typedef void* (*ThreadFunc)(void*);

struct ThreadEntry {
Expand Down Expand Up @@ -103,21 +108,21 @@ static void* dlopen_hook(const char* filename, int flags) {

// LD_PRELOAD hooks

extern "C" DLLEXPORT
extern "C" WEAK DLLEXPORT
int pthread_create(pthread_t* thread, const pthread_attr_t* attr, ThreadFunc start_routine, void* arg) {
if (_orig_pthread_create == NULL) {
_orig_pthread_create = (pthread_create_t)dlsym(RTLD_NEXT, "pthread_create");
_orig_pthread_create = ADDRESS_OF(pthread_create);
}
if (Hooks::initialized()) {
return pthread_create_hook(thread, attr, start_routine, arg);
}
return _orig_pthread_create(thread, attr, start_routine, arg);
}

extern "C" DLLEXPORT
extern "C" WEAK DLLEXPORT
void pthread_exit(void* retval) {
if (_orig_pthread_exit == NULL) {
_orig_pthread_exit = (pthread_exit_t)dlsym(RTLD_NEXT, "pthread_exit");
_orig_pthread_exit = ADDRESS_OF(pthread_exit);
}
if (Hooks::initialized()) {
pthread_exit_hook(retval);
Expand All @@ -127,10 +132,10 @@ void pthread_exit(void* retval) {
abort(); // to suppress gcc warning
}

extern "C" DLLEXPORT
extern "C" WEAK DLLEXPORT
void* dlopen(const char* filename, int flags) {
if (_orig_dlopen == NULL) {
_orig_dlopen = (dlopen_t)dlsym(RTLD_NEXT, "dlopen");
_orig_dlopen = ADDRESS_OF(dlopen);
}
if (Hooks::initialized()) {
return dlopen_hook_impl(filename, flags, false);
Expand All @@ -152,9 +157,9 @@ bool Hooks::init(bool attach) {
Profiler::setupSignalHandlers();

if (attach) {
_orig_pthread_create = (pthread_create_t)dlsym(RTLD_NEXT, "pthread_create");
_orig_pthread_exit = (pthread_exit_t)dlsym(RTLD_NEXT, "pthread_exit");
_orig_dlopen = (dlopen_t)dlsym(RTLD_NEXT, "dlopen");
_orig_pthread_create = ADDRESS_OF(pthread_create);
_orig_pthread_exit = ADDRESS_OF(pthread_exit);
_orig_dlopen = ADDRESS_OF(dlopen);
patchLibraries();
}

Expand Down
2 changes: 1 addition & 1 deletion src/vmStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ void VMStructs::initThreadBridge() {
}

VMThread* VMThread::current() {
return (VMThread*)pthread_getspecific((pthread_key_t)_tls_index);
return _tls_index >= 0 ? (VMThread*)pthread_getspecific((pthread_key_t)_tls_index) : NULL;
}

int VMThread::nativeThreadId(JNIEnv* jni, jthread thread) {
Expand Down

0 comments on commit 3992d6a

Please sign in to comment.