Skip to content

[ObjC]: Make type encoding safe in symbol names #77797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions clang/lib/CodeGen/CGObjCGNU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1431,12 +1431,24 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
const std::string &TypeEncoding) override {
return GetConstantSelector(Sel, TypeEncoding);
}
std::string GetSymbolNameForTypeEncoding(const std::string &TypeEncoding) {
std::string MangledTypes = std::string(TypeEncoding);
// @ is used as a special character in ELF symbol names (used for symbol
// versioning), so mangle the name to not include it. Replace it with a
// character that is not a valid type encoding character (and, being
// non-printable, never will be!)
if (CGM.getTriple().isOSBinFormatELF())
std::replace(MangledTypes.begin(), MangledTypes.end(), '@', '\1');
// = in dll exported names causes lld to fail when linking on Windows.
if (CGM.getTriple().isOSWindows())
std::replace(MangledTypes.begin(), MangledTypes.end(), '=', '\2');
return MangledTypes;
}
llvm::Constant *GetTypeString(llvm::StringRef TypeEncoding) {
if (TypeEncoding.empty())
return NULLPtr;
std::string MangledTypes = std::string(TypeEncoding);
std::replace(MangledTypes.begin(), MangledTypes.end(),
'@', '\1');
std::string MangledTypes =
GetSymbolNameForTypeEncoding(std::string(TypeEncoding));
std::string TypesVarName = ".objc_sel_types_" + MangledTypes;
auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName);
if (!TypesGlobal) {
Expand All @@ -1453,13 +1465,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
}
llvm::Constant *GetConstantSelector(Selector Sel,
const std::string &TypeEncoding) override {
// @ is used as a special character in symbol names (used for symbol
// versioning), so mangle the name to not include it. Replace it with a
// character that is not a valid type encoding character (and, being
// non-printable, never will be!)
std::string MangledTypes = TypeEncoding;
std::replace(MangledTypes.begin(), MangledTypes.end(),
'@', '\1');
std::string MangledTypes = GetSymbolNameForTypeEncoding(TypeEncoding);
auto SelVarName = (StringRef(".objc_selector_") + Sel.getAsString() + "_" +
MangledTypes).str();
if (auto *GV = TheModule.getNamedGlobal(SelVarName))
Expand Down Expand Up @@ -1671,9 +1677,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
const ObjCIvarDecl *Ivar) override {
std::string TypeEncoding;
CGM.getContext().getObjCEncodingForType(Ivar->getType(), TypeEncoding);
// Prevent the @ from being interpreted as a symbol version.
std::replace(TypeEncoding.begin(), TypeEncoding.end(),
'@', '\1');
TypeEncoding = GetSymbolNameForTypeEncoding(TypeEncoding);
const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
+ '.' + Ivar->getNameAsString() + '.' + TypeEncoding;
return Name;
Expand Down
18 changes: 9 additions & 9 deletions clang/test/CodeGenObjC/dllstorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ @implementation J {

// CHECK-IR-DAG: @"OBJC_IVAR_$_J._ivar" = global i32

// CHECK-NF-DAG: @"__objc_ivar_offset_J._ivar.\01" = hidden global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_J._ivar.@" = hidden global i32

@interface K : J
@end
Expand All @@ -56,7 +56,7 @@ @implementation K {

// CHECK-IR-DAG: @"OBJC_IVAR_$_K._ivar" = global i32

// CHECK-NF-DAG: @"__objc_ivar_offset_K._ivar.\01" = hidden global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_K._ivar.@" = hidden global i32

__declspec(dllexport)
@interface L : K
Expand Down Expand Up @@ -94,11 +94,11 @@ @implementation L {
// CHECK-IR-DAG: @"OBJC_IVAR_$_L._package" = global i32
// CHECK-IR-DAG: @"OBJC_IVAR_$_L._private" = global i32

// CHECK-NF-DAG: @"__objc_ivar_offset_L._none.\01" = hidden global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._public.\01" = dso_local dllexport global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._protected.\01" = dso_local dllexport global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._package.\01" = hidden global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._private.\01" = hidden global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._none.@" = hidden global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._public.@" = dso_local dllexport global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._protected.@" = dso_local dllexport global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._package.@" = hidden global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_L._private.@" = hidden global i32

__declspec(dllimport)
@interface M : I {
Expand All @@ -112,7 +112,7 @@ @interface M : I {
// CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32

// CHECK-NF-DAG: @"$_OBJC_REF_CLASS_M" = external dllimport global ptr
// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.\01" = external global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32

__declspec(dllexport)
__attribute__((__objc_exception__))
Expand Down Expand Up @@ -151,7 +151,7 @@ id f(Q *q) {

// CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32

// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.\01" = external global i32
// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32

int g(void) {
@autoreleasepool {
Expand Down
42 changes: 33 additions & 9 deletions clang/test/CodeGenObjC/encode-test-6.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o %t %s
// RUN: FileCheck < %t %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck -check-prefix CHECK-DWARF %s

// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -emit-llvm -fobjc-runtime=gnustep-2.0 -o - %s | FileCheck -check-prefix CHECK-MINGW %s

// RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm -fobjc-runtime=gnustep-2.0 -o - %s | FileCheck -check-prefix CHECK-MSVC %s

// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -fobjc-runtime=gnustep-2.0 -o - %s | FileCheck -check-prefix CHECK-ELF %s

typedef struct {} Z;

Expand All @@ -13,8 +18,17 @@ -(void)bar:(Z)a {}
-(void)foo:(Z)a: (char*)b : (Z)c : (double) d {}
@end

// CHECK: private unnamed_addr constant [14 x i8] c"v16@0:8{?=}16
// CHECK: private unnamed_addr constant [26 x i8] c"v32@0:8{?=}16*16{?=}24d24
// CHECK-DWARF: private unnamed_addr constant [14 x i8] c"v16@0:8{?=}16
// CHECK-DWARF: private unnamed_addr constant [26 x i8] c"v32@0:8{?=}16*16{?=}24d24

// CHECK-MINGW: @".objc_sel_types_v16@0:8{?\02}16" = linkonce_odr hidden constant [14 x i8] c"v16@0:8{?=}16\00"
// CHECK-MINGW: @".objc_sel_types_v32@0:8{?\02}16*16{?\02}24d24" = linkonce_odr hidden constant [26 x i8] c"v32@0:8{?=}16*16{?=}24d24\00"

// CHECK-MSVC: @".objc_sel_types_v20@0:8{?\02}16" = linkonce_odr hidden constant [14 x i8] c"v20@0:8{?=}16\00"
// CHECK-MSVC: @".objc_sel_types_v40@0:8{?\02}16*20{?\02}28d32" = linkonce_odr hidden constant [26 x i8] c"v40@0:8{?=}16*20{?=}28d32\00"

// CHECK-ELF: @".objc_sel_types_v16\010:8{?=}16" = linkonce_odr hidden constant [14 x i8] c"v16@0:8{?=}16\00"
// CHECK-ELF: @".objc_sel_types_v32\010:8{?=}16*16{?=}24d24" = linkonce_odr hidden constant [26 x i8] c"v32@0:8{?=}16*16{?=}24d24\00"

@interface NSObject @end

Expand All @@ -31,7 +45,10 @@ @implementation BABugExample
@synthesize property = _property;
@end

// CHECK: private unnamed_addr constant [8 x i8] c"@16
// CHECK-DWARF: private unnamed_addr constant [8 x i8] c"@16
// CHECK-MINGW: @".objc_sel_types_@16@0:8" = linkonce_odr hidden constant [8 x i8] c"@16@0:8\00"
// CHECK-MSVC: @".objc_sel_types_@16@0:8" = linkonce_odr hidden constant [8 x i8] c"@16@0:8\00"
// CHECK-ELF @".objc_sel_types_\0116\010:8" = linkonce_odr hidden constant [8 x i8] c"@16@0:8\00"

@class SCNCamera;
typedef SCNCamera C3DCamera;
Expand All @@ -48,14 +65,21 @@ @implementation SCNCamera
C3DCameraStorage _storage;
}
@end
// CHECK: private unnamed_addr constant [39 x i8] c"{?=\22presentationInstance\22@\22SCNCamera\22}\00"
// CHECK-DWARF: private unnamed_addr constant [39 x i8] c"{?=\22presentationInstance\22@\22SCNCamera\22}\00"
// CHECK-MINGW: @"__objc_ivar_offset_SCNCamera._storage.{?\02@}"
// CHECK-MSVC: @"__objc_ivar_offset_SCNCamera._storage.{?\02@}"
// CHECK-ELF: @"__objc_ivar_offset_SCNCamera._storage.{?=\01}"

int i;
typeof(@encode(typeof(i))) e = @encode(typeof(i));
const char * Test(void)
{
return e;
}
// CHECK: @e ={{.*}} global [2 x i8] c"i\00", align 1
// CHECK: define{{.*}} ptr @Test()
// CHECK: ret ptr @e
// CHECK-DWARF: @e ={{.*}} global [2 x i8] c"i\00", align 1
// CHECK-DWARF: define{{.*}} ptr @Test()
// CHECK-DWARF: ret ptr @e

// CHECK-MSVC: @e = dso_local global [2 x i8] c"i\00", align 1
// CHECK-MINGW: @e = dso_local global [2 x i8] c"i\00", align 1
// CHECK-ELF: @e = global [2 x i8] c"i\00", align 1