Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | // // zone_names.c // libmalloc // // Documents default zone names and tests malloc_set_zone_name(). // #include <darwintest.h> T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true)); #include <malloc/malloc.h> #include <stdlib.h> // free() #include "../src/platform.h" // CONFIG_NANOZONE T_GLOBAL_META(T_META_ENVVAR("MallocProbGuard=0")); // A local T_META_ENVVAR declaration will override all global ones so we have to // restate this on tests that use T_META_ENVVAR. static void check_zone_names(malloc_zone_t **zones, const char **names, uint32_t count) { for (uint32_t i = 0; i < count; i++) { const char *zone_name = malloc_get_zone_name(zones[i]); T_EXPECT_NOTNULL(zone_name, "zone name not null"); T_EXPECT_EQ_STR(zone_name, names[i], "zone name %u", i); } } extern int32_t malloc_num_zones; extern malloc_zone_t **malloc_zones; static void check_default_zone_names(const char **names, uint32_t count) { T_EXPECT_EQ(malloc_num_zones, count, "zone count"); check_zone_names(malloc_zones, names, count); } T_DECL(default_zone, "Zone names: default", T_META_ENVVAR("MallocNanoZone=0"), T_META_ENVVAR("MallocProbGuard=0"), T_META_TAG_XZONE) { const char *names[] = {"DefaultMallocZone"}; check_default_zone_names(names, 1); } T_DECL(default_zone_and_nano, "Zone names: default + nano", T_META_ENVVAR("MallocNanoZone=1"), T_META_ENVVAR("MallocProbGuard=0")) { #if CONFIG_NANOZONE const char *names[] = {"DefaultMallocZone", "MallocHelperZone"}; check_default_zone_names(names, 2); #else T_SKIP("Nano allocator not configured"); #endif } T_DECL(default_zone_and_pgm, "Zone names: default + ProbGuard", T_META_ENVVAR("MallocProbGuard=1"), T_META_ENVVAR("MallocNanoZone=0"), T_META_TAG_XZONE) { const char *names[] = {"ProbGuardMallocZone", "DefaultMallocZone"}; check_default_zone_names(names, 2); } T_DECL(zone_singletons, "Zone singletons", T_META_TAG_XZONE) { malloc_zone_t *zones[] = { malloc_default_zone(), malloc_default_purgeable_zone() }; const char *names[] = { "DefaultMallocZone", "DefaultPurgeableMallocZone" }; check_zone_names(zones, names, 2); } static malloc_zone_t * call_malloc_zone_from_ptr(void) { void *ptr = malloc(5); malloc_zone_t *zone = malloc_zone_from_ptr(ptr); free(ptr); return zone; } T_DECL(virtual_zone0, "Ensure we return the virtual zone in place of zone 0") { malloc_zone_t *virtual_zone = malloc_default_zone(); T_EXPECT_NE(virtual_zone, malloc_zones[0], NULL); T_EXPECT_EQ(call_malloc_zone_from_ptr(), virtual_zone, NULL); } T_DECL(zone_creation, "Zone creation", T_META_TAG_XZONE) { T_EXPECT_NULL(malloc_create_zone(0, 0)->zone_name, "No name"); } static void check_set_zone_name(malloc_zone_t* zone) { #define zone_name malloc_get_zone_name(zone) char *literal_str = "string literal"; malloc_set_zone_name(zone, literal_str); T_EXPECT_EQ(zone_name, literal_str, "skip copy for immutable string"); char *heap_str = strdup("heap string"); malloc_set_zone_name(zone, heap_str); T_EXPECT_NE(zone_name, heap_str, "defensive copy"); T_EXPECT_EQ_STR(zone_name, heap_str, "same contents"); free(heap_str); const char *copy = zone_name; malloc_set_zone_name(zone, NULL); T_EXPECT_NULL(zone_name, "zone name set to NULL"); if (!getenv("MallocSecureAllocator")) { T_EXPECT_EQ(malloc_size(copy), 0ul, "copy freed"); } } T_DECL(malloc_set_zone_name, "malloc_set_zone_name", T_META_TAG_XZONE) { malloc_zone_t *zones[] = { malloc_default_zone(), malloc_create_zone(0, 0) }; size_t count = sizeof(zones) / sizeof(zones[0]); for (uint32_t i = 0; i < count; i++) { check_set_zone_name(zones[i]); } } |