Skip to content

Commit eef5f43

Browse files
committed
Reserve attribute to C-only and rename to 'kcfi_salt', indicating that it's specifically for the kernel.
1 parent 9e164e5 commit eef5f43

File tree

10 files changed

+37
-63
lines changed

10 files changed

+37
-63
lines changed

clang/include/clang/AST/Type.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4651,11 +4651,11 @@ class FunctionType : public Type {
46514651
/// \p AttributedType.
46524652
struct alignas(void *) FunctionTypeExtraAttributeInfo {
46534653
/// A CFI "salt" that differentiates functions with the same prototype.
4654-
StringRef CFISalt;
4654+
StringRef KCFISalt;
46554655

4656-
operator bool() const { return !CFISalt.empty(); }
4656+
operator bool() const { return !KCFISalt.empty(); }
46574657

4658-
void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddString(CFISalt); }
4658+
void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddString(KCFISalt); }
46594659
};
46604660

46614661
/// The AArch64 SME ACLE (Arm C/C++ Language Extensions) define a number

clang/include/clang/Basic/Attr.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3896,11 +3896,12 @@ def CFICanonicalJumpTable : InheritableAttr {
38963896
let SimpleHandler = 1;
38973897
}
38983898

3899-
def CFISalt : DeclOrTypeAttr {
3900-
let Spellings = [Clang<"cfi_salt">];
3899+
def KCFISalt : DeclOrTypeAttr {
3900+
let Spellings = [Clang<"kcfi_salt">];
39013901
let Args = [StringArgument<"Salt">];
39023902
let Subjects = SubjectList<[Function, Field, Var, TypedefName], ErrorDiag>;
3903-
let Documentation = [CFISaltDocs];
3903+
let Documentation = [KCFISaltDocs];
3904+
let LangOpts = [COnly];
39043905
}
39053906

39063907
// C/C++ Thread safety attributes (e.g. for deadlock, data race checking)

clang/include/clang/Basic/AttrDocs.td

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,10 +3643,10 @@ make the function's CFI jump table canonical. See :ref:`the CFI documentation
36433643
}];
36443644
}
36453645

