Issue 237869
Summary [Attributor] A negative GEP offset is clamped to 0, so `%p - 1` inherits the dereferenceable bytes of `%p`
Labels miscompilation, llvm::Attributor, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/rorTG6E5W

```llvm
@g = global [8 x i8] zeroinitializer

define ptr @f(ptr nofree noundef nonnull writeonly dereferenceable(8) %p) {
  store i8 1, ptr %p
  %q = getelementptr i8, ptr %p, i64 -1
  ret ptr %q
}

define i32 @main() {
  %r = call ptr @f(ptr @g)
  ret i32 0
}
```

```console
$ opt -S -passes=attributor issue-repro.ll
...
define noundef dereferenceable_or_null(8) ptr @f(ptr nofree noundef nonnull writeonly dereferenceable(8) %p) #0 {
  store i8 1, ptr %p, align 1
  %q = getelementptr i8, ptr %p, i64 -1
  ret ptr %q
}
...

$ cat issue-repro-control.ll
@g = global [8 x i8] zeroinitializer

define ptr @f(ptr nofree noundef nonnull writeonly dereferenceable(8) %p) {
  store i8 1, ptr %p
  %q = getelementptr i8, ptr %p, i64 1
  ret ptr %q
}

define i32 @main() {
  %r = call ptr @f(ptr @g)
  ret i32 0
}

$ opt -S -passes=attributor issue-repro-control.ll
...
define noundef dereferenceable_or_null(7) ptr @f(ptr nofree noundef nonnull writeonly dereferenceable(8) %p) #0 {
  store i8 1, ptr %p, align 1
  %q = getelementptr i8, ptr %p, i64 1
  ret ptr %q
}
...
```

### suggested fix
`AADereferenceableFloating::updateImpl` (AttributorAttributes.cpp) clamps a negative accumulated offset to 0 (`if (OffsetSExt < 0) OffsetSExt = 0;`) and then takes `DerefBytes - OffsetSExt`, so `%p - 1` gets all `DerefBytes` of `%p`, starting one byte before the known range.
The comment says this avoids "increasing" dereferenceability, but it shifts the known range down instead of shrinking it.
For a negative offset take 0 bytes, i.e. replace the clamp with `int64_t Bytes = OffsetSExt < 0 ? 0 : std::max<int64_t>(0, DerefBytes - OffsetSExt);` in both `take*DerefBytes*` calls.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to