Skip to content

Commit 2f6baac

Browse files
committed
[Runtime] Ensure we *always* generate names for ObjC classes.
If something fails while trying to generate a name for an ObjC class, we fall back to a generated name based on the metadata pointer. This ensures that we won't crash, and if the name turns up in the debugger or somewhere it should be obvious that something went wrong. rdar://107718586
1 parent acc465d commit 2f6baac

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,11 +3034,26 @@ static inline ClassROData *getROData(ClassMetadata *theClass) {
30343034
return (ClassROData*)(theClass->Data & ~uintptr_t(SWIFT_CLASS_IS_SWIFT_MASK));
30353035
}
30363036

3037+
// This gets called if we fail during copyGenericClassObjcName(). Its job is
3038+
// to generate a unique name, even though the name won't be very helpful if
3039+
// we end up looking at it in a debugger.
3040+
#define EMERGENCY_PREFIX "$SwiftEmergencyPlaceholderClassName"
3041+
static char *copyEmergencyName(ClassMetadata *theClass) {
3042+
char *nameBuf = nullptr;
3043+
asprintf(&nameBuf,
3044+
EMERGENCY_PREFIX "%016" PRIxPTR,
3045+
(uintptr_t)theClass);
3046+
return nameBuf;
3047+
}
3048+
30373049
static char *copyGenericClassObjCName(ClassMetadata *theClass) {
30383050
// Use the remangler to generate a mangled name from the type metadata.
30393051
Demangle::StackAllocatedDemangler<4096> Dem;
30403052

30413053
auto demangling = _swift_buildDemanglingForMetadata(theClass, Dem);
3054+
if (!demangling) {
3055+
return copyEmergencyName(theClass);
3056+
}
30423057

30433058
// Remangle that into a new type mangling string.
30443059
auto typeNode = Dem.createNode(Demangle::Node::Kind::TypeMangling);
@@ -3048,7 +3063,7 @@ static char *copyGenericClassObjCName(ClassMetadata *theClass) {
30483063

30493064
auto mangling = Demangle::mangleNodeOld(globalNode, Dem);
30503065
if (!mangling.isSuccess()) {
3051-
return nullptr;
3066+
return copyEmergencyName(theClass);
30523067
}
30533068
llvm::StringRef string = mangling.result();
30543069

@@ -3061,7 +3076,7 @@ static char *copyGenericClassObjCName(ClassMetadata *theClass) {
30613076
size_t allocationSize = string.size() + 1;
30623077
if (addSuffix)
30633078
allocationSize += 1;
3064-
3079+
30653080
auto fullNameBuf = (char*)swift_slowAlloc(allocationSize, 0);
30663081
memcpy(fullNameBuf, string.data(), string.size());
30673082

0 commit comments

Comments
 (0)