Skip to content
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

[loader] Fix crash when the loader is loaded multiple times #2954

Merged
merged 1 commit into from
Nov 15, 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
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');
Loading