LLVM 19.0.0git
Go to the documentation of this file.
1//===- SanitizerBinaryMetadata.cpp
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
10// This file is a part of SanitizerBinaryMetadata.
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/MDBuilder.h"
22#include "llvm/Pass.h"
23#include <algorithm>
25using namespace llvm;
27namespace {
28class MachineSanitizerBinaryMetadata : public MachineFunctionPass {
30 static char ID;
32 MachineSanitizerBinaryMetadata();
35} // namespace
37INITIALIZE_PASS(MachineSanitizerBinaryMetadata, "machine-sanmd",
38 "Machine Sanitizer Binary Metadata", false, false)
40char MachineSanitizerBinaryMetadata::ID = 0;
42 MachineSanitizerBinaryMetadata::ID;
50bool MachineSanitizerBinaryMetadata::runOnMachineFunction(MachineFunction &MF) {
51 MDNode *MD = MF.getFunction().getMetadata(LLVMContext::MD_pcsections);
52 if (!MD)
53 return false;
54 const auto &Section = *cast<MDString>(MD->getOperand(0));
55 if (!Section.getString().starts_with(kSanitizerBinaryMetadataCoveredSection))
56 return false;
57 auto &AuxMDs = *cast<MDTuple>(MD->getOperand(1));
58 // Assume it currently only has features.
59 assert(AuxMDs.getNumOperands() == 1);
60 Constant *Features =
61 cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue();
63 return false;
64 // Calculate size of stack args for the function.
65 int64_t Size = 0;
66 uint64_t Align = 0;
67 const MachineFrameInfo &MFI = MF.getFrameInfo();
68 for (int i = -1; i >= (int)-MFI.getNumFixedObjects(); --i) {
69 Size = std::max(Size, MFI.getObjectOffset(i) + MFI.getObjectSize(i));
70 Align = std::max(Align, MFI.getObjectAlign(i).value());
71 }
72 Size = (Size + Align - 1) & ~(Align - 1);
73 if (!Size)
74 return false;
75 // Non-zero size, update metadata.
76 auto &F = MF.getFunction();
77 IRBuilder<> IRB(F.getContext());
78 MDBuilder MDB(F.getContext());
79 // Keep the features and append size of stack args to the metadata.
80 APInt NewFeatures = Features->getUniqueInteger();
82 F.setMetadata(
83 LLVMContext::MD_pcsections,
84 MDB.createPCSections({{Section.getString(),
85 {IRB.getInt(NewFeatures), IRB.getInt32(Size)}}}));
86 return false;
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
Definition: APInt.h:77
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1309
This is an important base class in LLVM.
Definition: Constant.h:41
const APInt & getUniqueInteger() const
If C is a constant integer then return its value, otherwise C must be a vector of constant integers,...
Definition: Constants.cpp:1745
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Value.h:565
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
Metadata node.
Definition: Metadata.h:1067
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1428
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...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
char & MachineSanitizerBinaryMetadataID
constexpr int kSanitizerBinaryMetadataUARHasSizeBit
constexpr int kSanitizerBinaryMetadataUARBit
constexpr char kSanitizerBinaryMetadataCoveredSection[]
void initializeMachineSanitizerBinaryMetadataPass(PassRegistry &)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85