1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 20:51:52 +01:00

Convert files added in d157a9bc8ba1 to unix line endings.

Ran:
    git show  --diff-filter=A --stat d157a9bc8ba1 | grep '|' | \
    awk '{ print $1 }' | xargs dos2unix
This commit is contained in:
Nico Weber 2019-10-28 14:39:45 -04:00
parent 3ac69e43e9
commit d4711b7f30
11 changed files with 1121 additions and 1121 deletions

View File

@ -1,119 +1,119 @@
//===-- CFGuardLongjmp.cpp - Longjmp symbols for CFGuard --------*- C++ -*-===// //===-- CFGuardLongjmp.cpp - Longjmp symbols for CFGuard --------*- C++ -*-===//
// //
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information. // See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ///
/// \file /// \file
/// This file contains a machine function pass to insert a symbol after each /// This file contains a machine function pass to insert a symbol after each
/// call to _setjmp and store this in the MachineFunction's LongjmpTargets /// call to _setjmp and store this in the MachineFunction's LongjmpTargets
/// vector. This will be used to emit the table of valid longjmp targets used /// vector. This will be used to emit the table of valid longjmp targets used
/// by Control Flow Guard. /// by Control Flow Guard.
/// ///
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h" #include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineOperand.h" #include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/Passes.h"
using namespace llvm; using namespace llvm;
#define DEBUG_TYPE "cfguard-longjmp" #define DEBUG_TYPE "cfguard-longjmp"
STATISTIC(CFGuardLongjmpTargets, STATISTIC(CFGuardLongjmpTargets,
"Number of Control Flow Guard longjmp targets"); "Number of Control Flow Guard longjmp targets");
namespace { namespace {
/// MachineFunction pass to insert a symbol after each call to _setjmp and store /// MachineFunction pass to insert a symbol after each call to _setjmp and store
/// this in the MachineFunction's LongjmpTargets vector. /// this in the MachineFunction's LongjmpTargets vector.
class CFGuardLongjmp : public MachineFunctionPass { class CFGuardLongjmp : public MachineFunctionPass {
public: public:
static char ID; static char ID;
CFGuardLongjmp() : MachineFunctionPass(ID) { CFGuardLongjmp() : MachineFunctionPass(ID) {
initializeCFGuardLongjmpPass(*PassRegistry::getPassRegistry()); initializeCFGuardLongjmpPass(*PassRegistry::getPassRegistry());
} }
StringRef getPassName() const override { StringRef getPassName() const override {
return "Control Flow Guard longjmp targets"; return "Control Flow Guard longjmp targets";
} }
bool runOnMachineFunction(MachineFunction &MF) override; bool runOnMachineFunction(MachineFunction &MF) override;
}; };
} // end anonymous namespace } // end anonymous namespace
char CFGuardLongjmp::ID = 0; char CFGuardLongjmp::ID = 0;
INITIALIZE_PASS(CFGuardLongjmp, "CFGuardLongjmp", INITIALIZE_PASS(CFGuardLongjmp, "CFGuardLongjmp",
"Insert symbols at valid longjmp targets for /guard:cf", false, "Insert symbols at valid longjmp targets for /guard:cf", false,
false) false)
FunctionPass *llvm::createCFGuardLongjmpPass() { return new CFGuardLongjmp(); } FunctionPass *llvm::createCFGuardLongjmpPass() { return new CFGuardLongjmp(); }
bool CFGuardLongjmp::runOnMachineFunction(MachineFunction &MF) { bool CFGuardLongjmp::runOnMachineFunction(MachineFunction &MF) {
// Skip modules for which the cfguard flag is not set. // Skip modules for which the cfguard flag is not set.
if (!MF.getMMI().getModule()->getModuleFlag("cfguard")) if (!MF.getMMI().getModule()->getModuleFlag("cfguard"))
return false; return false;
// Skip functions that do not have calls to _setjmp. // Skip functions that do not have calls to _setjmp.
if (!MF.getFunction().callsFunctionThatReturnsTwice()) if (!MF.getFunction().callsFunctionThatReturnsTwice())
return false; return false;
SmallVector<MachineInstr *, 8> SetjmpCalls; SmallVector<MachineInstr *, 8> SetjmpCalls;
// Iterate over all instructions in the function and add calls to functions // Iterate over all instructions in the function and add calls to functions
// that return twice to the list of targets. // that return twice to the list of targets.
for (MachineBasicBlock &MBB : MF) { for (MachineBasicBlock &MBB : MF) {
for (MachineInstr &MI : MBB) { for (MachineInstr &MI : MBB) {
// Skip instructions that are not calls. // Skip instructions that are not calls.
if (!MI.isCall() || MI.getNumOperands() < 1) if (!MI.isCall() || MI.getNumOperands() < 1)
continue; continue;
// Iterate over operands to find calls to global functions. // Iterate over operands to find calls to global functions.
for (MachineOperand &MO : MI.operands()) { for (MachineOperand &MO : MI.operands()) {
if (!MO.isGlobal()) if (!MO.isGlobal())
continue; continue;
auto *F = dyn_cast<Function>(MO.getGlobal()); auto *F = dyn_cast<Function>(MO.getGlobal());
if (!F) if (!F)
continue; continue;
// If the instruction calls a function that returns twice, add // If the instruction calls a function that returns twice, add
// it to the list of targets. // it to the list of targets.
if (F->hasFnAttribute(Attribute::ReturnsTwice)) { if (F->hasFnAttribute(Attribute::ReturnsTwice)) {
SetjmpCalls.push_back(&MI); SetjmpCalls.push_back(&MI);
break; break;
} }
} }
} }
} }
if (SetjmpCalls.empty()) if (SetjmpCalls.empty())
return false; return false;
unsigned SetjmpNum = 0; unsigned SetjmpNum = 0;
// For each possible target, create a new symbol and insert it immediately // For each possible target, create a new symbol and insert it immediately
// after the call to setjmp. Add this symbol to the MachineFunction's list // after the call to setjmp. Add this symbol to the MachineFunction's list
// of longjmp targets. // of longjmp targets.
for (MachineInstr *Setjmp : SetjmpCalls) { for (MachineInstr *Setjmp : SetjmpCalls) {
SmallString<128> SymbolName; SmallString<128> SymbolName;
raw_svector_ostream(SymbolName) << "$cfgsj_" << MF.getName() << SetjmpNum++; raw_svector_ostream(SymbolName) << "$cfgsj_" << MF.getName() << SetjmpNum++;
MCSymbol *SjSymbol = MF.getContext().getOrCreateSymbol(SymbolName); MCSymbol *SjSymbol = MF.getContext().getOrCreateSymbol(SymbolName);
Setjmp->setPostInstrSymbol(MF, SjSymbol); Setjmp->setPostInstrSymbol(MF, SjSymbol);
MF.addLongjmpTarget(SjSymbol); MF.addLongjmpTarget(SjSymbol);
CFGuardLongjmpTargets++; CFGuardLongjmpTargets++;
} }
return true; return true;
} }

View File

@ -1,307 +1,307 @@
//===-- CFGuard.cpp - Control Flow Guard checks -----------------*- C++ -*-===// //===-- CFGuard.cpp - Control Flow Guard checks -----------------*- C++ -*-===//
// //
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information. // See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// ///
/// \file /// \file
/// This file contains the IR transform to add Microsoft's Control Flow Guard /// This file contains the IR transform to add Microsoft's Control Flow Guard
/// checks on Windows targets. /// checks on Windows targets.
/// ///
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Transforms/CFGuard.h" #include "llvm/Transforms/CFGuard.h"
#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/IR/CallingConv.h" #include "llvm/IR/CallingConv.h"
#include "llvm/IR/IRBuilder.h" #include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h" #include "llvm/IR/Instruction.h"
#include "llvm/InitializePasses.h" #include "llvm/InitializePasses.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
using namespace llvm; using namespace llvm;
using OperandBundleDef = OperandBundleDefT<Value *>; using OperandBundleDef = OperandBundleDefT<Value *>;
#define DEBUG_TYPE "cfguard" #define DEBUG_TYPE "cfguard"
STATISTIC(CFGuardCounter, "Number of Control Flow Guard checks added"); STATISTIC(CFGuardCounter, "Number of Control Flow Guard checks added");
namespace { namespace {
/// Adds Control Flow Guard (CFG) checks on indirect function calls/invokes. /// Adds Control Flow Guard (CFG) checks on indirect function calls/invokes.
/// These checks ensure that the target address corresponds to the start of an /// These checks ensure that the target address corresponds to the start of an
/// address-taken function. X86_64 targets use the CF_Dispatch mechanism. X86, /// address-taken function. X86_64 targets use the CF_Dispatch mechanism. X86,
/// ARM, and AArch64 targets use the CF_Check machanism. /// ARM, and AArch64 targets use the CF_Check machanism.
class CFGuard : public FunctionPass { class CFGuard : public FunctionPass {
public: public:
static char ID; static char ID;
enum Mechanism { CF_Check, CF_Dispatch }; enum Mechanism { CF_Check, CF_Dispatch };
// Default constructor required for the INITIALIZE_PASS macro. // Default constructor required for the INITIALIZE_PASS macro.
CFGuard() : FunctionPass(ID) { CFGuard() : FunctionPass(ID) {
initializeCFGuardPass(*PassRegistry::getPassRegistry()); initializeCFGuardPass(*PassRegistry::getPassRegistry());
// By default, use the guard check mechanism. // By default, use the guard check mechanism.
GuardMechanism = CF_Check; GuardMechanism = CF_Check;
} }
// Recommended constructor used to specify the type of guard mechanism. // Recommended constructor used to specify the type of guard mechanism.
CFGuard(Mechanism Var) : FunctionPass(ID) { CFGuard(Mechanism Var) : FunctionPass(ID) {
initializeCFGuardPass(*PassRegistry::getPassRegistry()); initializeCFGuardPass(*PassRegistry::getPassRegistry());
GuardMechanism = Var; GuardMechanism = Var;
} }
/// Inserts a Control Flow Guard (CFG) check on an indirect call using the CFG /// Inserts a Control Flow Guard (CFG) check on an indirect call using the CFG
/// check mechanism. When the image is loaded, the loader puts the appropriate /// check mechanism. When the image is loaded, the loader puts the appropriate
/// guard check function pointer in the __guard_check_icall_fptr global /// guard check function pointer in the __guard_check_icall_fptr global
/// symbol. This checks that the target address is a valid address-taken /// symbol. This checks that the target address is a valid address-taken
/// function. The address of the target function is passed to the guard check /// function. The address of the target function is passed to the guard check
/// function in an architecture-specific register (e.g. ECX on 32-bit X86, /// function in an architecture-specific register (e.g. ECX on 32-bit X86,
/// X15 on Aarch64, and R0 on ARM). The guard check function has no return /// X15 on Aarch64, and R0 on ARM). The guard check function has no return
/// value (if the target is invalid, the guard check funtion will raise an /// value (if the target is invalid, the guard check funtion will raise an
/// error). /// error).
/// ///
/// For example, the following LLVM IR: /// For example, the following LLVM IR:
/// \code /// \code
/// %func_ptr = alloca i32 ()*, align 8 /// %func_ptr = alloca i32 ()*, align 8
/// store i32 ()* @target_func, i32 ()** %func_ptr, align 8 /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
/// %0 = load i32 ()*, i32 ()** %func_ptr, align 8 /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
/// %1 = call i32 %0() /// %1 = call i32 %0()
/// \endcode /// \endcode
/// ///
/// is transformed to: /// is transformed to:
/// \code /// \code
/// %func_ptr = alloca i32 ()*, align 8 /// %func_ptr = alloca i32 ()*, align 8
/// store i32 ()* @target_func, i32 ()** %func_ptr, align 8 /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
/// %0 = load i32 ()*, i32 ()** %func_ptr, align 8 /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
/// %1 = load void (i8*)*, void (i8*)** @__guard_check_icall_fptr /// %1 = load void (i8*)*, void (i8*)** @__guard_check_icall_fptr
/// %2 = bitcast i32 ()* %0 to i8* /// %2 = bitcast i32 ()* %0 to i8*
/// call cfguard_checkcc void %1(i8* %2) /// call cfguard_checkcc void %1(i8* %2)
/// %3 = call i32 %0() /// %3 = call i32 %0()
/// \endcode /// \endcode
/// ///
/// For example, the following X86 assembly code: /// For example, the following X86 assembly code:
/// \code /// \code
/// movl $_target_func, %eax /// movl $_target_func, %eax
/// calll *%eax /// calll *%eax
/// \endcode /// \endcode
/// ///
/// is transformed to: /// is transformed to:
/// \code /// \code
/// movl $_target_func, %ecx /// movl $_target_func, %ecx
/// calll *___guard_check_icall_fptr /// calll *___guard_check_icall_fptr
/// calll *%ecx /// calll *%ecx
/// \endcode /// \endcode
/// ///
/// \param CB indirect call to instrument. /// \param CB indirect call to instrument.
void insertCFGuardCheck(CallBase *CB); void insertCFGuardCheck(CallBase *CB);
/// Inserts a Control Flow Guard (CFG) check on an indirect call using the CFG /// Inserts a Control Flow Guard (CFG) check on an indirect call using the CFG
/// dispatch mechanism. When the image is loaded, the loader puts the /// dispatch mechanism. When the image is loaded, the loader puts the
/// appropriate guard check function pointer in the /// appropriate guard check function pointer in the
/// __guard_dispatch_icall_fptr global symbol. This checks that the target /// __guard_dispatch_icall_fptr global symbol. This checks that the target
/// address is a valid address-taken function and, if so, tail calls the /// address is a valid address-taken function and, if so, tail calls the
/// target. The target address is passed in an architecture-specific register /// target. The target address is passed in an architecture-specific register
/// (e.g. RAX on X86_64), with all other arguments for the target function /// (e.g. RAX on X86_64), with all other arguments for the target function
/// passed as usual. /// passed as usual.
/// ///
/// For example, the following LLVM IR: /// For example, the following LLVM IR:
/// \code /// \code
/// %func_ptr = alloca i32 ()*, align 8 /// %func_ptr = alloca i32 ()*, align 8
/// store i32 ()* @target_func, i32 ()** %func_ptr, align 8 /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
/// %0 = load i32 ()*, i32 ()** %func_ptr, align 8 /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
/// %1 = call i32 %0() /// %1 = call i32 %0()
/// \endcode /// \endcode
/// ///
/// is transformed to: /// is transformed to:
/// \code /// \code
/// %func_ptr = alloca i32 ()*, align 8 /// %func_ptr = alloca i32 ()*, align 8
/// store i32 ()* @target_func, i32 ()** %func_ptr, align 8 /// store i32 ()* @target_func, i32 ()** %func_ptr, align 8
/// %0 = load i32 ()*, i32 ()** %func_ptr, align 8 /// %0 = load i32 ()*, i32 ()** %func_ptr, align 8
/// %1 = load i32 ()*, i32 ()** @__guard_dispatch_icall_fptr /// %1 = load i32 ()*, i32 ()** @__guard_dispatch_icall_fptr
/// %2 = call i32 %1() [ "cfguardtarget"(i32 ()* %0) ] /// %2 = call i32 %1() [ "cfguardtarget"(i32 ()* %0) ]
/// \endcode /// \endcode
/// ///
/// For example, the following X86_64 assembly code: /// For example, the following X86_64 assembly code:
/// \code /// \code
/// leaq target_func(%rip), %rax /// leaq target_func(%rip), %rax
/// callq *%rax /// callq *%rax
/// \endcode /// \endcode
/// ///
/// is transformed to: /// is transformed to:
/// \code /// \code
/// leaq target_func(%rip), %rax /// leaq target_func(%rip), %rax
/// callq *__guard_dispatch_icall_fptr(%rip) /// callq *__guard_dispatch_icall_fptr(%rip)
/// \endcode /// \endcode
/// ///
/// \param CB indirect call to instrument. /// \param CB indirect call to instrument.
void insertCFGuardDispatch(CallBase *CB); void insertCFGuardDispatch(CallBase *CB);
bool doInitialization(Module &M) override; bool doInitialization(Module &M) override;
bool runOnFunction(Function &F) override; bool runOnFunction(Function &F) override;
private: private:
// Only add checks if the module has the cfguard=2 flag. // Only add checks if the module has the cfguard=2 flag.
int cfguard_module_flag = 0; int cfguard_module_flag = 0;
Mechanism GuardMechanism = CF_Check; Mechanism GuardMechanism = CF_Check;
FunctionType *GuardFnType = nullptr; FunctionType *GuardFnType = nullptr;
PointerType *GuardFnPtrType = nullptr; PointerType *GuardFnPtrType = nullptr;
Constant *GuardFnGlobal = nullptr; Constant *GuardFnGlobal = nullptr;
}; };
} // end anonymous namespace } // end anonymous namespace
void CFGuard::insertCFGuardCheck(CallBase *CB) { void CFGuard::insertCFGuardCheck(CallBase *CB) {
assert(Triple(CB->getModule()->getTargetTriple()).isOSWindows() && assert(Triple(CB->getModule()->getTargetTriple()).isOSWindows() &&
"Only applicable for Windows targets"); "Only applicable for Windows targets");
assert(CB->isIndirectCall() && assert(CB->isIndirectCall() &&
"Control Flow Guard checks can only be added to indirect calls"); "Control Flow Guard checks can only be added to indirect calls");
IRBuilder<> B(CB); IRBuilder<> B(CB);
Value *CalledOperand = CB->getCalledOperand(); Value *CalledOperand = CB->getCalledOperand();
// Load the global symbol as a pointer to the check function. // Load the global symbol as a pointer to the check function.
LoadInst *GuardCheckLoad = B.CreateLoad(GuardFnPtrType, GuardFnGlobal); LoadInst *GuardCheckLoad = B.CreateLoad(GuardFnPtrType, GuardFnGlobal);
// Create new call instruction. The CFGuard check should always be a call, // Create new call instruction. The CFGuard check should always be a call,
// even if the original CallBase is an Invoke or CallBr instruction. // even if the original CallBase is an Invoke or CallBr instruction.
CallInst *GuardCheck = CallInst *GuardCheck =
B.CreateCall(GuardFnType, GuardCheckLoad, B.CreateCall(GuardFnType, GuardCheckLoad,
{B.CreateBitCast(CalledOperand, B.getInt8PtrTy())}); {B.CreateBitCast(CalledOperand, B.getInt8PtrTy())});
// Ensure that the first argument is passed in the correct register // Ensure that the first argument is passed in the correct register
// (e.g. ECX on 32-bit X86 targets). // (e.g. ECX on 32-bit X86 targets).
GuardCheck->setCallingConv(CallingConv::CFGuard_Check); GuardCheck->setCallingConv(CallingConv::CFGuard_Check);
} }
void CFGuard::insertCFGuardDispatch(CallBase *CB) { void CFGuard::insertCFGuardDispatch(CallBase *CB) {
assert(Triple(CB->getModule()->getTargetTriple()).isOSWindows() && assert(Triple(CB->getModule()->getTargetTriple()).isOSWindows() &&
"Only applicable for Windows targets"); "Only applicable for Windows targets");
assert(CB->isIndirectCall() && assert(CB->isIndirectCall() &&
"Control Flow Guard checks can only be added to indirect calls"); "Control Flow Guard checks can only be added to indirect calls");
IRBuilder<> B(CB); IRBuilder<> B(CB);
Value *CalledOperand = CB->getCalledOperand(); Value *CalledOperand = CB->getCalledOperand();
Type *CalledOperandType = CalledOperand->getType(); Type *CalledOperandType = CalledOperand->getType();
// Cast the guard dispatch global to the type of the called operand. // Cast the guard dispatch global to the type of the called operand.
PointerType *PTy = PointerType::get(CalledOperandType, 0); PointerType *PTy = PointerType::get(CalledOperandType, 0);
if (GuardFnGlobal->getType() != PTy) if (GuardFnGlobal->getType() != PTy)
GuardFnGlobal = ConstantExpr::getBitCast(GuardFnGlobal, PTy); GuardFnGlobal = ConstantExpr::getBitCast(GuardFnGlobal, PTy);
// Load the global as a pointer to a function of the same type. // Load the global as a pointer to a function of the same type.
LoadInst *GuardDispatchLoad = B.CreateLoad(CalledOperandType, GuardFnGlobal); LoadInst *GuardDispatchLoad = B.CreateLoad(CalledOperandType, GuardFnGlobal);
// Add the original call target as a cfguardtarget operand bundle. // Add the original call target as a cfguardtarget operand bundle.
SmallVector<llvm::OperandBundleDef, 1> Bundles; SmallVector<llvm::OperandBundleDef, 1> Bundles;
CB->getOperandBundlesAsDefs(Bundles); CB->getOperandBundlesAsDefs(Bundles);
Bundles.emplace_back("cfguardtarget", CalledOperand); Bundles.emplace_back("cfguardtarget", CalledOperand);
// Create a copy of the call/invoke instruction and add the new bundle. // Create a copy of the call/invoke instruction and add the new bundle.
CallBase *NewCB; CallBase *NewCB;
if (CallInst *CI = dyn_cast<CallInst>(CB)) { if (CallInst *CI = dyn_cast<CallInst>(CB)) {
NewCB = CallInst::Create(CI, Bundles, CB); NewCB = CallInst::Create(CI, Bundles, CB);
} else { } else {
assert(isa<InvokeInst>(CB) && "Unknown indirect call type"); assert(isa<InvokeInst>(CB) && "Unknown indirect call type");
InvokeInst *II = cast<InvokeInst>(CB); InvokeInst *II = cast<InvokeInst>(CB);
NewCB = llvm::InvokeInst::Create(II, Bundles, CB); NewCB = llvm::InvokeInst::Create(II, Bundles, CB);
} }
// Change the target of the call to be the guard dispatch function. // Change the target of the call to be the guard dispatch function.
NewCB->setCalledOperand(GuardDispatchLoad); NewCB->setCalledOperand(GuardDispatchLoad);
// Replace the original call/invoke with the new instruction. // Replace the original call/invoke with the new instruction.
CB->replaceAllUsesWith(NewCB); CB->replaceAllUsesWith(NewCB);
// Delete the original call/invoke. // Delete the original call/invoke.
CB->eraseFromParent(); CB->eraseFromParent();
} }
bool CFGuard::doInitialization(Module &M) { bool CFGuard::doInitialization(Module &M) {
// Check if this module has the cfguard flag and read its value. // Check if this module has the cfguard flag and read its value.
if (auto *MD = if (auto *MD =
mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard"))) mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("cfguard")))
cfguard_module_flag = MD->getZExtValue(); cfguard_module_flag = MD->getZExtValue();
// Skip modules for which CFGuard checks have been disabled. // Skip modules for which CFGuard checks have been disabled.
if (cfguard_module_flag != 2) if (cfguard_module_flag != 2)
return false; return false;
// Set up prototypes for the guard check and dispatch functions. // Set up prototypes for the guard check and dispatch functions.
GuardFnType = FunctionType::get(Type::getVoidTy(M.getContext()), GuardFnType = FunctionType::get(Type::getVoidTy(M.getContext()),
{Type::getInt8PtrTy(M.getContext())}, false); {Type::getInt8PtrTy(M.getContext())}, false);
GuardFnPtrType = PointerType::get(GuardFnType, 0); GuardFnPtrType = PointerType::get(GuardFnType, 0);
// Get or insert the guard check or dispatch global symbols. // Get or insert the guard check or dispatch global symbols.
if (GuardMechanism == CF_Check) { if (GuardMechanism == CF_Check) {
GuardFnGlobal = GuardFnGlobal =
M.getOrInsertGlobal("__guard_check_icall_fptr", GuardFnPtrType); M.getOrInsertGlobal("__guard_check_icall_fptr", GuardFnPtrType);
} else { } else {
assert(GuardMechanism == CF_Dispatch && "Invalid CFGuard mechanism"); assert(GuardMechanism == CF_Dispatch && "Invalid CFGuard mechanism");
GuardFnGlobal = GuardFnGlobal =
M.getOrInsertGlobal("__guard_dispatch_icall_fptr", GuardFnPtrType); M.getOrInsertGlobal("__guard_dispatch_icall_fptr", GuardFnPtrType);
} }
return true; return true;
} }
bool CFGuard::runOnFunction(Function &F) { bool CFGuard::runOnFunction(Function &F) {
// Skip modules and functions for which CFGuard checks have been disabled. // Skip modules and functions for which CFGuard checks have been disabled.
if (cfguard_module_flag != 2 || F.hasFnAttribute(Attribute::NoCfCheck)) if (cfguard_module_flag != 2 || F.hasFnAttribute(Attribute::NoCfCheck))
return false; return false;
SmallVector<CallBase *, 8> IndirectCalls; SmallVector<CallBase *, 8> IndirectCalls;
// Iterate over the instructions to find all indirect call/invoke/callbr // Iterate over the instructions to find all indirect call/invoke/callbr
// instructions. Make a separate list of pointers to indirect // instructions. Make a separate list of pointers to indirect
// call/invoke/callbr instructions because the original instructions will be // call/invoke/callbr instructions because the original instructions will be
// deleted as the checks are added. // deleted as the checks are added.
for (BasicBlock &BB : F.getBasicBlockList()) { for (BasicBlock &BB : F.getBasicBlockList()) {
for (Instruction &I : BB.getInstList()) { for (Instruction &I : BB.getInstList()) {
auto *CB = dyn_cast<CallBase>(&I); auto *CB = dyn_cast<CallBase>(&I);
if (CB && CB->isIndirectCall()) { if (CB && CB->isIndirectCall()) {
IndirectCalls.push_back(CB); IndirectCalls.push_back(CB);
CFGuardCounter++; CFGuardCounter++;
} }
} }
} }
// If no checks are needed, return early and add this attribute to indicate // If no checks are needed, return early and add this attribute to indicate
// that subsequent CFGuard passes can skip this function. // that subsequent CFGuard passes can skip this function.
if (IndirectCalls.empty()) { if (IndirectCalls.empty()) {
F.addFnAttr(Attribute::NoCfCheck); F.addFnAttr(Attribute::NoCfCheck);
return false; return false;
} }
// For each indirect call/invoke, add the appropriate dispatch or check. // For each indirect call/invoke, add the appropriate dispatch or check.
if (GuardMechanism == CF_Dispatch) { if (GuardMechanism == CF_Dispatch) {
for (CallBase *CB : IndirectCalls) { for (CallBase *CB : IndirectCalls) {
insertCFGuardDispatch(CB); insertCFGuardDispatch(CB);
} }
} else { } else {
for (CallBase *CB : IndirectCalls) { for (CallBase *CB : IndirectCalls) {
insertCFGuardCheck(CB); insertCFGuardCheck(CB);
} }
} }
return true; return true;
} }
char CFGuard::ID = 0; char CFGuard::ID = 0;
INITIALIZE_PASS(CFGuard, "CFGuard", "CFGuard", false, false) INITIALIZE_PASS(CFGuard, "CFGuard", "CFGuard", false, false)
FunctionPass *llvm::createCFGuardCheckPass() { FunctionPass *llvm::createCFGuardCheckPass() {
return new CFGuard(CFGuard::CF_Check); return new CFGuard(CFGuard::CF_Check);
} }
FunctionPass *llvm::createCFGuardDispatchPass() { FunctionPass *llvm::createCFGuardDispatchPass() {
return new CFGuard(CFGuard::CF_Dispatch); return new CFGuard(CFGuard::CF_Dispatch);
} }

View File

@ -1,9 +1,9 @@
add_llvm_library(LLVMCFGuard add_llvm_library(LLVMCFGuard
CFGuard.cpp CFGuard.cpp
ADDITIONAL_HEADER_DIRS ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms
DEPENDS DEPENDS
intrinsics_gen intrinsics_gen
) )

View File

@ -1,147 +1,147 @@
; RUN: llc < %s -mtriple=aarch64-pc-windows-msvc | FileCheck %s ; RUN: llc < %s -mtriple=aarch64-pc-windows-msvc | FileCheck %s
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are correctly added when required. ; Test that Control Flow Guard checks are correctly added when required.
declare i32 @target_func() declare i32 @target_func()
; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute. ; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute.
define i32 @func_nocf_checks() #0 { define i32 @func_nocf_checks() #0 {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; CHECK-LABEL: func_nocf_checks ; CHECK-LABEL: func_nocf_checks
; CHECK: adrp x8, target_func ; CHECK: adrp x8, target_func
; CHECK: add x8, x8, target_func ; CHECK: add x8, x8, target_func
; CHECK-NOT: __guard_check_icall_fptr ; CHECK-NOT: __guard_check_icall_fptr
; CHECK: blr x8 ; CHECK: blr x8
} }
attributes #0 = { nocf_check } attributes #0 = { nocf_check }
; Test that Control Flow Guard checks are added even at -O0. ; Test that Control Flow Guard checks are added even at -O0.
define i32 @func_optnone_cf() #1 { define i32 @func_optnone_cf() #1 {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; The call to __guard_check_icall_fptr should come immediately before the call to the target function. ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
; CHECK-LABEL: func_optnone_cf ; CHECK-LABEL: func_optnone_cf
; CHECK: adrp x8, __guard_check_icall_fptr ; CHECK: adrp x8, __guard_check_icall_fptr
; CHECK: add x9, x8, __guard_check_icall_fptr ; CHECK: add x9, x8, __guard_check_icall_fptr
; CHECK: adrp x8, target_func ; CHECK: adrp x8, target_func
; CHECK: add x8, x8, target_func ; CHECK: add x8, x8, target_func
; CHECK: ldr x9, [x9] ; CHECK: ldr x9, [x9]
; CHECK: mov x15, x8 ; CHECK: mov x15, x8
; CHECK: blr x9 ; CHECK: blr x9
; CHECK-NEXT: blr x8 ; CHECK-NEXT: blr x8
} }
attributes #1 = { noinline optnone } attributes #1 = { noinline optnone }
; Test that Control Flow Guard checks are correctly added in optimized code (common case). ; Test that Control Flow Guard checks are correctly added in optimized code (common case).
define i32 @func_cf() { define i32 @func_cf() {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; The call to __guard_check_icall_fptr should come immediately before the call to the target function. ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
; CHECK-LABEL: func_cf ; CHECK-LABEL: func_cf
; CHECK: adrp x8, __guard_check_icall_fptr ; CHECK: adrp x8, __guard_check_icall_fptr
; CHECK: ldr x9, [x8, __guard_check_icall_fptr] ; CHECK: ldr x9, [x8, __guard_check_icall_fptr]
; CHECK: adrp x8, target_func ; CHECK: adrp x8, target_func
; CHECK: add x8, x8, target_func ; CHECK: add x8, x8, target_func
; CHECK: mov x15, x8 ; CHECK: mov x15, x8
; CHECK: blr x9 ; CHECK: blr x9
; CHECK-NEXT: blr x8 ; CHECK-NEXT: blr x8
} }
; Test that Control Flow Guard checks are correctly added on invoke instructions. ; Test that Control Flow Guard checks are correctly added on invoke instructions.
define i32 @func_cf_invoke() personality i8* bitcast (void ()* @h to i8*) { define i32 @func_cf_invoke() personality i8* bitcast (void ()* @h to i8*) {
entry: entry:
%0 = alloca i32, align 4 %0 = alloca i32, align 4
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%1 = load i32 ()*, i32 ()** %func_ptr, align 8 %1 = load i32 ()*, i32 ()** %func_ptr, align 8
%2 = invoke i32 %1() %2 = invoke i32 %1()
to label %invoke.cont unwind label %lpad to label %invoke.cont unwind label %lpad
invoke.cont: ; preds = %entry invoke.cont: ; preds = %entry
ret i32 %2 ret i32 %2
lpad: ; preds = %entry lpad: ; preds = %entry
%tmp = landingpad { i8*, i32 } %tmp = landingpad { i8*, i32 }
catch i8* null catch i8* null
ret i32 -1 ret i32 -1
; The call to __guard_check_icall_fptr should come immediately before the call to the target function. ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
; CHECK-LABEL: func_cf_invoke ; CHECK-LABEL: func_cf_invoke
; CHECK: adrp x8, __guard_check_icall_fptr ; CHECK: adrp x8, __guard_check_icall_fptr
; CHECK: ldr x9, [x8, __guard_check_icall_fptr] ; CHECK: ldr x9, [x8, __guard_check_icall_fptr]
; CHECK: adrp x8, target_func ; CHECK: adrp x8, target_func
; CHECK: add x8, x8, target_func ; CHECK: add x8, x8, target_func
; CHECK: mov x15, x8 ; CHECK: mov x15, x8
; CHECK: blr x9 ; CHECK: blr x9
; CHECK-NEXT: .Ltmp0: ; CHECK-NEXT: .Ltmp0:
; CHECK-NEXT: blr x8 ; CHECK-NEXT: blr x8
; CHECK: ; %invoke.cont ; CHECK: ; %invoke.cont
; CHECK: ; %lpad ; CHECK: ; %lpad
} }
declare void @h() declare void @h()
; Test that longjmp targets have public labels and are included in the .gljmp section. ; Test that longjmp targets have public labels and are included in the .gljmp section.
%struct._SETJMP_FLOAT128 = type { [2 x i64] } %struct._SETJMP_FLOAT128 = type { [2 x i64] }
@buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16 @buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16
define i32 @func_cf_setjmp() { define i32 @func_cf_setjmp() {
%1 = alloca i32, align 4 %1 = alloca i32, align 4
%2 = alloca i32, align 4 %2 = alloca i32, align 4
store i32 0, i32* %1, align 4 store i32 0, i32* %1, align 4
store i32 -1, i32* %2, align 4 store i32 -1, i32* %2, align 4
%3 = call i8* @llvm.frameaddress(i32 0) %3 = call i8* @llvm.frameaddress(i32 0)
%4 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %3) #2 %4 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %3) #2
; CHECK-LABEL: func_cf_setjmp ; CHECK-LABEL: func_cf_setjmp
; CHECK: bl _setjmp ; CHECK: bl _setjmp
; CHECK-NEXT: $cfgsj_func_cf_setjmp0: ; CHECK-NEXT: $cfgsj_func_cf_setjmp0:
%5 = call i8* @llvm.frameaddress(i32 0) %5 = call i8* @llvm.frameaddress(i32 0)
%6 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %5) #3 %6 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %5) #3
; CHECK: bl _setjmp ; CHECK: bl _setjmp
; CHECK-NEXT: $cfgsj_func_cf_setjmp1: ; CHECK-NEXT: $cfgsj_func_cf_setjmp1:
store i32 1, i32* %2, align 4 store i32 1, i32* %2, align 4
%7 = load i32, i32* %2, align 4 %7 = load i32, i32* %2, align 4
ret i32 %7 ret i32 %7
; CHECK: .section .gljmp$y,"dr" ; CHECK: .section .gljmp$y,"dr"
; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp0 ; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp0
; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp1 ; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp1
} }
declare i8* @llvm.frameaddress(i32) declare i8* @llvm.frameaddress(i32)
; Function Attrs: returns_twice ; Function Attrs: returns_twice
declare dso_local i32 @_setjmp(i8*, i8*) #2 declare dso_local i32 @_setjmp(i8*, i8*) #2
attributes #2 = { returns_twice } attributes #2 = { returns_twice }
attributes #3 = { returns_twice } attributes #3 = { returns_twice }
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 2} !0 = !{i32 2, !"cfguard", i32 2}

