Skip to content

Commit a79060e

Browse files
committed
Revert "KCFI sanitizer"
This reverts commit 67504c9 as using PointerEmbeddedInt to store 32 bits breaks 32-bit arm builds.
1 parent dda3878 commit a79060e

File tree

81 files changed

+53
-1607
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+53
-1607
lines changed

clang/docs/ControlFlowIntegrity.rst

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,6 @@ the identity of function pointers is maintained, and calls across shared
306306
library boundaries are no different from calls within a single program or
307307
shared library.
308308

309-
.. _kcfi:
310-
311-
``-fsanitize=kcfi``
312-
-------------------
313-
314-
This is an alternative indirect call control-flow integrity scheme designed
315-
for low-level system software, such as operating system kernels. Unlike
316-
``-fsanitize=cfi-icall``, it doesn't require ``-flto``, won't result in
317-
function pointers being replaced with jump table references, and never breaks
318-
cross-DSO function address equality. These properties make KCFI easier to
319-
adopt in low-level software. KCFI is limited to checking only function
320-
pointers, and isn't compatible with executable-only memory.
321-
322309
Member Function Pointer Call Checking
323310
=====================================
324311

clang/docs/UsersManual.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,8 +1720,6 @@ are listed below.
17201720
flow analysis.
17211721
- ``-fsanitize=cfi``: :doc:`control flow integrity <ControlFlowIntegrity>`
17221722
checks. Requires ``-flto``.
1723-
- ``-fsanitize=kcfi``: kernel indirect call forward-edge control flow
1724-
integrity.
17251723
- ``-fsanitize=safe-stack``: :doc:`safe stack <SafeStack>`
17261724
protection against stack-based memory corruption errors.
17271725

clang/include/clang/Basic/Features.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ FEATURE(is_trivially_assignable, LangOpts.CPlusPlus)
228228
FEATURE(is_trivially_constructible, LangOpts.CPlusPlus)
229229
FEATURE(is_trivially_copyable, LangOpts.CPlusPlus)
230230
FEATURE(is_union, LangOpts.CPlusPlus)
231-
FEATURE(kcfi, LangOpts.Sanitize.has(SanitizerKind::KCFI))
232231
FEATURE(modules, LangOpts.Modules)
233232
FEATURE(safe_stack, LangOpts.Sanitize.has(SanitizerKind::SafeStack))
234233
FEATURE(shadow_call_stack,

clang/include/clang/Basic/Sanitizers.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ SANITIZER_GROUP("cfi", CFI,
127127
CFIDerivedCast | CFIICall | CFIMFCall | CFIUnrelatedCast |
128128
CFINVCall | CFIVCall)
129129

130-
// Kernel Control Flow Integrity
131-
SANITIZER("kcfi", KCFI)
132-
133130
// Safe Stack
134131
SANITIZER("safe-stack", SafeStack)
135132

clang/lib/CodeGen/CGCall.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5368,10 +5368,6 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
53685368
SmallVector<llvm::OperandBundleDef, 1> BundleList =
53695369
getBundlesForFunclet(CalleePtr);
53705370

5371-
if (SanOpts.has(SanitizerKind::KCFI) &&
5372-
!isa_and_nonnull<FunctionDecl>(TargetDecl))
5373-
EmitKCFIOperandBundle(ConcreteCallee, BundleList);
5374-
53755371
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl))
53765372
if (FD->hasAttr<StrictFPAttr>())
53775373
// All calls within a strictfp function are marked strictfp

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,14 +2606,6 @@ void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) {
26062606
CGM.getSanStats().create(IRB, SSK);
26072607
}
26082608

