2021-12-21 10:21:41 -08:00
|
|
|
//===- bolt/RuntimeLibs/InstrumentationRuntimeLibrary.cpp -----------------===//
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
//
|
2021-03-15 18:04:18 -07:00
|
|
|
// 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
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2021-12-21 10:21:41 -08:00
|
|
|
// This file implements the InstrumentationRuntimeLibrary class.
|
|
|
|
|
//
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2021-10-08 11:47:10 -07:00
|
|
|
#include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h"
|
|
|
|
|
#include "bolt/Core/BinaryFunction.h"
|
|
|
|
|
#include "bolt/Core/JumpTable.h"
|
|
|
|
|
#include "bolt/Utils/CommandLineOpts.h"
|
2021-04-30 13:54:02 -07:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2020-12-01 16:29:39 -08:00
|
|
|
#include "llvm/Support/Alignment.h"
|
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace bolt;
|
|
|
|
|
|
|
|
|
|
namespace opts {
|
|
|
|
|
|
2021-10-08 11:47:10 -07:00
|
|
|
cl::opt<std::string> RuntimeInstrumentationLib(
|
|
|
|
|
"runtime-instrumentation-lib",
|
|
|
|
|
cl::desc("specify file name of the runtime instrumentation library"),
|
2022-06-05 13:29:49 -07:00
|
|
|
cl::init("libbolt_rt_instr.a"), cl::cat(BoltOptCategory));
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
|
|
|
|
|
extern cl::opt<bool> InstrumentationFileAppendPID;
|
2021-08-08 04:50:06 +08:00
|
|
|
extern cl::opt<bool> ConservativeInstrumentation;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
extern cl::opt<std::string> InstrumentationFilename;
|
2021-07-30 18:07:53 +03:00
|
|
|
extern cl::opt<std::string> InstrumentationBinpath;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
extern cl::opt<uint32_t> InstrumentationSleepTime;
|
2021-03-09 16:18:11 -08:00
|
|
|
extern cl::opt<bool> InstrumentationNoCountersClear;
|
|
|
|
|
extern cl::opt<bool> InstrumentationWaitForks;
|
2021-03-15 16:34:25 -07:00
|
|
|
extern cl::opt<JumpTableSupportLevel> JumpTables;
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
|
|
|
|
|
} // namespace opts
|
|
|
|
|
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-02 11:14:38 -07:00
|
|
|
void InstrumentationRuntimeLibrary::adjustCommandLineOptions(
|
|
|
|
|
const BinaryContext &BC) const {
|
|
|
|
|
if (!BC.HasRelocations) {
|
|
|
|
|
errs() << "BOLT-ERROR: instrumentation runtime libraries require "
|
|
|
|
|
"relocations\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2021-03-15 16:34:25 -07:00
|
|
|
if (opts::JumpTables != JTS_MOVE) {
|
|
|
|
|
opts::JumpTables = JTS_MOVE;
|
|
|
|
|
outs() << "BOLT-INFO: forcing -jump-tables=move for instrumentation\n";
|
|
|
|
|
}
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-02 11:14:38 -07:00
|
|
|
if (!BC.StartFunctionAddress) {
|
|
|
|
|
errs() << "BOLT-ERROR: instrumentation runtime libraries require a known "
|
|
|
|
|
"entry point of "
|
|
|
|
|
"the input binary\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2021-06-21 01:59:38 +08:00
|
|
|
if (!BC.FiniFunctionAddress && !BC.IsStaticExecutable) {
|
Adding automatic huge page support
Summary:
This patch enables automated hugify for Bolt.
When running Bolt against a binary with -hugify specified, Bolt will inject a call to a runtime library function at the entry of the binary. The runtime library calls madvise to map the hot code region into a 2M huge page. We support both new kernel with THP support and old kernels. For kernels with THP support we simply make a madvise call, while for old kernels, we first copy the code out, remap the memory with huge page, and then copy the code back.
With this change, we no longer need to manually call into hugify_self and precompile it with --hot-text. Instead, we could simply combine --hugify option with existing optimizations, and at runtime it will automatically move hot code into 2M pages.
Some details around the changes made:
1. Add an command line option to support --hugify. --hugify will automatically turn on --hot-text to get the proper hot code symbols. However, running with both --hugify and --hot-text is not allowed, since --hot-text is used on binaries that has precompiled call to hugify_self, which contradicts with the purpose of --hugify.
2. Moved the common utility functions out of instr.cpp to common.h, which will also be used by hugify.cpp. Added a few new system calls definitions.
3. Added a new class that inherits RuntimeLibrary, and implemented the necessary emit and link logic for hugify.
4. Added a simple test for hugify.
(cherry picked from FBD21384529)
2020-05-02 11:14:38 -07:00
|
|
|
errs() << "BOLT-ERROR: input binary lacks DT_FINI entry in the dynamic "
|
|
|
|
|
"section but instrumentation currently relies on patching "
|
|
|
|
|
"DT_FINI to write the profile\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
void InstrumentationRuntimeLibrary::emitBinary(BinaryContext &BC,
|
|
|
|
|
MCStreamer &Streamer) {
|
2020-10-12 10:11:17 -07:00
|
|
|
MCSection *Section = BC.isELF()
|
|
|
|
|
? static_cast<MCSection *>(BC.Ctx->getELFSection(
|
|
|
|
|
".bolt.instr.counters", ELF::SHT_PROGBITS,
|
|
|
|
|
BinarySection::getFlags(/*IsReadOnly=*/false,
|
|
|
|
|
/*IsText=*/false,
|
|
|
|
|
/*IsAllocatable=*/true)
|
|
|
|
|
|
|
|
|
|
))
|
|
|
|
|
: static_cast<MCSection *>(BC.Ctx->getMachOSection(
|
2021-01-28 12:04:46 -08:00
|
|
|
"__BOLT", "__counters", MachO::S_REGULAR,
|
2020-10-12 10:11:17 -07:00
|
|
|
SectionKind::getData()));
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
|
2021-06-21 01:59:38 +08:00
|
|
|
if (BC.IsStaticExecutable && !opts::InstrumentationSleepTime) {
|
|
|
|
|
errs() << "BOLT-ERROR: instrumentation of static binary currently does not "
|
|
|
|
|
"support profile output on binary finalization, so it "
|
|
|
|
|
"requires -instrumentation-sleep-time=N (N>0) usage\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 16:18:11 -08:00
|
|
|
Section->setAlignment(llvm::Align(BC.RegularPageSize));
|
2022-06-10 22:50:55 -07:00
|
|
|
Streamer.switchSection(Section);
|
2021-03-09 16:18:11 -08:00
|
|
|
|
2021-05-14 14:09:05 +03:00
|
|
|
// EmitOffset is used to determine padding size for data alignment
|
|
|
|
|
uint64_t EmitOffset = 0;
|
|
|
|
|
|
|
|
|
|
auto emitLabel = [&Streamer](MCSymbol *Symbol, bool IsGlobal = true) {
|
2021-03-09 16:18:11 -08:00
|
|
|
Streamer.emitLabel(Symbol);
|
|
|
|
|
if (IsGlobal)
|
|
|
|
|
Streamer.emitSymbolAttribute(Symbol, MCSymbolAttr::MCSA_Global);
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-14 14:09:05 +03:00
|
|
|
auto emitLabelByName = [&BC, emitLabel](StringRef Name,
|
|
|
|
|
bool IsGlobal = true) {
|
2021-03-09 16:18:11 -08:00
|
|
|
MCSymbol *Symbol = BC.Ctx->getOrCreateSymbol(Name);
|
2021-05-14 14:09:05 +03:00
|
|
|
emitLabel(Symbol, IsGlobal);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto emitPadding = [&Streamer, &EmitOffset](unsigned Size) {
|
|
|
|
|
const uint64_t Padding = alignTo(EmitOffset, Size) - EmitOffset;
|
|
|
|
|
if (Padding) {
|
|
|
|
|
Streamer.emitFill(Padding, 0);
|
|
|
|
|
EmitOffset += Padding;
|
|
|
|
|
}
|
2021-03-09 16:18:11 -08:00
|
|
|
};
|
|
|
|
|
|
2021-05-14 14:09:05 +03:00
|
|
|
auto emitDataSize = [&EmitOffset](unsigned Size) { EmitOffset += Size; };
|
|
|
|
|
|
|
|
|
|
auto emitDataPadding = [emitPadding, emitDataSize](unsigned Size) {
|
|
|
|
|
emitPadding(Size);
|
|
|
|
|
emitDataSize(Size);
|
2021-03-09 16:18:11 -08:00
|
|
|
};
|
|
|
|
|
|
2021-05-14 14:09:05 +03:00
|
|
|
auto emitFill = [&Streamer, emitDataSize,
|
|
|
|
|
emitLabel](unsigned Size, MCSymbol *Symbol = nullptr,
|
|
|
|
|
uint8_t Byte = 0) {
|
|
|
|
|
emitDataSize(Size);
|
|
|
|
|
if (Symbol)
|
|
|
|
|
emitLabel(Symbol, /*IsGlobal*/ false);
|
|
|
|
|
Streamer.emitFill(Size, Byte);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto emitValue = [&BC, &Streamer, emitDataPadding,
|
|
|
|
|
emitLabel](MCSymbol *Symbol, const MCExpr *Value) {
|
|
|
|
|
const unsigned Psize = BC.AsmInfo->getCodePointerSize();
|
|
|
|
|
emitDataPadding(Psize);
|
|
|
|
|
emitLabel(Symbol);
|
2021-06-23 18:24:09 +00:00
|
|
|
if (Value)
|
|
|
|
|
Streamer.emitValue(Value, Psize);
|
|
|
|
|
else
|
|
|
|
|
Streamer.emitFill(Psize, 0);
|
2021-05-14 14:09:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto emitIntValue = [&Streamer, emitDataPadding, emitLabelByName](
|
|
|
|
|
StringRef Name, uint64_t Value, unsigned Size = 4) {
|
|
|
|
|
emitDataPadding(Size);
|
|
|
|
|
emitLabelByName(Name);
|
2021-03-09 16:18:11 -08:00
|
|
|
Streamer.emitIntValue(Value, Size);
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-14 14:09:05 +03:00
|
|
|
auto emitString = [&Streamer, emitDataSize, emitLabelByName,
|
|
|
|
|
emitFill](StringRef Name, StringRef Contents) {
|
|
|
|
|
emitDataSize(Contents.size());
|
|
|
|
|
emitLabelByName(Name);
|
2021-03-09 16:18:11 -08:00
|
|
|
Streamer.emitBytes(Contents);
|
2021-05-14 14:09:05 +03:00
|
|
|
emitFill(1);
|
2021-03-09 16:18:11 -08:00
|
|
|
};
|
|
|
|
|
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
// All of the following symbols will be exported as globals to be used by the
|
|
|
|
|
// instrumentation runtime library to dump the instrumentation data to disk.
|
|
|
|
|
// Label marking start of the memory region containing instrumentation
|
|
|
|
|
// counters, total vector size is Counters.size() 8-byte counters
|
2021-05-14 14:09:05 +03:00
|
|
|
emitLabelByName("__bolt_instr_locations");
|
|
|
|
|
for (MCSymbol *const &Label : Summary->Counters)
|
|
|
|
|
emitFill(sizeof(uint64_t), Label);
|
|
|
|
|
|
|
|
|
|
emitPadding(BC.RegularPageSize);
|
|
|
|
|
emitIntValue("__bolt_instr_sleep_time", opts::InstrumentationSleepTime);
|
|
|
|
|
emitIntValue("__bolt_instr_no_counters_clear",
|
2021-03-09 16:18:11 -08:00
|
|
|
!!opts::InstrumentationNoCountersClear, 1);
|
2021-08-08 04:50:06 +08:00
|
|
|
emitIntValue("__bolt_instr_conservative", !!opts::ConservativeInstrumentation,
|
|
|
|
|
1);
|
2021-05-14 14:09:05 +03:00
|
|
|
emitIntValue("__bolt_instr_wait_forks", !!opts::InstrumentationWaitForks, 1);
|
|
|
|
|
emitIntValue("__bolt_num_counters", Summary->Counters.size());
|
2021-06-23 18:24:09 +00:00
|
|
|
emitValue(Summary->IndCallCounterFuncPtr, nullptr);
|
|
|
|
|
emitValue(Summary->IndTailCallCounterFuncPtr, nullptr);
|
2021-05-14 14:09:05 +03:00
|
|
|
emitIntValue("__bolt_instr_num_ind_calls",
|
2021-03-09 16:18:11 -08:00
|
|
|
Summary->IndCallDescriptions.size());
|
2021-05-14 14:09:05 +03:00
|
|
|
emitIntValue("__bolt_instr_num_ind_targets",
|
2021-03-09 16:18:11 -08:00
|
|
|
Summary->IndCallTargetDescriptions.size());
|
2021-05-14 14:09:05 +03:00
|
|
|
emitIntValue("__bolt_instr_num_funcs", Summary->FunctionDescriptions.size());
|
|
|
|
|
emitString("__bolt_instr_filename", opts::InstrumentationFilename);
|
2021-07-30 18:07:53 +03:00
|
|
|
emitString("__bolt_instr_binpath", opts::InstrumentationBinpath);
|
2021-05-14 14:09:05 +03:00
|
|
|
emitIntValue("__bolt_instr_use_pid", !!opts::InstrumentationFileAppendPID, 1);
|
2021-01-28 12:44:14 -08:00
|
|
|
|
|
|
|
|
if (BC.isMachO()) {
|
|
|
|
|
MCSection *TablesSection = BC.Ctx->getMachOSection(
|
2021-12-14 16:52:51 -08:00
|
|
|
"__BOLT", "__tables", MachO::S_REGULAR, SectionKind::getData());
|
2020-12-01 16:29:39 -08:00
|
|
|
TablesSection->setAlignment(llvm::Align(BC.RegularPageSize));
|
2022-06-10 22:50:55 -07:00
|
|
|
Streamer.switchSection(TablesSection);
|
2021-05-14 14:09:05 +03:00
|
|
|
emitString("__bolt_instr_tables", buildTables(BC));
|
2021-01-28 12:44:14 -08:00
|
|
|
}
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
void InstrumentationRuntimeLibrary::link(
|
|
|
|
|
BinaryContext &BC, StringRef ToolPath, RuntimeDyld &RTDyld,
|
|
|
|
|
std::function<void(RuntimeDyld &)> OnLoad) {
|
2021-04-08 00:19:26 -07:00
|
|
|
std::string LibPath = getLibPath(ToolPath, opts::RuntimeInstrumentationLib);
|
2020-12-01 16:29:39 -08:00
|
|
|
loadLibrary(LibPath, RTDyld);
|
|
|
|
|
OnLoad(RTDyld);
|
|
|
|
|
RTDyld.finalizeWithMemoryManagerLocking();
|
|
|
|
|
if (RTDyld.hasError()) {
|
|
|
|
|
outs() << "BOLT-ERROR: RTDyld failed: " << RTDyld.getErrorString() << "\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
|
2020-11-17 13:57:29 -08:00
|
|
|
if (BC.isMachO())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
RuntimeFiniAddress = RTDyld.getSymbol("__bolt_instr_fini").getAddress();
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
if (!RuntimeFiniAddress) {
|
|
|
|
|
errs() << "BOLT-ERROR: instrumentation library does not define "
|
|
|
|
|
"__bolt_instr_fini: "
|
|
|
|
|
<< LibPath << "\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2020-12-01 16:29:39 -08:00
|
|
|
RuntimeStartAddress = RTDyld.getSymbol("__bolt_instr_start").getAddress();
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
if (!RuntimeStartAddress) {
|
|
|
|
|
errs() << "BOLT-ERROR: instrumentation library does not define "
|
|
|
|
|
"__bolt_instr_start: "
|
|
|
|
|
<< LibPath << "\n";
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
outs() << "BOLT-INFO: output linked against instrumentation runtime "
|
|
|
|
|
"library, lib entry point is 0x"
|
|
|
|
|
<< Twine::utohexstr(RuntimeFiniAddress) << "\n";
|
2020-12-01 16:29:39 -08:00
|
|
|
outs() << "BOLT-INFO: clear procedure is 0x"
|
|
|
|
|
<< Twine::utohexstr(
|
|
|
|
|
RTDyld.getSymbol("__bolt_instr_clear_counters").getAddress())
|
|
|
|
|
<< "\n";
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
|
|
|
|
|
emitTablesAsELFNote(BC);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-28 12:44:14 -08:00
|
|
|
std::string InstrumentationRuntimeLibrary::buildTables(BinaryContext &BC) {
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
std::string TablesStr;
|
|
|
|
|
raw_string_ostream OS(TablesStr);
|
|
|
|
|
|
2021-01-28 12:44:14 -08:00
|
|
|
// This is sync'ed with runtime/instr.cpp:readDescriptions()
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
auto getOutputAddress = [](const BinaryFunction &Func,
|
|
|
|
|
uint64_t Offset) -> uint64_t {
|
|
|
|
|
return Offset == 0
|
|
|
|
|
? Func.getOutputAddress()
|
|
|
|
|
: Func.translateInputToOutputAddress(Func.getAddress() + Offset);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Indirect targets need to be sorted for fast lookup during runtime
|
2022-06-23 22:15:47 -07:00
|
|
|
llvm::sort(Summary->IndCallTargetDescriptions,
|
|
|
|
|
[&](const IndCallTargetDescription &A,
|
|
|
|
|
const IndCallTargetDescription &B) {
|
|
|
|
|
return getOutputAddress(*A.Target, A.ToLoc.Offset) <
|
|
|
|
|
getOutputAddress(*B.Target, B.ToLoc.Offset);
|
|
|
|
|
});
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
|
|
|
|
|
// Start of the vector with descriptions (one CounterDescription for each
|
|
|
|
|
// counter), vector size is Counters.size() CounterDescription-sized elmts
|
2021-04-08 00:19:26 -07:00
|
|
|
const size_t IDSize =
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
Summary->IndCallDescriptions.size() * sizeof(IndCallDescription);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&IDSize), 4);
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const IndCallDescription &Desc : Summary->IndCallDescriptions) {
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&Desc.FromLoc.FuncString), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Desc.FromLoc.Offset), 4);
|
|
|
|
|
}
|
2021-01-28 12:44:14 -08:00
|
|
|
|
2021-04-08 00:19:26 -07:00
|
|
|
const size_t ITDSize = Summary->IndCallTargetDescriptions.size() *
|
|
|
|
|
sizeof(IndCallTargetDescription);
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&ITDSize), 4);
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const IndCallTargetDescription &Desc :
|
|
|
|
|
Summary->IndCallTargetDescriptions) {
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&Desc.ToLoc.FuncString), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Desc.ToLoc.Offset), 4);
|
|
|
|
|
uint64_t TargetFuncAddress =
|
|
|
|
|
getOutputAddress(*Desc.Target, Desc.ToLoc.Offset);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&TargetFuncAddress), 8);
|
|
|
|
|
}
|
2021-01-28 12:44:14 -08:00
|
|
|
|
2021-04-08 00:19:26 -07:00
|
|
|
uint32_t FuncDescSize = Summary->getFDSize();
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&FuncDescSize), 4);
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const FunctionDescription &Desc : Summary->FunctionDescriptions) {
|
|
|
|
|
const size_t LeafNum = Desc.LeafNodes.size();
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&LeafNum), 4);
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const InstrumentedNode &LeafNode : Desc.LeafNodes) {
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&LeafNode.Node), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&LeafNode.Counter), 4);
|
|
|
|
|
}
|
2021-04-08 00:19:26 -07:00
|
|
|
const size_t EdgesNum = Desc.Edges.size();
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&EdgesNum), 4);
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const EdgeDescription &Edge : Desc.Edges) {
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&Edge.FromLoc.FuncString), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Edge.FromLoc.Offset), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Edge.FromNode), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Edge.ToLoc.FuncString), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Edge.ToLoc.Offset), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Edge.ToNode), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Edge.Counter), 4);
|
|
|
|
|
}
|
2021-04-08 00:19:26 -07:00
|
|
|
const size_t CallsNum = Desc.Calls.size();
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&CallsNum), 4);
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const CallDescription &Call : Desc.Calls) {
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&Call.FromLoc.FuncString), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Call.FromLoc.Offset), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Call.FromNode), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Call.ToLoc.FuncString), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Call.ToLoc.Offset), 4);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&Call.Counter), 4);
|
|
|
|
|
uint64_t TargetFuncAddress =
|
|
|
|
|
getOutputAddress(*Call.Target, Call.ToLoc.Offset);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&TargetFuncAddress), 8);
|
|
|
|
|
}
|
2021-04-08 00:19:26 -07:00
|
|
|
const size_t EntryNum = Desc.EntryNodes.size();
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&EntryNum), 4);
|
2021-04-08 00:19:26 -07:00
|
|
|
for (const EntryNode &EntryNode : Desc.EntryNodes) {
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
OS.write(reinterpret_cast<const char *>(&EntryNode.Node), 8);
|
|
|
|
|
uint64_t TargetFuncAddress =
|
|
|
|
|
getOutputAddress(*Desc.Function, EntryNode.Address);
|
|
|
|
|
OS.write(reinterpret_cast<const char *>(&TargetFuncAddress), 8);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Our string table lives immediately after descriptions vector
|
|
|
|
|
OS << Summary->StringTable;
|
|
|
|
|
OS.flush();
|
2021-01-28 12:44:14 -08:00
|
|
|
|
|
|
|
|
return TablesStr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstrumentationRuntimeLibrary::emitTablesAsELFNote(BinaryContext &BC) {
|
|
|
|
|
std::string TablesStr = buildTables(BC);
|
2021-04-08 00:19:26 -07:00
|
|
|
const std::string BoltInfo = BinarySection::encodeELFNote(
|
Refactor runtime library
Summary:
As we are adding more types of runtime libraries, it would be better to move the runtime library out of RewriteInstance so that it could grow separately. This also requires splitting the current implementation of Instrumentation.cpp to two separate pieces, one as normal Pass, one as the runtime library. The Instrumentation Pass would pass over the generated data to the runtime library, which will use to emit binary and perform linking.
This patch does the following:
1. Turn Instrumentation class into an optimization pass. Register the pass in the pass manager instead of in RewriteInstance.
2. Split all the data that are generated by Instrumentation that's needed by runtime library into a separate data structure called InstrumentationSummary. At the creation of Instrumentation pass, we create an instance of such data structure, which will be moved over to the runtime at the end of the pass.
3. Added a runtime library member to BinaryContext. Set the member at the end of Instrumentation pass.
4. In BinaryEmitter, make BinaryContext to also emit runtime library binary.
5. Created a base class RuntimeLibrary, that defines the interface of a runtime library, along with a few common helper functions.
6. Created InstrumentationRuntimeLibrary which inherits from RuntimeLibrary, that does all the work (mostly copied over) for emit and linking.
7. Added a new directory called RuntimeLibs, and put all the runtime library related files into it.
(cherry picked from FBD21694762)
2020-05-21 14:28:47 -07:00
|
|
|
"BOLT", TablesStr, BinarySection::NT_BOLT_INSTRUMENTATION_TABLES);
|
|
|
|
|
BC.registerOrUpdateNoteSection(".bolt.instr.tables", copyByteArray(BoltInfo),
|
|
|
|
|
BoltInfo.size(),
|
|
|
|
|
/*Alignment=*/1,
|
|
|
|
|
/*IsReadOnly=*/true, ELF::SHT_NOTE);
|
|
|
|
|
}
|