@@ -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