Skip to content
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

Pointer arithmetic *(--p) fails verification #119

Closed
ttaubert opened this issue Mar 20, 2016 · 2 comments
Closed

Pointer arithmetic *(--p) fails verification #119

ttaubert opened this issue Mar 20, 2016 · 2 comments
Assignees
Labels
priority High-priority issues
Milestone

Comments

@ttaubert
Copy link

Suppose we have this somewhat nonsensical function:

uint8_t test(uint8_t* a) {
    uint8_t* pa = a + 1;
    return *(pa - 1);
}

And here's some SAW code to test it:

llvm_verify m "test" [] do {
    llvm_ptr "a" (llvm_array 1 (llvm_int 8));
    a <- llvm_var "*a" (llvm_array 1 (llvm_int 8));
    llvm_return {{ a @ 0 }};
    llvm_verify_tactic abc;
};

This yields Successfully verified @test. If you change the function to the following though you get a user error:

uint8_t test(uint8_t* a) {
    uint8_t* pa = a + 1;
    return *(--pa);
}
Simulation error: Invalid load address (Prelude.bvAdd
                        64
                        lss__alloc0
                        (Prelude.bvNat 64 4294967296))
  at #0:@test:%0.0:9
All paths yielded errors!
--------------------------------------------------------------------------------
Error reason        : Invalid load address (Prelude.bvAdd
                        64
                        lss__alloc0
                        (Prelude.bvNat 64 4294967296))
Error path state    :
Path #0:@test:%0.0:9
Stack frame
  Allocations:
    StackAlloc lss__alloc2 Prelude.bvNat
                             64
                             8
    StackAlloc lss__alloc1 Prelude.bvNat
                             64
                             8
  Writes:
    *lss__alloc2 := Prelude.bvAdd
                      64
                      lss__alloc0
                      (Prelude.bvNat 64 4294967296)
    *lss__alloc2 := Prelude.bvAdd 64
                      lss__alloc0
                      (Prelude.bvNat 64 1)
    *lss__alloc1 := lss__alloc0
    *lss__alloc0 := *(%a)
Base memory
  Allocations:
    HeapAlloc lss__alloc0 Prelude.bvNat
                            64
                            1
  Writes:

--------------------------------------------------------------------------------
All paths yielded errors!
--------------------------------------------------------------------------------
Error reason        : Invalid load address (Prelude.bvAdd
                        64
                        lss__alloc0
                        (Prelude.bvNat 64 4294967296))
Error path state    :
Path #0:@test:%0.0:9
Stack frame
  Allocations:
    StackAlloc lss__alloc2 Prelude.bvNat
                             64
                             8
    StackAlloc lss__alloc1 Prelude.bvNat
                             64
                             8
  Writes:
    *lss__alloc2 := Prelude.bvAdd
                      64
                      lss__alloc0
                      (Prelude.bvNat 64 4294967296)
    *lss__alloc2 := Prelude.bvAdd 64
                      lss__alloc0
                      (Prelude.bvNat 64 1)
    *lss__alloc1 := lss__alloc0
    *lss__alloc0 := *(%a)
Base memory
  Allocations:
    HeapAlloc lss__alloc0 Prelude.bvNat
                            64
                            1
  Writes:

--------------------------------------------------------------------------------
saw: user error (simulator did not return value when expected)
@ttaubert
Copy link
Author

It looks like decrementing the pointer with --pa just doesn't work, you can fix the code by doing pa -= 1; return *pa; if we wanted to keep the behavior modifying the pointer.

@atomb atomb added the priority High-priority issues label Mar 30, 2016
@atomb atomb modified the milestone: 0.2-alpha Mar 30, 2016
@atomb atomb self-assigned this Mar 30, 2016
@atomb
Copy link
Contributor

atomb commented Mar 30, 2016

This is a subtle bug! The difference in the generated LLVM code is twofold:

  • The latter version stores the result of the pointer decrement, which I think is irrelevant.
  • The latter version uses a 32-bit -1 constant in the getelementptr instruction whereas the former uses a 64-bit -1 constant. We currently assume 64-bit addresses.

I expect that the 32-bit -1 isn't being sign-extended, so it gets treated as a large positive value instead of a negative value.

brianhuffman pushed a commit that referenced this issue Apr 26, 2021
Switch Lambda and Pi constructors to use Text instead of String.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority High-priority issues
Projects
None yet
Development

No branches or pull requests

2 participants