← 返回首页
flush instruction cache after performing relocations by hMihaiDavid · Pull Request #59 · fancycode/MemoryModule · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

flush instruction cache after performing relocations#59

Open
hMihaiDavid wants to merge 5 commits into
fancycode:masterfrom
hMihaiDavid:master
Open

flush instruction cache after performing relocations#59
hMihaiDavid wants to merge 5 commits into
fancycode:masterfrom
hMihaiDavid:master

Conversation

Copy link
Copy Markdown

Flush instruction cache to avoid executing stale code after performing relocations.
According to MSDN: "Applications should call FlushInstructionCache if they generate or modify code in memory. The CPU cannot detect the change, and may execute the old code it cached.".
After performing relocation we have modified executable code, so if we don't flush the cache maybe the old code without relocation is executed instead. So far the code has woked without flushing instruction cache but it's better to be safe.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms679350(v=vs.85).aspx

Flush instruction cache to avoid executing stale code after performing relocations. According to MSDN: "Applications should call FlushInstructionCache if they generate or modify code in memory. The CPU cannot detect the change, and may execute the old code it cached.". After performing relocation we have modified executable code, so if we don't flush the cache maybe the old code without relocation is executed instead. So far the code has woked without flushing instruction cache but it's better to be safe. https://msdn.microsoft.com/en-us/library/windows/desktop/ms679350(v=vs.85).aspx
flush instruction cache after performing relocations
Comment thread MemoryModule.c Outdated Show resolved Hide resolved
Copy link
Copy Markdown
Author

Now the instruction cache is flushed only for each page-sized block of image where relocations have been performed. The FlushInstructionCache is now called inside PerformBaseRelocations, and it uses GetCurrentProcess instead of hard-coding (HANDLE)-1

Copy link
Copy Markdown

Elmue commented Jul 2, 2020
edited
Loading

I cannot see ANY sense in calling FlushInstructionCache().
You want to execute this function after relocating the code section.
But this happens long before the code is executed by calling the entry point.
You want to call FlushInstructionCache() when the section has not even been marked as executable with VirtualProtect()

Copy link
Copy Markdown

akasandra commented Jul 3, 2020
edited
Loading

The correct usage of this function may be called by user code, not MemoryModule itself - after multiple loads in a row, for example.

This also may be an undesirable behavior to call additional APIs with unclear functions.

Copy link
Copy Markdown

Elmue commented Jul 4, 2020

I don't know what you want to say with "additional APIs with unclear functions" ????
However I cannot see the sense of calling this function.
You did not help to clarify this.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Footer

© 2026 GitHub, Inc.