Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bolt/include/bolt/Rewrite/MetadataRewriters.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ class BinaryContext;

// The list of rewriter build functions.

std::unique_ptr<MetadataRewriter> createLinuxKernelRewriter(BinaryContext &);

std::unique_ptr<MetadataRewriter> createBuildIDRewriter(BinaryContext &);

std::unique_ptr<MetadataRewriter> createLinuxKernelRewriter(BinaryContext &);

std::unique_ptr<MetadataRewriter> createPseudoProbeRewriter(BinaryContext &);

std::unique_ptr<MetadataRewriter> createRSeqRewriter(BinaryContext &);

std::unique_ptr<MetadataRewriter> createSDTRewriter(BinaryContext &);

} // namespace bolt
Expand Down
1 change: 1 addition & 0 deletions bolt/lib/Rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_llvm_library(LLVMBOLTRewrite
BuildIDRewriter.cpp
PseudoProbeRewriter.cpp
RewriteInstance.cpp
RSeqRewriter.cpp
SDTRewriter.cpp

NO_EXPORT
Expand Down
71 changes: 71 additions & 0 deletions bolt/lib/Rewrite/RSeqRewriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//===- bolt/Rewrite/RSeqRewriter.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
//
//===----------------------------------------------------------------------===//
//
// Basic support for restartable sequences used by tcmalloc. Prevent critical
// section overrides by ignoring optimizations in containing functions.
//
// References:
// * https://google.github.io/tcmalloc/rseq.html
// * tcmalloc/internal/percpu_rseq_x86_64.S
//
//===----------------------------------------------------------------------===//

#include "bolt/Core/BinaryFunction.h"
#include "bolt/Rewrite/MetadataRewriter.h"
#include "bolt/Rewrite/MetadataRewriters.h"
#include "llvm/Support/Errc.h"

using namespace llvm;
using namespace bolt;

namespace {

class RSeqRewriter final : public MetadataRewriter {
public:
RSeqRewriter(StringRef Name, BinaryContext &BC)
: MetadataRewriter(Name, BC) {}

Error postCFGInitializer() override {
for (const BinarySection &Section : BC.allocatableSections()) {
if (Section.getName() != "__rseq_cs")
continue;

auto handleRelocation = [&](const Relocation &Rel) {
BinaryFunction *BF = nullptr;
if (Rel.Symbol)
BF = BC.getFunctionForSymbol(Rel.Symbol);
else if (Relocation::isRelative(Rel.Type))
BF = BC.getBinaryFunctionContainingAddress(Rel.Addend);

if (BF) {
BC.outs() << "BOLT-INFO: restartable sequence detected in " << *BF
<< ". Function will not be optimized\n";
BF->setIgnored();
} else {
BC.errs() << "BOLT-WARNING: no function found matching dynamic "
"relocation in __rseq_cs\n";
}
};

for (const Relocation &Rel : Section.dynamicRelocations())
handleRelocation(Rel);

for (const Relocation &Rel : Section.relocations())
handleRelocation(Rel);
}

return Error::success();
}
};

} // namespace

std::unique_ptr<MetadataRewriter>
llvm::bolt::createRSeqRewriter(BinaryContext &BC) {
return std::make_unique<RSeqRewriter>("rseq-cs-rewriter", BC);
}
2 changes: 2 additions & 0 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3338,6 +3338,8 @@ void RewriteInstance::initializeMetadataManager() {

MetadataManager.registerRewriter(createPseudoProbeRewriter(*BC));

MetadataManager.registerRewriter(createRSeqRewriter(*BC));

MetadataManager.registerRewriter(createSDTRewriter(*BC));
}

Expand Down
32 changes: 32 additions & 0 deletions bolt/test/X86/rseq.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Check that llvm-bolt avoids optimization of functions referenced from
## __rseq_cs section, i.e. containing critical sections used by restartable
## sequences in tcmalloc.

# RUN: %clang %cflags %s -o %t -nostdlib -no-pie -Wl,-q
# RUN: llvm-bolt %t -o %t.bolt --print-cfg 2>&1 \
# RUN: | FileCheck %s --check-prefix=CHECK-NO-PIE
# RUN: %clang %cflags %s -o %t.pie -nostdlib -pie -Wl,-q
# RUN: llvm-bolt %t.pie -o %t.pie.bolt 2>&1 \
# RUN: | FileCheck %s --check-prefix=CHECK-PIE

# CHECK-NO-PIE: Binary Function "_start"
# CHECK-NO-PIE: IsSimple
# CHECK-NO-PIE-SAME: 0

# CHECK-PIE: restartable sequence detected in _start

.global _start
.type _start, %function
_start:
pushq %rbp
mov %rsp, %rbp
.L1:
pop %rbp
retq
.size _start, .-_start

.reloc 0, R_X86_64_NONE

.section __rseq_cs, "aw"
.balign 32
.quad .L1
Loading