Skip to content

Commit e6e8011

Browse files
authored
Merge pull request #58 from fortanix/lvi-hardening
x86 LVI hardening
2 parents 943dbdd + fb13965 commit e6e8011

Some content is hidden

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

45 files changed

+2849
-392
lines changed

clang/docs/ClangCommandLineReference.rst

+4
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,10 @@ Use Intel MCU ABI
25772577

25782578
Generate branches with extended addressability, usually via indirect jumps.
25792579

2580+
.. option:: -mlvi-cfi, -mno-lvi-cfi
2581+
2582+
Enable only control-flow mitigations for Load Value Injection (LVI)
2583+
25802584
.. option:: -mmacosx-version-min=<arg>, -mmacos-version-min=<arg>
25812585

25822586
Set Mac OS X deployment target

clang/include/clang/Driver/Options.td

+8
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,14 @@ def mspeculative_load_hardening : Flag<["-"], "mspeculative-load-hardening">,
22672267
Group<m_Group>, Flags<[CoreOption,CC1Option]>;
22682268
def mno_speculative_load_hardening : Flag<["-"], "mno-speculative-load-hardening">,
22692269
Group<m_Group>, Flags<[CoreOption]>;
2270+
def mlvi_hardening : Flag<["-"], "mlvi-hardening">, Group<m_Group>, Flags<[CoreOption,DriverOption]>,
2271+
HelpText<"Enable all mitigations for Load Value Injection (LVI)">;
2272+
def mno_lvi_hardening : Flag<["-"], "mno-lvi-hardening">, Group<m_Group>, Flags<[CoreOption,DriverOption]>,
2273+
HelpText<"Disable mitigations for Load Value Injection (LVI)">;
2274+
def mlvi_cfi : Flag<["-"], "mlvi-cfi">, Group<m_Group>, Flags<[CoreOption,DriverOption]>,
2275+
HelpText<"Enable only control-flow mitigations for Load Value Injection (LVI)">;
2276+
def mno_lvi_cfi : Flag<["-"], "mno-lvi-cfi">, Group<m_Group>, Flags<[CoreOption,DriverOption]>,
2277+
HelpText<"Disable control-flow mitigations for Load Value Injection (LVI)">;
22702278

22712279
def mrelax : Flag<["-"], "mrelax">, Group<m_riscv_Features_Group>,
22722280
HelpText<"Enable linker relaxation">;

clang/lib/Driver/ToolChains/Arch/X86.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -146,26 +146,49 @@ void x86::getX86TargetFeatures(const Driver &D, const llvm::Triple &Triple,
146146
// flags). This is a bit hacky but keeps existing usages working. We should
147147
// consider deprecating this and instead warn if the user requests external
148148
// retpoline thunks and *doesn't* request some form of retpolines.
149+
auto SpectreOpt = clang::driver::options::ID::OPT_INVALID;
149150
if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline,
150151
options::OPT_mspeculative_load_hardening,
151152
options::OPT_mno_speculative_load_hardening)) {
152153
if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline,
153154
false)) {
154155
Features.push_back("+retpoline-indirect-calls");
155156
Features.push_back("+retpoline-indirect-branches");
157+
SpectreOpt = options::OPT_mretpoline;
156158
} else if (Args.hasFlag(options::OPT_mspeculative_load_hardening,
157159
options::OPT_mno_speculative_load_hardening,
158160
false)) {
159161
// On x86, speculative load hardening relies on at least using retpolines
160162
// for indirect calls.
161163
Features.push_back("+retpoline-indirect-calls");
164+
SpectreOpt = options::OPT_mspeculative_load_hardening;
162165
}
163166
} else if (Args.hasFlag(options::OPT_mretpoline_external_thunk,
164167
options::OPT_mno_retpoline_external_thunk, false)) {
165168
// FIXME: Add a warning about failing to specify `-mretpoline` and
166169
// eventually switch to an error here.
167170
Features.push_back("+retpoline-indirect-calls");
168171
Features.push_back("+retpoline-indirect-branches");
172+
SpectreOpt = options::OPT_mretpoline_external_thunk;
173+
}
174+
175+
auto LVIOpt = clang::driver::options::ID::OPT_INVALID;
176+
if (Args.hasFlag(options::OPT_mlvi_hardening, options::OPT_mno_lvi_hardening,
177+
false)) {
178+
Features.push_back("+lvi-load-hardening");
179+
Features.push_back("+lvi-cfi"); // load hardening implies CFI protection
180+
LVIOpt = options::OPT_mlvi_hardening;
181+
} else if (Args.hasFlag(options::OPT_mlvi_cfi, options::OPT_mno_lvi_cfi,
182+
false)) {
183+
Features.push_back("+lvi-cfi");
184+
LVIOpt = options::OPT_mlvi_cfi;
185+
}
186+
187+
if (SpectreOpt != clang::driver::options::ID::OPT_INVALID &&
188+
LVIOpt != clang::driver::options::ID::OPT_INVALID) {
189+
D.Diag(diag::err_drv_argument_not_allowed_with)
190+
<< D.getOpts().getOptionName(SpectreOpt)
191+
<< D.getOpts().getOptionName(LVIOpt);
169192
}
170193

