Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/dev/BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ Set `APS5_THREAD_PRIORITY=high` to raise the AGC driver's queue workers and the

The shader recompilation logic in [core/shader/recompiler](../../core/shader/recompiler) is isolated from the rest of the project and is a pure function of its input data, designed for integration into any other project. The current CMake target also includes cache support and links a supplied runtime target, glslang, and optionally SPIRV-Tools.

## Hardened runtime probe

`tools/macos_hardened_probe.cpp` prints the result of `mmap` with read, write, and execute, `mprotect` to read and execute, and the `mach_vm_remap` plus `mach_vm_protect` sequence used by call-trace thunks. It also prints `DYLD_INSERT_LIBRARIES`. `tools/macos_sign_guest.sh` ad-hoc signs one executable with `--options runtime` and `tools/macos_hardened_runtime.entitlements`. Those entitlements are `allow-unsigned-executable-memory`, `allow-dyld-environment-variables`, and `disable-library-validation`. This tree does not change the allocators and does not claim that Rosetta accepts any of those calls. Read the probe output on a Mac.

## Register context tests

The x86-64 register capture and restore tests can be built separately on Linux and macOS, without submodules or the prx libraries. On macOS:
Expand Down
63 changes: 63 additions & 0 deletions tools/macos_hardened_probe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <cerrno>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <sys/mman.h>
#include <unistd.h>

static void ReportErrno(const char* name) {
std::printf("%s: errno %d %s\n", name, errno, std::strerror(errno));
}

int main() {
const long pageSize = ::getpagesize();
const auto page = static_cast<std::size_t>(pageSize > 0 ? pageSize : 4096);
const char* inserted = std::getenv("DYLD_INSERT_LIBRARIES");
std::printf("dyld_insert_libraries: %s\n", inserted == nullptr ? "unset" : inserted);
#if defined(__x86_64__)
std::printf("arch: x86_64\n");
#elif defined(__aarch64__)
std::printf("arch: arm64\n");
#else
std::printf("arch: other\n");
#endif

void* rwx = ::mmap(nullptr, page, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
if (rwx == MAP_FAILED) ReportErrno("mmap_rwx");
else {
std::printf("mmap_rwx: ok\n");
::munmap(rwx, page);
}

void* rw = ::mmap(nullptr, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (rw == MAP_FAILED) ReportErrno("mmap_rw");
else {
std::printf("mmap_rw: ok\n");
if (::mprotect(rw, page, PROT_READ | PROT_EXEC) != 0) ReportErrno("mprotect_rx");
else std::printf("mprotect_rx: ok\n");
::munmap(rw, page);
}

void* writable = ::mmap(nullptr, page, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (writable == MAP_FAILED) ReportErrno("calltrace_mmap");
else {
mach_vm_address_t executable = 0;
vm_prot_t current = VM_PROT_NONE;
vm_prot_t maximum = VM_PROT_NONE;
const kern_return_t remapped = ::mach_vm_remap(mach_task_self(), &executable, page, 0, VM_FLAGS_ANYWHERE,
mach_task_self(), reinterpret_cast<mach_vm_address_t>(writable), false, &current, &maximum, VM_INHERIT_NONE);
if (remapped != KERN_SUCCESS) std::printf("mach_vm_remap: kern %d\n", remapped);
else {
const kern_return_t protectedPages = ::mach_vm_protect(mach_task_self(), executable, page, false, VM_PROT_READ | VM_PROT_EXECUTE);
if (protectedPages != KERN_SUCCESS) std::printf("mach_vm_protect_rx: kern %d\n", protectedPages);
else std::printf("mach_vm_protect_rx: ok\n");
::mach_vm_deallocate(mach_task_self(), executable, page);
}
::munmap(writable, page);
}
return 0;
}
12 changes: 12 additions & 0 deletions tools/macos_hardened_runtime.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
<true/>
<key>com.apple.security.cs.allow-dyld-environment-variables</key>
<true/>
<key>com.apple.security.cs.disable-library-validation</key>
<true/>
</dict>
</plist>
17 changes: 17 additions & 0 deletions tools/macos_sign_guest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -euo pipefail
if [[ $# -lt 1 || $# -gt 2 ]]; then
echo "usage: tools/macos_sign_guest.sh executable [library-directory]" >&2
exit 2
fi
root=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
entitlements=$root/tools/macos_hardened_runtime.entitlements
identity=${CODESIGN_IDENTITY:--}
codesign --force --sign "$identity" --options runtime --timestamp=none --entitlements "$entitlements" "$1"
if [[ $# -eq 2 ]]; then
find "$2" -type f \( -name '*.prx' -o -name '*.dylib' \) -print0 |
while IFS= read -r -d '' library; do
codesign --force --sign "$identity" --timestamp=none "$library"
done
fi
codesign --display --verbose=2 --entitlements - "$1"
Loading