Loading...
tests/malloc_with_options_test.c libmalloc-646.0.13 libmalloc-792.41.1
--- libmalloc/libmalloc-646.0.13/tests/malloc_with_options_test.c
+++ libmalloc/libmalloc-792.41.1/tests/malloc_with_options_test.c
@@ -6,7 +6,7 @@
 //
 #include <stdlib.h>
 #include <darwintest.h>
-#include "malloc_private.h"
+#include <malloc_private.h>
 #include <time.h>
 
 #include "../src/platform.h"
@@ -17,6 +17,13 @@
 #include <ktrace.h>
 #endif // !MALLOC_TARGET_EXCLAVES
 
+// Returns true if memory is canonically tagged
+// equivalent to "are bits 59:56 cleared"
+static bool
+check_canonical_tag(void *ptr)
+{
+	return !((uintptr_t)ptr & 0x0f00000000000000);
+}
 
 static bool
 check_zeroed_memory(void *ptr, size_t size)
@@ -55,11 +62,15 @@
 		bool aligned = opt_rand & 0x1;
 
 		bool zeroed = opt_rand & 0x2;
-		malloc_options_np_t options = 0;
+		malloc_zone_malloc_options_t options = MALLOC_ZONE_MALLOC_OPTION_NONE;
 		if (zeroed) {
-			options |= MALLOC_NP_OPTION_CLEAR;
-		}
-
+			options |= MALLOC_ZONE_MALLOC_OPTION_CLEAR;
+		}
+
+		bool canonical = opt_rand & 0x4;
+		if (canonical) {
+			options |= MALLOC_NP_OPTION_CANONICAL_TAG;
+		}
 
 		opt_rand = rand();
 		size_t align = 0;
@@ -79,21 +90,33 @@
 
 
 		free(pointers[index]);
-		pointers[index] = malloc_zone_malloc_with_options_np(NULL, align, size,
+		if (opt_rand % 2) {
+			pointers[index] = malloc_zone_malloc_with_options(NULL, align, size,
 				options);
+		} else {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+			pointers[index] = malloc_zone_malloc_with_options_np(NULL, align,
+				size, options);
+#pragma GCC diagnostic pop
+		}
 		T_QUIET; T_ASSERT_NOTNULL(pointers[index], "Allocation failed\n");
 
 		if (zeroed) {
 			T_QUIET; T_ASSERT_TRUE(check_zeroed_memory(pointers[index], size),
 					"Memory wasn't cleared");
 		}
+		if (canonical) {
+			T_QUIET; T_ASSERT_TRUE(check_canonical_tag(pointers[index]),
+					"Tag isn't canonical");
+		}
 		if (align) {
 			T_QUIET; T_ASSERT_TRUE(check_ptr_is_aligned(pointers[index], align),
 				"Pointer isn't aligned");
 		}
 
 		// Scribble the memory to make sure that malloc is properly clearing
-		// when MALLOC_NP_OPTION_CLEAR is set
+		// when MALLOC_ZONE_MALLOC_OPTION_CLEAR is set
 		scribble_memory(pointers[index], size);
 	}
 
@@ -103,7 +126,7 @@
 }
 
 T_DECL(malloc_options, "malloc with options",
-	T_META_TAG_XZONE, T_META_TAG_VM_NOT_PREFERRED)
+	T_META_TAG_ALL_ALLOCATORS, T_META_TAG_VM_NOT_PREFERRED)
 {
 	unsigned seed = time(NULL);
 	T_LOG("seed value = %u", seed);
@@ -114,7 +137,7 @@
 
 T_DECL(malloc_pgm_options, "malloc with options, but PGM is enabled",
 	T_META_ENVVAR("ProbGuardMalloc=1"),
-	T_META_TAG_XZONE, T_META_TAG_VM_NOT_PREFERRED)
+	T_META_TAG_ALL_ALLOCATORS, T_META_TAG_VM_NOT_PREFERRED)
 {
 	unsigned seed = time(NULL);
 	T_LOG("seed value = %u", seed);
@@ -125,7 +148,7 @@
 
 T_DECL(malloc_msl_lite_options, "malloc with options, but MSL Lite is enabled",
 	T_META_ENVVAR("MallocStackLogging=lite"),
-	T_META_TAG_XZONE, T_META_TAG_VM_NOT_PREFERRED)
+	T_META_TAG_ALL_ALLOCATORS, T_META_TAG_VM_NOT_PREFERRED)
 {
 	unsigned seed = time(NULL);
 	T_LOG("seed value = %u", seed);
@@ -137,7 +160,7 @@
 T_DECL(malloc_data_only_options, "Malloc with options, all xzones pure data",
 		T_META_ENVVAR("MallocXzoneDataOnly=1"),
 		T_META_ENVVAR("MallocXzoneGuarded=1"),
-		T_META_TAG_XZONE_ONLY)
+		T_META_TAG_XZONE_ONLY, T_META_TAG_VM_NOT_PREFERRED)
 {
 	unsigned seed = time(NULL);
 	T_LOG("seed value = %u", seed);
@@ -188,8 +211,8 @@
 
 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL);
 
-	void *ptr = malloc_zone_malloc_with_options_np(NULL, expected_alignment,
-			expected_size, MALLOC_NP_OPTION_CLEAR);
+	void *ptr = malloc_zone_malloc_with_options(NULL, expected_alignment,
+			expected_size, MALLOC_ZONE_MALLOC_OPTION_CLEAR);
 	T_ASSERT_NOTNULL(ptr, "allocate");
 	T_ASSERT_TRUE(check_zeroed_memory(ptr, expected_size), "zeroed");
 	T_ASSERT_TRUE(check_ptr_is_aligned(ptr, expected_alignment), "aligned");
@@ -200,3 +223,32 @@
 	dispatch_main();
 }
 #endif // !MALLOC_TARGET_EXCLAVES
+
+T_DECL(malloc_options_alignment, "malloc with options, alignment argument",
+		T_META_TAG_XZONE_ONLY, T_META_TAG_VM_PREFERRED)
+{
+	void *ptr;
+	unsigned align = MALLOC_ZONE_MALLOC_DEFAULT_ALIGN;
+	for (unsigned size = 0; size <= 32; ++size) {
+		ptr = malloc_zone_malloc_with_options(NULL,
+				MALLOC_ZONE_MALLOC_DEFAULT_ALIGN, size,
+				MALLOC_ZONE_MALLOC_OPTION_NONE);
+		T_ASSERT_NOTNULL(ptr, "allocate default alignment %u with size %u",
+				align, size);
+		free(ptr);
+	}
+
+	align = 16;
+	for (unsigned size = 0; size <= 32; ++size) {
+		ptr = malloc_zone_malloc_with_options(NULL,
+				align, size, MALLOC_ZONE_MALLOC_OPTION_NONE);
+		if (size % align) {
+			T_ASSERT_NULL(ptr, "allocate non-default alignment %u with non-multiple size %u",
+					align, size);
+		} else {
+			T_ASSERT_NOTNULL(ptr, "allocate non-default alignment %u with multiple size %u",
+					align, size);
+		}
+		free(ptr);
+	}
+}