Make get_php_base_addr more reliable - #152
Merged
Merged
Conversation
In order to calculate the base address of PHP's memory space, we
were previously using the first PHP mapping in procfs_maps, which
may not have been an executable range. Similarly we were using the
first vaddr in ELF headers, which again may not have been an
executable segment. In both cases we now look for the first entry
with an "x" flag (executable).
For example, consider:
```
$ # procfs_maps
557a78c00000-557a78d5f000 r--p 00000000 fe:01 15623632 /home/adam/php/bin/php
557a78e00000-557a7966d000 r-xp 00200000 fe:01 15623632 /home/adam/php/bin/php
557a79800000-557a7a5b7000 r--p 00c00000 fe:01 15623632 /home/adam/php/bin/php
557a7a72d000-557a7a800000 r--p 01b2d000 fe:01 15623632 /home/adam/php/bin/php
557a7a800000-557a7a85d000 rw-p 01c00000 fe:01 15623632 /home/adam/php/bin/php
$ # objdump -p
LOAD off 0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**21
filesz 0x000000000015e438 memsz 0x000000000015e438 flags r--
LOAD off 0x0000000000200000 vaddr 0x0000000000200000 paddr 0x0000000000200000 align 2**21
filesz 0x000000000086c87d memsz 0x000000000086c87d flags r-x
LOAD off 0x0000000000c00000 vaddr 0x0000000000c00000 paddr 0x0000000000c00000 align 2**21
filesz 0x0000000000db63d8 memsz 0x0000000000db63d8 flags r--
LOAD off 0x0000000001b2ddc8 vaddr 0x0000000001b2ddc8 paddr 0x0000000001b2ddc8 align 2**21
filesz 0x000000000012f186 memsz 0x0000000000156848 flags rw-
```
We were calculating base addr as:
557a78c00000 - 0x0000000000000000 == 557a78c00000
but really we should have been calculating it as:
557a78e00000 - 0x0000000000200000 == 557a78c00000
Note you get the same answer both ways, so this went unnoticed.
Recently aarch64 support was added, and on that architecture
sometimes the executable mapping or executable vaddr record does not
come first, leading to a bad base address. As the inline comment
mentions, if PHP happens to have multiple executable segments, this
may not work, since it just looks for the first executable record in
both cases. I wouldn't expect that to happen in practice but I'm not
sure. At least this seems to be an improvement over the existing
function.
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.
In order to calculate the base address of PHP's memory space, we were previously using the first PHP mapping in procfs_maps, which may not have been an executable range. Similarly we were using the first vaddr in ELF headers, which again may not have been an executable segment. In both cases we now look for the first entry with an "x" flag (executable).
For example, consider:
We were calculating base addr as:
but really we should have been calculating it as:
Note you get the same answer both ways, so this went unnoticed.
Recently aarch64 support was added, and on that architecture sometimes the executable mapping or executable vaddr record does not come first, leading to a bad base address. As the inline comment mentions, if PHP happens to have multiple executable segments, this may not work, since it just looks for the first executable record in both cases. I wouldn't expect that to happen in practice but I'm not sure. At least this seems to be an improvement over the existing function.