View File

@ -1,25 +1,25 @@
; RUN: llc < %s -mtriple=aarch64-pc-windows-msvc | FileCheck %s ; RUN: llc < %s -mtriple=aarch64-pc-windows-msvc | FileCheck %s
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are not added in modules with the ; Test that Control Flow Guard checks are not added in modules with the
; cfguard=1 flag (emit tables but no checks). ; cfguard=1 flag (emit tables but no checks).
declare void @target_func() declare void @target_func()
define void @func_in_module_without_cfguard() #0 { define void @func_in_module_without_cfguard() #0 {
entry: entry:
%func_ptr = alloca void ()*, align 8 %func_ptr = alloca void ()*, align 8
store void ()* @target_func, void ()** %func_ptr, align 8 store void ()* @target_func, void ()** %func_ptr, align 8
%0 = load void ()*, void ()** %func_ptr, align 8 %0 = load void ()*, void ()** %func_ptr, align 8
call void %0() call void %0()
ret void ret void
; CHECK-NOT: __guard_check_icall_fptr ; CHECK-NOT: __guard_check_icall_fptr
; CHECK-NOT: __guard_dispatch_icall_fptr ; CHECK-NOT: __guard_dispatch_icall_fptr
} }
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 1} !0 = !{i32 2, !"cfguard", i32 1}

