1 /* 2 * Copyright (c) 2025 Apple Computer, 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 #if __arm64__ 30 #include <AppleFeatures/AppleFeatures.h> 31 #include <arm64/mte.h> 32 #include <os/security_config.h> 33 #include <darwintest.h> 34 35 #include "arm_mte_utilities.h" 36 #include "test_utils.h" 37 38 T_DECL(mte_api_test, 39 "Test the API exposed in mte.h work in user space", 40 T_META_REQUIRES_SYSCTL_EQ("hw.optional.arm.FEAT_MTE2", 1), 41 XNU_T_META_SOC_SPECIFIC) 42 { 43 void *address; 44 vm_size_t size; 45 46 T_SETUPBEGIN; 47 { 48 /* verify that the process is running with MTE enabled */ 49 bool has_mte = os_security_config_get() & OS_SECURITY_CONFIG_MTE; 50 T_QUIET; T_ASSERT_TRUE(has_mte, "Process should have MTE"); 51 52 size = PAGE_SIZE; 53 address = allocate_tagged_memory(size, NULL); 54 } 55 T_SETUPEND; 56 57 mte_exclude_mask_t m0 = mte_update_exclude_mask(address, 0); 58 T_QUIET; T_ASSERT_EQ(__builtin_popcount(m0), 1, "mte_update_exclude_mask"); 59 void *t1 = mte_generate_random_tag(address, m0); 60 mte_exclude_mask_t m1 = mte_update_exclude_mask(t1, m0); 61 T_QUIET; T_ASSERT_EQ(__builtin_popcount(m1), 2, "mte_update_exclude_mask"); 62 63 mte_store_tag_16(t1); 64 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), t1, "mte_load_tag (16)"); 65 mte_store_tag_16(address); 66 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), address, "mte_load_tag (zero, 16)"); 67 68 mte_store_tag_32(t1); 69 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), t1, "mte_load_tag (32, 0)"); 70 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 16), t1 + 16, "mte_load_tag (32, 16)"); 71 mte_store_tag_32(address); 72 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), address, "mte_load_tag (zero, 32, 0)"); 73 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 16), address + 16, "mte_load_tag (zero, 32, 16)"); 74 75 mte_store_tag_64(t1); 76 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), t1, "mte_load_tag (32, 0)"); 77 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 16), t1 + 16, "mte_load_tag (32, 16)"); 78 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 32), t1 + 32, "mte_load_tag (32, 32)"); 79 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 48), t1 + 48, "mte_load_tag (32, 48)"); 80 mte_store_tag_64(address); 81 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), address, "mte_load_tag (zero, 32, 0)"); 82 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 16), address + 16, "mte_load_tag (zero, 32, 16)"); 83 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 32), address + 32, "mte_load_tag (zero, 32, 32)"); 84 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 48), address + 48, "mte_load_tag (zero, 32, 48)"); 85 86 mte_store_tag(t1, 1024); 87 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), t1, "mte_load_tag (1024, 0)"); 88 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 768), t1 + 768, "mte_load_tag (1024, 768)"); 89 mte_store_tag(address, 1024); 90 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1), address, "mte_load_tag (zero, 1024, 0)"); 91 T_QUIET; T_ASSERT_EQ(mte_load_tag(t1 + 768), address + 768, "mte_load_tag (zero, 1024, 768)"); 92 93 mte_store_tag(t1, 512); 94 mte_disable_tag_checking(); 95 *(uint8_t *)address = 0xff; 96 mte_enable_tag_checking(); 97 T_QUIET; T_ASSERT_EQ(*(uint8_t *)address, 0xff, "Write with TCO=1"); 98 99 mte_bzero_unchecked(address, 16); 100 T_QUIET; T_ASSERT_EQ(*(uint8_t *)address, 0x00, "bzero with TCO=1"); 101 102 mte_bzero_fast_checked(t1, 512); 103 } 104 #endif /* __arm64__ */ 105