[CORE] Add MallocHookRun to call startMallocHook before RunElfInit#3999
Merged
Conversation
The `startMallocHook` function is only invoked inside `RunElfInit`, and
`RunElfInit` is only called for the main ELF binary within `__libc_start_main`.
In the `RunElfInit` function, `startMallocHook` is currently invoked only after
all initialization routines of dynamic libraries have been executed. However,
if the malloc hook is defined within a dependent library and that library’s
initialization routine invokes the malloc hook interface, activation of the
malloc hook will be delayed, leading to unexpected runtime exceptions.
Therefore, I add a call to `MallocHookRun` so that the malloc hook can be
available before the execution of library init functions.
Before:
```
__libc_start_main
|
---> RunElfInit
|
----> libA.so Init
| |
| ---> _Znam
| |
| ---> Native actual_malloc
---> startMallocHook
```
After:
```
__libc_start_main
|
---> MallocHookRun
| |
| ---> startMallocHook
|
---> RunElfInit
|
----> libA.so Init
|
---> _Znam
|
---> X86-64 real_malloc
```
Contributor
Author
|
I found this issue while analyzing startup crashes of x86-64 Linux WeChat (An IM app developed by Tencent) running on RISC-V Linux. |
Owner
|
Ok, make sense, thanks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
startMallocHookfunction is only invoked insideRunElfInit, andRunElfInitis only called for the main ELF binary within__libc_start_main.In the
RunElfInitfunction,startMallocHookis currently invoked only after all initialization routines of dynamic libraries have been executed. However, if the malloc hook is defined within a dependent library and that library’s initialization routine invokes the malloc hook interface, activation of the malloc hook will be delayed, leading to unexpected runtime exceptions.Therefore, I add a call to
MallocHookRunso that the malloc hook can be available before the execution of library init functions.Before:
After: