V0ldek opened PR #14967 from V0ldek:v0ldek/amd-mpk to bytecodealliance:main:
While poking around in the pooling allocator I noticed that my machine actually does not use the MPK features, which was weird since I've used them before. Turns out AMD is just rejected by wasmtime for this feature. After looking into it I actually found a more critical issue.
The check for
mpk::is_supportedexplicitly checked for GenuineIntel, and then for the PKU bit on the cpuid message. This is misguided for two reasons:
- Zen3 AMD also supports MPKs but wasmtime misses out on using them;
- the OSPKE bit also has to be checked to actually be correct.
Number 1. seems to have been done as an attempt to avoid CI crashes. In #7445, #7446 MPK was disabled and then reenabled for Intel only in PR #7513. I believe that was actually a red herring and those crashes were caused by the second point.
PKU only says if the CPU supports MPKs on the hardware level. However, the OS still needs to enable it (
CR4.PKE). If it is not enabled, RDPKRU/WRPKRU raise #UD, which kills the process with SIGILL. An example is a VM that runs on a modern CPU as the hardware, but whose hypervisor has PKU disabled. This would actually fault on any vendor, Intel included. You can test it today on the old version by running wasmtime in QEMU with nopku passed (-append "nopku"), it will crash with the pooling allocator when it tries to set up PKs.In this commit we switch to checking both PKE and OSPKE, removing the vendor check. This is confirmed to work on modern AMD and Intel, and correctly recognises MPK when ran under QEMU:
- with
-cpu hostand default kernel settings (supported),- with
-cpu hostbutnopkuin the kernel (unsupported),- with
-cpu host,-pku(unsupported).We now have a test in tests/mpk.rs to ensure the result of the check agrees with actual capabilities the process has.
Additionally, removed a stale test in
pkru::tests::check_read, it was ignored since forever and the new mpk test supersedes it.If you want confirmation on the CPU bits to be read:
- Intel SDM Volume 1, Chapter 21, Section 21.3, Table 21-20 (PDF at https://cdrdv2.intel.com/v1/dl/getContent/671436).
- AMD64 Architecture Programmer's Manual, Volume 3, Appendix E, section E.3.6 (PDF at https://docs.amd.com/api/khub/documents/BqEoC2FadIAgbrUdEzDJIg/content).
V0ldek removed the review request for wasmtime-core-reviewers from PR #14967.
V0ldek requested pchickey for a review on PR #14967.
V0ldek requested wasmtime-core-reviewers for a review on PR #14967.
V0ldek requested fitzgen for a review on PR #14967.
V0ldek requested wasmtime-default-reviewers for a review on PR #14967.
V0ldek removed the review request for wasmtime-default-reviewers from PR #14967.
pchickey removed the review request for pchickey from PR #14967.
:memo: alexcrichton submitted PR review:
Thanks!
:speech_balloon: alexcrichton created PR review comment:
Could this be compiled/run on all platforms and non-x86_64 platforms exit with a failure? That'd help test that
is_supportedreturns false on non-linux and non-x86_64 platforms fo example
:speech_balloon: alexcrichton created PR review comment:
Instead of using
#[test], coudl this test be written withharness = falselike this test?
:memo: V0ldek submitted PR review.
:speech_balloon: V0ldek created PR review comment:
Maybe just assert that
mpk::is_supportedis false on non-x86?
:memo: alexcrichton submitted PR review.
:speech_balloon: alexcrichton created PR review comment:
That sounds reasonable yeah, but I think that'd naturally fall out of "the spawned subprocess failed" as opposed to special-casing it, where the special-casing would be the
libc::syscallandasm!blocks.
:memo: V0ldek submitted PR review.
:speech_balloon: V0ldek created PR review comment:
Sure, may I ask why? What's the advantage?
:memo: alexcrichton submitted PR review.
:speech_balloon: alexcrichton created PR review comment:
I personally find that more precise than working around the built-in
--testrunner since it's easier to seefn mainand see that it's doing funny business.
fitzgen removed the review request for fitzgen from PR #14967.
V0ldek updated PR #14967.
:memo: V0ldek submitted PR review.
:speech_balloon: V0ldek created PR review comment:
Rewrote to not use a harness and used libtest_mimic like in the test you linked.
V0ldek updated PR #14967.
:memo: V0ldek submitted PR review.
:speech_balloon: V0ldek created PR review comment:
I'll need to work more on this and test how it interacts with different platforms. One finding is that I'm pretty sure we actually don't want to gate
is_supportbehind x86_64 cause on 32-bit you can also use MPKs.Another one is FreeBSD. They don't have libc
pkey_allocorpkey_freebut they do support PKUs, so theis_supportflag would actually say yes. Their API is different, so wasmtime would have to have a separate cfg path for FreeBSD: https://man.freebsd.org/cgi/man.cgi?query=pkru&sektion=3.I have no idea what happens on macOS and no means to test it either.
V0ldek updated PR #14967.
github-actions[bot] added the label wasmtime:api on PR #14967.
:memo: V0ldek submitted PR review.
:speech_balloon: V0ldek created PR review comment:
So ARM also has its own MPK system, but it is different, e.g. they only have 7 usable keys instead of 15. It would also require rewriting the code in
vm/mpk/pkru.rs.At this point I honestly think that the test should just be "do we return false on those platforms", because FreeBSD and Linux on ARM support MPK architecturally, but wasmtime does not hook into that and it'd be a separate PR to allow that. I will try to figure out how hard it'd be for FreeBSD in this PR, but for ARM I have no machine to test it on so I wouldn't tackle that.
V0ldek updated PR #14967.
V0ldek updated PR #14967.
V0ldek updated PR #14967.
:thumbs_up: alexcrichton submitted PR review.
alexcrichton commented on PR #14967:
Looks like rustfmt needs a run before merging
V0ldek updated PR #14967.
alexcrichton added PR #14967 Revise MPK support check to allow non-Intel CPUs. to the merge queue
github-merge-queue[bot] removed PR #14967 Revise MPK support check to allow non-Intel CPUs. from the merge queue
V0ldek updated PR #14967.
alexcrichton added PR #14967 Revise MPK support check to allow non-Intel CPUs. to the merge queue
github-merge-queue[bot] removed PR #14967 Revise MPK support check to allow non-Intel CPUs. from the merge queue
Last updated: Oct 31 2026 at 23:03 UTC