Skip to content

Commit 11f675c

Browse files
author
Gabriel Schulhof
committed
src: use _init as the start of large pages
The assumption that the .text section starts at the mapping following the one that contains `__executable_start` is not valid on 64-bit Ubuntu 18.04 where the whole code resides in one mapping. OTOH, The symbol `_init` is usually located at the beginning of the .text section and it does not need the assumption that the next mapping has the .text section. Thus, we use the symbol, if available, to perform the mapping on Ubuntu 18.04. We also rename the section into which we place the remapping code to `lpstub`. This causes the linker to produce symbols `__start_lpstub` and `__stop_lpstub`, the latter of which we do not use. Still, `__start_lpstub` helps us find the end of the .text section because on Ubuntu 18.04 this section is inserted before the end of the sole mapping, so we use `__start_lpstub` as the end instead of the end of the mapping.
1 parent 0dff851 commit 11f675c

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/large_pages/node_large_page.cc

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@
7070
// If successful copy the code there and unmap the original region.
7171

7272
#if defined(__linux__)
73+
#include <dlfcn.h> // For retrieving _init at runtime
7374
extern "C" {
7475
extern char __executable_start;
76+
extern char __start_lpstub;
7577
} // extern "C"
7678
#endif // defined(__linux__)
7779

@@ -106,6 +108,20 @@ inline uintptr_t hugepage_align_down(uintptr_t addr) {
106108
return ((addr) & ~((hps) - 1));
107109
}
108110

111+
#if defined(__linux__)
112+
inline uintptr_t RetrieveInitOffset() {
113+
uintptr_t init_offset = 0;
114+
void* dlhandle = dlopen(nullptr, RTLD_NOW | RTLD_NOLOAD);
115+
if (dlhandle != nullptr) {
116+
init_offset = reinterpret_cast<uintptr_t>(dlsym(dlhandle, "_init"));
117+
if (dlclose(dlhandle) != 0) {
118+
PrintWarning("Failed to dlclose() self after retrieving _init");
119+
}
120+
}
121+
return init_offset;
122+
}
123+
#endif
124+
109125
// The format of the maps file is the following
110126
// address perms offset dev inode pathname
111127
// 00400000-00452000 r-xp 00000000 08:02 173521 /usr/bin/dbus-daemon
@@ -121,6 +137,7 @@ struct text_region FindNodeTextRegion() {
121137
std::string dev;
122138
char dash;
123139
uintptr_t start, end, offset, inode;
140+
uintptr_t init_offset = RetrieveInitOffset();
124141

125142
ifs.open("/proc/self/maps");
126143
if (!ifs) {
@@ -147,15 +164,31 @@ struct text_region FindNodeTextRegion() {
147164
if (start != reinterpret_cast<uintptr_t>(&__executable_start))
148165
continue;
149166

150-
// The next line is our .text section.
151-
if (!std::getline(ifs, map_line))
152-
break;
167+
// On Ubuntu 18.04 the binary gets loaded into a single mapping. So, before
168+
// we make the assumption that the next mapping contains the .text section
169+
// we check if this mapping contains the symbol `_init` -- which, on
170+
// Ubuntu 18.04 it does. If so, we calculate `start` and `end` from this
171+
// mapping and take into account that we must exclude the section `lpstub`
172+
// from the returned range, because `lpstub` contains the code responsible
173+
// for re-mapping the .text section, and we don't want it re-mapping itself
174+
// as it's doing that, because that will cause the process to crash.
175+
if (init_offset != 0 && init_offset >= start && init_offset < end) {
176+
uintptr_t lpstub_start = reinterpret_cast<uintptr_t>(&__start_lpstub);
177+
if (lpstub_start > start && lpstub_start <= end) {
178+
end = lpstub_start;
179+
}
180+
start = init_offset;
181+
} else {
182+
// The next line is our .text section.
183+
if (!std::getline(ifs, map_line))
184+
break;
153185

154-
iss = std::istringstream(map_line);
155-
iss >> std::hex >> start;
156-
iss >> dash;
157-
iss >> std::hex >> end;
158-
iss >> permission;
186+
iss = std::istringstream(map_line);
187+
iss >> std::hex >> start;
188+
iss >> dash;
189+
iss >> std::hex >> end;
190+
iss >> permission;
191+
}
159192

160193
if (permission != "r-xp")
161194
break;
@@ -318,7 +351,7 @@ static bool IsSuperPagesEnabled() {
318351
// d. If successful copy the code there and unmap the original region
319352
int
320353
#if !defined(__APPLE__)
321-
__attribute__((__section__(".lpstub")))
354+
__attribute__((__section__("lpstub")))
322355
#else
323356
__attribute__((__section__("__TEXT,__lpstub")))
324357
#endif

0 commit comments

Comments
 (0)