Skip to content

Commit e088334

Browse files
authored
[BOLT][NFC] Add const qualifier to certain pointers to read-only objects (#148543)
1 parent 16534d1 commit e088334

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

bolt/runtime/instr.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,13 @@ struct FunctionDescription {
568568
/// should be straightforward as most data is POD or an array of POD elements.
569569
/// This metadata is used to reconstruct function CFGs.
570570
struct ProfileWriterContext {
571-
IndCallDescription *IndCallDescriptions;
572-
IndCallTargetDescription *IndCallTargets;
573-
uint8_t *FuncDescriptions;
574-
char *Strings; // String table with function names used in this binary
571+
const IndCallDescription *IndCallDescriptions;
572+
const IndCallTargetDescription *IndCallTargets;
573+
const uint8_t *FuncDescriptions;
574+
const char *Strings; // String table with function names used in this binary
575575
int FileDesc; // File descriptor for the file on disk backing this
576576
// information in memory via mmap
577-
void *MMapPtr; // The mmap ptr
577+
const void *MMapPtr; // The mmap ptr
578578
int MMapSize; // The mmap size
579579

580580
/// Hash table storing all possible call destinations to detect untracked
@@ -721,7 +721,7 @@ static char *getBinaryPath() {
721721

722722
ProfileWriterContext readDescriptions() {
723723
ProfileWriterContext Result;
724-
char *BinPath = getBinaryPath();
724+
const char *BinPath = getBinaryPath();
725725
assert(BinPath && BinPath[0] != '\0', "failed to find binary path");
726726

727727
uint64_t FD = __open(BinPath, O_RDONLY,
@@ -732,23 +732,24 @@ ProfileWriterContext readDescriptions() {
732732

733733
// mmap our binary to memory
734734
uint64_t Size = __lseek(FD, 0, SEEK_END);
735-
uint8_t *BinContents = reinterpret_cast<uint8_t *>(
735+
const uint8_t *BinContents = reinterpret_cast<uint8_t *>(
736736
__mmap(0, Size, PROT_READ, MAP_PRIVATE, FD, 0));
737737
assert(BinContents != MAP_FAILED, "readDescriptions: Failed to mmap self!");
738738
Result.MMapPtr = BinContents;
739739
Result.MMapSize = Size;
740-
Elf64_Ehdr *Hdr = reinterpret_cast<Elf64_Ehdr *>(BinContents);
741-
Elf64_Shdr *Shdr = reinterpret_cast<Elf64_Shdr *>(BinContents + Hdr->e_shoff);
742-
Elf64_Shdr *StringTblHeader = reinterpret_cast<Elf64_Shdr *>(
740+
const Elf64_Ehdr *Hdr = reinterpret_cast<const Elf64_Ehdr *>(BinContents);
741+
const Elf64_Shdr *Shdr =
742+
reinterpret_cast<const Elf64_Shdr *>(BinContents + Hdr->e_shoff);
743+
const Elf64_Shdr *StringTblHeader = reinterpret_cast<const Elf64_Shdr *>(
743744
BinContents + Hdr->e_shoff + Hdr->e_shstrndx * Hdr->e_shentsize);
744745

745746
// Find .bolt.instr.tables with the data we need and set pointers to it
746747
for (int I = 0; I < Hdr->e_shnum; ++I) {
747-
char *SecName = reinterpret_cast<char *>(
748+
const char *SecName = reinterpret_cast<const char *>(
748749
BinContents + StringTblHeader->sh_offset + Shdr->sh_name);
749750
if (compareStr(SecName, ".bolt.instr.tables", 64) != 0) {
750-
Shdr = reinterpret_cast<Elf64_Shdr *>(BinContents + Hdr->e_shoff +
751-
(I + 1) * Hdr->e_shentsize);
751+
Shdr = reinterpret_cast<const Elf64_Shdr *>(BinContents + Hdr->e_shoff +
752+
(I + 1) * Hdr->e_shentsize);
752753
continue;
753754
}
754755
// Actual contents of the ELF note start after offset 20 decimal:
@@ -758,19 +759,19 @@ ProfileWriterContext readDescriptions() {
758759
// Offset 12: Producer name (BOLT\0) (5 bytes + align to 4-byte boundary)
759760
// Offset 20: Contents
760761
uint32_t IndCallDescSize =
761-
*reinterpret_cast<uint32_t *>(BinContents + Shdr->sh_offset + 20);
762-
uint32_t IndCallTargetDescSize = *reinterpret_cast<uint32_t *>(
762+
*reinterpret_cast<const uint32_t *>(BinContents + Shdr->sh_offset + 20);
763+
uint32_t IndCallTargetDescSize = *reinterpret_cast<const uint32_t *>(
763764
BinContents + Shdr->sh_offset + 24 + IndCallDescSize);
764-
uint32_t FuncDescSize =
765-
*reinterpret_cast<uint32_t *>(BinContents + Shdr->sh_offset + 28 +
766-
IndCallDescSize + IndCallTargetDescSize);
767-
Result.IndCallDescriptions = reinterpret_cast<IndCallDescription *>(
765+
uint32_t FuncDescSize = *reinterpret_cast<const uint32_t *>(
766+
BinContents + Shdr->sh_offset + 28 + IndCallDescSize +
767+
IndCallTargetDescSize);
768+
Result.IndCallDescriptions = reinterpret_cast<const IndCallDescription *>(
768769
BinContents + Shdr->sh_offset + 24);
769-
Result.IndCallTargets = reinterpret_cast<IndCallTargetDescription *>(
770+
Result.IndCallTargets = reinterpret_cast<const IndCallTargetDescription *>(
770771
BinContents + Shdr->sh_offset + 28 + IndCallDescSize);
771772
Result.FuncDescriptions = BinContents + Shdr->sh_offset + 32 +
772773
IndCallDescSize + IndCallTargetDescSize;
773-
Result.Strings = reinterpret_cast<char *>(
774+
Result.Strings = reinterpret_cast<const char *>(
774775
BinContents + Shdr->sh_offset + 32 + IndCallDescSize +
775776
IndCallTargetDescSize + FuncDescSize);
776777
return Result;
@@ -814,13 +815,14 @@ void printStats(const ProfileWriterContext &Ctx) {
814815
strCopy(StatPtr,
815816
"\nBOLT INSTRUMENTATION RUNTIME STATISTICS\n\nIndCallDescSize: ");
816817
StatPtr = intToStr(StatPtr,
817-
Ctx.FuncDescriptions -
818-
reinterpret_cast<uint8_t *>(Ctx.IndCallDescriptions),
818+
Ctx.FuncDescriptions - reinterpret_cast<const uint8_t *>(
819+
Ctx.IndCallDescriptions),
819820
10);
820821
StatPtr = strCopy(StatPtr, "\nFuncDescSize: ");
821-
StatPtr = intToStr(
822-
StatPtr,
823-
reinterpret_cast<uint8_t *>(Ctx.Strings) - Ctx.FuncDescriptions, 10);
822+
StatPtr = intToStr(StatPtr,
823+
reinterpret_cast<const uint8_t *>(Ctx.Strings) -
824+
Ctx.FuncDescriptions,
825+
10);
824826
StatPtr = strCopy(StatPtr, "\n__bolt_instr_num_ind_calls: ");
825827
StatPtr = intToStr(StatPtr, __bolt_instr_num_ind_calls, 10);
826828
StatPtr = strCopy(StatPtr, "\n__bolt_instr_num_funcs: ");
@@ -1549,7 +1551,7 @@ __bolt_instr_data_dump(int FD) {
15491551
Ctx.CallFlowTable->forEachElement(visitCallFlowEntry, FD, &Ctx);
15501552

15511553
__fsync(FD);
1552-
__munmap(Ctx.MMapPtr, Ctx.MMapSize);
1554+
__munmap((void *)Ctx.MMapPtr, Ctx.MMapSize);
15531555
__close(Ctx.FileDesc);
15541556
HashAlloc.destroy();
15551557
GlobalWriteProfileMutex->release();
@@ -1756,7 +1758,7 @@ extern "C" __attribute((naked)) void __bolt_instr_start()
17561758
"jal x1, __bolt_instr_setup\n"
17571759
RESTORE_ALL
17581760
"setup_symbol:\n"
1759-
"auipc x5, %%pcrel_hi(__bolt_start_trampoline)\n"
1761+
"auipc x5, %%pcrel_hi(__bolt_start_trampoline)\n"
17601762
"addi x5, x5, %%pcrel_lo(setup_symbol)\n"
17611763
"jr x5\n"
17621764
:::);
@@ -1788,8 +1790,8 @@ extern "C" void __bolt_instr_fini() {
17881790
__asm__ __volatile__(
17891791
SAVE_ALL
17901792
"fini_symbol:\n"
1791-
"auipc x5, %%pcrel_hi(__bolt_fini_trampoline)\n"
1792-
"addi x5, x5, %%pcrel_lo(fini_symbol)\n"
1793+
"auipc x5, %%pcrel_hi(__bolt_fini_trampoline)\n"
1794+
"addi x5, x5, %%pcrel_lo(fini_symbol)\n"
17931795
"jalr x1, 0(x5)\n"
17941796
RESTORE_ALL
17951797
:::);

0 commit comments

Comments
 (0)