Skip to content

hint::spin_loop invokes undefined behavior on targets without SSE2 #59237

Closed
@gnzlbg

Description

@gnzlbg
Contributor

Currently hint::spin_loop is implemented with the pause instruction on all x86 / x86_64 targets, but this instruction is only available on CPUs with SSE2. The behavior of pause on CPUs without SSE2 is undefined.


This hint should not be implemented with inline assembly, but should call the appropriate core::arch intrinsics instead. Those explicitly state which features are required for each operation on each target.

Activity

added
I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
on Mar 16, 2019
Fanael

Fanael commented on Mar 17, 2019

@Fanael

This is incorrect: pause is defined in a way that makes it a harmless NOP on all earlier CPUs, and is even explicitly defined as such in Intel manuals:

This instruction was introduced in the Pentium 4 processors, but is backward compatible with all IA-32 processors. In earlier IA-32 processors, the PAUSE instruction operates like a NOP instruction.

gnzlbg

gnzlbg commented on Mar 17, 2019

@gnzlbg
ContributorAuthor

@Fanael indeed. Is there an advantage to emitting pause in earlier processors with respect to emitting nothing ?

Fanael

Fanael commented on Mar 17, 2019

@Fanael

Not that I know, there is no advantage on any of Intel's old microarchitectures, they just interpret it as a nop and have no special way of handling spin loops. It might be different on AMD's K7 or VIA Nehemiah, but I don't have that hardware to test, and I doubt it makes any difference anyway. It lets you avoid an ugly compile time conditional and that's it I think.

added
O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)
on Mar 17, 2019
gnzlbg

gnzlbg commented on Mar 17, 2019

@gnzlbg
ContributorAuthor

This bug report is invalid, thanks @Fanael .

added a commit that references this issue on Mar 21, 2019
830c98d
added
O-x86_32Target: x86 processors, 32 bit (like i686-*) (IA-32)
and removed on Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-x86_32Target: x86 processors, 32 bit (like i686-*) (IA-32)O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kennytm@Fanael@Centril@gnzlbg@Noratrieb

        Issue actions

          hint::spin_loop invokes undefined behavior on targets without SSE2 · Issue #59237 · rust-lang/rust