Skip to content

Commit 9e74dd6

Browse files
ralfbiedertCopilot
andcommitted
test(rallocator): exercise Darwin syscall failures [mac-coverage] [mac-intel]
Cover rejected maximum protections, invalid page advice and timed-out wait dispatch with owned native reservations and atomics. Iterate on coverage independently from the unchanged interpreter packages. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 536d29d0-3909-44b0-989c-acd2aa9b2a91
1 parent a7dfd37 commit 9e74dd6

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

‎.github/workflows/rallocator-macos-checks.yml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Branch-local, three-package PR validation. Add [mac-checks] to a commit
55
# message to run these checks without opening a PR or invoking unrelated CI.
6+
# Use [mac-coverage] to iterate on coverage without restarting interpreter jobs.
67
name: Rallocator macOS validation
78

89
on:
@@ -13,7 +14,7 @@ permissions:
1314
contents: read
1415

1516
concurrency:
16-
group: rallocator-macos-checks-${{ github.ref }}
17+
group: rallocator-macos-checks-${{ github.ref }}-${{ contains(github.event.head_commit.message, '[mac-coverage]') && 'coverage' || 'full' }}
1718
cancel-in-progress: true
1819

1920
env:
@@ -25,15 +26,15 @@ env:
2526

2627
jobs:
2728
check:
28-
if: contains(github.event.head_commit.message, '[mac-checks]')
29+
if: contains(github.event.head_commit.message, '[mac-checks]') || contains(github.event.head_commit.message, '[mac-coverage]')
2930
name: "${{ matrix.check }} (${{ matrix.runner }})"
3031
runs-on: ${{ matrix.runner }}
3132
timeout-minutes: 180
3233
strategy:
3334
fail-fast: false
3435
matrix:
3536
runner: [macos-15, macos-15-intel]
36-
check: [compatibility, coverage, careful, miri, mutation]
37+
check: ${{ fromJSON(contains(github.event.head_commit.message, '[mac-coverage]') && '["coverage"]' || '["compatibility", "coverage", "careful", "miri", "mutation"]') }}
3738
defaults:
3839
run:
3940
shell: pwsh

‎crates/rallocator/src/hal/macos/mod.rs‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ unsafe extern "C" {
3838
maximum_protection: libc::c_int,
3939
inheritance: u32,
4040
) -> libc::c_int;
41+
#[cfg(test)]
42+
fn mach_vm_protect(task: u32, address: u64, size: u64, set_maximum: libc::c_int, protection: libc::c_int) -> libc::c_int;
4143
fn os_sync_wait_on_address(address: *mut libc::c_void, value: u64, size: usize, flags: u32) -> libc::c_int;
4244
#[cfg(test)]
4345
fn os_sync_wait_on_address_with_timeout(
@@ -146,9 +148,13 @@ pub(crate) fn wait(word: &AtomicU32, expected: u32) {
146148
#[cfg(not(test))]
147149
// SAFETY: The live, aligned atomic provides the four-byte wait word.
148150
let result = unsafe { os_sync_wait_on_address(word.as_ptr().cast(), u64::from(expected), std::mem::size_of::<u32>(), 0) };
149-
if result == -1 {
150-
check_wait_error(std::io::Error::last_os_error().raw_os_error());
151-
}
151+
check_wait_result(result);
152+
}
153+
}
154+
155+
fn check_wait_result(result: libc::c_int) {
156+
if result == -1 {
157+
check_wait_error(std::io::Error::last_os_error().raw_os_error());
152158
}
153159
}
154160

@@ -209,6 +215,56 @@ mod tests {
209215
}
210216
}
211217

218+
#[test]
219+
fn rejected_commit_preserves_the_owned_reservation() {
220+
let pointer = reserve(RESERVE_MIN, RESERVE_MIN);
221+
assert!(!pointer.is_null());
222+
// SAFETY: libSystem maintains this process's task port.
223+
let task = unsafe { mach_task_self_ };
224+
// SAFETY: The test owns the complete, inaccessible reservation. Lowering
225+
// its maximum protection to NONE deliberately prevents a later commit.
226+
let result = unsafe { mach_vm_protect(task, pointer.addr() as u64, RESERVE_MIN as u64, 1, libc::PROT_NONE) };
227+
assert_eq!(result, 0);
228+
// SAFETY: The page-granular range still belongs to this live reservation.
229+
assert!(!unsafe { commit(pointer, PAGE) });
230+
// SAFETY: The rejected commit leaves the complete reservation owned.
231+
unsafe { release(pointer, RESERVE_MIN) };
232+
}
233+
234+
#[test]
235+
fn rejected_advice_leaves_owned_pages_accessible() {
236+
let pointer = reserve(RESERVE_MIN, RESERVE_MIN);
237+
assert!(!pointer.is_null());
238+
// SAFETY: The test owns this complete, page-aligned reservation.
239+
assert!(unsafe { commit(pointer, PAGE) });
240+
// SAFETY: The committed page is exclusively writable.
241+
unsafe { pointer.write_bytes(0x5a, PAGE) };
242+
// SAFETY: The owned page is writable, contains no live objects, and the
243+
// invalid advice is rejected by the kernel without changing its mapping.
244+
assert!(!unsafe { advise(pointer, PAGE, -1) });
245+
// SAFETY: The rejected advice preserves this initialized page.
246+
assert_eq!(unsafe { *pointer }, 0x5a);
247+
// SAFETY: No access remains into the complete owned reservation.
248+
unsafe { release(pointer, RESERVE_MIN) };
249+
}
250+
251+
#[test]
252+
fn timed_out_test_wait_aborts_through_the_kernel_result_dispatch() {
253+
crate::abort::assert_aborts(
254+
"hal::macos::tests::timed_out_test_wait_aborts_through_the_kernel_result_dispatch",
255+
|| {
256+
let word = AtomicU32::new(0);
257+
// SAFETY: The live, aligned word matches the expected value.
258+
// A one-nanosecond timeout bounds this intentional failed wait.
259+
let result =
260+
unsafe { os_sync_wait_on_address_with_timeout(word.as_ptr().cast(), 0, std::mem::size_of::<u32>(), 0, WAIT_CLOCK, 1) };
261+
assert_eq!(result, -1);
262+
assert_eq!(std::io::Error::last_os_error().raw_os_error(), Some(libc::ETIMEDOUT));
263+
check_wait_result(result);
264+
},
265+
);
266+
}
267+
212268
#[test]
213269
fn changed_words_do_not_block_and_departed_waiters_are_tolerated() {
214270
let word = AtomicU32::new(1);

0 commit comments

Comments
 (0)