171194
// Now add any that the user explicitly requested on the command line,

clang/test/Driver/x86-target-features.c

+24
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@
154154
// SLH: "-mspeculative-load-hardening"
155155
// NO-SLH-NOT: retpoline
156156

157+
// RUN: %clang -target i386-linux-gnu -mlvi-cfi %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVICFI %s
158+
// RUN: %clang -target i386-linux-gnu -mno-lvi-cfi %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-LVICFI %s
159+
// LVICFI: "-target-feature" "+lvi-cfi"
160+
// NO-LVICFI-NOT: lvi-cfi
161+
162+
// RUN: %clang -target i386-linux-gnu -mlvi-cfi -mspeculative-load-hardening %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVICFI-SLH %s
163+
// LVICFI-SLH: error: invalid argument 'mspeculative-load-hardening' not allowed with 'mlvi-cfi'
164+
// RUN: %clang -target i386-linux-gnu -mlvi-cfi -mretpoline %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVICFI-RETPOLINE %s
165+
// LVICFI-RETPOLINE: error: invalid argument 'mretpoline' not allowed with 'mlvi-cfi'
166+
// RUN: %clang -target i386-linux-gnu -mlvi-cfi -mretpoline-external-thunk %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVICFI-RETPOLINE-EXTERNAL-THUNK %s
167+
// LVICFI-RETPOLINE-EXTERNAL-THUNK: error: invalid argument 'mretpoline-external-thunk' not allowed with 'mlvi-cfi'
168+
169+
// RUN: %clang -target i386-linux-gnu -mlvi-hardening %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVIHARDENING %s
170+
// RUN: %clang -target i386-linux-gnu -mno-lvi-hardening %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-LVIHARDENING %s
171+
// LVIHARDENING: "-target-feature" "+lvi-load-hardening" "-target-feature" "+lvi-cfi"
172+
// NO-LVIHARDENING-NOT: lvi
173+
174+
// RUN: %clang -target i386-linux-gnu -mlvi-hardening -mspeculative-load-hardening %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVIHARDENING-SLH %s
175+
// LVIHARDENING-SLH: error: invalid argument 'mspeculative-load-hardening' not allowed with 'mlvi-hardening'
176+
// RUN: %clang -target i386-linux-gnu -mlvi-hardening -mretpoline %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVIHARDENING-RETPOLINE %s
177+
// LVIHARDENING-RETPOLINE: error: invalid argument 'mretpoline' not allowed with 'mlvi-hardening'
178+
// RUN: %clang -target i386-linux-gnu -mlvi-hardening -mretpoline-external-thunk %s -### -o %t.o 2>&1 | FileCheck -check-prefix=LVIHARDENING-RETPOLINE-EXTERNAL-THUNK %s
179+
// LVIHARDENING-RETPOLINE-EXTERNAL-THUNK: error: invalid argument 'mretpoline-external-thunk' not allowed with 'mlvi-hardening'
180+
157181
// RUN: %clang -target i386-linux-gnu -mwaitpkg %s -### -o %t.o 2>&1 | FileCheck -check-prefix=WAITPKG %s
158182
// RUN: %clang -target i386-linux-gnu -mno-waitpkg %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-WAITPKG %s
159183
// WAITPKG: "-target-feature" "+waitpkg"

