Skip to content

Conversation

sayantn
Copy link
Contributor

@sayantn sayantn commented Oct 4, 2025

It turns out that MSVC was right, and we were wrong (such a weird thing to say).

Our xsave tests used a fixed-size 2560 byte buffer for saving all register data. But the problem is that the required size for xsave is not fixed, it depends on the CPU. This piece of code is ancient, and when it was written the authors calculated this limit based on the typical size. But currently, the maximum size required for xsave can be as high as 11892 bytes (heck on my laptop it is 2696 bytes, if anyone wants to try it out the code is in playground), for our SDE run it is 11008 bytes (which also explains why these tests were disabled in the x86_64-unknown-linux-gnu run, due to such a huge discrepancy in size, it always generated a segfault). So in a way, MSVC helped us detect this bug.

See #1933 for some more context

As for why we are only seeing this now, if the CPU only supports up to AVX, 2560 bytes is enough space, but my guess is that GH introduced some better (windows?) runners, with AVX512 or AMX support, and both of those are capable of skyrocketing the XSAVE area size.

I will still rerun the CI a few times to ensure no weird random behavior is snooping around.

@rustbot
Copy link
Collaborator

rustbot commented Oct 4, 2025

r? @folkertdev

rustbot has assigned @folkertdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@sayantn
Copy link
Contributor Author

sayantn commented Oct 4, 2025

something still seems wrong, the segfaulting rate is now 100% lol, imma try it again in #1933

@sayantn sayantn force-pushed the fix-xsave branch 2 times, most recently from f60757a to 22f89b4 Compare October 4, 2025 10:26
@sayantn
Copy link
Contributor Author

sayantn commented Oct 4, 2025

i'm just dumb, made the Box aligned to 64 bytes instead of the inner slice 😑

Comment on lines 133 to +142
#[simd_test(enable = "xsave")]
#[cfg_attr(miri, ignore)] // Register saving/restoring is not supported in Miri
unsafe fn test_xsave64() {
let m = 0xFFFFFFFFFFFFFFFF_u64; //< all registers
let mut a = XsaveArea::new();
let mut b = XsaveArea::new();

xsave::_xsave64(a.ptr(), m);
xsave::_xrstor64(a.ptr(), m);
xsave::_xsave64(b.ptr(), m);
_xsave64(a.ptr(), m);
_xrstor64(a.ptr(), m);
_xsave64(b.ptr(), m);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're re-enabling 4 functions xsave,xsaveopt,xsave64,xsaveopt64, but there are only 3 tests here. Also shouldn't there be some sort of assert? or is this just a test that the functions work at all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah looking at it again all 4 functions do occur in these tests. Still, I'm not sure what this is actually testing beyond that these functions don't just segfault/sigill.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were asserts before, they were removed for some reason, can't remember why. I'll check if I can add them back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking back on #1672, I don't think adding back the asserts is a good idea. The compiler can insert some other calls in between _xrstor and the second _xsave, which can change the register states. These tests should just be there to ensure the intrinsics don't segfault/sigill

@folkertdev folkertdev added this pull request to the merge queue Oct 5, 2025
Merged via the queue into rust-lang:master with commit 53e8661 Oct 5, 2025
63 checks passed
@sayantn sayantn deleted the fix-xsave branch October 8, 2025 10:31
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.

3 participants