View File

@ -1,151 +1,151 @@
; RUN: llc < %s -mtriple=arm-pc-windows-msvc | FileCheck %s ; RUN: llc < %s -mtriple=arm-pc-windows-msvc | FileCheck %s
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are correctly added when required. ; Test that Control Flow Guard checks are correctly added when required.
declare i32 @target_func() declare i32 @target_func()
; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute. ; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute.
define i32 @func_nocf_checks() #0 { define i32 @func_nocf_checks() #0 {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call arm_aapcs_vfpcc i32 %0() %1 = call arm_aapcs_vfpcc i32 %0()
ret i32 %1 ret i32 %1
; CHECK-LABEL: func_nocf_checks ; CHECK-LABEL: func_nocf_checks
; CHECK: movw r0, :lower16:target_func ; CHECK: movw r0, :lower16:target_func
; CHECK: movt r0, :upper16:target_func ; CHECK: movt r0, :upper16:target_func
; CHECK-NOT: __guard_check_icall_fptr ; CHECK-NOT: __guard_check_icall_fptr
; CHECK: blx r0 ; CHECK: blx r0
} }
attributes #0 = { nocf_check "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"} attributes #0 = { nocf_check "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
; Test that Control Flow Guard checks are added even at -O0. ; Test that Control Flow Guard checks are added even at -O0.
define i32 @func_optnone_cf() #1 { define i32 @func_optnone_cf() #1 {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; The call to __guard_check_icall_fptr should come immediately before the call to the target function. ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
; CHECK-LABEL: func_optnone_cf ; CHECK-LABEL: func_optnone_cf
; CHECK: movw r0, :lower16:target_func ; CHECK: movw r0, :lower16:target_func
; CHECK: movt r0, :upper16:target_func ; CHECK: movt r0, :upper16:target_func
; CHECK: str r0, [sp] ; CHECK: str r0, [sp]
; CHECK: ldr r4, [sp] ; CHECK: ldr r4, [sp]
; CHECK: movw r0, :lower16:__guard_check_icall_fptr ; CHECK: movw r0, :lower16:__guard_check_icall_fptr
; CHECK: movt r0, :upper16:__guard_check_icall_fptr ; CHECK: movt r0, :upper16:__guard_check_icall_fptr
; CHECK: ldr r1, [r0] ; CHECK: ldr r1, [r0]
; CHECK: mov r0, r4 ; CHECK: mov r0, r4
; CHECK: blx r1 ; CHECK: blx r1
; CHECK-NEXT: blx r4 ; CHECK-NEXT: blx r4
} }
attributes #1 = { noinline optnone "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"} attributes #1 = { noinline optnone "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
; Test that Control Flow Guard checks are correctly added in optimized code (common case). ; Test that Control Flow Guard checks are correctly added in optimized code (common case).
define i32 @func_cf() #2 { define i32 @func_cf() #2 {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; The call to __guard_check_icall_fptr should come immediately before the call to the target function. ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
; CHECK-LABEL: func_cf ; CHECK-LABEL: func_cf
; CHECK: movw r0, :lower16:__guard_check_icall_fptr ; CHECK: movw r0, :lower16:__guard_check_icall_fptr
; CHECK: movt r0, :upper16:__guard_check_icall_fptr ; CHECK: movt r0, :upper16:__guard_check_icall_fptr
; CHECK: ldr r1, [r0] ; CHECK: ldr r1, [r0]
; CHECK: movw r4, :lower16:target_func ; CHECK: movw r4, :lower16:target_func
; CHECK: movt r4, :upper16:target_func ; CHECK: movt r4, :upper16:target_func
; CHECK: mov r0, r4 ; CHECK: mov r0, r4
; CHECK: blx r1 ; CHECK: blx r1
; CHECK-NEXT: blx r4 ; CHECK-NEXT: blx r4
} }
attributes #2 = { "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"} attributes #2 = { "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3"}
; Test that Control Flow Guard checks are correctly added on invoke instructions. ; Test that Control Flow Guard checks are correctly added on invoke instructions.
define i32 @func_cf_invoke() #2 personality i8* bitcast (void ()* @h to i8*) { define i32 @func_cf_invoke() #2 personality i8* bitcast (void ()* @h to i8*) {
entry: entry:
%0 = alloca i32, align 4 %0 = alloca i32, align 4
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%1 = load i32 ()*, i32 ()** %func_ptr, align 8 %1 = load i32 ()*, i32 ()** %func_ptr, align 8
%2 = invoke i32 %1() %2 = invoke i32 %1()
to label %invoke.cont unwind label %lpad to label %invoke.cont unwind label %lpad
invoke.cont: ; preds = %entry invoke.cont: ; preds = %entry
ret i32 %2 ret i32 %2
lpad: ; preds = %entry lpad: ; preds = %entry
%tmp = landingpad { i8*, i32 } %tmp = landingpad { i8*, i32 }
catch i8* null catch i8* null
ret i32 -1 ret i32 -1
; The call to __guard_check_icall_fptr should come immediately before the call to the target function. ; The call to __guard_check_icall_fptr should come immediately before the call to the target function.
; CHECK-LABEL: func_cf_invoke ; CHECK-LABEL: func_cf_invoke
; CHECK: movw r0, :lower16:__guard_check_icall_fptr ; CHECK: movw r0, :lower16:__guard_check_icall_fptr
; CHECK: movt r0, :upper16:__guard_check_icall_fptr ; CHECK: movt r0, :upper16:__guard_check_icall_fptr
; CHECK: ldr r1, [r0] ; CHECK: ldr r1, [r0]
; CHECK: movw r4, :lower16:target_func ; CHECK: movw r4, :lower16:target_func
; CHECK: movt r4, :upper16:target_func ; CHECK: movt r4, :upper16:target_func
; CHECK: mov r0, r4 ; CHECK: mov r0, r4
; CHECK: blx r1 ; CHECK: blx r1
; CHECK-NEXT: $Mtmp0: ; CHECK-NEXT: $Mtmp0:
; CHECK-NEXT: blx r4 ; CHECK-NEXT: blx r4
; CHECK: ; %invoke.cont ; CHECK: ; %invoke.cont
; CHECK: ; %lpad ; CHECK: ; %lpad
} }
declare void @h() declare void @h()
; Test that longjmp targets have public labels and are included in the .gljmp section. ; Test that longjmp targets have public labels and are included in the .gljmp section.
%struct._SETJMP_FLOAT128 = type { [2 x i64] } %struct._SETJMP_FLOAT128 = type { [2 x i64] }
@buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16 @buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16
define i32 @func_cf_setjmp() #2 { define i32 @func_cf_setjmp() #2 {
%1 = alloca i32, align 4 %1 = alloca i32, align 4
%2 = alloca i32, align 4 %2 = alloca i32, align 4
store i32 0, i32* %1, align 4 store i32 0, i32* %1, align 4
store i32 -1, i32* %2, align 4 store i32 -1, i32* %2, align 4
%3 = call i8* @llvm.frameaddress(i32 0) %3 = call i8* @llvm.frameaddress(i32 0)
%4 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %3) #3 %4 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %3) #3
; CHECK-LABEL: func_cf_setjmp ; CHECK-LABEL: func_cf_setjmp
; CHECK: bl _setjmp ; CHECK: bl _setjmp
; CHECK-NEXT: $cfgsj_func_cf_setjmp0: ; CHECK-NEXT: $cfgsj_func_cf_setjmp0:
%5 = call i8* @llvm.frameaddress(i32 0) %5 = call i8* @llvm.frameaddress(i32 0)
%6 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %5) #3 %6 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %5) #3
; CHECK: bl _setjmp ; CHECK: bl _setjmp
; CHECK-NEXT: $cfgsj_func_cf_setjmp1: ; CHECK-NEXT: $cfgsj_func_cf_setjmp1:
store i32 1, i32* %2, align 4 store i32 1, i32* %2, align 4
%7 = load i32, i32* %2, align 4 %7 = load i32, i32* %2, align 4
ret i32 %7 ret i32 %7
; CHECK: .section .gljmp$y,"dr" ; CHECK: .section .gljmp$y,"dr"
; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp0 ; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp0
; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp1 ; CHECK-NEXT: .symidx $cfgsj_func_cf_setjmp1
} }
declare i8* @llvm.frameaddress(i32) declare i8* @llvm.frameaddress(i32)
; Function Attrs: returns_twice ; Function Attrs: returns_twice
declare dso_local i32 @_setjmp(i8*, i8*) #3 declare dso_local i32 @_setjmp(i8*, i8*) #3
attributes #3 = { returns_twice } attributes #3 = { returns_twice }
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 2} !0 = !{i32 2, !"cfguard", i32 2}

