LLVM 22.0.0git
SanitizerBinaryMetadata.cpp
Go to the documentation of this file.
1//===- SanitizerBinaryMetadata.cpp
2//----------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of SanitizerBinaryMetadata.
11//
12//===----------------------------------------------------------------------===//
13
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/MDBuilder.h"
22#include "llvm/Pass.h"
25#include <algorithm>
26
27using namespace llvm;
28
29namespace {
30// FIXME: This pass modifies Function metadata, which is not to be done in
31// MachineFunctionPass. It should probably be moved to a FunctionPass.
32class MachineSanitizerBinaryMetadataLegacy : public MachineFunctionPass {
33public:
34 static char ID;
35
36 MachineSanitizerBinaryMetadataLegacy();
37 bool runOnMachineFunction(MachineFunction &F) override;
38};
39
40struct MachineSanitizerBinaryMetadata {
41 bool run(MachineFunction &MF);
42};
43
44} // namespace
45
46INITIALIZE_PASS(MachineSanitizerBinaryMetadataLegacy, "machine-sanmd",
47 "Machine Sanitizer Binary Metadata", false, false)
48
49char MachineSanitizerBinaryMetadataLegacy::ID = 0;
51 MachineSanitizerBinaryMetadataLegacy::ID;
52
53MachineSanitizerBinaryMetadataLegacy::MachineSanitizerBinaryMetadataLegacy()
57}
58
59bool MachineSanitizerBinaryMetadataLegacy::runOnMachineFunction(
60 MachineFunction &MF) {
61 return MachineSanitizerBinaryMetadata().run(MF);
62}
63
64PreservedAnalyses
67 if (!MachineSanitizerBinaryMetadata().run(MF))
69
71}
72
73bool MachineSanitizerBinaryMetadata::run(MachineFunction &MF) {
74 MDNode *MD = MF.getFunction().getMetadata(LLVMContext::MD_pcsections);
75 if (!MD)
76 return false;
77 const auto &Section = *cast<MDString>(MD->getOperand(0));
78 if (!Section.getString().starts_with(kSanitizerBinaryMetadataCoveredSection))
79 return false;
80 auto &AuxMDs = *cast<MDTuple>(MD->getOperand(1));
81 // Assume it currently only has features.
82 assert(AuxMDs.getNumOperands() == 1);
83 Constant *Features =
84 cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue();
86 return false;
87 // Calculate size of stack args for the function.
88 int64_t Size = 0;
89 uint64_t Align = 0;
90 const MachineFrameInfo &MFI = MF.getFrameInfo();
91 for (int i = -1; i >= (int)-MFI.getNumFixedObjects(); --i) {
92 Size = std::max(Size, MFI.getObjectOffset(i) + MFI.getObjectSize(i));
93 Align = std::max(Align, MFI.getObjectAlign(i).value());
94 }
95 Size = (Size + Align - 1) & ~(Align - 1);
96 if (!Size)
97 return false;
98 // Non-zero size, update metadata.
99 auto &F = MF.getFunction();
100 IRBuilder<> IRB(F.getContext());
101 MDBuilder MDB(F.getContext());
102 // Keep the features and append size of stack args to the metadata.
103 APInt NewFeatures = Features->getUniqueInteger();
105 F.setMetadata(
106 LLVMContext::MD_pcsections,
107 MDB.createPCSections({{Section.getString(),
108 {IRB.getInt(NewFeatures), IRB.getInt32(Size)}}}));
109 return false;
110}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:54
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
Defines the virtual file system interface vfs::FileSystem.
Class for arbitrary precision integers.
Definition APInt.h:78
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1331
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI const APInt & getUniqueInteger() const
If C is a constant integer then return its value, otherwise C must be a vector of constant integers,...
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition Value.h:576
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
unsigned getNumFixedObjects() const
Return the number of fixed objects.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI char & MachineSanitizerBinaryMetadataID
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
constexpr int kSanitizerBinaryMetadataUARHasSizeBit
constexpr int kSanitizerBinaryMetadataUARBit
constexpr char kSanitizerBinaryMetadataCoveredSection[]
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI void initializeMachineSanitizerBinaryMetadataLegacyPass(PassRegistry &)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77