Skip to content

Commit 5e91239

Browse files
committed
Fix compiler Test Registry to use a StringMap.
We have no guarantee that the StringRef passed to the registry lives in the static data segment. This resulted in memory corruption during bootstrapping, which is particularly difficult to diagnose and reproduce.
1 parent 52dfae5 commit 5e91239

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/SIL/Utils/Test.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using namespace swift::test;
2525
namespace {
2626

2727
class Registry {
28-
DenseMap<StringRef, FunctionTest *> registeredTests;
28+
StringMap<FunctionTest *> registeredTests;
2929
SwiftNativeFunctionTestThunk thunk;
3030

3131
public:
@@ -47,7 +47,8 @@ class Registry {
4747
SwiftNativeFunctionTestThunk getFunctionTestThunk() { return thunk; }
4848

4949
FunctionTest *getFunctionTest(StringRef name) {
50-
auto *res = registeredTests[name];
50+
// Avoid creating a new entry here.
51+
auto *res = registeredTests.lookup(name);
5152
if (!res) {
5253
llvm::errs() << "Found no test named " << name << "!\n";
5354
print(llvm::errs());
@@ -58,8 +59,9 @@ class Registry {
5859
void print(raw_ostream &OS) const {
5960
OS << "test::Registry(" << this << ") with " << registeredTests.size()
6061
<< " entries: {{\n";
61-
for (auto pair : registeredTests) {
62-
OS << "\t" << pair.getFirst() << " -> " << pair.getSecond() << "\n";
62+
for (auto &stringMapEntry : registeredTests) {
63+
OS << "\t" << stringMapEntry.getKey() << " -> "
64+
<< stringMapEntry.getValue() << "\n";
6365
}
6466
OS << "}} test::Registry(" << this << ")\n";
6567
}

0 commit comments

Comments
 (0)