[BasicAA] Add simple depth limit to avoid stack overflow (PR49151)
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 18 Feb 2021 22:13:33 +0000 (23:13 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 19 Feb 2021 10:05:42 +0000 (11:05 +0100)
This is a simpler variant of D96647. It just adds a straightforward
depth limit with a high cutoff, without introducing complex logic
for BatchAA consistency. It accepts that we may cache a sub-optimal
result if the depth limit is hit.

Eventually this should be more fully addressed by D96647 or similar,
but in the meantime this avoids stack overflows in a cheap way.

Differential Revision: https://reviews.llvm.org/D96996

llvm/lib/Analysis/BasicAliasAnalysis.cpp

index 81996f3..828d913 100644 (file)
@@ -1559,6 +1559,13 @@ AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
     V2Size = LocationSize::afterPointer();
   }
 
+  // FIXME: If this depth limit is hit, then we may cache sub-optimal results
+  // for recursive queries. For this reason, this limit is chosen to be large
+  // enough to be very rarely hit, while still being small enough to avoid
+  // stack overflows.
+  if (AAQI.Depth >= 512)
+    return MayAlias;
+
   // Check the cache before climbing up use-def chains. This also terminates
   // otherwise infinitely recursive queries.
   AAQueryInfo::LocPair Locs(MemoryLocation(V1, V1Size, V1AAInfo),