#include <stdbool.h> // fprintf(), NULL
#include <stdio.h> // fprintf(), NULL
#include <stdlib.h> // exit(), EXIT_SUCCESS
#include <stdbool.h>
#include <string.h>
#include <mach-o/dyld.h>
#include <mach-o/dyld_priv.h>
#include <dlfcn.h>
#include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
static const char* singleMappedHandler(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
{
if ( strstr(info[0].imageFilePath, "libz") != NULL )
return "can't load libz";
return NULL;
}
int main(int argc, const char* argv[])
{
dyld_register_image_state_change_handler(dyld_image_state_mapped, false, singleMappedHandler);
void* handle = dlopen("libfoo1.dylib", RTLD_LAZY);
if ( handle != NULL ) {
FAIL("image-state-deny-cache-leak: dlopen(libfoo.dylib) should have failed");
return EXIT_SUCCESS;
}
handle = dlopen("libfoo2.dylib", RTLD_LAZY);
if ( handle == NULL ) {
FAIL("image-state-deny-cache-leak: dlopen(libfoo.dylib) should have succeeded");
return EXIT_SUCCESS;
}
dlclose(handle);
char cmd[512];
sprintf(cmd, "sudo leaks %u > /dev/null\n", getpid());
int result = system(cmd);
if ( result == EXIT_SUCCESS )
PASS("image-state-deny-cache-leak");
else
FAIL("image-state-deny-cache-leak");
return EXIT_SUCCESS;
}