]> xenbits.xensource.com Git - libvirt.git/commitdiff
virhostmem: Get total memory on macOS properly
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 20 Oct 2023 08:14:39 +0000 (10:14 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 6 Nov 2023 11:01:57 +0000 (12:01 +0100)
Problem with HW_PHYSMEM sysctl on 64-bit macOS is that it
returns a 32-bit signed value. Thus it overflows. Switching to
HW_MEMSIZE is recommended as it's of an uint_64 type [1].

1: https://github.com/apple-oss-distributions/xnu/blob/xnu-10002.1.13/bsd/sys/sysctl.h

Reported-by: Jaroslav Suchanek <jsuchane@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/util/virhostmem.c

index 1da2759ac3553e8ef43b946c38fc04a78bbfbe20..a7027af835f868ef42091414d33e3631b3aa561a 100644 (file)
@@ -617,7 +617,10 @@ virHostMemGetTotal(void)
     unsigned long long physmem = 0;
     size_t len = sizeof(physmem);
 
-    if (sysctlbyname("hw.physmem", &physmem, &len, NULL, 0) < 0) {
+    /* On macOS hw.physmem is int32_t which doesn't fly with >4GiB of memory.
+     * But hw.memsize is uint64_t. */
+    if (sysctlbyname("hw.memsize", &physmem, &len, NULL, 0) < 0 &&
+        sysctlbyname("hw.physmem", &physmem, &len, NULL, 0) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to query memory total"));
         return 0;