Skip to content

22.1 Unsafe Asm: Clobbered registers cpuid example can write to an invalid pointer #1540

Closed
@kateeckhart

Description

@kateeckhart

In https://doc.rust-lang.org/nightly/rust-by-example/unsafe/asm.html#clobbered-registers there is no protection for rbx being used for an input register.

According to the reference, reserved registers such as rbx may be allocated into the register class "reg".
If the pointer to the cpu maker name gets put into rbx, the following asm is emitted.

push rbx
cpuid ;rbx is replaced with part of the cpu maker name
mov [rbx], ebx ;rbx is not a valid pointer UNDEFINED BEHAVIOR
;...

This bug should be fixed and a warning should be put in to prevent people from making similar mistakes. An example fix would be using explicit input operands and warning against using automatic integer/pointer register assignment when using rbx(64-bit), esi(32-bit) or rbp in asm.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions