-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix loading of R2R assemblies from byte arrays/streams #74118
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
Conversation
|
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
|
Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov Issue DetailsFixes:#71507
|
src/coreclr/vm/peimagelayout.cpp
Outdated
|
|
||
| #ifdef TARGET_OSX | ||
| // We need to allocate executable memory on OSX in order to do relocation of R2R code. | ||
| // Converted layout currently uses VirtualAlloc, so it will not work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I understand this comment. Why would allocating executable memory not work on OSX?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would work if correct APIs are used. It is probably not as simple as just passing MAP_JIT to VirtualAlloc or just replacing VirtualAlloc with another call.
I want to backport this to 7.0, so I'd like to not do a lot of changes to how we convert layouts, which would carry extra risk to scenarios that are not being fixed.
The goal is to make sure that assemblies can be loaded from byte arrays even if they contain R2R code.
Whether the R2R is actually enabled is of less importance. It is a relatively rare case and a reflection scenario with unclear benefits from R2R.
Linux part was just tweaking the assert, but for OSX enabling R2R needs more changes. I think it is ok to do those changes in 8.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that the change is fine as is.
Just FYI though, the VirtualAlloc sets MAP_JIT automatically for executable memory allocations. Either it gets memory from the g_executableMemoryAllocator.AllocateMemory or it sets the special flag that results in MAP_JIT addition during memory reservation:
runtime/src/coreclr/pal/src/map/virtual.cpp
Lines 923 to 927 in 6f69dde
| if ((flProtect & 0xff) == PAGE_EXECUTE_READWRITE) | |
| { | |
| flAllocationType |= MEM_RESERVE_EXECUTABLE; | |
| } | |
| pRetVal = ReserveVirtualMemory(pthrCurrent, (LPVOID)StartBoundary, MemSize, flAllocationType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean that I could just pass PAGE_EXECUTE_READWRITE to VirtualAlloc on OSX?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried going that path (setting PAGE_EXECUTE_READWRITE), but I think I also need to toggle PAL_JitWriteProtect when copying sections and then the change started making me worried about the complexity and possible bugs.
I think I will leave that for after-7.0, we do not have to do this now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right (for both)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've logged #74203 to follow up with enabling R2R on OSX
|
Loading from byte arrays/streams is expected to ignore R2R payload if there is one. Respecting R2R payload in this scenario has a dubious value (you are giving up perf advantage of memory mapped files) and there are number of problems that would be have to be fixed to make it work end-to-end well (don't forget diagnostic tools). |
I was not sure about this primarily because it seems to work on Windows and on Linux (modulo an assert) and probably worked in past releases. "work" here is basically "code runs". I do not know what the situation with diagnostics etc. I guess this is enough controversy to just disable R2R in this scenario on all platforms. |
Co-authored-by: Jan Vorlicek <[email protected]>
|
Thanks!! |
|
/backport to release/7.0-rc1 |
|
Started backporting to release/7.0-rc1: https://github.com/dotnet/runtime/actions/runs/2893425031 |
Fixes:#71507