22 changes: 0 additions & 22 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,6 @@ using namespace llvm;
// Dwarf Emission Helper Routines
//===----------------------------------------------------------------------===//

/// EmitSLEB128 - emit the specified signed leb128 value.
void AsmPrinter::emitSLEB128(int64_t Value, const char *Desc) const {
if (isVerbose() && Desc)
OutStreamer->AddComment(Desc);

OutStreamer->emitSLEB128IntValue(Value);
}

void AsmPrinter::emitULEB128(uint64_t Value, const char *Desc,
unsigned PadTo) const {
if (isVerbose() && Desc)
OutStreamer->AddComment(Desc);

OutStreamer->emitULEB128IntValue(Value, PadTo);
}

/// Emit something like ".uleb128 Hi-Lo".
void AsmPrinter::emitLabelDifferenceAsULEB128(const MCSymbol *Hi,
const MCSymbol *Lo) const {
OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
}

static const char *DecodeDWARFEncoding(unsigned Encoding) {
switch (Encoding) {
case dwarf::DW_EH_PE_absptr:
Expand Down
17 changes: 12 additions & 5 deletions llvm/lib/CodeGen/SanitizerBinaryMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ bool MachineSanitizerBinaryMetadata::runOnMachineFunction(MachineFunction &MF) {
if (!MD)
return false;
const auto &Section = *cast<MDString>(MD->getOperand(0));
if (!Section.getString().equals(kSanitizerBinaryMetadataCoveredSection))
if (!Section.getString().startswith(kSanitizerBinaryMetadataCoveredSection))
return false;
auto &AuxMDs = *cast<MDTuple>(MD->getOperand(1));
// Assume it currently only has features.
assert(AuxMDs.getNumOperands() == 1);
auto *Features = cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue();
Constant *Features =
cast<ConstantAsMetadata>(AuxMDs.getOperand(0))->getValue();
if (!Features->getUniqueInteger()[kSanitizerBinaryMetadataUARBit])
return false;
// Calculate size of stack args for the function.
Expand All @@ -69,12 +70,18 @@ bool MachineSanitizerBinaryMetadata::runOnMachineFunction(MachineFunction &MF) {
Align = std::max(Align, MFI.getObjectAlign(i).value());
}
Size = (Size + Align - 1) & ~(Align - 1);
if (!Size)
return false;
// Non-zero size, update metadata.
auto &F = MF.getFunction();
IRBuilder<> IRB(F.getContext());
MDBuilder MDB(F.getContext());
// Keep the features and append size of stack args to the metadata.
F.setMetadata(LLVMContext::MD_pcsections,
MDB.createPCSections(
{{Section.getString(), {Features, IRB.getInt32(Size)}}}));
APInt NewFeatures = Features->getUniqueInteger();
NewFeatures.setBit(kSanitizerBinaryMetadataUARHasSizeBit);
F.setMetadata(
LLVMContext::MD_pcsections,
MDB.createPCSections({{Section.getString(),
{IRB.getInt(NewFeatures), IRB.getInt32(Size)}}}));
return false;
}
22 changes: 13 additions & 9 deletions llvm/lib/Transforms/Instrumentation/SanitizerBinaryMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
Expand All @@ -52,7 +54,7 @@ namespace {

//===--- Constants --------------------------------------------------------===//

constexpr uint32_t kVersionBase = 1; // occupies lower 16 bits
constexpr uint32_t kVersionBase = 2; // occupies lower 16 bits
constexpr uint32_t kVersionPtrSizeRel = (1u << 16); // offsets are pointer-sized
constexpr int kCtorDtorPriority = 2;

Expand Down Expand Up @@ -147,7 +149,7 @@ class SanitizerBinaryMetadata {
// to determine if a memory operation is atomic or not in modules compiled
// with SanitizerBinaryMetadata.
bool runOn(Instruction &I, MetadataInfoSet &MIS, MDBuilder &MDB,
uint32_t &FeatureMask);
uint64_t &FeatureMask);

// Get start/end section marker pointer.
GlobalVariable *getSectionMarker(const Twine &MarkerName, Type *Ty);
Expand All @@ -168,6 +170,8 @@ class SanitizerBinaryMetadata {
const SanitizerBinaryMetadataOptions Options;
const Triple TargetTriple;
IRBuilder<> IRB;
BumpPtrAllocator Alloc;
UniqueStringSaver StringPool{Alloc};
};

bool SanitizerBinaryMetadata::run() {
Expand Down Expand Up @@ -244,7 +248,7 @@ void SanitizerBinaryMetadata::runOn(Function &F, MetadataInfoSet &MIS) {

// The metadata features enabled for this function, stored along covered
// metadata (if enabled).
uint32_t FeatureMask = 0;
uint64_t FeatureMask = 0;
// Don't emit unnecessary covered metadata for all functions to save space.
bool RequiresCovered = false;

Expand All @@ -269,9 +273,8 @@ void SanitizerBinaryMetadata::runOn(Function &F, MetadataInfoSet &MIS) {
const auto *MI = &MetadataInfo::Covered;
MIS.insert(MI);
const StringRef Section = getSectionName(MI->SectionSuffix);
// The feature mask will be placed after the size (32 bit) of the function,
// so in total one covered entry will use `sizeof(void*) + 4 + 4`.
Constant *CFM = IRB.getInt32(FeatureMask);
// The feature mask will be placed after the function size.
Constant *CFM = IRB.getInt64(FeatureMask);
F.setMetadata(LLVMContext::MD_pcsections,
MDB.createPCSections({{Section, {CFM}}}));
}
Expand Down Expand Up @@ -378,7 +381,7 @@ bool maybeSharedMutable(const Value *Addr) {
}

bool SanitizerBinaryMetadata::runOn(Instruction &I, MetadataInfoSet &MIS,
MDBuilder &MDB, uint32_t &FeatureMask) {
MDBuilder &MDB, uint64_t &FeatureMask) {
SmallVector<const MetadataInfo *, 1> InstMetadata;
bool RequiresCovered = false;

Expand Down Expand Up @@ -433,8 +436,9 @@ SanitizerBinaryMetadata::getSectionMarker(const Twine &MarkerName, Type *Ty) {
}

StringRef SanitizerBinaryMetadata::getSectionName(StringRef SectionSuffix) {
// FIXME: Other TargetTriple (req. string pool)
return SectionSuffix;
// FIXME: Other TargetTriples.
// Request ULEB128 encoding for all integer constants.
return StringPool.save(SectionSuffix + "!C");
}

Twine SanitizerBinaryMetadata::getSectionStart(StringRef SectionSuffix) {
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/CodeGen/X86/pcsections.ll
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,32 @@ entry:
ret void
}

define void @multiple_uleb128() !pcsections !6 {
; CHECK-LABEL: multiple_uleb128:
; CHECK: .section section_aux,"awo",@progbits,.text
; CHECK-NEXT: .Lpcsection_base8:
; DEFCM-NEXT: .long .Lfunc_begin3-.Lpcsection_base8
; LARGE-NEXT: .quad .Lfunc_begin3-.Lpcsection_base8
; CHECK-NEXT: .uleb128 .Lfunc_end6-.Lfunc_begin3
; CHECK-NEXT: .byte 42
; CHECK-NEXT: .ascii "\345\216&"
; CHECK-NEXT: .byte 255
; CHECK-NEXT: .section section_aux_21264,"awo",@progbits,.text
; CHECK-NEXT: .Lpcsection_base9:
; DEFCM-NEXT: .long .Lfunc_begin3-.Lpcsection_base9
; LARGE-NEXT: .quad .Lfunc_begin3-.Lpcsection_base9
; CHECK-NEXT: .long .Lfunc_end6-.Lfunc_begin3
; CHECK-NEXT: .long 21264
; CHECK-NEXT: .text
entry:
ret void
}

!0 = !{!"section_no_aux"}
!1 = !{!"section_aux", !3}
!2 = !{!"section_aux_42", !4, !"section_aux_21264", !5}
!3 = !{i32 10, i32 20, i32 30}
!4 = !{i32 42}
!5 = !{i32 21264}
!6 = !{!"section_aux!C", !7, !"section_aux_21264", !5}
!7 = !{i64 42, i32 624485, i8 255}
14 changes: 7 additions & 7 deletions llvm/test/Instrumentation/SanitizerBinaryMetadata/atomics.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,7 @@ entry:
; CHECK-DAG: entry:
; CHECK-NEXT: br i1 icmp ne (ptr @__sanitizer_metadata_atomics_add, ptr null), label %callfunc, label %ret
; CHECK-DAG: callfunc:
; CHECK-NEXT: call void @__sanitizer_metadata_atomics_add(i32 1, ptr @__start_sanmd_atomics, ptr @__stop_sanmd_atomics)
; CHECK-NEXT: call void @__sanitizer_metadata_atomics_add(i32 2, ptr @__start_sanmd_atomics, ptr @__stop_sanmd_atomics)
; CHECK-NEXT: br label %ret
; CHECK-DAG: ret:
; CHECK-NEXT: ret void
Expand All @@ -2048,7 +2048,7 @@ entry:
; CHECK-DAG: entry:
; CHECK-NEXT: br i1 icmp ne (ptr @__sanitizer_metadata_atomics_del, ptr null), label %callfunc, label %ret
; CHECK-DAG: callfunc:
; CHECK-NEXT: call void @__sanitizer_metadata_atomics_del(i32 1, ptr @__start_sanmd_atomics, ptr @__stop_sanmd_atomics)
; CHECK-NEXT: call void @__sanitizer_metadata_atomics_del(i32 2, ptr @__start_sanmd_atomics, ptr @__stop_sanmd_atomics)
; CHECK-NEXT: br label %ret
; CHECK-DAG: ret:
; CHECK-NEXT: ret void
Expand All @@ -2057,7 +2057,7 @@ entry:
; CHECK-DAG: entry:
; CHECK-NEXT: br i1 icmp ne (ptr @__sanitizer_metadata_covered_add, ptr null), label %callfunc, label %ret
; CHECK-DAG: callfunc:
; CHECK-NEXT: call void @__sanitizer_metadata_covered_add(i32 1, ptr @__start_sanmd_covered, ptr @__stop_sanmd_covered)
; CHECK-NEXT: call void @__sanitizer_metadata_covered_add(i32 2, ptr @__start_sanmd_covered, ptr @__stop_sanmd_covered)
; CHECK-NEXT: br label %ret
; CHECK-DAG: ret:
; CHECK-NEXT: ret void
Expand All @@ -2066,11 +2066,11 @@ entry:
; CHECK-DAG: entry:
; CHECK-NEXT: br i1 icmp ne (ptr @__sanitizer_metadata_covered_del, ptr null), label %callfunc, label %ret
; CHECK-DAG: callfunc:
; CHECK-NEXT: call void @__sanitizer_metadata_covered_del(i32 1, ptr @__start_sanmd_covered, ptr @__stop_sanmd_covered)
; CHECK-NEXT: call void @__sanitizer_metadata_covered_del(i32 2, ptr @__start_sanmd_covered, ptr @__stop_sanmd_covered)
; CHECK-NEXT: br label %ret
; CHECK-DAG: ret:
; CHECK-NEXT: ret void

; CHECK: !0 = !{!"sanmd_covered", !1}
; CHECK: !1 = !{i32 1}
; CHECK: !2 = !{!"sanmd_atomics"}
; CHECK: !0 = !{!"sanmd_covered!C", !1}
; CHECK: !1 = !{i64 1}
; CHECK: !2 = !{!"sanmd_atomics!C"}