xref: /xnu-12377.81.4/tests/vm/vm_guard_objects_telemetry.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2025 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include "exc_guard_helper.h"
30 #include <darwintest.h>
31 #include <mach/mach.h>
32 #include <mach/mach_vm.h>
33 #include <mach/task_info.h>
34 #include <kern/exc_guard.h>
35 #include <mach/vm_statistics.h>
36 #include "test_utils.h"
37 
38 T_GLOBAL_META(
39 	T_META_NAMESPACE("xnu.vm.guard_objects_telemetry"),
40 	T_META_RADAR_COMPONENT_NAME("xnu"),
41 	T_META_RADAR_COMPONENT_VERSION("VM"));
42 
43 #ifndef kGUARD_EXC_LARGE_ALLOCATION_TELEMETRY
44 #define kGUARD_EXC_LARGE_ALLOCATION_TELEMETRY (13)
45 #endif
46 
47 /*
48  * This test is signed with com.apple.security.hardened-process.guard-objects,
49  * so it will run with guard objects enabled.
50  */
51 T_DECL(test_allocation_denied_under_guard_objects,
52     "Ensure simulated crash occurs when violating guard objects allocation limit",
53     T_META_ENABLED(!TARGET_OS_OSX)) {
54 	exc_guard_helper_info_t exc_info;
55 
56 	exc_guard_helper_init();
57 	enable_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY);
58 
59 	bool caught_exception = block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY, &exc_info, ^{
60 		kern_return_t     kr;
61 		mach_vm_address_t addr;
62 		mach_vm_size_t    limit = (1 << 30);
63 		mach_vm_size_t    size  = limit + PAGE_SIZE;
64 
65 		/*
66 		 * Only the first iteration should generate an exception.
67 		 */
68 		for (int i = 0; i < 4; i++) {
69 		        addr = 0;
70 
71 		        kr = mach_vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE);
72 		        T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_vm_allocate");
73 
74 		        kr = mach_vm_deallocate(mach_task_self(), addr, size);
75 		        T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_vm_deallocate");
76 		}
77 	});
78 
79 	T_QUIET; T_ASSERT_TRUE(caught_exception, "guard exception received for large allocation");
80 	T_QUIET; T_ASSERT_EQ(exc_info.catch_count, 1, "only a single exception should be received");
81 	T_QUIET; T_ASSERT_EQ(exc_info.guard_type, GUARD_TYPE_VIRT_MEMORY, "exception should be type GUARD_TYPE_VIRT_MEMORY");
82 	T_QUIET; T_ASSERT_EQ(exc_info.guard_flavor, kGUARD_EXC_LARGE_ALLOCATION_TELEMETRY, "exception should be flavor kGUARD_EXC_LARGE_ALLOCATION_TELEMETRY");
83 
84 	T_PASS("Successfully raised a single guard exception of the expected type");
85 }
86