llvm/lib/CodeGen/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ add_llvm_component_library(LLVMCodeGen
114114
ProcessImplicitDefs.cpp
115115
PrologEpilogInserter.cpp
116116
PseudoSourceValue.cpp
117+
RDFGraph.cpp
118+
RDFLiveness.cpp
119+
RDFRegisters.cpp
117120
ReachingDefAnalysis.cpp
118121
RegAllocBase.cpp
119122
RegAllocBasic.cpp

llvm/lib/Target/Hexagon/RDFGraph.cpp renamed to llvm/lib/CodeGen/RDFGraph.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
//
99
// Target-independent, SSA-based data flow graph for register data flow (RDF).
1010
//
11-
#include "RDFGraph.h"
12-
#include "RDFRegisters.h"
1311
#include "llvm/ADT/BitVector.h"
1412
#include "llvm/ADT/STLExtras.h"
1513
#include "llvm/ADT/SetVector.h"
@@ -20,6 +18,8 @@
2018
#include "llvm/CodeGen/MachineInstr.h"
2119
#include "llvm/CodeGen/MachineOperand.h"
2220
#include "llvm/CodeGen/MachineRegisterInfo.h"
21+
#include "llvm/CodeGen/RDFGraph.h"
22+
#include "llvm/CodeGen/RDFRegisters.h"
2323
#include "llvm/CodeGen/TargetInstrInfo.h"
2424
#include "llvm/CodeGen/TargetLowering.h"
2525
#include "llvm/CodeGen/TargetRegisterInfo.h"
@@ -753,8 +753,10 @@ RegisterSet DataFlowGraph::getLandingPadLiveIns() const {
753753
const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
754754
if (RegisterId R = TLI.getExceptionPointerRegister(PF))
755755
LR.insert(RegisterRef(R));
756-
if (RegisterId R = TLI.getExceptionSelectorRegister(PF))
757-
LR.insert(RegisterRef(R));
756+
if (!isFuncletEHPersonality(classifyEHPersonality(PF))) {
757+
if (RegisterId R = TLI.getExceptionSelectorRegister(PF))
758+
LR.insert(RegisterRef(R));
759+
}
758760
return LR;
759761
}
760762

llvm/lib/Target/Hexagon/RDFLiveness.cpp renamed to llvm/lib/CodeGen/RDFLiveness.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
// and Embedded Architectures and Compilers", 8 (4),
2323
// <10.1145/2086696.2086706>. <hal-00647369>
2424
//
25-
#include "RDFLiveness.h"
26-
#include "RDFGraph.h"
27-
#include "RDFRegisters.h"
2825
#include "llvm/ADT/BitVector.h"
2926
#include "llvm/ADT/STLExtras.h"
3027
#include "llvm/ADT/SetVector.h"
@@ -33,6 +30,9 @@
3330
#include "llvm/CodeGen/MachineDominators.h"
3431
#include "llvm/CodeGen/MachineFunction.h"
3532
#include "llvm/CodeGen/MachineInstr.h"
33+
#include "llvm/CodeGen/RDFLiveness.h"
34+
#include "llvm/CodeGen/RDFGraph.h"
35+
#include "llvm/CodeGen/RDFRegisters.h"
3636
#include "llvm/CodeGen/TargetRegisterInfo.h"
3737
#include "llvm/MC/LaneBitmask.h"
3838
#include "llvm/MC/MCRegisterInfo.h"

llvm/lib/Target/Hexagon/RDFRegisters.cpp renamed to llvm/lib/CodeGen/RDFRegisters.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "RDFRegisters.h"
109
#include "llvm/ADT/BitVector.h"
1110
#include "llvm/CodeGen/MachineFunction.h"
1211
#include "llvm/CodeGen/MachineInstr.h"
1312
#include "llvm/CodeGen/MachineOperand.h"
13+
#include "llvm/CodeGen/RDFRegisters.h"
1414
#include "llvm/CodeGen/TargetRegisterInfo.h"
1515
#include "llvm/MC/LaneBitmask.h"
1616
#include "llvm/MC/MCRegisterInfo.h"

llvm/lib/Target/Hexagon/CMakeLists.txt

-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ add_llvm_target(HexagonCodeGen
6464
HexagonVLIWPacketizer.cpp
6565
RDFCopy.cpp
6666
RDFDeadCode.cpp
67-
RDFGraph.cpp
68-
RDFLiveness.cpp
69-
RDFRegisters.cpp
7067
)
7168

7269
add_subdirectory(AsmParser)

llvm/lib/Target/Hexagon/HexagonOptAddrMode.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#include "HexagonInstrInfo.h"
1313
#include "HexagonSubtarget.h"
1414
#include "MCTargetDesc/HexagonBaseInfo.h"
15-
#include "RDFGraph.h"
16-
#include "RDFLiveness.h"
17-
#include "RDFRegisters.h"
1815
#include "llvm/ADT/DenseMap.h"
1916
#include "llvm/ADT/DenseSet.h"
2017
#include "llvm/ADT/StringRef.h"
@@ -27,6 +24,9 @@
2724
#include "llvm/CodeGen/MachineInstrBuilder.h"
2825
#include "llvm/CodeGen/MachineOperand.h"
2926
#include "llvm/CodeGen/MachineRegisterInfo.h"
27+
#include "llvm/CodeGen/RDFGraph.h"
28+
#include "llvm/CodeGen/RDFLiveness.h"
29+
#include "llvm/CodeGen/RDFRegisters.h"
3030
#include "llvm/CodeGen/TargetSubtargetInfo.h"
3131
#include "llvm/InitializePasses.h"
3232
#include "llvm/MC/MCInstrDesc.h"

llvm/lib/Target/Hexagon/HexagonRDFOpt.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#include "MCTargetDesc/HexagonBaseInfo.h"
1212
#include "RDFCopy.h"
1313
#include "RDFDeadCode.h"
14-
#include "RDFGraph.h"
15-
#include "RDFLiveness.h"
16-
#include "RDFRegisters.h"
1714
#include "llvm/ADT/DenseMap.h"
1815
#include "llvm/ADT/STLExtras.h"
1916
#include "llvm/ADT/SetVector.h"
@@ -24,6 +21,9 @@
2421
#include "llvm/CodeGen/MachineInstr.h"
2522
#include "llvm/CodeGen/MachineOperand.h"
2623
#include "llvm/CodeGen/MachineRegisterInfo.h"
24+
#include "llvm/CodeGen/RDFGraph.h"
25+
#include "llvm/CodeGen/RDFLiveness.h"
26+
#include "llvm/CodeGen/RDFRegisters.h"
2727
#include "llvm/InitializePasses.h"
2828
#include "llvm/Pass.h"
2929
#include "llvm/Support/CommandLine.h"

llvm/lib/Target/Hexagon/RDFCopy.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "RDFCopy.h"
14-
#include "RDFGraph.h"
15-
#include "RDFLiveness.h"
16-
#include "RDFRegisters.h"
1714
#include "llvm/CodeGen/MachineDominators.h"
1815
#include "llvm/CodeGen/MachineInstr.h"
1916
#include "llvm/CodeGen/MachineOperand.h"
2017
#include "llvm/CodeGen/MachineRegisterInfo.h"
18+
#include "llvm/CodeGen/RDFGraph.h"
19+
#include "llvm/CodeGen/RDFLiveness.h"
20+
#include "llvm/CodeGen/RDFRegisters.h"
2121
#include "llvm/CodeGen/TargetOpcodes.h"
2222
#include "llvm/CodeGen/TargetRegisterInfo.h"
2323
#include "llvm/MC/MCRegisterInfo.h"

llvm/lib/Target/Hexagon/RDFCopy.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#ifndef LLVM_LIB_TARGET_HEXAGON_RDFCOPY_H
1010
#define LLVM_LIB_TARGET_HEXAGON_RDFCOPY_H
1111

12-
#include "RDFGraph.h"
13-
#include "RDFLiveness.h"
14-
#include "RDFRegisters.h"
12+
#include "llvm/CodeGen/RDFGraph.h"
13+
#include "llvm/CodeGen/RDFLiveness.h"
14+
#include "llvm/CodeGen/RDFRegisters.h"
1515
#include "llvm/CodeGen/MachineFunction.h"
1616
#include <map>
1717
#include <vector>

llvm/lib/Target/Hexagon/RDFDeadCode.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
// RDF-based generic dead code elimination.
1010

1111
#include "RDFDeadCode.h"
12-
#include "RDFGraph.h"
13-
#include "RDFLiveness.h"
1412

1513
#include "llvm/ADT/SetVector.h"
1614
#include "llvm/CodeGen/MachineBasicBlock.h"
1715
#include "llvm/CodeGen/MachineFunction.h"
1816
#include "llvm/CodeGen/MachineRegisterInfo.h"
17+
#include "llvm/CodeGen/RDFGraph.h"
18+
#include "llvm/CodeGen/RDFLiveness.h"
1919
#include "llvm/Support/Debug.h"
2020

2121
#include <queue>

llvm/lib/Target/Hexagon/RDFDeadCode.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#ifndef RDF_DEADCODE_H
2424
#define RDF_DEADCODE_H
2525

26-
#include "RDFGraph.h"
27-
#include "RDFLiveness.h"
26+
#include "llvm/CodeGen/RDFGraph.h"
27+
#include "llvm/CodeGen/RDFLiveness.h"
2828
#include "llvm/ADT/SetVector.h"
2929

3030
namespace llvm {

0 commit comments

Comments
 (0)