View File

@ -1,26 +1,26 @@
; RUN: llc < %s -mtriple=arm-pc-windows-msvc | FileCheck %s ; RUN: llc < %s -mtriple=arm-pc-windows-msvc | FileCheck %s
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are not added in modules with the ; Test that Control Flow Guard checks are not added in modules with the
; cfguard=1 flag (emit tables but no checks). ; cfguard=1 flag (emit tables but no checks).
declare void @target_func() declare void @target_func()
define void @func_in_module_without_cfguard() #0 { define void @func_in_module_without_cfguard() #0 {
entry: entry:
%func_ptr = alloca void ()*, align 8 %func_ptr = alloca void ()*, align 8
store void ()* @target_func, void ()** %func_ptr, align 8 store void ()* @target_func, void ()** %func_ptr, align 8
%0 = load void ()*, void ()** %func_ptr, align 8 %0 = load void ()*, void ()** %func_ptr, align 8
call void %0() call void %0()
ret void ret void
; CHECK-NOT: __guard_check_icall_fptr ; CHECK-NOT: __guard_check_icall_fptr
; CHECK-NOT: __guard_dispatch_icall_fptr ; CHECK-NOT: __guard_dispatch_icall_fptr
} }
attributes #0 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3" "unsafe-fp-math"="false" "use-soft-float"="false"} attributes #0 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="cortex-a9" "target-features"="+armv7-a,+dsp,+fp16,+neon,+strict-align,+thumb-mode,+vfp3" "unsafe-fp-math"="false" "use-soft-float"="false"}
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 1} !0 = !{i32 2, !"cfguard", i32 1}

