java-topology/defects/llvm/patch/llvm-0002-aliasset-memorylocations-denseset.patch

70 lines
2.8 KiB
Diff

# UNDF: UNDF-2026-000000153
diff --git a/llvm/include/llvm/Analysis/AliasSetTracker.h b/llvm/include/llvm/Analysis/AliasSetTracker.h
index a1b2c3d..b4e5f6a 100644
--- a/llvm/include/llvm/Analysis/AliasSetTracker.h
+++ b/llvm/include/llvm/Analysis/AliasSetTracker.h
@@ -13,6 +13,7 @@
#ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
#define LLVM_ANALYSIS_ALIASSETTRACKER_H
+#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
@@ -52,8 +53,8 @@ class AliasSet : public ilist_node<AliasSet> {
// Forwarding pointer.
AliasSet *Forward = nullptr;
- /// Memory locations in this alias set.
- SmallVector<MemoryLocation, 0> MemoryLocs;
+ /// Memory locations in this alias set. CWE-407 fix: DenseSet for O(1)
+ /// membership test; replaces SmallVector whose is_contained was O(N).
+ DenseSet<MemoryLocation> MemoryLocs;
/// All instructions without a specific address in this alias set.
std::vector<AssertingVH<Instruction>> UnknownInsts;
@@ -119,8 +120,9 @@ public:
// Alias Set iteration - Allow access to all of the memory locations which are
// part of this alias set.
- using iterator = SmallVectorImpl<MemoryLocation>::const_iterator;
- iterator begin() const { return MemoryLocs.begin(); }
- iterator end() const { return MemoryLocs.end(); }
+ using iterator = DenseSet<MemoryLocation>::const_iterator;
+ iterator begin() const { return MemoryLocs.begin(); }
+ iterator end() const { return MemoryLocs.end(); }
unsigned size() const { return MemoryLocs.size(); }
diff --git a/llvm/lib/Analysis/AliasSetTracker.cpp b/llvm/lib/Analysis/AliasSetTracker.cpp
index 295e267..f1a2b3c 100644
--- a/llvm/lib/Analysis/AliasSetTracker.cpp
+++ b/llvm/lib/Analysis/AliasSetTracker.cpp
@@ -62,9 +62,9 @@ void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST,
// Merge the list of constituent memory locations...
if (MemoryLocs.empty()) {
- std::swap(MemoryLocs, AS.MemoryLocs);
+ MemoryLocs = std::move(AS.MemoryLocs);
+ AS.MemoryLocs.clear();
} else {
- append_range(MemoryLocs, AS.MemoryLocs);
+ MemoryLocs.insert(AS.MemoryLocs.begin(), AS.MemoryLocs.end());
AS.MemoryLocs.clear();
}
@@ -119,7 +119,7 @@ void AliasSet::addMemoryLocation(AliasSetTracker &AST,
// If we cannot find a must-alias with any of the existing MemoryLocs, we
// upgrade the alias lattice to may-alias.
...
- MemoryLocs.push_back(MemLoc);
+ MemoryLocs.insert(MemLoc);
}
@@ -275,7 +275,8 @@ AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
if (MapEntry) {
collapseForwardingIn(MapEntry);
- if (is_contained(MapEntry->MemoryLocs, MemLoc)) // O(N) — CWE-407
+ if (MapEntry->MemoryLocs.count(MemLoc)) // CWE-407 fix: O(1)
return *MapEntry;
}