aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-08-27 17:30:34 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-08-27 17:30:34 -0700
commit8b3afa5831b7ac5fcabb47138c67d60e86247cd3 (patch)
tree8f847b74dd48fbaa7d07bde6bcb4b165f8f89d18
parent574ecfb9c438423f236ae85ca9a18e36a4d9008b (diff)
downloadbun-8b3afa5831b7ac5fcabb47138c67d60e86247cd3.tar.gz
bun-8b3afa5831b7ac5fcabb47138c67d60e86247cd3.tar.zst
bun-8b3afa5831b7ac5fcabb47138c67d60e86247cd3.zip
Fix crash in `os` module on macOS
-rw-r--r--src/bun.js/bindings/node_os/cpuinfo.cpp169
-rw-r--r--src/bun.js/bindings/node_os/interface_addresses.cpp13
2 files changed, 108 insertions, 74 deletions
diff --git a/src/bun.js/bindings/node_os/cpuinfo.cpp b/src/bun.js/bindings/node_os/cpuinfo.cpp
index f0f8b47eb..31c8f356a 100644
--- a/src/bun.js/bindings/node_os/cpuinfo.cpp
+++ b/src/bun.js/bindings/node_os/cpuinfo.cpp
@@ -1,3 +1,4 @@
+#include "mimalloc.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
@@ -14,66 +15,80 @@
#include "cpuinfo.h"
+#define free mi_free
+#define malloc mi_malloc
+#define realloc mi_realloc
+#define strdup mi_strdup
+
#ifdef __linux__
-extern "C" CpuInfo *getCpuInfo()
+extern "C" CpuInfo* getCpuInfo()
{
- CpuInfo *cores = (CpuInfo*) malloc(sizeof(CpuInfo));
- FILE *file = fopen("/proc/cpuinfo", "r");
- if (file == NULL) return NULL;
+ CpuInfo* cores = (CpuInfo*)malloc(sizeof(CpuInfo));
+ FILE* file = fopen("/proc/cpuinfo", "r");
+ if (file == NULL)
+ return NULL;
char buff[2048];
int coresIndex = -1;
+ memset(cores, 0, sizeof(CpuInfo));
+
while (fgets(buff, 2048, file)) {
- if (strlen(buff) == 0) continue;
+ if (strlen(buff) == 0)
+ continue;
short columnSplit = 0;
- for (int i = 0; i < (int) strlen(buff); i++) {
+ for (int i = 0; i < (int)strlen(buff); i++) {
if (buff[i] == ':') {
columnSplit = i;
break;
}
}
- char *columnName = strndup(buff, columnSplit);
- if (columnName == NULL) return NULL;
-
+ char* columnName = strndup(buff, columnSplit);
+ if (columnName == NULL)
+ return NULL;
+
if (!strncmp("processor", columnName, strlen("processor"))) {
coresIndex++;
- if (coresIndex > 0) {
- cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
- if (cores == NULL) return NULL;
+ if (coresIndex > 0) {
+ cores = (CpuInfo*)realloc(cores, (coresIndex + 1) * sizeof(CpuInfo));
+ if (cores == NULL)
+ return NULL;
}
#ifdef __PPC__
- } else if(!strncmp("cpu", columnName, 3)) {
+ } else if (!strncmp("cpu", columnName, 3)) {
#else
- } else if(!strncmp("model name", columnName, strlen("model name"))) {
+ } else if (!strncmp("model name", columnName, strlen("model name"))) {
#endif
- cores[coresIndex].manufacturer = strndup((buff+columnSplit+2), strlen(buff) - 3 - columnSplit);
- if (cores[coresIndex].manufacturer == NULL) return NULL;
+ cores[coresIndex].manufacturer = strndup((buff + columnSplit + 2), strlen(buff) - 3 - columnSplit);
+ if (cores[coresIndex].manufacturer == NULL)
+ return NULL;
#ifdef __PPC__
- } else if(!strncmp("clock", columnName, strlen("clock"))) {
+ } else if (!strncmp("clock", columnName, strlen("clock"))) {
#else
- } else if(!strncmp("cpu MHz", columnName, strlen("cpu MHz"))) {
+ } else if (!strncmp("cpu MHz", columnName, strlen("cpu MHz"))) {
#endif
- char *columnData = strndup((buff+columnSplit+2), strlen(buff) - 3 - columnSplit);
- if (columnData == NULL) return NULL;
+ char* columnData = strndup((buff + columnSplit + 2), strlen(buff) - 3 - columnSplit);
+ if (columnData == NULL)
+ return NULL;
cores[coresIndex].clockSpeed = atof(columnData);
free(columnData);
}
free(columnName);
}
-
+
coresIndex++;
- cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
- if (cores == NULL) return NULL;
- cores[coresIndex] = (CpuInfo) {NULL, 0, 0, 0, 0, 0, 0, 0};
+ cores = (CpuInfo*)realloc(cores, (coresIndex + 1) * sizeof(CpuInfo));
+ if (cores == NULL)
+ return NULL;
+ cores[coresIndex] = (CpuInfo) { NULL, 0, 0, 0, 0, 0, 0, 0 };
fclose(file);
return cores;
}
#elif __APPLE__
-extern "C" CpuInfo *getCpuInfo()
+extern "C" CpuInfo* getCpuInfo()
{
- unsigned int ticks = (unsigned int) sysconf(_SC_CLK_TCK), multiplier = ((uint64_t)1000L / ticks);
+ unsigned int ticks = (unsigned int)sysconf(_SC_CLK_TCK), multiplier = ((uint64_t)1000L / ticks);
char model[512];
unsigned int freq;
@@ -83,36 +98,39 @@ extern "C" CpuInfo *getCpuInfo()
size_t size;
unsigned int i;
- natural_t numcpus;
+ natural_t numcpus = 0;
mach_msg_type_number_t msg_type;
- processor_cpu_load_info_data_t *info;
- CpuInfo *cores;
+ processor_cpu_load_info_data_t* info;
+ CpuInfo* cores;
size = sizeof(model);
- if (sysctlbyname("machdep.cpu.brand_string", model, &size, NULL, 0) &&
- sysctlbyname("hw.model", model, &size, NULL, 0)) {
+ if (sysctlbyname("machdep.cpu.brand_string", model, &size, NULL, 0) && sysctlbyname("hw.model", model, &size, NULL, 0)) {
return NULL;
}
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numcpus,
- (processor_info_array_t*) &info,
- &msg_type) != KERN_SUCCESS) {
+ (processor_info_array_t*)&info,
+ &msg_type)
+ != KERN_SUCCESS) {
return NULL;
}
freq = freq / 1000000; // Hz to MHz
- cores = (CpuInfo*) malloc(sizeof(CpuInfo) * numcpus);
+ cores = (CpuInfo*)malloc(sizeof(CpuInfo) * numcpus);
- if (cores == NULL) return NULL;
+ if (cores == NULL)
+ return NULL;
+ memset(cores, 0, sizeof(CpuInfo) * numcpus);
for (i = 0; i < numcpus; i++) {
- cores[i].manufacturer = (char*) malloc(strlen(model)+1);
- if(cores[i].manufacturer == NULL) return NULL;
- memcpy(cores[i].manufacturer, model, strlen(model));
+ cores[i].manufacturer = (char*)malloc(strlen(model) + 1);
+ if (cores[i].manufacturer == NULL)
+ return NULL;
+ memcpy(cores[i].manufacturer, &model, strlen(model));
cores[i].manufacturer[strlen(model)] = '\0';
cores[i].clockSpeed = freq;
-
+
cores[i].userTime = info[i].cpu_ticks[0] * multiplier;
cores[i].niceTime = info[i].cpu_ticks[3] * multiplier;
cores[i].systemTime = info[i].cpu_ticks[1] * multiplier;
@@ -121,52 +139,56 @@ extern "C" CpuInfo *getCpuInfo()
cores[i].irqTime = 0;
}
cores[numcpus] = (CpuInfo) { NULL, 0, 0, 0, 0, 0, 0, 0 };
- sysctlbyname("machdep.cpu.brand_string", model, &size, NULL, 0);
- memcpy(cores[0].manufacturer, model, strlen(model)); // Manufacturer of the first core dissapears for some reason
return cores;
}
#endif
-extern "C" CpuInfo *getCpuTime()
+extern "C" CpuInfo* getCpuTime()
{
- CpuInfo *cores = (CpuInfo*) malloc(sizeof(CpuInfo));
- FILE *file = fopen("/proc/stat", "r");
- if (file == NULL) return NULL;
+ CpuInfo* cores = (CpuInfo*)malloc(sizeof(CpuInfo));
+ FILE* file = fopen("/proc/stat", "r");
+ if (file == NULL)
+ return NULL;
char buff[2048];
int coresIndex = -1;
int j = 0;
while (fgets(buff, 2048, file)) {
- char *name = strndup(buff, 3);
- if (name == NULL) return NULL;
+ char* name = strndup(buff, 3);
+ if (name == NULL)
+ return NULL;
if (!strncmp("cpu", name, 3) && isdigit(buff[3])) {
coresIndex++;
if (coresIndex > 0) {
- cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
- if (cores == NULL) return NULL;
+ cores = (CpuInfo*)realloc(cores, (coresIndex + 1) * sizeof(CpuInfo));
+ if (cores == NULL)
+ return NULL;
}
int space;
- for (int i = 0; i < (int) strlen(buff); i++) {
+ for (int i = 0; i < (int)strlen(buff); i++) {
if (buff[i] == ' ') {
space = i;
break;
}
}
- char *cpuDataStart = strndup((buff+space+1), strlen(buff));
- if (cpuDataStart == NULL) return NULL;
- char *cpuData = cpuDataStart;
+ char* cpuDataStart = strndup((buff + space + 1), strlen(buff));
+ if (cpuDataStart == NULL)
+ return NULL;
+ char* cpuData = cpuDataStart;
// Time to be smart, What I am about to do is dangerous.
- char *temp = (char*) &cores[coresIndex];
+ char* temp = (char*)&cores[coresIndex];
size_t start = offsetof(CpuInfo, userTime); // getting offset from `userTime` member.
temp = temp + start;
j = 0;
for (int i = 0; i < 6; i++, j++) {
- cpuData = (cpuData+j); // offseting string.
- for (j = 0; cpuData[j] != ' '; j++);
- char *parseStr = strndup(cpuData, j);
- if (parseStr == NULL) return NULL;
- *(int*) temp = atoi(parseStr);
+ cpuData = (cpuData + j); // offseting string.
+ for (j = 0; cpuData[j] != ' '; j++)
+ ;
+ char* parseStr = strndup(cpuData, j);
+ if (parseStr == NULL)
+ return NULL;
+ *(int*)temp = atoi(parseStr);
free(parseStr);
temp = temp + sizeof(int); // switching to next int member.
}
@@ -175,25 +197,29 @@ extern "C" CpuInfo *getCpuTime()
free(name);
}
coresIndex++;
- cores = (CpuInfo*) realloc(cores, (coresIndex+1) * sizeof(CpuInfo));
- if (cores == NULL) return NULL;
- cores[coresIndex] = (CpuInfo) {NULL, 0, 0, 0, 0, 0, 0, 0};
+ cores = (CpuInfo*)realloc(cores, (coresIndex + 1) * sizeof(CpuInfo));
+ if (cores == NULL)
+ return NULL;
+ cores[coresIndex] = (CpuInfo) { NULL, 0, 0, 0, 0, 0, 0, 0 };
fclose(file);
return cores;
}
-extern "C" CpuInfo *getCpuInfoAndTime()
+extern "C" CpuInfo* getCpuInfoAndTime()
{
#ifdef __APPLE__
CpuInfo* arr = getCpuInfo();
- if (arr == NULL) return (CpuInfo*) malloc(sizeof(CpuInfo));
+ if (arr == NULL)
+ return (CpuInfo*)malloc(sizeof(CpuInfo));
return arr;
#elif __linux__
CpuInfo* arr = getCpuInfo();
- if (arr == NULL) return NULL;
+ if (arr == NULL)
+ return NULL;
CpuInfo* arr2 = getCpuTime();
- if (arr2 == NULL) return NULL;
+ if (arr2 == NULL)
+ return NULL;
for (int i = 0; arr[i].manufacturer; i++) {
arr2[i].manufacturer = arr[i].manufacturer;
@@ -205,18 +231,19 @@ extern "C" CpuInfo *getCpuInfoAndTime()
#endif
}
-extern "C" int getCpuArrayLen(CpuInfo *arr)
+extern "C" int getCpuArrayLen(CpuInfo* arr)
{
int i = 0;
- for (; arr[i].manufacturer; i++);
- return i-1;
+ for (; arr[i].manufacturer; i++)
+ ;
+ return i - 1;
}
-extern "C" void freeCpuInfoArray(CpuInfo *arr, int len)
+extern "C" void freeCpuInfoArray(CpuInfo* arr, int len)
{
for (int i = 0; i < len; i++) {
free(arr[i].manufacturer);
}
-
+
free(arr);
} \ No newline at end of file
diff --git a/src/bun.js/bindings/node_os/interface_addresses.cpp b/src/bun.js/bindings/node_os/interface_addresses.cpp
index 706c43340..d39f66bd5 100644
--- a/src/bun.js/bindings/node_os/interface_addresses.cpp
+++ b/src/bun.js/bindings/node_os/interface_addresses.cpp
@@ -1,3 +1,4 @@
+#include "mimalloc.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@@ -6,6 +7,11 @@
#include <ifaddrs.h>
#include <arpa/inet.h>
+#define free mi_free
+#define malloc mi_malloc
+#define realloc mi_realloc
+#define strdup mi_strdup
+
#ifdef __linux__
#include <netpacket/packet.h>
#include <net/ethernet.h>
@@ -22,8 +28,9 @@ extern "C" uint32_t getBitCountFromIPv4Mask(uint32_t mask) {
extern "C" uint32_t getBitCountFromIPv6Mask(const in6_addr &mask) {
uint32_t bitCount = 0;
- for (uint32_t ii = 0; ii < 4; ii++) {
- bitCount += __builtin_popcount(mask.s6_addr32[ii]);
+ // uint32 version is unsupported on some platforms
+ for (size_t ii = 0; ii < 16; ii++) {
+ bitCount += __builtin_popcount(mask.s6_addr[ii]);
}
return bitCount;
@@ -37,7 +44,7 @@ extern "C" NetworkInterface *getNetworkInterfaces() {
struct ifaddrs *ifap, *ifa;
unsigned char *ptr;
- getifaddrs (&ifap);
+ getifaddrs(&ifap);
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_INET) {
struct sockaddr_in *sa = (struct sockaddr_in *) ifa->ifa_addr;