View File

@ -1,231 +1,231 @@
; RUN: llc < %s -mtriple=i686-pc-windows-msvc | FileCheck %s -check-prefix=X32 ; RUN: llc < %s -mtriple=i686-pc-windows-msvc | FileCheck %s -check-prefix=X32
; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefix=X64 ; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefix=X64
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are correctly added when required. ; Test that Control Flow Guard checks are correctly added when required.
declare i32 @target_func() declare i32 @target_func()
; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute. ; Test that Control Flow Guard checks are not added to functions with nocf_checks attribute.
define i32 @func_nocf_checks() #0 { define i32 @func_nocf_checks() #0 {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; X32-LABEL: func_nocf_checks ; X32-LABEL: func_nocf_checks
; X32: movl $_target_func, %eax ; X32: movl $_target_func, %eax
; X32-NOT: __guard_check_icall_fptr ; X32-NOT: __guard_check_icall_fptr
; X32: calll *%eax ; X32: calll *%eax
; X64-LABEL: func_nocf_checks ; X64-LABEL: func_nocf_checks
; X64: leaq target_func(%rip), %rax ; X64: leaq target_func(%rip), %rax
; X64-NOT: __guard_dispatch_icall_fptr ; X64-NOT: __guard_dispatch_icall_fptr
; X64: callq *%rax ; X64: callq *%rax
} }
attributes #0 = { nocf_check } attributes #0 = { nocf_check }
; Test that Control Flow Guard checks are added even at -O0. ; Test that Control Flow Guard checks are added even at -O0.
; FIXME Ideally these checks should be added as a single call instruction, as in the optimized case. ; FIXME Ideally these checks should be added as a single call instruction, as in the optimized case.
define i32 @func_optnone_cf() #1 { define i32 @func_optnone_cf() #1 {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function. ; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function.
; X32-LABEL: func_optnone_cf ; X32-LABEL: func_optnone_cf
; X32: leal _target_func, %eax ; X32: leal _target_func, %eax
; X32: movl %eax, (%esp) ; X32: movl %eax, (%esp)
; X32: movl (%esp), %ecx ; X32: movl (%esp), %ecx
; X32: movl ___guard_check_icall_fptr, %eax ; X32: movl ___guard_check_icall_fptr, %eax
; X32: calll *%eax ; X32: calll *%eax
; X32-NEXT: calll *%ecx ; X32-NEXT: calll *%ecx
; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction. ; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction.
; X64-LABEL: func_optnone_cf ; X64-LABEL: func_optnone_cf
; X64: leaq target_func(%rip), %rax ; X64: leaq target_func(%rip), %rax
; X64: movq __guard_dispatch_icall_fptr(%rip), %rcx ; X64: movq __guard_dispatch_icall_fptr(%rip), %rcx
; X64: callq *%rcx ; X64: callq *%rcx
; X64-NOT: callq ; X64-NOT: callq
} }
attributes #1 = { noinline optnone } attributes #1 = { noinline optnone }
; Test that Control Flow Guard checks are correctly added in optimized code (common case). ; Test that Control Flow Guard checks are correctly added in optimized code (common case).
define i32 @func_cf() { define i32 @func_cf() {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = call i32 %0() %1 = call i32 %0()
ret i32 %1 ret i32 %1
; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function. ; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function.
; X32-LABEL: func_cf ; X32-LABEL: func_cf
; X32: movl $_target_func, %esi ; X32: movl $_target_func, %esi
; X32: movl $_target_func, %ecx ; X32: movl $_target_func, %ecx
; X32: calll *___guard_check_icall_fptr ; X32: calll *___guard_check_icall_fptr
; X32-NEXT: calll *%esi ; X32-NEXT: calll *%esi
; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction. ; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction.
; X64-LABEL: func_cf ; X64-LABEL: func_cf
; X64: leaq target_func(%rip), %rax ; X64: leaq target_func(%rip), %rax
; X64: callq *__guard_dispatch_icall_fptr(%rip) ; X64: callq *__guard_dispatch_icall_fptr(%rip)
; X64-NOT: callq ; X64-NOT: callq
} }
; Test that Control Flow Guard checks are correctly added on invoke instructions. ; Test that Control Flow Guard checks are correctly added on invoke instructions.
define i32 @func_cf_invoke() personality i8* bitcast (void ()* @h to i8*) { define i32 @func_cf_invoke() personality i8* bitcast (void ()* @h to i8*) {
entry: entry:
%0 = alloca i32, align 4 %0 = alloca i32, align 4
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%1 = load i32 ()*, i32 ()** %func_ptr, align 8 %1 = load i32 ()*, i32 ()** %func_ptr, align 8
%2 = invoke i32 %1() %2 = invoke i32 %1()
to label %invoke.cont unwind label %lpad to label %invoke.cont unwind label %lpad
invoke.cont: ; preds = %entry invoke.cont: ; preds = %entry
ret i32 %2 ret i32 %2
lpad: ; preds = %entry lpad: ; preds = %entry
%tmp = landingpad { i8*, i32 } %tmp = landingpad { i8*, i32 }
catch i8* null catch i8* null
ret i32 -1 ret i32 -1
; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function. ; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function.
; X32-LABEL: func_cf_invoke ; X32-LABEL: func_cf_invoke
; X32: movl $_target_func, %esi ; X32: movl $_target_func, %esi
; X32: movl $_target_func, %ecx ; X32: movl $_target_func, %ecx
; X32: calll *___guard_check_icall_fptr ; X32: calll *___guard_check_icall_fptr
; X32-NEXT: calll *%esi ; X32-NEXT: calll *%esi
; X32: # %invoke.cont ; X32: # %invoke.cont
; X32: # %lpad ; X32: # %lpad
; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction. ; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction.
; X64-LABEL: func_cf_invoke ; X64-LABEL: func_cf_invoke
; X64: leaq target_func(%rip), %rax ; X64: leaq target_func(%rip), %rax
; X64: callq *__guard_dispatch_icall_fptr(%rip) ; X64: callq *__guard_dispatch_icall_fptr(%rip)
; X64-NOT: callq ; X64-NOT: callq
; X64: # %invoke.cont ; X64: # %invoke.cont
; X64: # %lpad ; X64: # %lpad
} }
declare void @h() declare void @h()
; Test that Control Flow Guard preserves floating point arguments. ; Test that Control Flow Guard preserves floating point arguments.
declare double @target_func_doubles(double, double, double, double) declare double @target_func_doubles(double, double, double, double)
define double @func_cf_doubles() { define double @func_cf_doubles() {
entry: entry:
%func_ptr = alloca double (double, double, double, double)*, align 8 %func_ptr = alloca double (double, double, double, double)*, align 8
store double (double, double, double, double)* @target_func_doubles, double (double, double, double, double)** %func_ptr, align 8 store double (double, double, double, double)* @target_func_doubles, double (double, double, double, double)** %func_ptr, align 8
%0 = load double (double, double, double, double)*, double (double, double, double, double)** %func_ptr, align 8 %0 = load double (double, double, double, double)*, double (double, double, double, double)** %func_ptr, align 8
%1 = call double %0(double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00) %1 = call double %0(double 1.000000e+00, double 2.000000e+00, double 3.000000e+00, double 4.000000e+00)
ret double %1 ret double %1
; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function. ; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function.
; X32-LABEL: func_cf_doubles ; X32-LABEL: func_cf_doubles
; X32: movl $_target_func_doubles, %esi ; X32: movl $_target_func_doubles, %esi
; X32: movl $_target_func_doubles, %ecx ; X32: movl $_target_func_doubles, %ecx
; X32: calll *___guard_check_icall_fptr ; X32: calll *___guard_check_icall_fptr
; X32: calll *%esi ; X32: calll *%esi
; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction. ; On x86_64, __guard_dispatch_icall_fptr tail calls the function, so there should be only one call instruction.
; X64-LABEL: func_cf_doubles ; X64-LABEL: func_cf_doubles
; X64: leaq target_func_doubles(%rip), %rax ; X64: leaq target_func_doubles(%rip), %rax
; X64: movsd __real@3ff0000000000000(%rip), %xmm0 ; X64: movsd __real@3ff0000000000000(%rip), %xmm0
; X64: movsd __real@4000000000000000(%rip), %xmm1 ; X64: movsd __real@4000000000000000(%rip), %xmm1
; X64: movsd __real@4008000000000000(%rip), %xmm2 ; X64: movsd __real@4008000000000000(%rip), %xmm2
; X64: movsd __real@4010000000000000(%rip), %xmm3 ; X64: movsd __real@4010000000000000(%rip), %xmm3
; X64: callq *__guard_dispatch_icall_fptr(%rip) ; X64: callq *__guard_dispatch_icall_fptr(%rip)
; X64-NOT: callq ; X64-NOT: callq
} }
; Test that Control Flow Guard checks are correctly added for tail calls. ; Test that Control Flow Guard checks are correctly added for tail calls.
define i32 @func_cf_tail() { define i32 @func_cf_tail() {
entry: entry:
%func_ptr = alloca i32 ()*, align 8 %func_ptr = alloca i32 ()*, align 8
store i32 ()* @target_func, i32 ()** %func_ptr, align 8 store i32 ()* @target_func, i32 ()** %func_ptr, align 8
%0 = load i32 ()*, i32 ()** %func_ptr, align 8 %0 = load i32 ()*, i32 ()** %func_ptr, align 8
%1 = musttail call i32 %0() %1 = musttail call i32 %0()
ret i32 %1 ret i32 %1
; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function. ; On i686, the call to __guard_check_icall_fptr should come immediately before the call to the target function.
; X32-LABEL: func_cf_tail ; X32-LABEL: func_cf_tail
; X32: movl $_target_func, %ecx ; X32: movl $_target_func, %ecx
; X32: calll *___guard_check_icall_fptr ; X32: calll *___guard_check_icall_fptr
; X32: movl $_target_func, %eax ; X32: movl $_target_func, %eax
; X32: jmpl *%eax # TAILCALL ; X32: jmpl *%eax # TAILCALL
; X32-NOT: calll ; X32-NOT: calll
; X64-LABEL: func_cf_tail ; X64-LABEL: func_cf_tail
; X64: leaq target_func(%rip), %rax ; X64: leaq target_func(%rip), %rax
; X64: movq __guard_dispatch_icall_fptr(%rip), %rcx ; X64: movq __guard_dispatch_icall_fptr(%rip), %rcx
; X64: rex64 jmpq *%rcx # TAILCALL ; X64: rex64 jmpq *%rcx # TAILCALL
; X64-NOT: callq ; X64-NOT: callq
} }
; Test that longjmp targets have public labels and are included in the .gljmp section. ; Test that longjmp targets have public labels and are included in the .gljmp section.
%struct._SETJMP_FLOAT128 = type { [2 x i64] } %struct._SETJMP_FLOAT128 = type { [2 x i64] }
@buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16 @buf1 = internal global [16 x %struct._SETJMP_FLOAT128] zeroinitializer, align 16
define i32 @func_cf_setjmp() { define i32 @func_cf_setjmp() {
%1 = alloca i32, align 4 %1 = alloca i32, align 4
%2 = alloca i32, align 4 %2 = alloca i32, align 4
store i32 0, i32* %1, align 4 store i32 0, i32* %1, align 4
store i32 -1, i32* %2, align 4 store i32 -1, i32* %2, align 4
%3 = call i8* @llvm.frameaddress(i32 0) %3 = call i8* @llvm.frameaddress(i32 0)
%4 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %3) #2 %4 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %3) #2
; X32-LABEL: func_cf_setjmp ; X32-LABEL: func_cf_setjmp
; X32: calll __setjmp ; X32: calll __setjmp
; X32-NEXT: $cfgsj_func_cf_setjmp0: ; X32-NEXT: $cfgsj_func_cf_setjmp0:
; X64-LABEL: func_cf_setjmp ; X64-LABEL: func_cf_setjmp
; X64: callq _setjmp ; X64: callq _setjmp
; X64-NEXT: $cfgsj_func_cf_setjmp0: ; X64-NEXT: $cfgsj_func_cf_setjmp0:
%5 = call i8* @llvm.frameaddress(i32 0) %5 = call i8* @llvm.frameaddress(i32 0)
%6 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %5) #2 %6 = call i32 @_setjmp(i8* bitcast ([16 x %struct._SETJMP_FLOAT128]* @buf1 to i8*), i8* %5) #2
; X32: calll __setjmp ; X32: calll __setjmp
; X32-NEXT: $cfgsj_func_cf_setjmp1: ; X32-NEXT: $cfgsj_func_cf_setjmp1:
; X64: callq _setjmp ; X64: callq _setjmp
; X64-NEXT: $cfgsj_func_cf_setjmp1: ; X64-NEXT: $cfgsj_func_cf_setjmp1:
store i32 1, i32* %2, align 4 store i32 1, i32* %2, align 4
%7 = load i32, i32* %2, align 4 %7 = load i32, i32* %2, align 4
ret i32 %7 ret i32 %7
; X32: .section .gljmp$y,"dr" ; X32: .section .gljmp$y,"dr"
; X32-NEXT: .symidx $cfgsj_func_cf_setjmp0 ; X32-NEXT: .symidx $cfgsj_func_cf_setjmp0
; X32-NEXT: .symidx $cfgsj_func_cf_setjmp1 ; X32-NEXT: .symidx $cfgsj_func_cf_setjmp1
; X64: .section .gljmp$y,"dr" ; X64: .section .gljmp$y,"dr"
; X64-NEXT: .symidx $cfgsj_func_cf_setjmp0 ; X64-NEXT: .symidx $cfgsj_func_cf_setjmp0
; X64-NEXT: .symidx $cfgsj_func_cf_setjmp1 ; X64-NEXT: .symidx $cfgsj_func_cf_setjmp1
} }
declare i8* @llvm.frameaddress(i32) declare i8* @llvm.frameaddress(i32)
; Function Attrs: returns_twice ; Function Attrs: returns_twice
declare dso_local i32 @_setjmp(i8*, i8*) #2 declare dso_local i32 @_setjmp(i8*, i8*) #2
attributes #2 = { returns_twice } attributes #2 = { returns_twice }
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 2} !0 = !{i32 2, !"cfguard", i32 2}

