KEMBAR78
#1250 validate that the obtained dlopen handle is same as the originally discovered lib in maps by Baraa-Hasheesh · Pull Request #1261 · async-profiler/async-profiler · GitHub
Skip to content

Conversation

@Baraa-Hasheesh
Copy link
Contributor

Description

This fixes fixes one of the potential problems discovered in the #1250
This change detects if the library was reloaded to a different location & marks the handle as invalid

Note: This solution isn't complete & needs to change handling of dlerror() == null to only be valid for ld library

Related issues

#1250

Motivation and context

Reduce effect of race conditions to async profiler

How has this been tested?

make test


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

// Also, dlopen() ensures the library is fully loaded.
// Main executable and ld-linux interpreter cannot be dlopen'ed, but dlerror() returns NULL for them on some systems.
void* handle = dlopen(lib.file, RTLD_LAZY | RTLD_NOLOAD);
bool valid_handle = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If handle is not null, it is valid. I would rename the variable to reflect intent: "handle is to a different instance of the shared object, compared to what was parsed from /maps".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to original_handle

Signed-off-by: Bara' Hasheesh <bara.hasheesh@gmail.com>
@Baraa-Hasheesh Baraa-Hasheesh requested a review from krk April 24, 2025 17:05
// Parse main executable regardless of dlopen result, since it cannot be unloaded.
bool is_main_exe = main_phdr >= lib.image_base && main_phdr < lib.map_end;
if (handle != NULL || dlerror() == NULL || is_main_exe) {
if (original_handle || dlerror() == NULL || is_main_exe) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct: because of "dlerror() == NULL", condition will be still true, even if base address differs.
Fortunately, dlerror() should go away after Kerem's fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct
@krk change is required here

I will sync & resolve conflicts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apangin branch synced & conflicts handled

@Baraa-Hasheesh
Copy link
Contributor Author

included here #1264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants