Loading...
tests/pgm_integration.c libmalloc-474.0.13 libmalloc-646.40.3
--- libmalloc/libmalloc-474.0.13/tests/pgm_integration.c
+++ libmalloc/libmalloc-646.40.3/tests/pgm_integration.c
@@ -6,18 +6,18 @@
 //
 
 #include <darwintest.h>
+#include <MallocStackLogging/MallocStackLogging.h>
 
 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(TRUE), T_META_NAMESPACE("pgm"),
 		T_META_TAG_XZONE);
 
+#include <mach/mach_vm.h>  // mach_vm_map()
+#include <mach/mach.h>  // mach_task_self()
 #include <mach/vm_page_size.h>
+#include <malloc_private.h>
 #include <malloc/malloc.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <mach/mach.h>
-#include <mach/mach_vm.h>
-
-#include "../src/platform.h"  // CONFIG_PGM_WRAP_CUSTOM_ZONES
 
 T_GLOBAL_META(
 	T_META_ENVVAR("MallocProbGuard=1"),
@@ -26,12 +26,28 @@
 	T_META_ENVVAR("MallocProbGuardAllocations=300")
 );
 
+static malloc_zone_t *
+get_wrapped_zone(malloc_zone_t *zone)
+{
+	malloc_zone_t *wrapped_zone;
+	kern_return_t kr = malloc_get_wrapped_zone(mach_task_self(),
+			/*memory_reader=*/NULL, (vm_address_t)zone, (vm_address_t *)&wrapped_zone);
+	T_QUIET; T_ASSERT_EQ(kr, KERN_SUCCESS, "malloc_get_wrapped_zone() failed");
+	T_EXPECT_NOTNULL(wrapped_zone, "Wrapped zone");
+	return wrapped_zone;
+}
+
 extern int32_t malloc_num_zones;
 extern malloc_zone_t **malloc_zones;
-T_DECL(zone_setup, "ProbGuard zone is default zone and not full")
-{
-	const char *default_zone_name = malloc_get_zone_name(malloc_zones[0]);
-	T_EXPECT_EQ_STR(default_zone_name, "ProbGuardMallocZone", "ProbGuard zone is default zone");
+T_DECL(zone_setup, "ProbGuard zone is default zone and not full", T_META_TAG_VM_PREFERRED)
+{
+	// malloc_default_zone() returns virtual zone, which delegates to zone 0.
+	malloc_zone_t *zone0 = malloc_zones[0];
+	T_EXPECT_EQ_STR(malloc_get_zone_name(zone0), "ProbGuardMallocZone",
+			"ProbGuard zone is default zone");
+	T_EXPECT_EQ(zone0->introspect->zone_type, 2, "MALLOC_ZONE_TYPE_PGM");
+	T_EXPECT_EQ(get_wrapped_zone(zone0), malloc_zones[1],
+			"Wrapped zone is registered");
 
 	void *ptr = malloc(5);
 	malloc_zone_t *zone = malloc_zone_from_ptr(ptr);
@@ -80,40 +96,18 @@
 	touch_memory(ptr + 17);
 }
 
-static void
-out_of_bounds_within_block(void)
-{
-	uint8_t *ptr = malloc(5);
-	T_ASSERT_EQ(malloc_size(ptr), 5ul, "strict alignment");
-
-	touch_memory(ptr - 1);  // left-alignment is always perfect
-	touch_memory(ptr + 5);
-}
-
 T_DECL(uaf_detection, "Use-after-free detection",
-		T_META_IGNORECRASHES("pgm_integration"))
+		T_META_IGNORECRASHES("pgm_integration"),
+		T_META_TAG_VM_PREFERRED)
 {
 	assert_crash(use_after_free);
 }
 
 T_DECL(oob_detection, "Out-of-bounds detection",
-		T_META_IGNORECRASHES("pgm_integration"))
+		T_META_IGNORECRASHES("pgm_integration"),
+		T_META_TAG_VM_PREFERRED)
 {
 	assert_crash(out_of_bounds);
-}
-
-T_DECL(oob_detection_within_block, "Intra-block out-of-bounds detection",
-		T_META_IGNORECRASHES("pgm_integration"),
-		T_META_ENVVAR("MallocProbGuard=1"),
-		T_META_ENVVAR("MallocProbGuardSampleRate=1"),
-		T_META_ENVVAR("MallocProbGuardAllocations=300"),
-		T_META_ENVVAR("MallocProbGuardStrictAlignment=1"))
-{
-#if defined(__LP64__)  // MALLOC_TARGET_64BIT
-	assert_crash(out_of_bounds_within_block);
-#else
-	T_SKIP("ARM (32 bit) crashes on misaligned memory accesses: EXC_ARM_DA_ALIGN");
-#endif
 }
 
 static void
@@ -129,7 +123,7 @@
 }
 
 T_DECL(bogus_pgm_region, "Handle crashes in bogus PGM regions",
-		T_META_IGNORECRASHES("pgm_integration"))
+		T_META_IGNORECRASHES("pgm_integration"), T_META_TAG_VM_PREFERRED)
 {
 	// What we're really testing here is the code that runs in ReportCrash to
 	// generate the PGM report - it needs to gracefully handle unexpected PGM
@@ -137,7 +131,6 @@
 	assert_crash(access_in_bogus_pgm_region);
 }
 
