From 250198220d8aa3a619b0b40b0d7babfb936d2da7 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 29 Dec 2019 15:43:19 +0000 Subject: [PATCH] deps: V8: backport a4545db Original commit message: FreeBSD update of process library mapping listing. Not best to rely on /proc presence basically when the linux compatibily layer is enabled so going through more programmatically. Change-Id: Ida4973f9da6dec6e9caa6e419f3612ec5ef95048 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1710664 Reviewed-by: Ulan Degenbaev Commit-Queue: Ulan Degenbaev Cr-Commit-Position: refs/heads/master@{#65442} PR-URL: https://github.com/nodejs/node/pull/31127 Refs: https://github.com/v8/v8/commit/a4545db74e5204125033ccec7294d1b0a8d96389 Reviewed-By: Jiawen Geng Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig --- common.gypi | 2 +- deps/v8/src/base/platform/platform-freebsd.cc | 76 ++++++++++--------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/common.gypi b/common.gypi index 89a8c23761462d..36047fcb91d768 100644 --- a/common.gypi +++ b/common.gypi @@ -38,7 +38,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.26', + 'v8_embedder_string': '-node.27', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/src/base/platform/platform-freebsd.cc b/deps/v8/src/base/platform/platform-freebsd.cc index d40452f3d48417..8d63dd2f84df34 100644 --- a/deps/v8/src/base/platform/platform-freebsd.cc +++ b/deps/v8/src/base/platform/platform-freebsd.cc @@ -13,10 +13,12 @@ #include #include #include +#include #include // open #include // mmap & munmap #include // open +#include #include // getpagesize // If you don't have execinfo.h then you need devel/libexecinfo from ports. #include @@ -46,41 +48,47 @@ static unsigned StringToLong(char* buffer) { std::vector OS::GetSharedLibraryAddresses() { std::vector result; - static const int MAP_LENGTH = 1024; - int fd = open("/proc/self/maps", O_RDONLY); - if (fd < 0) return result; - while (true) { - char addr_buffer[11]; - addr_buffer[0] = '0'; - addr_buffer[1] = 'x'; - addr_buffer[10] = 0; - ssize_t bytes_read = read(fd, addr_buffer + 2, 8); - if (bytes_read < 8) break; - unsigned start = StringToLong(addr_buffer); - bytes_read = read(fd, addr_buffer + 2, 1); - if (bytes_read < 1) break; - if (addr_buffer[2] != '-') break; - bytes_read = read(fd, addr_buffer + 2, 8); - if (bytes_read < 8) break; - unsigned end = StringToLong(addr_buffer); - char buffer[MAP_LENGTH]; - bytes_read = -1; - do { - bytes_read++; - if (bytes_read >= MAP_LENGTH - 1) break; - bytes_read = read(fd, buffer + bytes_read, 1); - if (bytes_read < 1) break; - } while (buffer[bytes_read] != '\n'); - buffer[bytes_read] = 0; - // Ignore mappings that are not executable. - if (buffer[3] != 'x') continue; - char* start_of_path = index(buffer, '/'); - // There may be no filename in this line. Skip to next. - if (start_of_path == nullptr) continue; - buffer[bytes_read] = 0; - result.push_back(SharedLibraryAddress(start_of_path, start, end)); + int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid()}; + size_t miblen = sizeof(mib) / sizeof(mib[0]); + size_t buffer_size; + if (sysctl(mib, miblen, nullptr, &buffer_size, nullptr, 0) == 0) { + // Overallocate the buffer by 1/3 to account for concurrent + // kinfo_vmentry change. 1/3 is an arbitrary constant that + // works in practice. + buffer_size = buffer_size * 4 / 3; + std::vector buffer(buffer_size); + int ret = sysctl(mib, miblen, buffer.data(), &buffer_size, nullptr, 0); + + if (ret == 0 || (ret == -1 && errno == ENOMEM)) { + char* start = buffer.data(); + char* end = start + buffer_size; + + while (start < end) { + struct kinfo_vmentry* map = + reinterpret_cast(start); + const size_t ssize = map->kve_structsize; + char* path = map->kve_path; + + CHECK_NE(0, ssize); + + if ((map->kve_protection & KVME_PROT_READ) != 0 && + (map->kve_protection & KVME_PROT_EXEC) != 0 && path[0] != '\0') { + char* sep = strrchr(path, '/'); + std::string lib_name; + if (sep != nullptr) { + lib_name = std::string(++sep); + } else { + lib_name = std::string(path); + } + result.push_back(SharedLibraryAddress( + lib_name, reinterpret_cast(map->kve_start), + reinterpret_cast(map->kve_end))); + } + + start += ssize; + } + } } - close(fd); return result; }