View File

@ -1,26 +1,26 @@
; RUN: llc < %s -mtriple=i686-pc-windows-msvc | FileCheck %s -check-prefix=X32 ; RUN: llc < %s -mtriple=i686-pc-windows-msvc | FileCheck %s -check-prefix=X32
; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefix=X64 ; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefix=X64
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are not added in modules with the ; Test that Control Flow Guard checks are not added in modules with the
; cfguard=1 flag (emit tables but no checks). ; cfguard=1 flag (emit tables but no checks).
declare void @target_func() declare void @target_func()
define void @func_in_module_without_cfguard() #0 { define void @func_in_module_without_cfguard() #0 {
entry: entry:
%func_ptr = alloca void ()*, align 8 %func_ptr = alloca void ()*, align 8
store void ()* @target_func, void ()** %func_ptr, align 8 store void ()* @target_func, void ()** %func_ptr, align 8
%0 = load void ()*, void ()** %func_ptr, align 8 %0 = load void ()*, void ()** %func_ptr, align 8
call void %0() call void %0()
ret void ret void
; X32-NOT: __guard_check_icall_fptr ; X32-NOT: __guard_check_icall_fptr
; X64-NOT: __guard_dispatch_icall_fptr ; X64-NOT: __guard_dispatch_icall_fptr
} }
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 1} !0 = !{i32 2, !"cfguard", i32 1}

