[vm/service]: Add OS::SafeReadMemory for macOS using mach_vm_read_overwrite

TEST=runtime/vm/service_test.cc

Cq-Include-Trybots: dart/try:vm-asan-mac-release-arm64-try,vm-dyn-mac-debug-arm64-try,vm-mac-debug-arm64-try,vm-mac-debug-x64-try,vm-tsan-mac-release-arm64-try,vm-ubsan-mac-release-arm64-try
Change-Id: I1be990f3debb7e0b7102f54e8c5bec25252756d4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/507040
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Ben Konyi <bkonyi@google.com>
This commit is contained in:
Nourhan Hasan
2026-06-11 11:30:22 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent 0f9e6da044
commit bd5ccf95d1
4 changed files with 40 additions and 15 deletions
-4
View File
@@ -232,10 +232,6 @@ dart/data_uri_spawn_test: Skip # TODO(zra): package:unittest is not in the image
dart/spawn_shutdown_test: Skip # OOM crash can bring down the OS.
[ $system == macos ]
cc/Service_ReadNativeMemory_InvalidAddress: Skip
cc/Service_ReadNativeMemory_LargeRead: Skip
cc/Service_ReadNativeMemory_NullAddress: Skip
cc/Service_ReadNativeMemory_ValidAddress: Skip
dart/transferable_throws_oom_test: SkipByDesign # Allocating too much memory to cause OOM doesn't work on mac
[ $system == windows ]
+36 -10
View File
@@ -7,18 +7,21 @@
#include "vm/os.h"
#include <dlfcn.h> // NOLINT
#include <errno.h> // NOLINT
#include <limits.h> // NOLINT
#include <mach-o/loader.h> // NOLINT
#include <mach/clock.h> // NOLINT
#include <mach/mach.h> // NOLINT
#include <mach/mach_time.h> // NOLINT
#include <sys/resource.h> // NOLINT
#include <sys/time.h> // NOLINT
#include <unistd.h> // NOLINT
#include <dlfcn.h> // NOLINT
#include <errno.h> // NOLINT
#include <limits.h> // NOLINT
#include <mach-o/loader.h> // NOLINT
#include <mach/clock.h> // NOLINT
#include <mach/mach.h> // NOLINT
#include <mach/mach_error.h> // NOLINT
#include <mach/mach_time.h> // NOLINT
#include <sys/resource.h> // NOLINT
#include <sys/time.h> // NOLINT
#include <unistd.h> // NOLINT
#if DART_HOST_OS_IOS
#include <syslog.h> // NOLINT
#else
#include <mach/mach_vm.h> // NOLINT
#endif
#include "platform/utils.h"
@@ -131,6 +134,29 @@ uintptr_t OS::CurrentRSS() {
return info.resident_size;
}
bool OS::SafeReadMemory(void* address,
uint8_t* buffer,
size_t size_in_bytes,
const char** error) {
#if DART_HOST_OS_IOS
vm_size_t bytes_read = 0;
kern_return_t kr = vm_read_overwrite(
mach_task_self(), reinterpret_cast<vm_address_t>(address), size_in_bytes,
reinterpret_cast<vm_address_t>(buffer), &bytes_read);
#else
mach_vm_size_t bytes_read = 0;
kern_return_t kr = mach_vm_read_overwrite(
mach_task_self(), reinterpret_cast<mach_vm_address_t>(address),
size_in_bytes, reinterpret_cast<mach_vm_address_t>(buffer), &bytes_read);
#endif
if (kr != KERN_SUCCESS || bytes_read != size_in_bytes) {
*error = OS::SCreate(nullptr, "%s (kr=%d)", mach_error_string(kr), kr);
return false;
}
return true;
}
void OS::Sleep(int64_t millis) {
int64_t micros = millis * kMicrosecondsPerMillisecond;
SleepMicros(micros);
+2 -1
View File
@@ -6268,7 +6268,8 @@ static void ReadNativeMemoryHelper(JSONStream* js,
bool ok = OS::SafeReadMemory(reinterpret_cast<void*>(address), buffer.get(),
size, &read_error);
#elif defined(DART_HOST_OS_MACOS) || defined(DART_HOST_OS_IOS)
bool ok = false; // TODO(thenourhan): implement using mach_vm_read
bool ok = OS::SafeReadMemory(reinterpret_cast<void*>(address), buffer.get(),
size, &read_error);
#elif defined(DART_HOST_OS_WINDOWS)
bool ok = OS::SafeReadMemory(reinterpret_cast<void*>(address), buffer.get(),
size, &read_error);
+2
View File
@@ -824,6 +824,8 @@ ISOLATE_UNIT_TEST_CASE(Service_ReadNativeMemory_InvalidAddress) {
EXPECT_SUBSTRING("Input\\/output error", handler.msg());
#elif defined(DART_HOST_OS_WINDOWS)
EXPECT_SUBSTRING("error 299", handler.msg());
#elif defined(DART_HOST_OS_MACOS)
EXPECT_SUBSTRING("invalid address", handler.msg());
#endif
}