Skip to content

Commit

Permalink
Fix memory leak:
Browse files Browse the repository at this point in the history
Direct leak of 24 byte(s) in 1 object(s) allocated from:
    #0 0xffff8dfed43b in malloc (/usr/lib/aarch64-linux-gnu/libasan.so.5+0xcf43b)
    #1 0xffff81eb8f4f in __cxa_thread_atexit_impl /home/circleci/datadog/tmp/build_extension/ext/ddtrace.c:560
    #2 0xffff82ed7c8b in std::sys::unix::thread_local_dtor::register_dtor::ha7abe21b2e8f0491 library/std/src/sys/unix/thread_local_dtor.rs:31
    #3 0xffff81ec332b in _dd_writer_loop /home/circleci/datadog/tmp/build_extension/ext/coms.c:1053
    #4 0xffff8db7a7e3 in start_thread /build/glibc-tVuo8E/glibc-2.28/nptl/pthread_create.c:486
    #5 0xffff8b16a70b  (/lib/aarch64-linux-gnu/libc.so.6+0xcf70b)

SUMMARY: AddressSanitizer: 24 byte(s) leaked in 1 allocation(s).
  • Loading branch information
iamluc committed May 27, 2024
1 parent edda0f0 commit c87a954
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 7 additions & 1 deletion ext/coms.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "compatibility.h"
#include "configuration.h"
#include "ddshared.h"
#include "ddtrace.h"
#include "ext/version.h"
#include "logging.h"
#include "mpack/mpack.h"
Expand Down Expand Up @@ -935,7 +936,12 @@ static void _dd_signal_data_processed(struct _writer_loop_data_t *writer) {
#define TIMEOUT_SIG SIGPROF
#endif

static void _dd_writer_loop_cleanup(void *ctx) { _dd_signal_writer_finished((struct _writer_loop_data_t *)ctx); }
static void _dd_writer_loop_cleanup(void *ctx) {
_dd_signal_writer_finished((struct _writer_loop_data_t *)ctx);
#ifdef CXA_THREAD_ATEXIT_WRAPPER
dd_run_rust_thread_destructors(NULL);
#endif
}

static void *_dd_writer_loop(void *_) {
UNUSED(_);
Expand Down
5 changes: 1 addition & 4 deletions ext/ddtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,6 @@ static PHP_GINIT_FUNCTION(ddtrace) {
// This prevents us from a) having the weak symbols updated to the new locations and b) having ddtrace updates going live without hard restart.
// Thus, we need to intercept it: define it ourselves so that the linker will force the rust code to call our code here.
// Then we can collect the callbacks and invoke them ourselves right at thread shutdown, i.e. GSHUTDOWN.
#if defined(COMPILE_DL_DDTRACE) && defined(__GLIBC__) && __GLIBC_MINOR__
#define CXA_THREAD_ATEXIT_WRAPPER 1
#endif
#ifdef CXA_THREAD_ATEXIT_WRAPPER
#define CXA_THREAD_ATEXIT_PHP ((void *)0)
#define CXA_THREAD_ATEXIT_UNINITIALIZED ((void *)1)
Expand All @@ -526,7 +523,7 @@ struct dd_rust_thread_destructor {
static __thread struct dd_rust_thread_destructor *dd_rust_thread_destructors = NULL;
ZEND_TLS bool dd_is_main_thread = false;

static void dd_run_rust_thread_destructors(void *unused) {
void dd_run_rust_thread_destructors(void *unused) {
UNUSED(unused);
struct dd_rust_thread_destructor *entry = dd_rust_thread_destructors;
dd_rust_thread_destructors = NULL; // destructors _may_ be invoked multiple times. We need to reset thus.
Expand Down
10 changes: 10 additions & 0 deletions ext/ddtrace.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifndef DDTRACE_H
#define DDTRACE_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Zend/zend_types.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down Expand Up @@ -41,6 +44,10 @@ static inline zend_array *ddtrace_property_array(zval *zv) {
return Z_ARR_P(zv);
}

#if defined(COMPILE_DL_DDTRACE) && defined(__GLIBC__) && __GLIBC_MINOR__
#define CXA_THREAD_ATEXIT_WRAPPER 1
#endif

bool ddtrace_tracer_is_limited(void);
// prepare the tracer state to start handling a new trace
void dd_prepare_for_new_trace(void);
Expand All @@ -53,6 +60,9 @@ bool ddtrace_alter_dd_env(zval *old_value, zval *new_value);
bool ddtrace_alter_dd_version(zval *old_value, zval *new_value);
void dd_force_shutdown_tracing(void);
void dd_internal_handle_fork(void);
#ifdef CXA_THREAD_ATEXIT_WRAPPER
void dd_run_rust_thread_destructors(void *unused);
#endif

typedef struct {
int type;
Expand Down

0 comments on commit c87a954

Please sign in to comment.