[clang] [llvm] [LFI][X86] Add X86 LFI target and system instruction rewrites (PR #189569)
Zachary Yedidia via cfe-commits
cfe-commits at lists.llvm.org
Tue May 26 01:41:21 PDT 2026
================
@@ -0,0 +1,211 @@
+//===- X86MCLFIRewriter.cpp -------------------------------------*- C++ -*-===//
+//
+// 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 implements the X86MCLFIRewriter class, which rewrites X86-64
+// instructions for LFI (Lightweight Fault Isolation) sandboxing.
+//
+//===----------------------------------------------------------------------===//
+
+#include "X86MCLFIRewriter.h"
+#include "X86BaseInfo.h"
+#include "X86MCTargetDesc.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCStreamer.h"
+#include "llvm/MC/MCSubtargetInfo.h"
+
+using namespace llvm;
+
+// LFI reserved registers.
+static constexpr MCRegister LFIBaseReg = X86::R14;
+static constexpr MCRegister LFIScratchReg = X86::R11;
+static constexpr MCRegister LFITPReg = X86::R15;
+
+// Byte offset into the context register file (pointed to by R15) where the
+// thread pointer is stored.
+static constexpr int TPOffset = 16;
+
+static bool isSyscall(const MCInst &Inst) {
+ return Inst.getOpcode() == X86::SYSCALL;
+}
+
+// Find the index of the first memory operand with %fs segment override.
+// Returns -1 if not found.
+static int findFSMemOperand(const MCInst &Inst, const MCInstrInfo &InstInfo) {
+ const MCInstrDesc &Desc = InstInfo.get(Inst.getOpcode());
+ for (unsigned I = 0, E = Desc.getNumOperands(); I < E; ++I) {
+ if (Desc.operands()[I].OperandType == MCOI::OPERAND_MEMORY) {
+ if (I + 4 < Inst.getNumOperands() && Inst.getOperand(I + 4).isReg() &&
+ Inst.getOperand(I + 4).getReg() == X86::FS)
+ return I;
+ I += 4;
----------------
zyedidia wrote:
They are grouped like that, but I see that I can rewrite this by using some helpers (`getMemoryOperandNo` and `getOperandBias`) instead.
https://github.com/llvm/llvm-project/pull/189569
More information about the cfe-commits
mailing list