Skip to content

Commit

Permalink
Fix crash when the loader is loaded multiple times (#2954)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamluc authored Nov 15, 2024
1 parent 02fe693 commit b3c078a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion loader/dd_library_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static unsigned int php_api_no = 0;
static const char *runtime_version = "unknown";
static bool injection_forced = false;

static bool already_done = false;

#if defined(__MUSL__)
# define OS_PATH "linux-musl/"
#else
Expand Down Expand Up @@ -554,6 +556,7 @@ static int ddloader_load_extension(unsigned int php_api_no, char *module_build_i
config->module_number = module_entry->module_number;
config->version = (char *)module_entry->version;

LOG(INFO, "Extension '%s' loaded", config->ext_name);
goto ok;

abort_and_unload:
Expand Down Expand Up @@ -617,6 +620,11 @@ static int ddloader_api_no_check(int api_no) {
return SUCCESS;
}

if (already_done) {
LOG(WARN, "dd_library_loader has been loaded multiple times, aborting");
return SUCCESS;
}

ddloader_configure();

switch (api_no) {
Expand Down Expand Up @@ -687,10 +695,12 @@ static int ddloader_api_no_check(int api_no) {

static int ddloader_build_id_check(const char *build_id) {
// Guardrail
if (!ddloader_libc_check() || !php_api_no) {
if (!ddloader_libc_check() || !php_api_no || already_done) {
return SUCCESS;
}

already_done = true;

bool is_zts = (strstr(build_id, "NTS") == NULL);
bool is_debug = (strstr(build_id, "debug") != NULL);

Expand Down
11 changes: 11 additions & 0 deletions loader/tests/functional/test_load_loader_multiple_times.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once __DIR__."/includes/autoload.php";
skip_if_php5();

$output = runCLI('-dzend_extension='.getLoaderAbsolutePath().' '.__DIR__.'/fixtures/ddtrace.php', true, ['DD_TRACE_DEBUG=1']);

// PHP warning
assertContains($output, 'Cannot load dd_library_loader');
// dd_library_loader warning
assertContains($output, 'dd_library_loader has been loaded multiple times, aborting');

0 comments on commit b3c078a

Please sign in to comment.