llvm/llvm/lib/Target/AArch64/AArch64SLSHardening.cpp

//===- AArch64SLSHardening.cpp - Harden Straight Line Missspeculation -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains a pass to insert code to mitigate against side channel
// vulnerabilities that may happen under straight line miss-speculation.
//
//===----------------------------------------------------------------------===//

#include "AArch64InstrInfo.h"
#include "AArch64Subtarget.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/CodeGen/IndirectThunks.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/RegisterScavenging.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/Pass.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Target/TargetMachine.h"
#include <cassert>
#include <climits>
#include <tuple>

usingnamespacellvm;

#define DEBUG_TYPE

#define AARCH64_SLS_HARDENING_NAME

// Common name prefix of all thunks generated by this pass.
//
// The generic form is
// __llvm_slsblr_thunk_xN            for BLR thunks
// __llvm_slsblr_thunk_(aaz|abz)_xN  for BLRAAZ and BLRABZ thunks
// __llvm_slsblr_thunk_(aa|ab)_xN_xM for BLRAA and BLRAB thunks
static constexpr StringRef CommonNamePrefix =;

namespace {

struct ThunkKind {};

// Set of inserted thunks.
class ThunksSet {};

struct SLSHardeningInserter : ThunkInserter<SLSHardeningInserter, ThunksSet> {};

} // end anonymous namespace

const ThunkKind ThunkKind::BR =;
const ThunkKind ThunkKind::BRAA =;
const ThunkKind ThunkKind::BRAB =;
const ThunkKind ThunkKind::BRAAZ =;
const ThunkKind ThunkKind::BRABZ =;

// Returns thunk kind to emit, or nullptr if not a BLR* instruction.
static const ThunkKind *getThunkKind(unsigned OriginalOpcode) {}

static bool isBLR(const MachineInstr &MI) {}

unsigned ThunksSet::indexOfXReg(Register Reg) {}

Register ThunksSet::xRegByIndex(unsigned N) {}

static void insertSpeculationBarrier(const AArch64Subtarget *ST,
                                     MachineBasicBlock &MBB,
                                     MachineBasicBlock::iterator MBBI,
                                     DebugLoc DL,
                                     bool AlwaysUseISBDSB = false) {}

ThunksSet SLSHardeningInserter::insertThunks(MachineModuleInfo &MMI,
                                             MachineFunction &MF,
                                             ThunksSet ExistingThunks) {}

bool SLSHardeningInserter::hardenReturnsAndBRs(MachineModuleInfo &MMI,
                                               MachineBasicBlock &MBB) {}

// Currently, the longest possible thunk name is
//   __llvm_slsblr_thunk_aa_xNN_xMM
// which is 31 characters (without the '\0' character).
static SmallString<32> createThunkName(const ThunkKind &Kind, Register Xn,
                                       Register Xm) {}

static std::tuple<const ThunkKind &, Register, Register>
parseThunkName(StringRef ThunkName) {}

void SLSHardeningInserter::populateThunk(MachineFunction &MF) {}

void SLSHardeningInserter::convertBLRToBL(
    MachineModuleInfo &MMI, MachineBasicBlock &MBB,
    MachineBasicBlock::instr_iterator MBBI, ThunksSet &Thunks) {}

bool SLSHardeningInserter::hardenBLRs(MachineModuleInfo &MMI,
                                      MachineBasicBlock &MBB,
                                      ThunksSet &Thunks) {}

namespace {
class AArch64SLSHardening : public ThunkInserterPass<SLSHardeningInserter> {};

} // end anonymous namespace

char AArch64SLSHardening::ID =;

INITIALIZE_PASS()

FunctionPass *llvm::createAArch64SLSHardeningPass() {}