malloc_claimed_address_tests.c [plain text]
#include <darwintest.h>
#include <stdlib.h>
#include <stdio.h>
#include <mach/mach.h>
#include <mach/mach_vm.h>
#include <malloc.h>
#include <malloc/malloc.h>
#include <malloc_private.h>
#include <sys/mman.h>
T_DECL(malloc_claimed_address_default_zone_test,
"Tests for malloc_claimed_address, default zone only",
T_META_ENVVAR("MallocNanoZone=0"))
{
boolean_t result = malloc_claimed_address(NULL);
T_EXPECT_FALSE(result, "NULL is never a valid pointer");
void *ptr = malloc(16);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from tiny");
result = malloc_claimed_address(ptr + 8);
T_EXPECT_TRUE(result, "allocation from tiny with offset");
free(ptr);
ptr = malloc(2048);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from small");
result = malloc_claimed_address(ptr + 1000);
T_EXPECT_TRUE(result, "allocation from small with offset");
free(ptr);
ptr = malloc(140000);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from large");
result = malloc_claimed_address(ptr + 1000);
T_EXPECT_TRUE(result, "allocation from large with offset");
free(ptr);
mach_vm_address_t addr;
kern_return_t kr = mach_vm_allocate(mach_task_self(), &addr, 1024, VM_FLAGS_ANYWHERE);
T_ASSERT_TRUE(kr == KERN_SUCCESS, "allocate vm space");
result = malloc_claimed_address((void *)addr);
T_EXPECT_FALSE(result, "address in VM allocated memory");
mach_vm_deallocate(mach_task_self(), addr, 1024);
}
T_DECL(malloc_zone_claimed_address_test,
"Tests for malloc_zone_claimed_address",
T_META_ENVVAR("MallocNanoZone=0"))
{
malloc_zone_t *zone = malloc_create_zone(0, 0);
boolean_t result = malloc_zone_claimed_address(zone, NULL);
T_EXPECT_FALSE(result, "NULL is never a valid pointer");
void *ptr = malloc_zone_malloc(zone, 16);
result = malloc_zone_claimed_address(zone, ptr);
T_EXPECT_TRUE(result, "allocation from tiny");
result = malloc_zone_claimed_address(zone, ptr + 8);
T_EXPECT_TRUE(result, "allocation from tiny with offset");
free(ptr);
ptr = malloc(16);
result = malloc_zone_claimed_address(zone, ptr);
T_EXPECT_FALSE(result, "allocation from tiny in default zone");
result = malloc_zone_claimed_address(zone, ptr + 8);
T_EXPECT_FALSE(result, "allocation from tiny in default zone with offset");
free(ptr);
ptr = malloc_zone_malloc(zone, 2048);
result = malloc_zone_claimed_address(zone, ptr);
T_EXPECT_TRUE(result, "allocation from small");
result = malloc_zone_claimed_address(zone, ptr + 1000);
T_EXPECT_TRUE(result, "allocation from small with offset");
free(ptr);
ptr = malloc(2048);
result = malloc_zone_claimed_address(zone, ptr);
T_EXPECT_FALSE(result, "allocation from small in default zone");
result = malloc_zone_claimed_address(zone, ptr + 8);
T_EXPECT_FALSE(result, "allocation from small in default zone with offset");
free(ptr);
ptr = malloc_zone_malloc(zone, 140000);
result = malloc_zone_claimed_address(zone, ptr);
T_EXPECT_TRUE(result, "allocation from large");
result = malloc_zone_claimed_address(zone, ptr + 1000);
T_EXPECT_TRUE(result, "allocation from large with offset");
free(ptr);
ptr = malloc(140000);
result = malloc_zone_claimed_address(zone, ptr);
T_EXPECT_FALSE(result, "allocation from large in default zone");
result = malloc_zone_claimed_address(zone, ptr + 8);
T_EXPECT_FALSE(result, "allocation from large in default zone with offset");
free(ptr);
mach_vm_address_t addr;
kern_return_t kr = mach_vm_allocate(mach_task_self(), &addr, 1024, VM_FLAGS_ANYWHERE);
T_ASSERT_TRUE(kr == KERN_SUCCESS, "allocate vm space");
result = malloc_zone_claimed_address(zone, (void *)addr);
T_EXPECT_FALSE(result, "address in VM allocated memory");
mach_vm_deallocate(mach_task_self(), addr, 1024);
malloc_destroy_zone(zone);
}
T_DECL(malloc_claimed_address_zone_test,
"Tests for malloc_claimed_address with another zone",
T_META_ENVVAR("MallocNanoZone=0"))
{
malloc_zone_t *zone = malloc_create_zone(0, 0);
void *ptr = malloc_zone_malloc(zone, 16);
boolean_t result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from tiny");
result = malloc_claimed_address(ptr + 8);
T_EXPECT_TRUE(result, "allocation from tiny with offset");
free(ptr);
ptr = malloc_zone_malloc(zone, 2048);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from small");
result = malloc_claimed_address(ptr + 1000);
T_EXPECT_TRUE(result, "allocation from small with offset");
free(ptr);
ptr = malloc_zone_malloc(zone, 140000);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from large");
result = malloc_claimed_address(ptr + 1000);
T_EXPECT_TRUE(result, "allocation from large with offset");
free(ptr);
malloc_destroy_zone(zone);
}
T_DECL(malloc_claimed_address_nanozone_test,
"Tests for malloc_claimed_address with nano",
T_META_ENVVAR("MallocNanoZone=1"))
{
boolean_t result = malloc_claimed_address(NULL);
T_EXPECT_FALSE(result, "NULL is never a valid pointer");
for (size_t sz = 16; sz <= 256; sz += 16) {
void *ptr = malloc(sz);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "nano allocation size %d", (int)sz);
result = malloc_claimed_address(ptr + sz/2);
T_EXPECT_TRUE(result, "nano allocation size %d offset %d", (int)sz, (int)sz/2);
free(ptr);
}
mach_vm_address_t addr;
kern_return_t kr = mach_vm_allocate(mach_task_self(), &addr, 1024, VM_FLAGS_ANYWHERE);
T_ASSERT_TRUE(kr == KERN_SUCCESS, "allocate vm space");
result = malloc_claimed_address((void *)addr);
T_EXPECT_FALSE(result, "address in VM allocated memory");
mach_vm_deallocate(mach_task_self(), addr, 1024);
}
T_DECL(malloc_claimed_address_custom_zone_test,
"Tests for malloc_claimed_address in a zone that does not implement it",
T_META_ENVVAR("MallocNanoZone=0"))
{
malloc_zone_t *zone = malloc_create_zone(0, 0);
mprotect(zone, sizeof(*zone), PROT_READ | PROT_WRITE);
zone->version = 9;
zone->claimed_address = NULL;
mprotect(zone, sizeof(*zone), PROT_READ);
boolean_t result = malloc_zone_claimed_address(zone, NULL);
T_EXPECT_FALSE(result, "NULL is never a valid pointer");
void *ptr = malloc_zone_malloc(zone, 16);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from tiny");
result = malloc_claimed_address(ptr + 8);
T_EXPECT_TRUE(result, "allocation from tiny with offset");
free(ptr);
ptr = malloc_zone_malloc(zone, 2048);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from small");
result = malloc_claimed_address(ptr + 1000);
T_EXPECT_TRUE(result, "allocation from small with offset");
free(ptr);
ptr = malloc_zone_malloc(zone, 140000);
result = malloc_claimed_address(ptr);
T_EXPECT_TRUE(result, "allocation from large");
result = malloc_claimed_address(ptr + 1000);
T_EXPECT_TRUE(result, "allocation from large with offset");
free(ptr);
malloc_destroy_zone(zone);
}