Skip to content

Provenance related LLVM miscompilation in release mode #20198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cryptocode opened this issue Jun 5, 2024 · 2 comments
Open

Provenance related LLVM miscompilation in release mode #20198

cryptocode opened this issue Jun 5, 2024 · 2 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. upstream An issue with a third party project that Zig uses.
Milestone

Comments

@cryptocode
Copy link
Contributor

cryptocode commented Jun 5, 2024

Zig Version

0.12.0

Steps to Reproduce and Observed Behavior

@matu3ba mentioned that long-standing pointer provenance related bugs exists in LLVM.

Relevant LLVM issues: llvm/llvm-project#34577 and llvm/llvm-project#33896

I found a C repro by Matthew House. I couldn't find a relevant Zig tracking issue for the miscompilation, so here's a corresponding Zig repro tested on Zig 0.12:

var x: usize = 0;

// Removing export makes the miscompilation go away
// Removing noalias makes the miscompilation go away
pub export fn f(noalias ptr_to_x: *usize) usize {

    // Changing this to const makes the miscompilation go away
    var p = ptr_to_x;
    _ = &p;

    p.* = 1;
    if (p == &x) {
        // Expected branch, taken only in Debug mode
        p.* = 2;
    }
    return p.*;
}

pub fn main() void {
    if (f(&x) == 1) @panic("p != &x");
}

This panics under all the three release modes, but not under debug mode.

Expected Behavior

No panic when compiling in release mode

@cryptocode cryptocode added the bug Observed behavior contradicts documented or intended behavior label Jun 5, 2024
@mlugg mlugg added upstream An issue with a third party project that Zig uses. miscompilation The compiler reports success but produces semantically incorrect code. backend-llvm The LLVM backend outputs an LLVM IR Module. labels Jun 5, 2024
@Vexu Vexu modified the milestone: 0.16.0 Jun 5, 2024
@rohlem
Copy link
Contributor

rohlem commented Jun 6, 2024

To me it looks like the compiler assumes that a pointer value copied from a pointer explicitly marked noalias is unequal to all other pointers (that it is related with, in this case by equality).
Why isn't that allowed?
noalias isn't documented in the langref yet, so it's possible I'm misunderstanding what exactly we want that attribute to do.
In my current understanding this seems reasonable, though it would be nicer to provide a safety-check that triggers in debug mode, unrelated to const-ness of p and export of f.
(I somehow doubt that safety checks for aliasing are planned, but I would find them useful.)

@tau-dev
Copy link
Contributor

tau-dev commented Jul 11, 2024

I somehow doubt that safety checks for aliasing are planned, but I would find them useful.

They are planned! #476. And I'm looking into it right now : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

No branches or pull requests

5 participants