-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Cannot invoke pthread_jit_write_protect_np
#64880
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Due to nesting problems it would be ideal if I could call is there any way to achieve this? (I'm not asking for a "clean" way to do this) but not being able to generate code would significantly affect our project. EDIT: after some fiddling with
|
Update (which confuses me tbh.) I tried allocating memory via Also I can change the protection back to rw, write new code to it, make it executable again and invoke it. So it seems to me that I don't need to call I would be glad for some help and/or hints on how to make this work properly |
It seems that the crash happens because calling |
I believe the reason is that dotnet run uses a special (apphost based) host for executing the application (which I've learned recently here: #63952 (comment)). That host is only adhoc signed without any entitlements and so the system doesn't guard it against the executable mapping flipping. |
Hey, thanks for the insights, so effectively this means i can't write to executable memory in dotnet using this mechanism (using the pthread-function) since the dotnet-generated code would then also become non-executable. However I validated that the mprotect-pattern (described above, switching between RW and EXEC) works also in a plain c project (without any special settings). My current suspicion is that pages can be marked executable without the pthread-function but they cannot be writeable at the same time. Can anyone shed some light on this? |
Yes, that's like the case of the apphost I've talked about - the case when the application is not signed with enlistments that would request the necessity of using the
You could create a native function that would do the pthread_jit_write_protect_np(0)
// do your writes here
pthread_jit_write_protect_np(1) And pinvoke it from the managed code. The writing would need to be purely native, of course. |
The whole I just figured I could create a native pthread worker that just handles copies to executable code and all the other threads would remain unaffected, so we could assemble our code to some non-executable region and then copy it using a special thread, but sadly that might be inefficient due to some fragmentation in our code |
Thanks, your idea is actually better than mine using the thread, but nonetheless the whole point of our assembler is to batch together thousands of native calls and some minimal logic in one managed-native transition. (While the real assembler remained completely managed) So i could essentially create a native function This leaves me with several options (all of which are suboptimal):
|
The protection bit here that matters for Mac is that you have the JIT entitlement. |
Hey, I'm aware that i need to sign things when distributing it, however dotnet itself (to my knowledge) needs this entitlement itself, so it shouldn't change much there... |
@krauthaufen can this issue be closed? |
Closing issue based on the above. |
Description
In an effort to make our rendering-engine run on Apple M1 machines we need to create a minimal ARM64 JIT running in our dotnet program (we have the same thing for x64 architectures).
I read a little about protection of executable code on M1 machines and have a running C++ example which uses
pthread_jit_write_protect_np(false)
to allow writing to the memory (obtained viammap
).While porting that to our dotnet project I ran into an issue with calling
pthread_jit_write_protect_np
:Whenever I call it the whole program exits instantly without any further information (maybe a signal-handler or something installed by dotnet itself?)
Reproduction Steps
Just try to call
pthread_jit_write_protect_np
from a dotnet program.Here's my F# code:
which will never reach the "writeable" print
Expected behavior
The call should work
Actual behavior
Whole Program just exits with code 0
Regression?
Can't really tell since dotnet 6 is the first one to run natively on M1 right?
Known Workarounds
None
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: