Skip to content

Dart on Apple M1 (arm64) doesn't call pthread_jit_write_protect_np when writing to executable memory #45793

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

Closed
knopp opened this issue Apr 21, 2021 · 10 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. vm-native

Comments

@knopp
Copy link
Contributor

knopp commented Apr 21, 2021

This results in immediate segfault when running on Apple M1. Relevant documention from Apple:

https://developer.apple.com/documentation/apple-silicon/porting-just-in-time-compilers-to-apple-silicon

Related to: #42773

This tracker is for issues related to:

  • Dart VM
@knopp
Copy link
Contributor Author

knopp commented Apr 21, 2021

Some background: Flutter engine can now cross compile to darwin-arm64 and with some changes flutter tool can assemble darwin-arm64 assets. Release (AOT) build works as expected on M1, JIT crashes when writing to executable memory.

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. vm-native labels Apr 22, 2021
@a-siva a-siva self-assigned this Apr 22, 2021
@knopp
Copy link
Contributor Author

knopp commented Apr 22, 2021

Something to keep in mind when implementing this: pthread_jit_write_protect_np does not have a sane initial value.

@mraleph
Copy link
Member

mraleph commented Apr 22, 2021

@knopp if you read the whole issue then it apparently does have a reasonable default and the issue was a misunderstanding: https://github.com/dotnet/runtime/pull/51135/files

@knopp
Copy link
Contributor Author

knopp commented Apr 22, 2021

My bad, I must have overlooked it. Never mind then.

@rmacnak-google
Copy link
Contributor

FWIW, I have built the standalone VM for ARM64 and successfully run both JIT and AOT on an M1, so I suspect there is some Flutter-specific issue.

@knopp
Copy link
Contributor Author

knopp commented Apr 22, 2021

@rmacnak-google, weird - that shouldn't be possible according to Apple documentation. You shouldn't be able to write into MAP_JIT pages without calling pthread_jit_write_protect_np(0) first. I don't suppose you have System Integrity Protection disabled? (not even sure that matters here)

@mraleph
Copy link
Member

mraleph commented Apr 23, 2021

I think this code in the engine which disables code write protection is to blame. Dart VM by default already flips code regions between RW and RX modes so that a memory region is never R and X at the same time (alternatively we use dual mapping to achieve the same on platforms where this is possible). That explains, why standalone VM works just fine. Flutter Engine disables this mode to avoid performance issues associated with it (unclear if this issues are relevant in development though), but as a result it uses VM in the mode where code pages are RWX and consequently we hit Mac OS X new protections.

@knopp
Copy link
Contributor Author

knopp commented Apr 23, 2021

@mraleph, you're right! Thanks. It seems that mprotect with right flags does the job. So I guess this issue can be closed and it is indeed a flutter problem.

@knopp knopp closed this as completed Apr 23, 2021
@eseidelGoogle
Copy link

Is there a link to the Flutter issue? :)

@knopp
Copy link
Contributor Author

knopp commented Apr 28, 2021

Here: flutter/flutter#81208 (with a PR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. vm-native
Projects
None yet
Development

No branches or pull requests

6 participants