Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use-after-return sanitizer binary metadata
Currently per-function metadata consists of: (start-pc, size, features) This adds a new UAR feature and if it's set an additional element: (start-pc, size, features, stack-args-size) Reviewed By: melver Differential Revision: https://reviews.llvm.org/D136078
- Loading branch information
Showing
20 changed files
with
375 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//===- SanitizerBinaryMetadata.cpp | ||
//----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file is a part of SanitizerBinaryMetadata. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h" | ||
#include "llvm/CodeGen/MachineFrameInfo.h" | ||
#include "llvm/CodeGen/MachineFunction.h" | ||
#include "llvm/CodeGen/MachineFunctionPass.h" | ||
#include "llvm/CodeGen/Passes.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/MDBuilder.h" | ||
#include "llvm/InitializePasses.h" | ||
#include "llvm/Pass.h" | ||
#include <algorithm> | ||
|
||
using namespace llvm; | ||
|
||
class MachineSanitizerBinaryMetadata : public MachineFunctionPass { | ||
public: | ||
static char ID; | ||
|
||
MachineSanitizerBinaryMetadata(); | ||
bool runOnMachineFunction(MachineFunction &F) override; | ||
}; | ||
|
||
INITIALIZE_PASS(MachineSanitizerBinaryMetadata, "machine-sanmd", | ||
"Machine Sanitizer Binary Metadata", false, false) | ||
|
||
char MachineSanitizerBinaryMetadata::ID = 0; | ||
char &llvm::MachineSanitizerBinaryMetadataID = | ||
MachineSanitizerBinaryMetadata::ID; | ||
|
||
MachineSanitizerBinaryMetadata::MachineSanitizerBinaryMetadata() | ||
: MachineFunctionPass(ID) { | ||
initializeMachineSanitizerBinaryMetadataPass( | ||
*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
bool MachineSanitizerBinaryMetadata::runOnMachineFunction(MachineFunction &MF) { | ||
MDNode *MD = MF.getFunction().getMetadata(LLVMContext::MD_pcsections); | ||
if (!MD) | ||
return false; | ||
const auto &Section = *cast<MDString>(MD->getOperand(0)); | ||
if (!Section.getString().equals(kSanitizerBinaryMetadataCoveredSection)) | ||
return false; | ||
auto &AuxMDs = *cast<MDTuple>(MD->getOperand(1)); | ||
// Assume it currently only has features. | ||
assert(AuxMDs.size() == 1); | ||
auto *Features = cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue(); | ||
if (!Features->getUniqueInteger()[kSanitizerBinaryMetadataUARBit]) | ||
return false; | ||
// Calculate size of stack args for the function. | ||
int64_t Size = 0; | ||
uint64_t Align = 0; | ||
const MachineFrameInfo &MFI = MF.getFrameInfo(); | ||
for (int i = -1; i >= (int)-MFI.getNumFixedObjects(); --i) { | ||
Size = std::max(Size, MFI.getObjectOffset(i) + MFI.getObjectSize(i)); | ||
Align = std::max(Align, MFI.getObjectAlign(i).value()); | ||
} | ||
Size = (Size + Align - 1) & ~(Align - 1); | ||
auto &F = MF.getFunction(); | ||
IRBuilder<> IRB(F.getContext()); | ||
MDBuilder MDB(F.getContext()); | ||
// Keep the features and append size of stack args to the metadata. | ||
const auto *NewMD = MDB.createPCSections( | ||
{{Section.getString(), {Features, IRB.getInt32(Size)}}}); | ||
MD->replaceOperandWith(1, NewMD->getOperand(1)); | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
a1255dc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've checked in dbb1130 to rever this commit due to a build error. Would you mind looking into the assertion? Thanks!