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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/mevi/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ impl Tracee {

if fd == -1
&& addr_in == 0
&& prot_flags.contains(ProtFlags::PROT_READ | ProtFlags::PROT_WRITE)
// 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
// && 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.

// && map_flags.contains(MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS)
&& map_flags.contains(MapFlags::MAP_ANONYMOUS)
{
Expand Down