-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
Dl_serinfo *serinfo = NULL; | ||
Dl_serinfo serinfo_size; | ||
void *module; | ||
|
||
module = original(module_path, RTLD_LAZY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. auto module = original(...); |
||
if (module == NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above: here just do |
||
serinfo = load_serinfo(dl_info.dli_fname); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherwise: |
||
if (serinfo == NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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?