LLVM 24.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()
53 : MachineFunctionPass(ID) {}
54
55bool MachineSanitizerBinaryMetadataLegacy::runOnMachineFunction(
56 MachineFunction &MF) {
57 return MachineSanitizerBinaryMetadata().run(MF);
58}
59
60PreservedAnalyses
63 if (!MachineSanitizerBinaryMetadata().run(MF))
65
67}
68
69bool MachineSanitizerBinaryMetadata::run(MachineFunction &MF) {
70 MDNode *MD = MF.getFunction().getMetadata(LLVMContext::MD_pcsections);
71 if (!MD)
72 return false;
73 const auto &Section = *cast<MDString>(MD->getOperand(0));
74 if (!Section.getString().starts_with(kSanitizerBinaryMetadataCoveredSection))
75 return false;
76 auto &AuxMDs = *cast<MDTuple>(MD->getOperand(1));
77 // Assume it currently only has features.
78 assert(AuxMDs.getNumOperands() == 1);
79 Constant *Features =
80 cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue();
82 return false;
83 // Calculate size of stack args for the function.
84 int64_t Size = 0;
85 uint64_t Align = 0;
86 const MachineFrameInfo &MFI = MF.getFrameInfo();
87 for (int i = -1; i >= (int)-MFI.getNumFixedObjects(); --i) {
88 Size = std::max(Size, MFI.getObjectOffset(i) + MFI.getObjectSize(i));
89 Align = std::max(Align, MFI.getObjectAlign(i).value());
90 }
91 Size = (Size + Align - 1) & ~(Align - 1);
92 if (!Size)
93 return false;
94 // Non-zero size, update metadata.
95 auto &F = MF.getFunction();
96 IRBuilder<> IRB(F.getContext());
97 MDBuilder MDB(F.getContext());
98 // Keep the features and append size of stack args to the metadata.
99 APInt NewFeatures = Features->getUniqueInteger();
101 F.setMetadata(
102 LLVMContext::MD_pcsections,
103 MDB.createPCSections({{Section.getString(),
104 {IRB.getInt(NewFeatures), IRB.getInt32(Size)}}}));
105 return false;
106}
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
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:1351
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 metadata of given kind attached to this GlobalObject.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2903
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
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.
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
DXILDebugInfoMap run(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
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