LLVM 23.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()
55
56bool MachineSanitizerBinaryMetadataLegacy::runOnMachineFunction(
57 MachineFunction &MF) {
58 return MachineSanitizerBinaryMetadata().run(MF);
59}
60
61PreservedAnalyses
64 if (!MachineSanitizerBinaryMetadata().run(MF))
66
68}
69
70bool MachineSanitizerBinaryMetadata::run(MachineFunction &MF) {
71 MDNode *MD = MF.getFunction().getMetadata(LLVMContext::MD_pcsections);
72 if (!MD)
73 return false;
74 const auto &Section = *cast<MDString>(MD->getOperand(0));
75 if (!Section.getString().starts_with(kSanitizerBinaryMetadataCoveredSection))
76 return false;
77 auto &AuxMDs = *cast<MDTuple>(MD->getOperand(1));
78 // Assume it currently only has features.
79 assert(AuxMDs.getNumOperands() == 1);
80 Constant *Features =
81 cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue();
83 return false;
84 // Calculate size of stack args for the function.
85 int64_t Size = 0;
86 uint64_t Align = 0;
87 const MachineFrameInfo &MFI = MF.getFrameInfo();
88 for (int i = -1; i >= (int)-MFI.getNumFixedObjects(); --i) {
89 Size = std::max(Size, MFI.getObjectOffset(i) + MFI.getObjectSize(i));
90 Align = std::max(Align, MFI.getObjectAlign(i).value());
91 }
92 Size = (Size + Align - 1) & ~(Align - 1);
93 if (!Size)
94 return false;
95 // Non-zero size, update metadata.
96 auto &F = MF.getFunction();
97 IRBuilder<> IRB(F.getContext());
98 MDBuilder MDB(F.getContext());
99 // Keep the features and append size of stack args to the metadata.
100 APInt NewFeatures = Features->getUniqueInteger();
102 F.setMetadata(
103 LLVMContext::MD_pcsections,
104 MDB.createPCSections({{Section.getString(),
105 {IRB.getInt(NewFeatures), IRB.getInt32(Size)}}}));
106 return false;
107}
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:1339
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:2772
Metadata node.
Definition Metadata.h:1079
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1443
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 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.
Definition Types.h:26
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
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