Skip to content

Shl/Shr should throw UB on too large shift #1920

Closed
rust-lang/rust
#91162
@RalfJung

Description

@RalfJung

The LLVM docs for shl say

If op2 is (statically or dynamically) equal to or larger than the number of bits in op1, this instruction returns a poison value.

However, the CTFE/Miri implementation of shl/shr currently just truncates the shift amount. In rustc-generated MIR this can never happen since there always is a check being emitted, but we should still make sure to implement all UB checks for this operation correctly -- and the simd_shl intrinsic also exposes direct access to this operation, making it possible to trigger this UB. (I plan to add tests for this.)

I just hope we didn't miss any other MIR operations which compile ti LLVM arithmetic operations that can produce poison/undef/UB... (we do check for div-by-zero, of course). Cc @rust-lang/wg-llvm

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