1*0f4c859eSApple OSS Distributions /* 2*0f4c859eSApple OSS Distributions * Copyright (c) 2022 Apple Inc. All rights reserved. 3*0f4c859eSApple OSS Distributions * 4*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*0f4c859eSApple OSS Distributions * 6*0f4c859eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*0f4c859eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*0f4c859eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*0f4c859eSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*0f4c859eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*0f4c859eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*0f4c859eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*0f4c859eSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*0f4c859eSApple OSS Distributions * 15*0f4c859eSApple OSS Distributions * Please obtain a copy of the License at 16*0f4c859eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*0f4c859eSApple OSS Distributions * 18*0f4c859eSApple OSS Distributions * The Original Code and all software distributed under the License are 19*0f4c859eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*0f4c859eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*0f4c859eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*0f4c859eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*0f4c859eSApple OSS Distributions * Please see the License for the specific language governing rights and 24*0f4c859eSApple OSS Distributions * limitations under the License. 25*0f4c859eSApple OSS Distributions * 26*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*0f4c859eSApple OSS Distributions */ 28*0f4c859eSApple OSS Distributions 29*0f4c859eSApple OSS Distributions 30*0f4c859eSApple OSS Distributions #define REF 1 31*0f4c859eSApple OSS Distributions #define CREF 2 32*0f4c859eSApple OSS Distributions #define PTR 3 33*0f4c859eSApple OSS Distributions #define BPTR 4 34*0f4c859eSApple OSS Distributions 35*0f4c859eSApple OSS Distributions #if defined(__CCT_TEST_ENABLED) 36*0f4c859eSApple OSS Distributions #define __CCT_ENABLE_USER_SPACE 37*0f4c859eSApple OSS Distributions #endif 38*0f4c859eSApple OSS Distributions 39*0f4c859eSApple OSS Distributions #include <sys/constrained_ctypes.h> 40*0f4c859eSApple OSS Distributions #include <darwintest.h> 41*0f4c859eSApple OSS Distributions #include <string.h> 42*0f4c859eSApple OSS Distributions #include <unistd.h> 43*0f4c859eSApple OSS Distributions 44*0f4c859eSApple OSS Distributions T_GLOBAL_META( 45*0f4c859eSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 46*0f4c859eSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"), 47*0f4c859eSApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE)); 48*0f4c859eSApple OSS Distributions 49*0f4c859eSApple OSS Distributions /* 50*0f4c859eSApple OSS Distributions * Verify that `int_ref_t' and `int_ref_ref_t' 51*0f4c859eSApple OSS Distributions * behave as expected, under different combinations 52*0f4c859eSApple OSS Distributions * of the CCT being enabled / enacted 53*0f4c859eSApple OSS Distributions */ 54*0f4c859eSApple OSS Distributions #if defined(__CCT_TEST_ENABLED) && defined(__CCT_TEST_ENACTED) 55*0f4c859eSApple OSS Distributions /* 56*0f4c859eSApple OSS Distributions * When the CCT are enabled in the user space, 57*0f4c859eSApple OSS Distributions * __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int) should 58*0f4c859eSApple OSS Distributions * define the types `int_ref_t' and `int_ref_ref_t'. 59*0f4c859eSApple OSS Distributions * 60*0f4c859eSApple OSS Distributions * If the CCT are enacted in addition to being enabled, 61*0f4c859eSApple OSS Distributions * the test code itself has to adhere to the type 62*0f4c859eSApple OSS Distributions * constraints. 63*0f4c859eSApple OSS Distributions */ 64*0f4c859eSApple OSS Distributions __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int); 65*0f4c859eSApple OSS Distributions 66*0f4c859eSApple OSS Distributions T_DECL(enacted_constrained_types_are_size_compatible_with_plain_types, "rdar://101223528") 67*0f4c859eSApple OSS Distributions { 68*0f4c859eSApple OSS Distributions T_ASSERT_EQ(sizeof(int_ref_t), sizeof(int*), NULL); 69*0f4c859eSApple OSS Distributions T_ASSERT_EQ(sizeof(int_ref_ref_t), sizeof(int **), NULL); 70*0f4c859eSApple OSS Distributions } 71*0f4c859eSApple OSS Distributions 72*0f4c859eSApple OSS Distributions T_DECL(enacted_constrained_types_are_assignable_from_plain_types, "rdar://101223528") 73*0f4c859eSApple OSS Distributions { 74*0f4c859eSApple OSS Distributions int s = 1; 75*0f4c859eSApple OSS Distributions T_ASSERT_EQ(s, 1, NULL); 76*0f4c859eSApple OSS Distributions 77*0f4c859eSApple OSS Distributions int * ps __single = &s; 78*0f4c859eSApple OSS Distributions int_ref_t rs = ps; 79*0f4c859eSApple OSS Distributions T_ASSERT_EQ(*rs, 1, NULL); 80*0f4c859eSApple OSS Distributions 81*0f4c859eSApple OSS Distributions int * * pps = &ps; 82*0f4c859eSApple OSS Distributions int_ref_ref_t rrs = pps; 83*0f4c859eSApple OSS Distributions T_ASSERT_EQ(**rrs, 1, NULL); 84*0f4c859eSApple OSS Distributions } 85*0f4c859eSApple OSS Distributions #elif defined(__CCT_TEST_ENABLED) && !defined(__CCT_TEST_ENACTED) 86*0f4c859eSApple OSS Distributions /* 87*0f4c859eSApple OSS Distributions * When the CCT are enabled in the user space, 88*0f4c859eSApple OSS Distributions * __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int) should 89*0f4c859eSApple OSS Distributions * define the types `int_ref_t' and `int_ref_ref_t'. 90*0f4c859eSApple OSS Distributions * 91*0f4c859eSApple OSS Distributions * When CCT are not enacted, the test code itself does not have to adhere 92*0f4c859eSApple OSS Distributions * to the type constraints. 93*0f4c859eSApple OSS Distributions */ 94*0f4c859eSApple OSS Distributions __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int); 95*0f4c859eSApple OSS Distributions 96*0f4c859eSApple OSS Distributions T_DECL(enabled_constrained_types_are_size_compatible_with_plain_types, "rdar://101223528") 97*0f4c859eSApple OSS Distributions { 98*0f4c859eSApple OSS Distributions T_ASSERT_EQ(sizeof(int_ref_t), sizeof(int*), NULL); 99*0f4c859eSApple OSS Distributions T_ASSERT_EQ(sizeof(int_ref_ref_t), sizeof(int **), NULL); 100*0f4c859eSApple OSS Distributions } 101*0f4c859eSApple OSS Distributions 102*0f4c859eSApple OSS Distributions /* 103*0f4c859eSApple OSS Distributions * When CCT are not enacted, the test code itself does not have to adhere 104*0f4c859eSApple OSS Distributions * to the type constraints. 105*0f4c859eSApple OSS Distributions */ 106*0f4c859eSApple OSS Distributions T_DECL(enabled_constrained_types_are_assignable_from_plain_types, "rdar://101223528") 107*0f4c859eSApple OSS Distributions { 108*0f4c859eSApple OSS Distributions int s = 1; 109*0f4c859eSApple OSS Distributions T_ASSERT_EQ(s, 1, NULL); 110*0f4c859eSApple OSS Distributions 111*0f4c859eSApple OSS Distributions int * ps __single = &s; 112*0f4c859eSApple OSS Distributions int_ref_t rs = ps; 113*0f4c859eSApple OSS Distributions T_ASSERT_EQ(*rs, 1, NULL); 114*0f4c859eSApple OSS Distributions 115*0f4c859eSApple OSS Distributions int * * pps = &ps; 116*0f4c859eSApple OSS Distributions int_ref_ref_t rrs = pps; 117*0f4c859eSApple OSS Distributions T_ASSERT_EQ(**rrs, 1, NULL); 118*0f4c859eSApple OSS Distributions } 119*0f4c859eSApple OSS Distributions #else /* !defined(__CCT_TEST_ENABLED) && !defined(__CCT_TEST_ENACTED) */ 120*0f4c859eSApple OSS Distributions /* 121*0f4c859eSApple OSS Distributions * When the CCT are disabled in the user space, 122*0f4c859eSApple OSS Distributions * attempt to define `int_ref_t' and `int_ref_ref_t' 123*0f4c859eSApple OSS Distributions * should be a no-op, and the subsequent redefintion 124*0f4c859eSApple OSS Distributions * of `int_ref_t' and `int_ref_ref_t' should succeed. 125*0f4c859eSApple OSS Distributions */ 126*0f4c859eSApple OSS Distributions __CCT_DECLARE_CONSTRAINED_PTR_TYPES(int, int); 127*0f4c859eSApple OSS Distributions 128*0f4c859eSApple OSS Distributions typedef int * int_ref_t; 129*0f4c859eSApple OSS Distributions typedef int * * int_ref_ref_t; 130*0f4c859eSApple OSS Distributions 131*0f4c859eSApple OSS Distributions T_DECL(disabled_constrained_types_decay_into_plain_types, "rdar://101223528") 132*0f4c859eSApple OSS Distributions { 133*0f4c859eSApple OSS Distributions int s = 1; 134*0f4c859eSApple OSS Distributions T_ASSERT_EQ(s, 1, NULL); 135*0f4c859eSApple OSS Distributions 136*0f4c859eSApple OSS Distributions int_ref_t rs = &s; 137*0f4c859eSApple OSS Distributions T_ASSERT_EQ(*rs, 1, NULL); 138*0f4c859eSApple OSS Distributions 139*0f4c859eSApple OSS Distributions int_ref_ref_t rrs = &rs; 140*0f4c859eSApple OSS Distributions T_ASSERT_EQ(**rrs, 1, NULL); 141*0f4c859eSApple OSS Distributions } 142*0f4c859eSApple OSS Distributions #endif /* !defined(__CCT_TEST_ENABLED) && !defined(__CCT_TEST_ENACTED) */ 143