Skip to content

Comments

Correct memory size calculation for modules on linux#255

Merged
Kenzzer merged 3 commits intomasterfrom
k/find_pattern_fix
Jan 11, 2026
Merged

Correct memory size calculation for modules on linux#255
Kenzzer merged 3 commits intomasterfrom
k/find_pattern_fix

Conversation

@Kenzzer
Copy link
Member

@Kenzzer Kenzzer commented Jan 9, 2026

Saw the conversation happening on discord about this specific change, as well as #253 and #251.

While the solution given isn't correct, @azzyr remains right in their statement that we're just lucky that .rodata happens to be found within the aligned page's size. I'm not at all familiar with how modules are loaded on linux, however a quick read of the manual on elf does tell us that PT_LOAD segments will have as final size in memory p_memsz which can be 0 (for PT_LOAD segments that end up not taking any place in memory).

I'm not quite sure why the #251 PR as well as the original code both stubbordingly limit their calculation to one PT_LOAD segment without really explaining the reasoning behind. The module size will at least be the addition of all PT_LOAD's p_memsz, so let's do just that.

@Kenzzer Kenzzer requested a review from Headline January 9, 2026 08:35
@Kenzzer
Copy link
Member Author

Kenzzer commented Jan 9, 2026

One thought that is only now occuring to me. I've made the assumption that PT_LOAD segments are continuously loaded, but that cannot be the case, so my module size is still going to end up underestimated.

I've no idea how much of a problem this is in practice, maybe this is fine. Otherwise I suggest we parse /proc/self/maps and no longer worry how the module was loaded into memory.

*/
lib.memorySize = PAGE_ALIGN_UP(hdr.p_filesz);
break;
lib.memorySize += hdr.p_memsz;
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't account for space between each segment - do we just want whatever the max is? The biggest p_vaddr + p_memsz

Copy link
Member

Choose a reason for hiding this comment

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

Just read your comment about this after reviewing, should only significantly change this if there's a requirement to do so

Copy link
Member Author

@Kenzzer Kenzzer Jan 10, 2026

Choose a reason for hiding this comment

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

This doesn't account for space between each segment - do we just want whatever the max is? The biggest p_vaddr + p_memsz

Given this was the original (the PR) solution and it led to a crash, I can only assume we cannot rely on p_vaddr

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, bit tired today and didn't fully process your message. But now I have lol. Anyways I think this is how we should be going about it.

@Kenzzer Kenzzer merged commit 2309b81 into master Jan 11, 2026
4 checks passed
@Kenzzer Kenzzer deleted the k/find_pattern_fix branch January 11, 2026 12:50
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.

2 participants