1 /* 2 * Copyright (c) 2022 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 30 #define REF 1 31 #define CREF 2 32 #define PTR 3 33 #define BPTR 4 34 35 #if defined(__CCT_TEST_ENABLED) 36 #define __CCT_ENABLE_USER_SPACE 37 #endif 38 39 #include <sys/constrained_ctypes.h> 40 #include <darwintest.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 T_GLOBAL_META( 45 T_META_RADAR_COMPONENT_NAME("xnu"), 46 T_META_RADAR_COMPONENT_VERSION("networking"), 47 T_META_RUN_CONCURRENTLY(TRUE)); 48 49 /* 50 * Verify that `int_ref_t' and `int_ref_ref_t' 51 * behave as expected, under different combinations 52 * of the CCT being enabled / enacted 53 */ 54 #if defined(__CCT_TEST_ENABLED) && defined(__CCT_TEST_ENACTED) 55 /* 56 * When the CCT are enabled in the user space, 57 * __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int) should 58 * define the types `int_ref_t' and `int_ref_ref_t'. 59 * 60 * If the CCT are enacted in addition to being enabled, 61 * the test code itself has to adhere to the type 62 * constraints. 63 */ 64 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int); 65 66 T_DECL(enacted_constrained_types_are_size_compatible_with_plain_types, "rdar://101223528") 67 { 68 T_ASSERT_EQ(sizeof(int_ref_t), sizeof(int*), NULL); 69 T_ASSERT_EQ(sizeof(int_ref_ref_t), sizeof(int **), NULL); 70 } 71 72 T_DECL(enacted_constrained_types_are_assignable_from_plain_types, "rdar://101223528") 73 { 74 int s = 1; 75 T_ASSERT_EQ(s, 1, NULL); 76 77 int * ps __single = &s; 78 int_ref_t rs = ps; 79 T_ASSERT_EQ(*rs, 1, NULL); 80 81 int * * pps = &ps; 82 int_ref_ref_t rrs = pps; 83 T_ASSERT_EQ(**rrs, 1, NULL); 84 } 85 #elif defined(__CCT_TEST_ENABLED) && !defined(__CCT_TEST_ENACTED) 86 /* 87 * When the CCT are enabled in the user space, 88 * __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int) should 89 * define the types `int_ref_t' and `int_ref_ref_t'. 90 * 91 * When CCT are not enacted, the test code itself does not have to adhere 92 * to the type constraints. 93 */ 94 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int); 95 96 T_DECL(enabled_constrained_types_are_size_compatible_with_plain_types, "rdar://101223528") 97 { 98 T_ASSERT_EQ(sizeof(int_ref_t), sizeof(int*), NULL); 99 T_ASSERT_EQ(sizeof(int_ref_ref_t), sizeof(int **), NULL); 100 } 101 102 /* 103 * When CCT are not enacted, the test code itself does not have to adhere 104 * to the type constraints. 105 */ 106 T_DECL(enabled_constrained_types_are_assignable_from_plain_types, "rdar://101223528") 107 { 108 int s = 1; 109 T_ASSERT_EQ(s, 1, NULL); 110 111 int * ps __single = &s; 112 int_ref_t rs = ps; 113 T_ASSERT_EQ(*rs, 1, NULL); 114 115 int * * pps = &ps; 116 int_ref_ref_t rrs = pps; 117 T_ASSERT_EQ(**rrs, 1, NULL); 118 } 119 #else /* !defined(__CCT_TEST_ENABLED) && !defined(__CCT_TEST_ENACTED) */ 120 /* 121 * When the CCT are disabled in the user space, 122 * attempt to define `int_ref_t' and `int_ref_ref_t' 123 * should be a no-op, and the subsequent redefintion 124 * of `int_ref_t' and `int_ref_ref_t' should succeed. 125 */ 126 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int); 127 128 typedef int * int_ref_t; 129 typedef int * * int_ref_ref_t; 130 131 T_DECL(disabled_constrained_types_decay_into_plain_types, "rdar://101223528") 132 { 133 int s = 1; 134 T_ASSERT_EQ(s, 1, NULL); 135 136 int_ref_t rs = &s; 137 T_ASSERT_EQ(*rs, 1, NULL); 138 139 int_ref_ref_t rrs = &rs; 140 T_ASSERT_EQ(**rrs, 1, NULL); 141 } 142 #endif /* !defined(__CCT_TEST_ENABLED) && !defined(__CCT_TEST_ENACTED) */ 143