src/os/linux/vm/os_linux.cpp
Print this page
rev 2100 : 7017193 Small memory leak in get_stack_bounds // os::create_stack_guard_pages
*** 2646,2694 ****
//
// We only need this for stacks that are growable: at the time of
// writing thread stacks don't use growable mappings (i.e. those
// creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
// only applies to the main thread.
- static bool
- get_stack_bounds(uintptr_t *bottom, uintptr_t *top)
- {
- FILE *f = fopen("/proc/self/maps", "r");
- if (f == NULL)
- return false;
! while (!feof(f)) {
! size_t dummy;
! char *str = NULL;
! ssize_t len = getline(&str, &dummy, f);
! if (len == -1) {
! fclose(f);
! return false;
! }
! if (len > 0 && str[len-1] == '\n') {
! str[len-1] = 0;
! len--;
}
! static const char *stack_str = "[stack]";
! if (len > (ssize_t)strlen(stack_str)
! && (strcmp(str + len - strlen(stack_str), stack_str) == 0)) {
! if (sscanf(str, "%" SCNxPTR "-%" SCNxPTR, bottom, top) == 2) {
! uintptr_t sp = (uintptr_t)__builtin_frame_address(0);
if (sp >= *bottom && sp <= *top) {
! free(str);
! fclose(f);
return true;
}
}
}
! free(str);
! }
! fclose(f);
return false;
}
// If the (growable) stack mapping already extends beyond the point
// where we're going to put our guard pages, truncate the mapping at
// that point by munmap()ping it. This ensures that when we later
// munmap() the guard pages we don't leave a hole in the stack
// mapping. This only affects the main/initial thread, but guard
--- 2646,2686 ----
//
// We only need this for stacks that are growable: at the time of
// writing thread stacks don't use growable mappings (i.e. those
// creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
// only applies to the main thread.
! static
! bool get_stack_bounds(uintptr_t *bottom, uintptr_t *top) {
!
! char buf[128], *np = buf;
! int fd;
! if ( (fd = ::open("/proc/self/maps", O_RDONLY)) < 0 ){
! return false;
}
! // Address part of /proc/maps couldn't be more than 128 bytes
! while ( os::get_line_chars(fd, buf, 128) > 0) {
! if (::strstr(buf, "[stack]") != 0) {
! // Extract addresses
! *bottom = ::strtoul(buf, &np, 16);
! assert(*np == '-', "Should be a dash");
! *top = ::strtoul(np + 1, 0, 16);
! uintptr_t sp = (uintptr_t) __builtin_frame_address(0);
if (sp >= *bottom && sp <= *top) {
! ::close(fd);
return true;
}
}
}
!
! ::close(fd);
return false;
}
+
// If the (growable) stack mapping already extends beyond the point
// where we're going to put our guard pages, truncate the mapping at
// that point by munmap()ping it. This ensures that when we later
// munmap() the guard pages we don't leave a hole in the stack
// mapping. This only affects the main/initial thread, but guard