From 5a3c646307a7dca3373d5380e98e6141a6d20b98 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 18 Dec 2022 02:10:18 -0800 Subject: [PATCH] Fix sysinfo() totalram on XNU HW_MEMSIZE is the 64-bit version of HW_PHYSMEM, which is inaccurate on systems having more than 4gb of RAM. --- libc/calls/struct/sysinfo.h | 3 ++- libc/calls/struct/vmmeter-meta.internal.h | 0 libc/calls/sysinfo.c | 5 ++--- 3 files changed, 4 insertions(+), 4 deletions(-) delete mode 100755 libc/calls/struct/vmmeter-meta.internal.h diff --git a/libc/calls/struct/sysinfo.h b/libc/calls/struct/sysinfo.h index 51c78588c..da90cf03a 100644 --- a/libc/calls/struct/sysinfo.h +++ b/libc/calls/struct/sysinfo.h @@ -13,7 +13,8 @@ struct sysinfo { uint64_t totalswap; /* size of emergency memory */ uint64_t freeswap; /* hopefully equal to totalswap */ int16_t procs; /* number of processes */ - int16_t __ignore; /* padding */ + int16_t __ignore1; /* padding */ + int32_t __ignore2; /* padding */ uint64_t totalhigh; /* wut */ uint64_t freehigh; /* wut */ uint32_t mem_unit; /* ram stuff above is multiples of this */ diff --git a/libc/calls/struct/vmmeter-meta.internal.h b/libc/calls/struct/vmmeter-meta.internal.h deleted file mode 100755 index e69de29bb..000000000 diff --git a/libc/calls/sysinfo.c b/libc/calls/sysinfo.c index 6a402793d..1e13afafa 100644 --- a/libc/calls/sysinfo.c +++ b/libc/calls/sysinfo.c @@ -21,7 +21,6 @@ #include "libc/calls/struct/sysinfo.internal.h" #include "libc/calls/struct/timespec.h" #include "libc/calls/struct/timeval.h" -#include "libc/calls/struct/vmmeter-meta.internal.h" #include "libc/dce.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/strace.internal.h" @@ -32,7 +31,7 @@ #define CTL_KERN 1 #define CTL_HW 6 #define KERN_BOOTTIME 21 -#define HW_PHYSMEM 5 +#define HW_PHYSMEM (IsXnu() ? 24 : 5) static int64_t GetUptime(void) { if (IsNetbsd()) return 0; // TODO(jart): Why? @@ -44,7 +43,7 @@ static int64_t GetUptime(void) { } static int64_t GetPhysmem(void) { - uint64_t x; + uint64_t x = 0; size_t n = sizeof(x); int mib[] = {CTL_HW, HW_PHYSMEM}; if (sys_sysctl(mib, ARRAYLEN(mib), &x, &n, 0, 0) == -1) return 0;