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

Handle RPATH in user program #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
73 changes: 72 additions & 1 deletion src/track/heaptrack_inject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,80 @@ struct dlopen
static constexpr auto name = "dlopen";
static constexpr auto original = &::dlopen;

static Dl_serinfo* load_serinfo(const char* module_path) noexcept
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of returning an memory allocated serinfo, can't you take that Dl_serinfo * as an out-param, and construct it on the stack of the caller and return bool instead?

{
Dl_serinfo *serinfo = NULL;
Dl_serinfo serinfo_size;
void *module;

module = original(module_path, RTLD_LAZY);
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto module = original(...);

if (module == NULL) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if (!module)

return NULL;
}

int res = dlinfo(module, RTLD_DI_SERINFOSIZE, &serinfo_size);
if (res) {
dlclose(module);
Copy link
Collaborator

Choose a reason for hiding this comment

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

output an error?

return NULL;
}

serinfo = (Dl_serinfo*) ::malloc(serinfo_size.dls_size);
*serinfo = serinfo_size;
res = dlinfo(module, RTLD_DI_SERINFO, serinfo);
if (res) {
::free(serinfo);
Copy link
Collaborator

Choose a reason for hiding this comment

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

output an error?

serinfo = NULL;
}
dlclose(module);
return serinfo;
}

static void* hook(const char* filename, int flag) noexcept
{
auto ret = original(filename, flag);
void *ret = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

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

please extract this code into a separate function with a descriptive name

bool fallback = false;
if (filename == NULL || filename[0] == '/') {
// absolute path
ret = original(filename, flag);
}
if (!ret) {
// need to respect rpath
// get module info
Dl_info dl_info;
int res = dladdr(__builtin_return_address(0), &dl_info);
if (!res) {
fallback = true;
} else {
Dl_serinfo *serinfo;
Copy link
Collaborator

Choose a reason for hiding this comment

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

see above: here just do Dl_serinfo serinfo; and then pass &serinfo to the load_serinfo call below?

serinfo = load_serinfo(dl_info.dli_fname);
Copy link
Collaborator

Choose a reason for hiding this comment

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

otherwise: auto serinfo = load_serinfo(...)

if (serinfo == NULL) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if (!serinfo)

fallback = true;
} else {
struct stat file_stat;
char file_path[PATH_MAX];
for (unsigned int i = 0; i < serinfo->dls_cnt; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

add documentation to explain what this loop does - it seems to stat some files and build some path, can you give some examples for how the path is build?

file_path[0] = 0;
strcat(file_path, serinfo->dls_serpath[i].dls_name);
Copy link
Collaborator

Choose a reason for hiding this comment

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

strcat is unsafe and can overflow, please use strncat and track buffer size, or can we just use std::string instead?

strcat(file_path, "/");
strcat(file_path, filename);
int res = stat(file_path, &file_stat);
if (res) {
continue;
}
::free(serinfo);
Copy link
Collaborator

Choose a reason for hiding this comment

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

potentially leaked, has to be freed outside - better use std::unique_ptr if the heap allocation is needed, but best use the stack

// found file
ret = original(file_path, flag);
break;
}
}
}
}

if (!ret && fallback) {
ret = original(filename, flag);
}


if (ret) {
heaptrack_invalidate_module_cache();
overwrite_symbols();
Expand Down