Skip to content

Commit 8f072d1

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 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 8f072d1

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
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: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ swift_stdlib_getTypeByMangledNameUntrusted(const char *typeNameStart,
19301930
if (c >= '\x01' && c <= '\x1F')
19311931
return nullptr;
19321932
}
1933-
1933+
19341934
return swift_getTypeByMangledName(MetadataState::Complete, typeName, nullptr,
19351935
{}, {}).getType().getMetadata();
19361936
}
@@ -2197,6 +2197,23 @@ swift_getOpaqueTypeConformance(const void * const *arguments,
21972197
// Return the ObjC class for the given type name.
21982198
// This gets installed as a callback from libobjc.
21992199

2200+
static bool validateObjCMangledName(const char *_Nonnull typeName) {
2201+
// Accept names with a mangling prefix.
2202+
if (getManglingPrefixLength(typeName))
2203+
return true;
2204+
2205+
// Accept names that start with a digit (unprefixed mangled names).
2206+
if (isdigit(typeName[0]))
2207+
return true;
2208+
2209+
// Accept names that contain a dot.
2210+
if (strchr(typeName, '.'))
2211+
return true;
2212+
2213+
// Reject anything else.
2214+
return false;
2215+
}
2216+
22002217
// FIXME: delete this #if and dlsym once we don't
22012218
// need to build with older libobjc headers
22022219
#if !OBJC_GETCLASSHOOK_DEFINED
@@ -2232,8 +2249,9 @@ getObjCClassByMangledName(const char * _Nonnull typeName,
22322249
[&](const Metadata *type, unsigned index) { return nullptr; }
22332250
).getType().getMetadata();
22342251
} else {
2235-
metadata = swift_stdlib_getTypeByMangledNameUntrusted(typeStr.data(),
2236-
typeStr.size());
2252+
if (validateObjCMangledName(typeName))
2253+
metadata = swift_stdlib_getTypeByMangledNameUntrusted(typeStr.data(),
2254+
typeStr.size());
22372255
}
22382256
if (metadata) {
22392257
auto objcClass =

0 commit comments

Comments
 (0)