3646-
def CFISaltDocs : Documentation {
3646+
def KCFISaltDocs : Documentation {
36473647
let Category = DocCatFunction;
36483648
let Content = [{
3649-
Use ``__attribute__((cfi_salt("<salt>")))`` on a function declaration, function
3649+
Use ``__attribute__((kcfi_salt("<salt>")))`` on a function declaration, function
36503650
definition, or typedef to help distinguish CFI hashes between functions with
36513651
the same type signature.
36523652

@@ -3655,11 +3655,11 @@ Example use:
36553655
.. code-block:: c
36563656

36573657
// .h file:
3658-
#define __cfi_salt __attribute__((cfi_salt("vogon")))
3658+
#define __kcfi_salt __attribute__((kcfi_salt("vogon")))
36593659

36603660
// Convenient typedefs to avoid nested declarator syntax.
36613661
typedef int (*fptr_t)(void); // Non-salted function call.
3662-
typedef int (*fptr_salted_t)(void) __cfi_salt;
3662+
typedef int (*fptr_salted_t)(void) __kcfi_salt;
36633663

36643664
struct widget_generator {
36653665
fptr_t init;
@@ -3669,7 +3669,7 @@ Example use:
36693669

36703670
// 1st .c file:
36713671
static int internal_init(void) { /* ... */ }
3672-
static int internal_salted_exec(void) __cfi_salt { /* ... */ }
3672+
static int internal_salted_exec(void) __kcfi_salt { /* ... */ }
36733673
static int internal_teardown(void) { /* ... */ }
36743674

36753675
static struct widget_generator _generator = {

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3680,7 +3680,7 @@ FunctionProtoType::FunctionProtoType(QualType result, ArrayRef<QualType> params,
36803680
// Propagate any extra attribute information.
36813681
if (epi.requiresFunctionProtoTypeExtraAttributeInfo()) {
36823682
auto &ExtraAttrInfo = *getTrailingObjects<FunctionTypeExtraAttributeInfo>();
3683-
ExtraAttrInfo.CFISalt = epi.ExtraAttributeInfo.CFISalt;
3683+
ExtraAttrInfo.KCFISalt = epi.ExtraAttributeInfo.KCFISalt;
36843684

36853685
// Also set the bit in FunctionTypeExtraBitfields.
36863686
auto &ExtraBits = *getTrailingObjects<FunctionTypeExtraBitfields>();

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,8 +2106,8 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
21062106
case attr::ExtVectorType:
21072107
OS << "ext_vector_type";
21082108
break;
2109-
case attr::CFISalt:
2110-
OS << "cfi_salt(\"" << cast<CFISaltAttr>(T->getAttr())->getSalt() << "\")";
2109+
case attr::KCFISalt:
2110+
OS << "kcfi_salt(\"" << cast<KCFISaltAttr>(T->getAttr())->getSalt() << "\")";
21112111
break;
21122112
}
21132113
OS << "))";

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2912,7 +2912,7 @@ void CodeGenFunction::EmitKCFIOperandBundle(
29122912

29132913
StringRef Salt;
29142914
if (const auto &Info = FP->getExtraAttributeInfo())
2915-
Salt = Info.CFISalt;
2915+
Salt = Info.KCFISalt;
29162916

29172917
Bundles.emplace_back("kcfi", CGM.CreateKCFITypeId(FP->desugar(), Salt));
29182918
}

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3022,7 +3022,7 @@ void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) {
30223022

30233023
if (const auto *FP = FD->getType()->getAs<FunctionProtoType>())
30243024
if (const auto &Info = FP->getExtraAttributeInfo())
3025-
Salt = Info.CFISalt;
3025+
Salt = Info.KCFISalt;
30263026

30273027
F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
30283028
llvm::MDNode::get(Ctx, MDB.createConstant(CreateKCFITypeId(

clang/lib/Sema/SemaType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ static void diagnoseBadTypeAttribute(Sema &S, const ParsedAttr &attr,
157157
case ParsedAttr::AT_Regparm: \
158158
case ParsedAttr::AT_CFIUncheckedCallee: \
159159
case ParsedAttr::AT_CmseNSCall: \
160+
case ParsedAttr::AT_KCFISalt: \
160161
case ParsedAttr::AT_ArmStreaming: \
161162
case ParsedAttr::AT_ArmStreamingCompatible: \
162163
case ParsedAttr::AT_ArmPreserves: \
163164
case ParsedAttr::AT_ArmIn: \
164165
case ParsedAttr::AT_ArmOut: \
165166
case ParsedAttr::AT_ArmInOut: \
166167
case ParsedAttr::AT_ArmAgnostic: \
167-
case ParsedAttr::AT_CFISalt: \
168168
case ParsedAttr::AT_AnyX86NoCallerSavedRegisters: \
169169
case ParsedAttr::AT_AnyX86NoCfCheck: \
170170
CALLING_CONV_ATTRS_CASELIST
@@ -7940,14 +7940,14 @@ static bool handleFunctionTypeAttr(TypeProcessingState &state, ParsedAttr &attr,
79407940
return true;
79417941
}
79427942

7943-
if (attr.getKind() == ParsedAttr::AT_CFISalt) {
7943+
if (attr.getKind() == ParsedAttr::AT_KCFISalt) {
79447944
StringRef Argument;
79457945
if (!S.checkStringLiteralArgumentAttr(attr, 0, Argument))
79467946
return false;
79477947

79487948
const auto *FnTy = unwrapped.get()->getAs<FunctionProtoType>();
79497949
FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
7950-
EPI.ExtraAttributeInfo.CFISalt = Argument;
7950+
EPI.ExtraAttributeInfo.KCFISalt = Argument;
79517951

79527952
QualType newtype = S.Context.getFunctionType(FnTy->getReturnType(),
79537953
FnTy->getParamTypes(), EPI);

clang/test/CodeGen/kcfi-salt.c

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fsanitize=kcfi -o - %s | FileCheck %s
2-
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fsanitize=kcfi -x c++ -o - %s | FileCheck %s --check-prefixes=CHECK,MEMBER
32
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fsanitize=kcfi -fpatchable-function-entry-offset=3 -o - %s | FileCheck %s --check-prefixes=CHECK,OFFSET
43

54
// Note that the interleving of functions, which normally would be in sequence,
@@ -9,13 +8,13 @@
98
#error Missing kcfi?
109
#endif
1110

12-
#define __cfi_salt __attribute__((cfi_salt("pepper")))
11+
#define __kcfi_salt __attribute__((kcfi_salt("pepper")))
1312

1413
typedef int (*fn_t)(void);
15-
typedef int __cfi_salt (*fn_salt_t)(void);
14+
typedef int __kcfi_salt (*fn_salt_t)(void);
1615

1716
typedef unsigned int (*ufn_t)(void);
18-
typedef unsigned int __cfi_salt (*ufn_salt_t)(void);
17+
typedef unsigned int __kcfi_salt (*ufn_salt_t)(void);
1918

2019
/// Must emit __kcfi_typeid symbols for address-taken function declarations
2120
// CHECK: module asm ".weak __kcfi_typeid_[[F4:[a-zA-Z0-9_]+]]"
@@ -27,25 +26,25 @@ typedef unsigned int __cfi_salt (*ufn_salt_t)(void);
2726
// CHECK-NOT: module asm ".weak __kcfi_typeid_{{f6|_Z2f6v}}"
2827

2928
int f1(void);
30-
int f1_salt(void) __cfi_salt;
29+
int f1_salt(void) __kcfi_salt;
3130

3231
unsigned int f2(void);
33-
unsigned int f2_salt(void) __cfi_salt;
32+
unsigned int f2_salt(void) __kcfi_salt;
3433

3534
static int f3(void);
36-
static int f3_salt(void) __cfi_salt;
35+
static int f3_salt(void) __kcfi_salt;
3736

3837
extern int f4(void);
39-
extern int f4_salt(void) __cfi_salt;
38+
extern int f4_salt(void) __kcfi_salt;
4039

4140
static int f5(void);
42-
static int f5_salt(void) __cfi_salt;
41+
static int f5_salt(void) __kcfi_salt;
4342

4443
extern int f6(void);
45-
extern int f6_salt(void) __cfi_salt;
44+
extern int f6_salt(void) __kcfi_salt;
4645

4746
struct cfi_struct {
48-
fn_t __cfi_salt fptr;
47+
fn_t __kcfi_salt fptr;
4948
fn_salt_t td_fptr;
5049
};
5150

@@ -67,7 +66,7 @@ int __call(fn_t f) __attribute__((__no_sanitize__("kcfi"))) {
6766
// CHECK-LABEL: @{{call_salt_ty|_Z12call_salt_tyPFivE}}
6867
// CHECK: call{{.*}} i32 %{{.}}(){{.*}} [ "kcfi"(i32 [[#SALTY_HASH]]) ]
6968
int call(fn_t f) { return f(); }
70-
int call_salt(fn_t __cfi_salt f) { return f(); }
69+
int call_salt(fn_t __kcfi_salt f) { return f(); }
7170
int call_salt_ty(fn_salt_t f) { return f(); }
7271

7372
// CHECK-LABEL: @{{ucall|_Z5ucallPFjvE}}
@@ -77,7 +76,7 @@ int call_salt_ty(fn_salt_t f) { return f(); }
7776
// CHECK-LABEL: @{{ucall_salt_ty|_Z13ucall_salt_tyPFjvE}}
7877
// CHECK: call{{.*}} i32 %{{.}}(){{.*}} [ "kcfi"(i32 [[#SALTY_UHASH]]) ]
7978
unsigned int ucall(ufn_t f) { return f(); }
80-
unsigned int ucall_salt(ufn_t __cfi_salt f) { return f(); }
79+
unsigned int ucall_salt(ufn_t __kcfi_salt f) { return f(); }
8180
unsigned int ucall_salt_ty(ufn_salt_t f) { return f(); }
8281

8382
int test1(struct cfi_struct *ptr) {
@@ -115,21 +114,21 @@ int test1(struct cfi_struct *ptr) {
115114
// CHECK-LABEL: define dso_local{{.*}} i32 @{{f1_salt|_Z7f1_saltv}}(){{.*}} !kcfi_type
116115
// CHECK-SAME: ![[#SALTY_TYPE:]]
117116
int f1(void) { return 0; }
118-
int f1_salt(void) __cfi_salt { return 0; }
117+
int f1_salt(void) __kcfi_salt { return 0; }
119118

120119
// CHECK-LABEL: define dso_local{{.*}} i32 @{{f2|_Z2f2v}}(){{.*}} !kcfi_type
121120
// CHECK-SAME: ![[#LOW_SODIUM_UTYPE:]]
122121
// CHECK: define dso_local{{.*}} i32 @{{f2_salt|_Z7f2_saltv}}(){{.*}} !kcfi_type
123122
// CHECK-SAME: ![[#SALTY_UTYPE:]]
124123
unsigned int f2(void) { return 2; }
125-
unsigned int f2_salt(void) __cfi_salt { return 2; }
124+
unsigned int f2_salt(void) __kcfi_salt { return 2; }
126125

127126
// CHECK-LABEL: define internal{{.*}} i32 @{{f3|_ZL2f3v}}(){{.*}} !kcfi_type
128127
// CHECK-SAME: ![[#LOW_SODIUM_TYPE]]
129128
// CHECK-LABEL: define internal{{.*}} i32 @{{f3_salt|_ZL7f3_saltv}}(){{.*}} !kcfi_type
130129
// CHECK-SAME: ![[#SALTY_TYPE]]
131130
static int f3(void) { return 1; }
132-
static int f3_salt(void) __cfi_salt { return 1; }
131+
static int f3_salt(void) __kcfi_salt { return 1; }
133132

134133
// CHECK: declare !kcfi_type ![[#LOW_SODIUM_TYPE]]{{.*}} i32 @[[F4]]()
135134
// CHECK: declare !kcfi_type ![[#SALTY_TYPE]]{{.*}} i32 @[[F4_SALT]]()
@@ -142,7 +141,7 @@ static int f3_salt(void) __cfi_salt { return 1; }
142141
// CHECK-NOT: !kcfi_type
143142
// CHECK-SAME: {
144143
static int f5(void) { return 2; }
145-
static int f5_salt(void) __cfi_salt { return 2; }
144+
static int f5_salt(void) __kcfi_salt { return 2; }
146145

147146
// CHECK: declare !kcfi_type ![[#LOW_SODIUM_TYPE]]{{.*}} i32 @{{f6|_Z2f6v}}()
148147
// CHECK: declare !kcfi_type ![[#SALTY_TYPE]]{{.*}} i32 @{{f6_salt|_Z7f6_saltv}}()
@@ -154,29 +153,6 @@ static int f5_salt(void) __cfi_salt { return 2; }
154153
int f7_salt(struct cfi_struct *ptr) { return ptr->fptr(); }
155154
int f7_typedef_salt(struct cfi_struct *ptr) { return ptr->td_fptr(); }
156155

157-
#ifdef __cplusplus
158-
// MEMBER-LABEL: define dso_local void @_Z16test_member_callv() #0 !kcfi_type
159-
// MEMBER: call void %[[#]](ptr{{.*}} [ "kcfi"(i32 [[#%d,MEMBER_LOW_SODIUM_HASH:]]) ]
160-
// MEMBER: call void %[[#]](ptr{{.*}} [ "kcfi"(i32 [[#%d,MEMBER_SALT_HASH:]]) ]
161-
162-
// MEMBER-LABEL: define{{.*}} void @_ZN1A1fEv(ptr{{.*}} %this){{.*}} !kcfi_type
163-
// MEMBER-SAME: [[#MEMBER_LOW_SODIUM_TYPE:]]
164-
// MEMBER-LABEL: define{{.*}} void @_ZN1A1gEv(ptr{{.*}} %this){{.*}} !kcfi_type
165-
// MEMBER-SAME: [[#MEMBER_SALT_TYPE:]]
166-
struct A {
167-
void f() {}
168-
void __cfi_salt g() {}
169-
};
170-
171-
void test_member_call(void) {
172-
void (A::* p)() = &A::f;
173-
(A().*p)();
174-
175-
void __cfi_salt (A::* q)() = &A::g;
176-
(A().*q)();
177-
}
178-
#endif
179-
180156
// CHECK: ![[#]] = !{i32 4, !"kcfi", i32 1}
181157
// OFFSET: ![[#]] = !{i32 4, !"kcfi-offset", i32 3}
182158
//
@@ -185,6 +161,3 @@ void test_member_call(void) {
185161
//
186162
// CHECK: ![[#LOW_SODIUM_UTYPE]] = !{i32 [[#LOW_SODIUM_UHASH]]}
187163
// CHECK: ![[#SALTY_UTYPE]] = !{i32 [[#SALTY_UHASH]]}
188-
//
189-
// MEMBER: ![[#MEMBER_LOW_SODIUM_TYPE]] = !{i32 [[#MEMBER_LOW_SODIUM_HASH]]}
190-
// MEMBER: ![[#MEMBER_SALT_TYPE]] = !{i32 [[#MEMBER_SALT_HASH]]}

clang/test/Misc/pragma-attribute-supported-attributes-list.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
// CHECK-NEXT: CFConsumed (SubjectMatchRule_variable_is_parameter)
3232
// CHECK-NEXT: CFGuard (SubjectMatchRule_function)
3333
// CHECK-NEXT: CFICanonicalJumpTable (SubjectMatchRule_function)
34-
// CHECK-NEXT: CFISalt (SubjectMatchRule_function, SubjectMatchRule_field, SubjectMatchRule_variable, SubjectMatchRule_type_alias)
3534
// CHECK-NEXT: CFUnknownTransfer (SubjectMatchRule_function)
3635
// CHECK-NEXT: CPUDispatch (SubjectMatchRule_function)
3736
// CHECK-NEXT: CPUSpecific (SubjectMatchRule_function)
@@ -97,6 +96,7 @@
9796
// CHECK-NEXT: Leaf (SubjectMatchRule_function)
9897
// CHECK-NEXT: LoaderUninitialized (SubjectMatchRule_variable_is_global)
9998
// CHECK-NEXT: Lockable (SubjectMatchRule_record)
99+
// CHECK-NEXT: KCFISalt (SubjectMatchRule_function, SubjectMatchRule_field, SubjectMatchRule_variable, SubjectMatchRule_type_alias)
100100
// CHECK-NEXT: MIGServerRoutine (SubjectMatchRule_function, SubjectMatchRule_objc_method, SubjectMatchRule_block)
101101
// CHECK-NEXT: MSConstexpr (SubjectMatchRule_function)
102102
// CHECK-NEXT: MSStruct (SubjectMatchRule_record)

0 commit comments

Comments
 (0)