Skip to content

Commit c5fc40e

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 (cherry picked from commit 8f072d1)
1 parent fec9add commit c5fc40e

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

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,7 +1919,7 @@ swift_stdlib_getTypeByMangledNameUntrusted(const char *typeNameStart,
19191919
if (c >= '\x01' && c <= '\x1F')
19201920
return nullptr;
19211921
}
1922-
1922+
19231923
return swift_getTypeByMangledName(MetadataState::Complete, typeName, nullptr,
19241924
{}, {}).getType().getMetadata();
19251925
}
@@ -2186,6 +2186,23 @@ swift_getOpaqueTypeConformance(const void * const *arguments,
21862186
// Return the ObjC class for the given type name.
21872187
// This gets installed as a callback from libobjc.
21882188

2189+
static bool validateObjCMangledName(const char *_Nonnull typeName) {
2190+
// Accept names with a mangling prefix.
2191+
if (getManglingPrefixLength(typeName))
2192+
return true;
2193+
2194+
// Accept names that start with a digit (unprefixed mangled names).
2195+
if (isdigit(typeName[0]))
2196+
return true;
2197+
2198+
// Accept names that contain a dot.
2199+
if (strchr(typeName, '.'))
2200+
return true;
2201+
2202+
// Reject anything else.
2203+
return false;
2204+
}
2205+
21892206
// FIXME: delete this #if and dlsym once we don't
21902207
// need to build with older libobjc headers
21912208
#if !OBJC_GETCLASSHOOK_DEFINED
@@ -2221,8 +2238,9 @@ getObjCClassByMangledName(const char * _Nonnull typeName,
22212238
[&](const Metadata *type, unsigned index) { return nullptr; }
22222239
).getType().getMetadata();
22232240
} else {
2224-
metadata = swift_stdlib_getTypeByMangledNameUntrusted(typeStr.data(),
2225-
typeStr.size());
2241+
if (validateObjCMangledName(typeName))
2242+
metadata = swift_stdlib_getTypeByMangledNameUntrusted(typeStr.data(),
2243+
typeStr.size());
22262244
}
22272245
if (metadata) {
22282246
auto objcClass =

0 commit comments

Comments
 (0)