Skip to content

Commit 604eb05

Browse files
committed
[Runtime] Don't try to demangle unprefixed untrusted names. Remove operator new/delete hackery.
The operator new/delete overrides aren't working out due to inconsistent inlining of std::string creation/deletion. We can end up creating one with the global new but destroying it with our local delete. If they aren't compatible, this crashes. Instead, avoid problematic new/delete activity coming from lookup of ObjC class names. Names passed to getObjCClassByMangledName/swift_stdlib_getTypeByMangledNameUntrusted must either have a standard mangled name prefix, start with a digit (for unprefixed mangled names) or use the convenience dot syntax. Check for those up front and immediately reject anything else. This has the added bonus of failing more quickly for non-Swift names. rdar://93863030
1 parent 0b2b9a3 commit 604eb05

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

stdlib/public/runtime/Heap.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -133,30 +133,3 @@ static void swift_slowDeallocImpl(void *ptr, size_t alignMask) {
133133
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
134134
swift_slowDeallocImpl(ptr, alignMask);
135135
}
136-
137-
#if defined(__APPLE__) && defined(__MACH__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
138-
// On Darwin, define our own, hidden operator new/delete implementations. We
139-
// don't want to pick up any overrides that come from other code, but we also
140-
// don't want to expose our overrides to any other code. We can't do this
141-
// directly in C++, as the compiler has an implicit prototype with default
142-
// visibility. However, if we implement them as C functions using the C++
143-
// mangled names, the compiler accepts them without complaint, and the linker
144-
// still links all internal uses with these overrides.
145-
146-
__attribute__((visibility(("hidden")))) extern "C" void *_Znwm(size_t size) {
147-
return swift_slowAlloc(size, MALLOC_ALIGN_MASK);
148-
}
149-
150-
__attribute__((visibility(("hidden")))) extern "C" void _ZdlPv(void *ptr) {
151-
swift_slowDeallocImpl(ptr, MALLOC_ALIGN_MASK);
152-
}
153-
154-
__attribute__((visibility(("hidden")))) extern "C" void *_Znam(size_t size) {
155-
return swift_slowAlloc(size, MALLOC_ALIGN_MASK);
156-
}
157-
158-
__attribute__((visibility(("hidden")))) extern "C" void _ZdaPv(void *ptr) {
159-
swift_slowDeallocImpl(ptr, MALLOC_ALIGN_MASK);
160-
}
161-
162-
#endif

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,17 +1920,42 @@ swift_getTypeByMangledNameInContextInMetadataState(
19201920
}).getType().getMetadata();
19211921
}
19221922

1923-
/// Demangle a mangled name, but don't allow symbolic references.
1923+
static bool validateUntrustedName(llvm::StringRef typeName) {
1924+
size_t dotCount = false;
1925+
for (char c : typeName) {
1926+
// No symbolic references allowed in untrusted names.
1927+
if (c >= '\x01' && c <= '\x1F')
1928+
return false;
1929+
if (c == '.')
1930+
dotCount += 1;
1931+
}
1932+
1933+
// Accept any name with one dot.
1934+
if (dotCount == 1)
1935+
return true;
1936+
1937+
// Accept names with a mangling prefix.
1938+
if (getManglingPrefixLength(typeName))
1939+
return true;
1940+
1941+
// Accept names that start with a digit (unprefixed mangled names).
1942+
if (typeName.size() > 0 && isdigit(typeName[0]))
1943+
return true;
1944+
1945+
// Reject anything else.
1946+
return false;
1947+
}
1948+
1949+
/// Demangle a mangled name, but don't allow symbolic references, and require a
1950+
/// known prefix or a dot.
19241951
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
19251952
const Metadata *_Nullable
19261953
swift_stdlib_getTypeByMangledNameUntrusted(const char *typeNameStart,
19271954
size_t typeNameLength) {
19281955
llvm::StringRef typeName(typeNameStart, typeNameLength);
1929-
for (char c : typeName) {
1930-
if (c >= '\x01' && c <= '\x1F')
1931-
return nullptr;
1932-
}
1933-
1956+
if (!validateUntrustedName(typeName))
1957+
return nullptr;
1958+
19341959
return swift_getTypeByMangledName(MetadataState::Complete, typeName, nullptr,
19351960
{}, {}).getType().getMetadata();
19361961
}

0 commit comments

Comments
 (0)