llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[DSE] Miscompile due misuse of AA

Open DaniilSuchkov opened this issue 6 years ago • 10 comments

Bugzilla Link 44704
Resolution FIXED
Resolved on Mar 02, 2020 00:09
Version trunk
OS Linux
CC @alinas,@arpilipe,@topperc,@DMG862,@efriedma-quic,@fhahn,@hfinkel,@duck-37,@preames

Extended Description

On this IR:

define i8* @​foo() { entry: %var1 = call i8* @​calloc(i64 10000, i64 1) br label %loop

loop: ; preds = %entry, %loop %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ] %iv.next = add i64 %iv, 1 %addr11 = getelementptr i8, i8* %var1, i64 %iv.next store i8 1, i8* %addr11 %addr18 = getelementptr i8, i8* %var1, i64 %iv store i8 0, i8* %addr18 %var9 = icmp ugt i64 %iv, 1000 br i1 %var9, label %exit, label %loop

exit: ; preds = %loop ret i8* %var1 }

declare noalias i8* @​calloc(i64, i64)

This command: opt -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -S input.ll

Says that %addr11 and %addr18 don't alias, while actually they do. Because of this, DSE eliminates (store i8 0, i8* %addr18), since store of 0 to memory allocated by calloc that doesn't alias with anything between the store and the allocation is redundant.

So this command: opt -aa-pipeline=basic-aa -passes=dse -S -o /dev/null -debug-only=dse input.ll

Produces this output: DSE: Remove null store to the calloc'ed object: DEAD: store i8 0, i8* %addr18 OBJECT: %var1 = call i8* @​calloc(i64 10000, i64 1)

If anyone is interested, C++ reproducer looks like this (reproducible with -O2 since clang 3.8): #include #include

attribute((noinline)) int *foo(int arr_size) { int arr = (int)calloc(arr_size, sizeof(int)); for (int i = 0; i < arr_size - 1; ++i) { arr[i + 1] = 1; arr[i] = 0; } return arr; }

int main() { int arr_size = 10; int *arr = foo(arr_size); for (int i = 0; i < arr_size; ++i) std::cout << arr[i] << ' '; free(arr); }

It prints 0 1 1 1 1 1 1 1 1 1

instead of 0 0 0 0 0 0 0 0 0 1

Here's what I found: For two GEPs with the same base pointer following logic in BasicAA leads to this bug:

  1. Decompose both GEPs into (Base + (var-offsets + const-offset))
  2. Find difference in offsets. In this case GEPs are (%var1 + %iv) and (%var1 + %iv + 1), so difference is 1.
  3. If the difference is >= size of one element, pointers don't alias.

Obviously, it doesn't work for cases when loop PHIs are parts of offsets. Function aliasSameBasePointerGEPs tries to analyze such cases, but in this case its answer is MayAlias, so we proceed to analyze till we reach the incorrect check mentioned above. I'm not really familiar with BasicAA, so any ideas on the best way to fix it are welcome. Based on what I know I only can conservatively disable this buggy analysis for the case when PHIs are involved.

DaniilSuchkov avatar Jan 29 '20 11:01 DaniilSuchkov

Fixed by https://reviews.llvm.org/rG02e3d5c3a25b9c00c0ab9bdd59931fde606ee0b4

DaniilSuchkov avatar Mar 02 '20 08:03 DaniilSuchkov

Apparently there is already a conservative fix for this bug on review: https://reviews.llvm.org/D68006

DaniilSuchkov avatar Jan 30 '20 04:01 DaniilSuchkov

You're right. There are tests in test/Transforms/DeadStoreElimination/simple.ll checking for diamond/triangle CFGs, so I'm assuming we care.

I agree. I think that we care.

hfinkel avatar Jan 29 '20 21:01 hfinkel

You're right. There are tests in test/Transforms/DeadStoreElimination/simple.ll checking for diamond/triangle CFGs, so I'm assuming we care.

alinas avatar Jan 29 '20 20:01 alinas

Just sticking "return false" there would also refuse to scan past if statments (triangle or diamond CFG). Not sure how much we care.

efriedma-quic avatar Jan 29 '20 20:01 efriedma-quic

A simple fix I see is: lib/Transforms/Scalar/DeadStoreElimination.cpp:643

  • continue;
  • return false;

AFAICT this DSE implementation has no checks for Loops, so it makes sense to bail as soon as a we know this method operates on a Loop (a block is already visited)

alinas avatar Jan 29 '20 19:01 alinas

Eli is correct, the issue is DSE ignoring loop backedges. BasicAA is correct. This needs phi translation, like this: https://github.com/llvm/llvm-project/blob/ b136238bb4e502a46b720d6002cd5b7c248486c2/llvm/include/llvm/Analysis/ MemorySSA.h#L1225 The patches for DSE with MemorySSA are still in the works by @​fhahn.

I'll post an updated set of patches soon, hopefully later today or tomorrow

But we should probably also try to fix it in the current DSE.

fhahn avatar Jan 29 '20 18:01 fhahn

Eli is correct, the issue is DSE ignoring loop backedges. BasicAA is correct. This needs phi translation, like this: https://github.com/llvm/llvm-project/blob/ b136238bb4e502a46b720d6002cd5b7c248486c2/llvm/include/llvm/Analysis/ MemorySSA.h#L1225 The patches for DSE with MemorySSA are still in the works by @​fhahn.

I'll post an updated set of patches soon, hopefully later today or tomorrow

fhahn avatar Jan 29 '20 18:01 fhahn

Eli is correct, the issue is DSE ignoring loop backedges. BasicAA is correct. This needs phi translation, like this: https://github.com/llvm/llvm-project/blob/b136238bb4e502a46b720d6002cd5b7c248486c2/llvm/include/llvm/Analysis/MemorySSA.h#L1225 The patches for DSE with MemorySSA are still in the works by @​fhahn.

alinas avatar Jan 29 '20 18:01 alinas

BasicAA is correct: for any given iteration of the loop, the two stores don't alias. BasicAA doesn't try to figure out whether different versions of the pointers could alias. That requires using LoopAccessAnalysis or something like that.

The problem is probably the way DSE is ignoring backedges in memoryIsNotModifiedBetween. See https://github.com/llvm/llvm-project/blob/b136238bb4e502a46b720d6002cd5b7c248486c2/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp#L643 .

efriedma-quic avatar Jan 29 '20 16:01 efriedma-quic