2609-
void CodeGenFunction::EmitKCFIOperandBundle(
2610-
const CGCallee &Callee, SmallVectorImpl<llvm::OperandBundleDef> &Bundles) {
2611-
const FunctionProtoType *FP =
2612-
Callee.getAbstractInfo().getCalleeFunctionProtoType();
2613-
if (FP)
2614-
Bundles.emplace_back("kcfi", CGM.CreateKCFITypeId(FP->desugar()));
2615-
}
2616-
26172609
llvm::Value *
26182610
CodeGenFunction::FormResolverCondition(const MultiVersionResolverOption &RO) {
26192611
llvm::Value *Condition = nullptr;

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4612,9 +4612,6 @@ class CodeGenFunction : public CodeGenTypeCache {
46124612
/// passing to a runtime sanitizer handler.
46134613
llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
46144614

4615-
void EmitKCFIOperandBundle(const CGCallee &Callee,
4616-
SmallVectorImpl<llvm::OperandBundleDef> &Bundles);
4617-
46184615
/// Create a basic block that will either trap or call a handler function in
46194616
/// the UBSan runtime with the provided arguments, and create a conditional
46204617
/// branch to it.

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
#include "clang/CodeGen/ConstantInitBuilder.h"
4949
#include "clang/Frontend/FrontendDiagnostic.h"
5050
#include "llvm/ADT/STLExtras.h"
51-
#include "llvm/ADT/StringExtras.h"
5251
#include "llvm/ADT/StringSwitch.h"
5352
#include "llvm/ADT/Triple.h"
5453
#include "llvm/Analysis/TargetLibraryInfo.h"
@@ -68,7 +67,6 @@
6867
#include "llvm/Support/MD5.h"
6968
#include "llvm/Support/TimeProfiler.h"
7069
#include "llvm/Support/X86TargetParser.h"
71-
#include "llvm/Support/xxhash.h"
7270

7371
using namespace clang;
7472
using namespace CodeGen;
@@ -579,8 +577,6 @@ void CodeGenModule::Release() {
579577
CodeGenFunction(*this).EmitCfiCheckFail();
580578
CodeGenFunction(*this).EmitCfiCheckStub();
581579
}
582-
if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
583-
finalizeKCFITypes();
584580
emitAtAvailableLinkGuard();
585581
if (Context.getTargetInfo().getTriple().isWasm())
586582
EmitMainVoidAlias();
@@ -763,9 +759,6 @@ void CodeGenModule::Release() {
763759
CodeGenOpts.SanitizeCfiCanonicalJumpTables);
764760
}
765761

766-
if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
767-
getModule().addModuleFlag(llvm::Module::Override, "kcfi", 1);
768-
769762
if (CodeGenOpts.CFProtectionReturn &&
770763
Target.checkCFProtectionReturnSupported(getDiags())) {
771764
// Indicate that we want to instrument return control flow protection.
@@ -1676,20 +1669,6 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
16761669
return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
16771670
}
16781671

1679-
llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
1680-
if (auto *FnType = T->getAs<FunctionProtoType>())
1681-
T = getContext().getFunctionType(
1682-
FnType->getReturnType(), FnType->getParamTypes(),
1683-
FnType->getExtProtoInfo().withExceptionSpec(EST_None));
1684-
1685-
std::string OutName;
1686-
llvm::raw_string_ostream Out(OutName);
1687-
getCXXABI().getMangleContext().mangleTypeName(T, Out);
1688-
1689-
return llvm::ConstantInt::get(Int32Ty,
1690-
static_cast<uint32_t>(llvm::xxHash64(OutName)));
1691-
}
1692-
16931672
void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD,
16941673
const CGFunctionInfo &Info,
16951674
llvm::Function *F, bool IsThunk) {
@@ -2308,57 +2287,6 @@ void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
23082287
F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
23092288
}
23102289

