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
14#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
15#include "llvm/CodeGen/MachineFrameInfo.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/MDBuilder.h"
21#include "llvm/InitializePasses.h"
22#include "llvm/Pass.h"
23#include <algorithm>
24
25using namespace llvm;
26
27namespace {
28class MachineSanitizerBinaryMetadata : public MachineFunctionPass {
29public:
30 static char ID;
31
32 MachineSanitizerBinaryMetadata();
33 bool runOnMachineFunction(MachineFunction &F) override;
34};
35} // namespace
36
37INITIALIZE_PASS(MachineSanitizerBinaryMetadata, "machine-sanmd",
38 "Machine Sanitizer Binary Metadata", false, false)
39
40char MachineSanitizerBinaryMetadata::ID = 0;
41char &llvm::MachineSanitizerBinaryMetadataID =
42 MachineSanitizerBinaryMetadata::ID;
43
44MachineSanitizerBinaryMetadata::MachineSanitizerBinaryMetadata()
45 : MachineFunctionPass(ID) {
46 initializeMachineSanitizerBinaryMetadataPass(
47 Registry&: *PassRegistry::getPassRegistry());
48}
49
50bool MachineSanitizerBinaryMetadata::runOnMachineFunction(MachineFunction &MF) {
51 MDNode *MD = MF.getFunction().getMetadata(KindID: LLVMContext::MD_pcsections);
52 if (!MD)
53 return false;
54 const auto &Section = *cast<MDString>(Val: MD->getOperand(I: 0));
55 if (!Section.getString().starts_with(Prefix: kSanitizerBinaryMetadataCoveredSection))
56 return false;
57 auto &AuxMDs = *cast<MDTuple>(Val: MD->getOperand(I: 1));
58 // Assume it currently only has features.
59 assert(AuxMDs.getNumOperands() == 1);
60 Constant *Features =
61 cast<ConstantAsMetadata>(Val: AuxMDs.getOperand(I: 0))->getValue();
62 if (!Features->getUniqueInteger()[kSanitizerBinaryMetadataUARBit])
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(a: Size, b: MFI.getObjectOffset(ObjectIdx: i) + MFI.getObjectSize(ObjectIdx: i));
70 Align = std::max(a: Align, b: MFI.getObjectAlign(ObjectIdx: 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();
81 NewFeatures.setBit(kSanitizerBinaryMetadataUARHasSizeBit);
82 F.setMetadata(
83 KindID: LLVMContext::MD_pcsections,
84 Node: MDB.createPCSections(Sections: {{Section.getString(),
85 {IRB.getInt(AI: NewFeatures), IRB.getInt32(C: Size)}}}));
86 return false;
87}
88