View File

@ -1,38 +1,38 @@
; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefix=X64 ; RUN: llc < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s -check-prefix=X64
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are correctly added for x86_64 vector calls. ; Test that Control Flow Guard checks are correctly added for x86_64 vector calls.
define void @func_cf_vector_x64(void (%struct.HVA)* %0, %struct.HVA* %1) #0 { define void @func_cf_vector_x64(void (%struct.HVA)* %0, %struct.HVA* %1) #0 {
entry: entry:
%2 = alloca %struct.HVA, align 8 %2 = alloca %struct.HVA, align 8
%3 = bitcast %struct.HVA* %2 to i8* %3 = bitcast %struct.HVA* %2 to i8*
%4 = bitcast %struct.HVA* %1 to i8* %4 = bitcast %struct.HVA* %1 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %3, i8* align 8 %4, i64 32, i1 false) call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %3, i8* align 8 %4, i64 32, i1 false)
%5 = load %struct.HVA, %struct.HVA* %2, align 8 %5 = load %struct.HVA, %struct.HVA* %2, align 8
call x86_vectorcallcc void %0(%struct.HVA inreg %5) call x86_vectorcallcc void %0(%struct.HVA inreg %5)
ret void ret void
; X64-LABEL: func_cf_vector_x64 ; X64-LABEL: func_cf_vector_x64
; X64: movq %rcx, %rax ; X64: movq %rcx, %rax
; X64: movups (%rdx), %xmm0 ; X64: movups (%rdx), %xmm0
; X64: movups 16(%rdx), %xmm1 ; X64: movups 16(%rdx), %xmm1
; X64: movaps %xmm0, 32(%rsp) ; X64: movaps %xmm0, 32(%rsp)
; X64: movaps %xmm1, 48(%rsp) ; X64: movaps %xmm1, 48(%rsp)
; X64: movsd 32(%rsp), %xmm0 # xmm0 = mem[0],zero ; X64: movsd 32(%rsp), %xmm0 # xmm0 = mem[0],zero
; X64: movsd 40(%rsp), %xmm1 # xmm1 = mem[0],zero ; X64: movsd 40(%rsp), %xmm1 # xmm1 = mem[0],zero
; X64: movsd 48(%rsp), %xmm2 # xmm2 = mem[0],zero ; X64: movsd 48(%rsp), %xmm2 # xmm2 = mem[0],zero
; X64: movsd 56(%rsp), %xmm3 # xmm3 = mem[0],zero ; X64: movsd 56(%rsp), %xmm3 # xmm3 = mem[0],zero
; X64: callq *__guard_dispatch_icall_fptr(%rip) ; X64: callq *__guard_dispatch_icall_fptr(%rip)
; X64-NOT: callq ; X64-NOT: callq
} }
attributes #0 = { "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" } attributes #0 = { "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" }
%struct.HVA = type { double, double, double, double } %struct.HVA = type { double, double, double, double }
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1 immarg) #1 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1 immarg) #1
attributes #1 = { argmemonly nounwind willreturn } attributes #1 = { argmemonly nounwind willreturn }
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 2} !0 = !{i32 2, !"cfguard", i32 2}

