]> git.saurik.com Git - apple/xnu.git/blobdiff - tests/voucher_traps.c
xnu-7195.81.3.tar.gz
[apple/xnu.git] / tests / voucher_traps.c
index f3e5a0a207412eb79cbc47061696acf7ed3ccd2a..837255f3a6ee5047bf9b43ac63e7e66069af4059 100644 (file)
 
 #include <darwintest.h>
 
+T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
 
-static mach_port_t get_atm_voucher(void)
+static mach_port_t
+get_user_data_port(mach_msg_type_number_t *size)
 {
-       mach_voucher_attr_recipe_data_t r = {
-               .key = MACH_VOUCHER_ATTR_KEY_ATM,
-               .command = MACH_VOUCHER_ATTR_ATM_CREATE
+#define DATA "Hello World!"
+       struct {
+               mach_voucher_attr_recipe_data_t recipe;
+               char data[sizeof(DATA)];
+       } buf = {
+               .recipe = {
+                       .key     = MACH_VOUCHER_ATTR_KEY_USER_DATA,
+                       .command = MACH_VOUCHER_ATTR_USER_DATA_STORE,
+                       .content_size = sizeof(DATA),
+               },
+               .data = DATA,
        };
+
        mach_port_t port = MACH_PORT_NULL;
        kern_return_t kr = host_create_mach_voucher(mach_host_self(),
-                                                   (mach_voucher_attr_raw_recipe_array_t)&r,
-                                                   sizeof(r), &port);
-       T_ASSERT_MACH_SUCCESS(kr, "Create ATM voucher: 0x%x", (unsigned int)port);
-
+           (mach_voucher_attr_raw_recipe_array_t)&buf,
+           sizeof(buf), &port);
+       T_ASSERT_MACH_SUCCESS(kr, "Create USER_DATA voucher: 0x%x",
+           (unsigned int)port);
+
+       if (size) {
+               *size = sizeof(buf);
+       }
        return port;
 }
 
@@ -43,6 +58,7 @@ T_DECL(voucher_extract_attr_recipe, "voucher_extract_attr_recipe")
        mach_vm_size_t alloc_sz;
        mach_port_t port;
        mach_vm_address_t alloc_addr;
+       mach_msg_type_number_t expected_size;
 
        /* map at least a page of memory at some arbitrary location */
        alloc_sz = (mach_vm_size_t)round_page(MACH_VOUCHER_TRAP_STACK_LIMIT + 1);
@@ -54,7 +70,7 @@ T_DECL(voucher_extract_attr_recipe, "voucher_extract_attr_recipe")
         */
        alloc_addr = (mach_vm_address_t)round_page(MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE + 1);
        kr = mach_vm_allocate(mach_task_self(), &alloc_addr,
-                             alloc_sz, VM_FLAGS_ANYWHERE);
+           alloc_sz, VM_FLAGS_ANYWHERE);
 
        /*
         * Make sure that the address of the allocation is larger than the
@@ -62,34 +78,36 @@ T_DECL(voucher_extract_attr_recipe, "voucher_extract_attr_recipe")
         * <rdar://problem/29379175>.
         */
        T_ASSERT_GT_ULLONG((uint64_t)alloc_addr,
-                          (uint64_t)MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE,
-                          "Recipe addr (%llu bytes): 0x%llx > max recipe sz: %llu",
-                          (uint64_t)alloc_sz, (uint64_t)alloc_addr,
-                          (uint64_t)MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE);
+           (uint64_t)MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE,
+           "Recipe addr (%llu bytes): 0x%llx > max recipe sz: %llu",
+           (uint64_t)alloc_sz, (uint64_t)alloc_addr,
+           (uint64_t)MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE);
 
        /* make the allocation look like a pointer to an int */
        mach_msg_type_number_t *recipe_size;
        recipe_size = (mach_msg_type_number_t *)((uintptr_t)alloc_addr);
        bzero(recipe_size, (unsigned long)alloc_sz);
-       if (alloc_sz > MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE)
+       if (alloc_sz > MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE) {
                *recipe_size = MACH_VOUCHER_ATTR_MAX_RAW_RECIPE_ARRAY_SIZE;
-       else
+       } else {
                *recipe_size = (mach_msg_type_number_t)alloc_sz;
+       }
 
        /* recipe buffer on the heap: memset it so panics show up loudly */
        size_t size = (size_t)(10 * 1024 * 1024);
        void *recipe = malloc(size);
        memset(recipe, 0x41, size);
 
-       port = get_atm_voucher();
+       port = get_user_data_port(&expected_size);
 
        /*
-        * This should try to extract the ATM attribute using a buffer on the
+        * This should try to extract the USER_DATA attribute using a buffer on the
         * kernel heap (probably zone memory).
         */
-       kr = mach_voucher_extract_attr_recipe_trap(port, MACH_VOUCHER_ATTR_KEY_ATM,
-                                                  recipe, recipe_size);
+       kr = mach_voucher_extract_attr_recipe_trap(port,
+           MACH_VOUCHER_ATTR_KEY_USER_DATA, recipe, recipe_size);
        T_ASSERT_MACH_SUCCESS(kr, "Extract attribute data with recipe: heap");
+       T_ASSERT_EQ(*recipe_size, expected_size, "size should match");
 
        /* reset the recipe memory */
        memset(recipe, 0x41, size);
@@ -97,12 +115,13 @@ T_DECL(voucher_extract_attr_recipe, "voucher_extract_attr_recipe")
        *recipe_size = MACH_VOUCHER_TRAP_STACK_LIMIT - 1;
 
        /*
-        * This should try to extract the ATM attribute using a buffer on the
+        * This should try to extract the USER_DATA attribute using a buffer on the
         * kernel stack.
         */
-       kr = mach_voucher_extract_attr_recipe_trap(port, MACH_VOUCHER_ATTR_KEY_ATM,
-                                                  recipe, recipe_size);
+       kr = mach_voucher_extract_attr_recipe_trap(port,
+           MACH_VOUCHER_ATTR_KEY_USER_DATA, recipe, recipe_size);
        T_ASSERT_MACH_SUCCESS(kr, "Extract attribute data with recipe: stack");
+       T_ASSERT_EQ(*recipe_size, expected_size, "size should match");
 
        /* cleanup */