Skip to content

Miscompilation: comparing a pointer against all possible addresses returns false #130388

Open
@RalfJung

Description

@RalfJung

Consider this code:

#![feature(strict_provenance)]

use std::ptr;

#[no_mangle]
pub fn test(x: *mut u8) -> bool {
    let local = 0u8;
    for i in 0..=usize::MAX {
        if x.addr() == ptr::from_ref(&local).wrapping_add(i).addr() {
            return true;
        }
    }
    false
}

This compares x.addr() with every possible address there is. And yet, the optimized LLVM IR for this is:

; Function Attrs: mustprogress nofree norecurse nosync nounwind nonlazybind willreturn memory(none) uwtable
define noundef zeroext i1 @test(ptr nocapture noundef readnone %x) unnamed_addr #0 {
start:
  ret i1 false
}

That's clearly nonsense.

This is the Rust version of llvm/llvm-project#34450.

Cc @nikic @rust-lang/opsem

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions