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"
24#include <algorithm>
25
26using namespace llvm;
27
28namespace {
29// FIXME: This pass modifies Function metadata, which is not to be done in
30// MachineFunctionPass. It should probably be moved to a FunctionPass.
31class MachineSanitizerBinaryMetadataLegacy : public MachineFunctionPass {
32public:
33 static char ID;
34
35 MachineSanitizerBinaryMetadataLegacy();
36 bool runOnMachineFunction(MachineFunction &F) override;
37};
38
39struct MachineSanitizerBinaryMetadata {
40 bool run(MachineFunction &MF);
41};
42
43} // namespace
44
45INITIALIZE_PASS(MachineSanitizerBinaryMetadataLegacy, "machine-sanmd",
46 "Machine Sanitizer Binary Metadata", false, false)
47
48char MachineSanitizerBinaryMetadataLegacy::ID = 0;
50 MachineSanitizerBinaryMetadataLegacy::ID;
51
52MachineSanitizerBinaryMetadataLegacy::MachineSanitizerBinaryMetadataLegacy()
56}
57
58bool MachineSanitizerBinaryMetadataLegacy::runOnMachineFunction(
59 MachineFunction &MF) {
60 return MachineSanitizerBinaryMetadata().run(MF);
61}
62
63PreservedAnalyses
66 if (!MachineSanitizerBinaryMetadata().run(MF))
68
70}
71
72bool MachineSanitizerBinaryMetadata::run(MachineFunction &MF) {
73 MDNode *MD = MF.getFunction().getMetadata(LLVMContext::MD_pcsections);
74 if (!MD)
75 return false;
76 const auto &Section = *cast<MDString>(MD->getOperand(0));
77 if (!Section.getString().starts_with(kSanitizerBinaryMetadataCoveredSection))
78 return false;
79 auto &AuxMDs = *cast<MDTuple>(MD->getOperand(1));
80 // Assume it currently only has features.
81 assert(AuxMDs.getNumOperands() == 1);
82 Constant *Features =
83 cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue();
85 return false;
86 // Calculate size of stack args for the function.
87 int64_t Size = 0;
88 uint64_t Align = 0;
89 const MachineFrameInfo &MFI = MF.getFrameInfo();
90 for (int i = -1; i >= (int)-MFI.getNumFixedObjects(); --i) {
91 Size = std::max(Size, MFI.getObjectOffset(i) + MFI.getObjectSize(i));
92 Align = std::max(Align, MFI.getObjectAlign(i).value());
93 }
94 Size = (Size + Align - 1) & ~(Align - 1);
95 if (!Size)
96 return false;
97 // Non-zero size, update metadata.
98 auto &F = MF.getFunction();
99 IRBuilder<> IRB(F.getContext());
100 MDBuilder MDB(F.getContext());
101 // Keep the features and append size of stack args to the metadata.
102 APInt NewFeatures = Features->getUniqueInteger();
104 F.setMetadata(
105 LLVMContext::MD_pcsections,
106 MDB.createPCSections({{Section.getString(),
107 {IRB.getInt(NewFeatures), IRB.getInt32(Size)}}}));
108 return false;
109}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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:1330
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:2780
Metadata node.
Definition Metadata.h:1077
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1441
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:565
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