Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: V8: cherry-pick a4545db74e52 #31127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Expand Up @@ -39,7 +39,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.25',
'v8_embedder_string': '-node.26',

##### V8 defaults for Node.js #####

Expand Down
76 changes: 42 additions & 34 deletions deps/v8/src/base/platform/platform-freebsd.cc
Expand Up @@ -13,10 +13,12 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ucontext.h>
#include <sys/user.h>

#include <sys/fcntl.h> // open
#include <sys/mman.h> // mmap & munmap
#include <sys/stat.h> // open
#include <sys/sysctl.h>
#include <unistd.h> // getpagesize
// If you don't have execinfo.h then you need devel/libexecinfo from ports.
#include <errno.h>
Expand Down Expand Up @@ -46,41 +48,47 @@ static unsigned StringToLong(char* buffer) {

std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
std::vector<SharedLibraryAddress> 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<char> 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<struct kinfo_vmentry*>(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<uintptr_t>(map->kve_start),
reinterpret_cast<uintptr_t>(map->kve_end)));
}

start += ssize;
}
}
}
close(fd);
return result;
}

Expand Down