Title: [289056] trunk/Source/bmalloc
Revision
289056
Author
mikh...@igalia.com
Date
2022-02-03 07:29:18 -0800 (Thu, 03 Feb 2022)

Log Message

Fix undefined behavior when querying linux memory size
https://bugs.webkit.org/show_bug.cgi?id=228280

Reviewed by Mark Lam.

When running JSC compiled with the undefined behavior sanitizer enabled in a 32 bits machine:

../../Source/bmalloc/bmalloc/AvailableMemory.cpp:115:60: runtime error:
signed integer overflow: 32839056 * 4096 cannot be represented in type 'long int'

This patch slighly rewrites how the available memory is calculated to
use sysinfo (like it's done for FREEBSD).

* bmalloc/AvailableMemory.cpp:
(bmalloc::LinuxMemory::singleton):
(bmalloc::computeAvailableMemory):
(bmalloc::memoryStatus):

Modified Paths

Diff

Modified: trunk/Source/bmalloc/ChangeLog (289055 => 289056)


--- trunk/Source/bmalloc/ChangeLog	2022-02-03 15:23:40 UTC (rev 289055)
+++ trunk/Source/bmalloc/ChangeLog	2022-02-03 15:29:18 UTC (rev 289056)
@@ -1,3 +1,23 @@
+2022-02-03  Mikhail R. Gadelha  <mikh...@igalia.com>
+
+        Fix undefined behavior when querying linux memory size
+        https://bugs.webkit.org/show_bug.cgi?id=228280
+
+        Reviewed by Mark Lam.
+
+        When running JSC compiled with the undefined behavior sanitizer enabled in a 32 bits machine:
+
+        ../../Source/bmalloc/bmalloc/AvailableMemory.cpp:115:60: runtime error:
+        signed integer overflow: 32839056 * 4096 cannot be represented in type 'long int'
+
+        This patch slighly rewrites how the available memory is calculated to
+        use sysinfo (like it's done for FREEBSD).
+
+        * bmalloc/AvailableMemory.cpp:
+        (bmalloc::LinuxMemory::singleton):
+        (bmalloc::computeAvailableMemory):
+        (bmalloc::memoryStatus):
+
 2022-01-31  David Kilzer  <ddkil...@apple.com>
 
         [libpas] get_num_free_bytes_for_each_heap_callback() is called with `arg` pointing to uninitialized stack memory

Modified: trunk/Source/bmalloc/bmalloc/AvailableMemory.cpp (289055 => 289056)


--- trunk/Source/bmalloc/bmalloc/AvailableMemory.cpp	2022-02-03 15:23:40 UTC (rev 289055)
+++ trunk/Source/bmalloc/bmalloc/AvailableMemory.cpp	2022-02-03 15:29:18 UTC (rev 289056)
@@ -47,10 +47,10 @@
 #if BOS(LINUX)
 #include <algorithm>
 #include <fcntl.h>
+#include <sys/sysinfo.h>
 #elif BOS(FREEBSD)
 #include "VMAllocate.h"
 #include <sys/sysctl.h>
-#include <sys/sysinfo.h>
 #include <sys/types.h>
 #include <sys/user.h>
 #endif
@@ -107,13 +107,7 @@
         static std::once_flag s_onceFlag;
         std::call_once(s_onceFlag,
             [] {
-                long numPages = sysconf(_SC_PHYS_PAGES);
                 s_singleton.pageSize = sysconf(_SC_PAGE_SIZE);
-                if (numPages == -1 || s_singleton.pageSize == -1)
-                    s_singleton.availableMemory = availableMemoryGuess;
-                else
-                    s_singleton.availableMemory = numPages * s_singleton.pageSize;
-
                 s_singleton.statmFd = open("/proc/self/statm", O_RDONLY | O_CLOEXEC);
             });
         return s_singleton;
@@ -149,8 +143,6 @@
     }
 
     long pageSize { 0 };
-    size_t availableMemory { 0 };
-
     int statmFd { -1 };
 };
 #endif
@@ -167,9 +159,7 @@
     // Round up the memory size to a multiple of 128MB because max_mem may not be exactly 512MB
     // (for example) and we have code that depends on those boundaries.
     return ((sizeAccordingToKernel + multiple - 1) / multiple) * multiple;
-#elif BOS(LINUX)
-    return LinuxMemory::singleton().availableMemory;
-#elif BOS(FREEBSD)
+#elif BOS(FREEBSD) || BOS(LINUX)
     struct sysinfo info;
     if (!sysinfo(&info))
         return info.totalram * info.mem_unit;
@@ -205,12 +195,9 @@
     size_t memoryFootprint = 0;
     if (KERN_SUCCESS == task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)(&vmInfo), &vmSize))
         memoryFootprint = static_cast<size_t>(vmInfo.phys_footprint);
-
-    double percentInUse = static_cast<double>(memoryFootprint) / static_cast<double>(availableMemory());
 #elif BOS(LINUX)
     auto& memory = LinuxMemory::singleton();
     size_t memoryFootprint = memory.footprint();
-    double percentInUse = static_cast<double>(memoryFootprint) / static_cast<double>(memory.availableMemory);
 #elif BOS(FREEBSD)
     struct kinfo_proc info;
     size_t infolen = sizeof(info);
@@ -224,10 +211,9 @@
     size_t memoryFootprint = 0;
     if (!sysctl(mib, 4, &info, &infolen, nullptr, 0))
         memoryFootprint = static_cast<size_t>(info.ki_rssize) * vmPageSize();
+#endif
 
     double percentInUse = static_cast<double>(memoryFootprint) / static_cast<double>(availableMemory());
-#endif
-
     double percentAvailableMemoryInUse = std::min(percentInUse, 1.0);
     return MemoryStatus(memoryFootprint, percentAvailableMemoryInUse);
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to