2311-
void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) {
2312-
if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
2313-
return;
2314-
2315-
llvm::LLVMContext &Ctx = F->getContext();
2316-
llvm::MDBuilder MDB(Ctx);
2317-
F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
2318-
llvm::MDNode::get(
2319-
Ctx, MDB.createConstant(CreateKCFITypeId(FD->getType()))));
2320-
}
2321-
2322-
static bool allowKCFIIdentifier(StringRef Name) {
2323-
// KCFI type identifier constants are only necessary for external assembly
2324-
// functions, which means it's safe to skip unusual names. Subset of
2325-
// MCAsmInfo::isAcceptableChar() and MCAsmInfoXCOFF::isAcceptableChar().
2326-
return llvm::all_of(Name, [](const char &C) {
2327-
return llvm::isAlnum(C) || C == '_' || C == '.';
2328-
});
2329-
}
2330-
2331-
void CodeGenModule::finalizeKCFITypes() {
2332-
llvm::Module &M = getModule();
2333-
for (auto &F : M.functions()) {
2334-
// Remove KCFI type metadata from non-address-taken local functions.
2335-
bool AddressTaken = F.hasAddressTaken();
2336-
if (!AddressTaken && F.hasLocalLinkage())
2337-
F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type);
2338-
2339-
// Generate a constant with the expected KCFI type identifier for all
2340-
// address-taken function declarations to support annotating indirectly
2341-
// called assembly functions.
2342-
if (!AddressTaken || !F.isDeclaration())
2343-
continue;
2344-
2345-
const llvm::ConstantInt *Type;
2346-
if (const llvm::MDNode *MD = F.getMetadata(llvm::LLVMContext::MD_kcfi_type))
2347-
Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0));
2348-
else
2349-
continue;
2350-
2351-
StringRef Name = F.getName();
2352-
if (!allowKCFIIdentifier(Name))
2353-
continue;
2354-
2355-
std::string Asm = (".weak __kcfi_typeid_" + Name + "\n.set __kcfi_typeid_" +
2356-
Name + ", " + Twine(Type->getZExtValue()) + "\n")
2357-
.str();
2358-
M.appendModuleInlineAsm(Asm);
2359-
}
2360-
}
2361-
23622290
void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
23632291
bool IsIncompleteFunction,
23642292
bool IsThunk) {
@@ -2441,9 +2369,6 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
24412369
!CodeGenOpts.SanitizeCfiCanonicalJumpTables)
24422370
CreateFunctionTypeMetadataForIcall(FD, F);
24432371

2444-
if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
2445-
setKCFIType(FD, F);
2446-
24472372
if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
24482373
getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
24492374

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,9 +1440,6 @@ class CodeGenModule : public CodeGenTypeCache {
14401440
/// Generate a cross-DSO type identifier for MD.
14411441
llvm::ConstantInt *CreateCrossDsoCfiTypeId(llvm::Metadata *MD);
14421442

1443-
/// Generate a KCFI type identifier for T.
1444-
llvm::ConstantInt *CreateKCFITypeId(QualType T);
1445-
14461443
/// Create a metadata identifier for the given type. This may either be an
14471444
/// MDString (for external identifiers) or a distinct unnamed MDNode (for
14481445
/// internal identifiers).
@@ -1461,12 +1458,6 @@ class CodeGenModule : public CodeGenTypeCache {
14611458
void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
14621459
llvm::Function *F);
14631460

1464-
/// Set type metadata to the given function.
1465-
void setKCFIType(const FunctionDecl *FD, llvm::Function *F);
1466-
1467-
/// Emit KCFI type identifier constants and remove unused identifiers.
1468-
void finalizeKCFITypes();
1469-
14701461
/// Whether this function's return type has no side effects, and thus may
14711462
/// be trivially discarded if it is unused.
14721463
bool MayDropFunctionReturn(const ASTContext &Context, QualType ReturnType);

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ static const SanitizerMask NotAllowedWithTrap = SanitizerKind::Vptr;
3737
static const SanitizerMask NotAllowedWithMinimalRuntime =
3838
SanitizerKind::Function | SanitizerKind::Vptr;
3939
static const SanitizerMask RequiresPIE =
40-
SanitizerKind::DataFlow | SanitizerKind::HWAddress | SanitizerKind::Scudo |
41-
SanitizerKind::KCFI;
40+
SanitizerKind::DataFlow | SanitizerKind::HWAddress | SanitizerKind::Scudo;
4241
static const SanitizerMask NeedsUnwindTables =
4342
SanitizerKind::Address | SanitizerKind::HWAddress | SanitizerKind::Thread |
4443
SanitizerKind::Memory | SanitizerKind::DataFlow;
@@ -60,9 +59,8 @@ static const SanitizerMask RecoverableByDefault =
6059
SanitizerKind::FloatDivideByZero | SanitizerKind::ObjCCast;
6160
static const SanitizerMask Unrecoverable =
6261
SanitizerKind::Unreachable | SanitizerKind::Return;
63-
static const SanitizerMask AlwaysRecoverable = SanitizerKind::KernelAddress |
64-
SanitizerKind::KernelHWAddress |
65-
SanitizerKind::KCFI;
62+
static const SanitizerMask AlwaysRecoverable =
63+
SanitizerKind::KernelAddress | SanitizerKind::KernelHWAddress;
6664
static const SanitizerMask NeedsLTO = SanitizerKind::CFI;
6765
static const SanitizerMask TrappingSupported =
6866
(SanitizerKind::Undefined & ~SanitizerKind::Vptr) | SanitizerKind::Integer |
@@ -714,13 +712,6 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
714712
options::OPT_fno_sanitize_cfi_canonical_jump_tables, true);
715713
}
716714

