Skip to content

Commit f0b45fb

Browse files
committed
[BOLT][Instrumentation][NFC] define and use mmap flags
Reviewed By: rafauler, Amir Differential Revision: https://reviews.llvm.org/D154056
1 parent 4b47c6e commit f0b45fb

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

bolt/runtime/common.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ typedef int int32_t;
8282
"pop %%rbx\n" \
8383
"pop %%rax\n"
8484

85+
#define PROT_READ 0x1 /* Page can be read. */
86+
#define PROT_WRITE 0x2 /* Page can be written. */
87+
#define PROT_EXEC 0x4 /* Page can be executed. */
88+
#define PROT_NONE 0x0 /* Page can not be accessed. */
89+
#define PROT_GROWSDOWN \
90+
0x01000000 /* Extend change to start of \
91+
growsdown vma (mprotect only). */
92+
#define PROT_GROWSUP \
93+
0x02000000 /* Extend change to start of \
94+
growsup vma (mprotect only). */
95+
96+
/* Sharing types (must choose one and only one of these). */
97+
#define MAP_SHARED 0x01 /* Share changes. */
98+
#define MAP_PRIVATE 0x02 /* Changes are private. */
99+
#define MAP_FIXED 0x10 /* Interpret addr exactly. */
100+
101+
#if defined(__APPLE__)
102+
#define MAP_ANONYMOUS 0x1000
103+
#else
104+
#define MAP_ANONYMOUS 0x20
105+
#endif
106+
107+
#define MAP_FAILED ((void *)-1)
108+
85109
// Functions that are required by freestanding environment. Compiler may
86110
// generate calls to these implicitly.
87111
extern "C" {

bolt/runtime/instr.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,9 @@ class BumpPtrAllocator {
134134
Lock L(M);
135135

136136
if (StackBase == nullptr) {
137-
#if defined(__APPLE__)
138-
int MAP_PRIVATE_MAP_ANONYMOUS = 0x1002;
139-
#else
140-
int MAP_PRIVATE_MAP_ANONYMOUS = 0x22;
141-
#endif
142137
StackBase = reinterpret_cast<uint8_t *>(
143-
__mmap(0, MaxSize, 0x3 /* PROT_READ | PROT_WRITE*/,
144-
Shared ? 0x21 /*MAP_SHARED | MAP_ANONYMOUS*/
145-
: MAP_PRIVATE_MAP_ANONYMOUS /* MAP_PRIVATE | MAP_ANONYMOUS*/,
146-
-1, 0));
138+
__mmap(0, MaxSize, PROT_READ | PROT_WRITE,
139+
(Shared ? MAP_SHARED : MAP_PRIVATE) | MAP_ANONYMOUS, -1, 0));
147140
StackSize = 0;
148141
}
149142

@@ -669,7 +662,7 @@ ProfileWriterContext readDescriptions() {
669662
// mmap our binary to memory
670663
uint64_t Size = __lseek(FD, 0, 2 /*SEEK_END*/);
671664
uint8_t *BinContents = reinterpret_cast<uint8_t *>(
672-
__mmap(0, Size, 0x1 /* PROT_READ*/, 0x2 /* MAP_PRIVATE*/, FD, 0));
665+
__mmap(0, Size, PROT_READ, MAP_PRIVATE, FD, 0));
673666
Result.MMapPtr = BinContents;
674667
Result.MMapSize = Size;
675668
Elf64_Ehdr *Hdr = reinterpret_cast<Elf64_Ehdr *>(BinContents);
@@ -1555,10 +1548,9 @@ extern "C" void __attribute((force_align_arg_pointer)) __bolt_instr_setup() {
15551548
assert (CountersEnd > CountersStart, "no counters");
15561549
// Maps our counters to be shared instead of private, so we keep counting for
15571550
// forked processes
1558-
__mmap(CountersStart, CountersEnd - CountersStart,
1559-
0x3 /*PROT_READ|PROT_WRITE*/,
1560-
0x31 /*MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED*/, -1, 0);
1561-
1551+
void *Ret =
1552+
__mmap(CountersStart, CountersEnd - CountersStart, PROT_READ | PROT_WRITE,
1553+
MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, -1, 0);
15621554
__bolt_ind_call_counter_func_pointer = __bolt_instr_indirect_call;
15631555
__bolt_ind_tailcall_counter_func_pointer = __bolt_instr_indirect_tailcall;
15641556
// Conservatively reserve 100MiB shared pages

0 commit comments

Comments
 (0)