-#if CONFIG_PGM_WRAP_CUSTOM_ZONES
 static void
 non_default_zone_use_after_free(void)
 {
@@ -149,11 +142,11 @@
 
 T_DECL(non_default_zone_uaf_detection,
 		"Use-after-free detection in a wrapped non-default zone",
-		T_META_IGNORECRASHES("pgm_integration"))
+		T_META_IGNORECRASHES("pgm_integration"),
+		T_META_TAG_VM_PREFERRED)
 {
 	assert_crash(non_default_zone_use_after_free);
 }
-#endif // CONFIG_PGM_WRAP_CUSTOM_ZONES
 
 static boolean_t
 check_bytes(uint8_t *ptr, size_t size)
@@ -213,14 +206,14 @@
 	}
 }
 
-T_DECL(allocation_sample_all, "Smoke test, sample 1/1")
+T_DECL(allocation_sample_all, "Smoke test, sample 1/1", T_META_TAG_VM_PREFERRED)
 {
 	smoke_test_100();
 	T_PASS("Smoke test, sample all");
 }
 
 T_DECL(allocation_sample_half, "Smoke test, sample 1/2",
-		T_META_ENVVAR("MallocProbGuard=1"),
+		T_META_ENVVAR("MallocProbGuard=1"), T_META_TAG_VM_PREFERRED,
 		T_META_ENVVAR("MallocProbGuardSampleRate=2"))
 {
 	smoke_test_100();
@@ -256,7 +249,7 @@
 	malloc_zone_statistics(zone, &stats_after);
 }
 
-T_DECL(introspection_statistics, "Zone statistics")
+T_DECL(introspection_statistics, "Zone statistics", T_META_TAG_VM_PREFERRED)
 {
 	setup_introspection_scenario();
 
@@ -338,36 +331,52 @@
 	free_read_memory();
 }
 
-T_DECL(introspection_enumerate_regions, "Region enumeration")
+T_DECL(introspection_enumerate_regions, "Region enumeration", T_META_TAG_VM_PREFERRED)
 {
 	setup_introspection_scenario();
 
 	check_enumerator(MALLOC_PTR_REGION_RANGE_TYPE);
 }
 
-T_DECL(introspection_enumerate_blocks, "Block enumeration")
+T_DECL(introspection_enumerate_blocks, "Block enumeration", T_META_TAG_VM_PREFERRED)
 {
 	setup_introspection_scenario();
 
 	check_enumerator(MALLOC_PTR_IN_USE_RANGE_TYPE);
 }
 
-T_DECL(wrap_malloc_create_zone, "Wrap malloc_create_zone()")
-{
+T_DECL(wrap_malloc_create_zone, "Wrap malloc_create_zone()", T_META_TAG_VM_PREFERRED)
+{
+	// Make sure we only query the environment during process launch.
+	setenv("MallocProbGuard", "0", /*overwrite=*/true);
+
 	uint32_t num_zones = malloc_num_zones;
-
-	// Make sure we avoid querying the environemnt.
-	setenv("MallocProbGuard", "0", /*overwrite=*/true);
-
 	malloc_zone_t *zone = malloc_create_zone(0, 0);
-#if CONFIG_PGM_WRAP_CUSTOM_ZONES
-	T_EXPECT_EQ_STR(malloc_get_zone_name(zone), "ProbGuardMallocZone", "PGM-wrapped zone");
+
+	T_EXPECT_EQ_STR(malloc_get_zone_name(zone), "ProbGuardMallocZone", "PGM zone");
 	T_EXPECT_EQ(zone->introspect->zone_type, 2, "MALLOC_ZONE_TYPE_PGM");
+
 	T_EXPECT_EQ(malloc_num_zones, num_zones + 2, "registered both zones");
+	T_EXPECT_EQ(malloc_zones[num_zones], zone, "PGM zone is registered");
+	T_EXPECT_EQ(malloc_zones[num_zones + 1], get_wrapped_zone(zone),
+			"Wrapped zone is registered");
 
 	malloc_destroy_zone(zone);
 	T_EXPECT_EQ(malloc_num_zones, num_zones, "unregistered both zones");
-#else
-	T_EXPECT_EQ(malloc_num_zones, num_zones + 1, "no PGM wrapper zone");
-#endif
-}
+}
+
+T_DECL(disable_pgm_on_lite_zone,
+		"The lite zone's helper zone shouldn't be wrapped by PGM")
+{
+	// Enabling MSL Lite will register the lite zone (via malloc_zone_register)
+	// and a helper zone (via malloc_create_zone). The latter needs to not
+	// insert PGM to avoid extra allocations in the underlying zone that don't
+	// have MSL metadata
+	uint32_t num_zones = malloc_num_zones;
+
+	bool enable = msl_turn_on_stack_logging(msl_mode_lite);
+	T_ASSERT_TRUE(enable, "Enabled MSL Lite");
+
+	T_EXPECT_EQ(malloc_num_zones, num_zones + 2,
+			"Enabling MSL generated 2 new zones");
+}