717-
if (AllAddedKinds & SanitizerKind::KCFI && DiagnoseErrors) {
718-
if (AllAddedKinds & SanitizerKind::CFI)
719-
D.Diag(diag::err_drv_argument_not_allowed_with)
720-
<< "-fsanitize=kcfi"
721-
<< lastArgumentForMask(D, Args, SanitizerKind::CFI);
722-
}
723-
724715
Stats = Args.hasFlag(options::OPT_fsanitize_stats,
725716
options::OPT_fno_sanitize_stats, false);
726717

clang/lib/Driver/ToolChain.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,6 @@ SanitizerMask ToolChain::getSupportedSanitizers() const {
10891089
getTriple().getArch() == llvm::Triple::arm || getTriple().isWasm() ||
10901090
getTriple().isAArch64() || getTriple().isRISCV())
10911091
Res |= SanitizerKind::CFIICall;
1092-
if (getTriple().getArch() == llvm::Triple::x86_64 ||
1093-
getTriple().isAArch64(64))
1094-
Res |= SanitizerKind::KCFI;
10951092
if (getTriple().getArch() == llvm::Triple::x86_64 ||
10961093
getTriple().isAArch64(64) || getTriple().isRISCV())
10971094
Res |= SanitizerKind::ShadowCallStack;

clang/test/CodeGen/kcfi.c

Lines changed: 0 additions & 58 deletions
This file was deleted.

clang/test/Driver/fsanitize.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -649,18 +649,6 @@
649649
// RUN: %clang -target x86_64-linux-gnu -fsanitize=cfi -fsanitize-stats -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-STATS
650650
// CHECK-CFI-STATS: -fsanitize-stats
651651

652-
// RUN: %clang -target x86_64-linux-gnu -fsanitize=kcfi -fsanitize=cfi -flto -fvisibility=hidden %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-KCFI-NOCFI
653-
// CHECK-KCFI-NOCFI: error: invalid argument '-fsanitize=kcfi' not allowed with '-fsanitize=cfi'
654-
655-
// RUN: %clang -target x86_64-linux-gnu -fsanitize=kcfi -fsanitize-trap=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-KCFI-NOTRAP
656-
// CHECK-KCFI-NOTRAP: error: unsupported argument 'kcfi' to option '-fsanitize-trap='
657-
658-
// RUN: %clang -target x86_64-linux-gnu -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-KCFI
659-
// CHECK-KCFI: "-fsanitize=kcfi"
660-
661-
// RUN: %clang -target x86_64-linux-gnu -fsanitize=kcfi -fno-sanitize-recover=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-KCFI-RECOVER
662-
// CHECK-KCFI-RECOVER: error: unsupported argument 'kcfi' to option '-fno-sanitize-recover='
663-
664652
// RUN: %clang_cl -fsanitize=address -c -MDd -### -- %s 2>&1 | FileCheck %s -check-prefix=CHECK-ASAN-DEBUGRTL
665653
// RUN: %clang_cl -fsanitize=address -c -MTd -### -- %s 2>&1 | FileCheck %s -check-prefix=CHECK-ASAN-DEBUGRTL
666654
// RUN: %clang_cl -fsanitize=address -c -LDd -### -- %s 2>&1 | FileCheck %s -check-prefix=CHECK-ASAN-DEBUGRTL

0 commit comments

Comments
 (0)