Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Properly handle mmap calls without any protection mask: #9

Closed
wants to merge 1 commit into from

Conversation

marxin
Copy link

@marxin marxin commented Jan 31, 2024

Newly created heap (alloc_new_heap) is created by glibc without any protection flags and they are adjusted by mprotect once the mmap succeeds: https://github.com/bminor/glibc/blob/master/malloc/arena.c#L404-L441

Fixes: #8

Newly created heap (alloc_new_heap) is created by glibc without any protection flags
and they are adjusted by `mprotect` once the `mmap` succeeds:
https://github.com/bminor/glibc/blob/master/malloc/arena.c#L404-L441

Fixes: fasterthanlime#8
// and they are adjusted by `mprotect` once the `mmap` succeeds:
// https://github.com/bminor/glibc/blob/master/malloc/arena.c#L404-L441
// && prot_flags.contains(ProtFlags::PROT_READ | ProtFlags::PROT_WRITE)
//
Copy link
Owner

Choose a reason for hiding this comment

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

So my problem with the trivial fix here is that I feel like it'll over-track.

What I'd like instead is for mevi to keep track of these, but not as "allocated memory", just as something to keep an eye on, and when it is mprotect'd, then it should become "allocated memory". Does that make sense?

Copy link
Author

Choose a reason for hiding this comment

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

Well, I'm not in favor of keeping track of separate memory ranges w/o a protection set as we would need to properly handle munmap and mremap as well.
To be honest, the Mevi tool focuses mainly on RSS memory usage and the Linux kernel should protect us, because nobody should cause a page fault (and allocation) for a range without PROT_READ or PROT_WRITE. An exception might be a mmap without a protection flag with MAP_POPULATE set. That would be correctly covered right now and handled by Mevi.

What do you think?

Copy link
Owner

Choose a reason for hiding this comment

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

as we would need to properly handle munmap and mremap as well.

Mevi does handle these properly though! Or do you mean there would need to be special handling re: ranges with no protection set?

Copy link
Author

Choose a reason for hiding this comment

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

Or do you mean there would need to be special handling re: ranges with no protection set?

Yes, I mean the special handling of these ranges.

For what it is worth, I made an experimental branch where I split the MemState into 2 components:
https://github.com/marxin/mevi/tree/track-mapping-flags

pub struct MemState {
    pub mapping: MemMapping,
    pub flags: MemProtFlags,
}

pub enum MemMapping {
    Resident,
    NotResident,
    Untracked,
}

pub struct MemProtFlags {
    pub read: bool,
    pub write: bool,
}

and the branch tries to properly manage the flags (MemProtFlags). I've included mprotect syscall and the information is only visible in the front-end if you hover over a range.

What do you think about it?

Copy link
Author

Choose a reason for hiding this comment

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

I noticed many mprotect calls for my https://github.com/marxin/debuginfod-rs test-case, where most of these are triggered for a single page (4 KiB). That being said, it might be a performance bottleneck.

@marxin marxin closed this Mar 29, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unhandled mmap for a region without protection flags (PROT_READ | PROT_WRITE)
2 participants