View File

@ -1,43 +1,43 @@
; RUN: llc < %s -mtriple=i686-pc-windows-msvc | FileCheck %s -check-prefix=X32 ; RUN: llc < %s -mtriple=i686-pc-windows-msvc | FileCheck %s -check-prefix=X32
; Control Flow Guard is currently only available on Windows ; Control Flow Guard is currently only available on Windows
; Test that Control Flow Guard checks are correctly added for x86 vector calls. ; Test that Control Flow Guard checks are correctly added for x86 vector calls.
define void @func_cf_vector_x86(void (%struct.HVA)* %0, %struct.HVA* %1) #0 { define void @func_cf_vector_x86(void (%struct.HVA)* %0, %struct.HVA* %1) #0 {
entry: entry:
%2 = alloca %struct.HVA, align 8 %2 = alloca %struct.HVA, align 8
%3 = bitcast %struct.HVA* %2 to i8* %3 = bitcast %struct.HVA* %2 to i8*
%4 = bitcast %struct.HVA* %1 to i8* %4 = bitcast %struct.HVA* %1 to i8*
call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 8 %3, i8* align 8 %4, i32 32, i1 false) call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 8 %3, i8* align 8 %4, i32 32, i1 false)
%5 = load %struct.HVA, %struct.HVA* %2, align 8 %5 = load %struct.HVA, %struct.HVA* %2, align 8
call x86_vectorcallcc void %0(%struct.HVA inreg %5) call x86_vectorcallcc void %0(%struct.HVA inreg %5)
ret void ret void
; X32-LABEL: func_cf_vector_x86 ; X32-LABEL: func_cf_vector_x86
; X32: movl 12(%ebp), %eax ; X32: movl 12(%ebp), %eax
; X32: movl 8(%ebp), %ecx ; X32: movl 8(%ebp), %ecx
; X32: movsd 24(%eax), %xmm4 # xmm4 = mem[0],zero ; X32: movsd 24(%eax), %xmm4 # xmm4 = mem[0],zero
; X32: movsd %xmm4, 24(%esp) ; X32: movsd %xmm4, 24(%esp)
; X32: movsd 16(%eax), %xmm5 # xmm5 = mem[0],zero ; X32: movsd 16(%eax), %xmm5 # xmm5 = mem[0],zero
; X32: movsd %xmm5, 16(%esp) ; X32: movsd %xmm5, 16(%esp)
; X32: movsd (%eax), %xmm6 # xmm6 = mem[0],zero ; X32: movsd (%eax), %xmm6 # xmm6 = mem[0],zero
; X32: movsd 8(%eax), %xmm7 # xmm7 = mem[0],zero ; X32: movsd 8(%eax), %xmm7 # xmm7 = mem[0],zero
; X32: movsd %xmm7, 8(%esp) ; X32: movsd %xmm7, 8(%esp)
; X32: movsd %xmm6, (%esp) ; X32: movsd %xmm6, (%esp)
; X32: calll *___guard_check_icall_fptr ; X32: calll *___guard_check_icall_fptr
; X32: movaps %xmm6, %xmm0 ; X32: movaps %xmm6, %xmm0
; X32: movaps %xmm7, %xmm1 ; X32: movaps %xmm7, %xmm1
; X32: movaps %xmm5, %xmm2 ; X32: movaps %xmm5, %xmm2
; X32: movaps %xmm4, %xmm3 ; X32: movaps %xmm4, %xmm3
; X32: calll *%ecx ; X32: calll *%ecx
} }
attributes #0 = { "target-cpu"="pentium4" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" } attributes #0 = { "target-cpu"="pentium4" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" }
%struct.HVA = type { double, double, double, double } %struct.HVA = type { double, double, double, double }
declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture writeonly, i8* nocapture readonly, i32, i1 immarg) #1 declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture writeonly, i8* nocapture readonly, i32, i1 immarg) #1
attributes #1 = { argmemonly nounwind willreturn } attributes #1 = { argmemonly nounwind willreturn }
!llvm.module.flags = !{!0} !llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 2} !0 = !{i32 2, !"cfguard", i32 2}