Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Implementing a standalone memory manager #22

Merged
merged 1 commit into from
Sep 26, 2022
Merged

Implementing a standalone memory manager #22

merged 1 commit into from
Sep 26, 2022

Conversation

Calastrophe
Copy link
Contributor

This is the first pass of the memory manager, I have not implemented it into dumpulator code. On its own, it works, it is not optimized at all.

Please let me know if you think anything should be done another way and how I should start replacing mem_map calls.

@Calastrophe
Copy link
Contributor Author

I need some clarification on how you want "free" handled, do you want to be able to free a certain size at a time? or just a whole entire parent memory chunk at a time? so like free(addr) and it will free the entire parent, or free(addr,size), so from addr to size is free'd.

Read/Write/Alloc/Protect wrappers have been implemented in MemoryManager

@mrexodia
Copy link
Owner

Hm I gotta check what Windows does when you decommit part of an allocated region. Will get back to you on that!

@oopsmishap
Copy link
Collaborator

oopsmishap commented Sep 25, 2022

In this you're not enforcing any page alignments, this will cause huge issues.
Below is a simple function to do so:

def page_align_up(num: int):
    return num + num % 0x1000

def page_align_down(num: int):
    return num - num % 0x1000

test_address = page_align_down(0x6230) # returns 0x6000
test_size  = page_align_up(0x4)        # returns 0x1000

you will need to use it on each address and size input

def mem_protect(self, addr, size, perms):
    addr = page_align_down(addr)
    size = page_align_up(size)
    ...

A given address is always rounded down to the next page alignment while the size is always rounded up to the next page alignment.

Why is this an issue?

People assume that VirtualProtect only applies to the region they call the function on and don't expect that what actually happens is that the entire page of memory gets set to the protection flags they've given.

Many people would call a function like VirtualProtect like the following:

VirtualProtect(0x04001120, 0x8, PAGE_EXECUTE_READWRITE, NULL );

A good example of this in widely used code is in minhook:
https://github.com/TsudaKageyu/minhook/blob/master/src/hook.c#L377
So you can see by not dealing with page alignment weird things can happen.

I haven't really gone through your code yet but this was just a glaring issue I noticed straight away.

@mrexodia mrexodia merged commit f0fbbe9 into mrexodia:main Sep 26, 2022
@mrexodia
Copy link
Owner

Thanks, I'll pick it up!

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.

3 participants