xref: /xnu-11215/tests/shared_cache_tests.c (revision 8d741a5d)
1a5e72196SApple OSS Distributions #include <darwintest.h>
2a5e72196SApple OSS Distributions #include <darwintest_utils.h>
3a5e72196SApple OSS Distributions #include <mach-o/dyld.h>
4a5e72196SApple OSS Distributions #include <mach-o/dyld_priv.h>
5a5e72196SApple OSS Distributions #include <TargetConditionals.h>
6a5e72196SApple OSS Distributions 
7a5e72196SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.shared_cache"));
8a5e72196SApple OSS Distributions 
9a5e72196SApple OSS Distributions // Give the test up to two minutes because in the failure case we want to invoke update_dyld_shared_cache, which
10a5e72196SApple OSS Distributions // might take a bit to do.
11*8d741a5dSApple OSS Distributions T_DECL(present, "tests that the device is running with a shared cache", T_META_ASROOT(true), T_META_TIMEOUT(120), T_META_TAG_VM_NOT_PREFERRED)
12a5e72196SApple OSS Distributions {
13a5e72196SApple OSS Distributions 	size_t shared_cache_len = 0;
14a5e72196SApple OSS Distributions 	const void *cache_header = _dyld_get_shared_cache_range(&shared_cache_len);
15bb611c8fSApple OSS Distributions 
16bb611c8fSApple OSS Distributions #if TARGET_OS_OSX
17bb611c8fSApple OSS Distributions 	T_SKIP("shared cache testing support incomplete (57267667)");
18bb611c8fSApple OSS Distributions #endif /* TARGET_OS_OSX */
19bb611c8fSApple OSS Distributions 
20a5e72196SApple OSS Distributions 	if ((cache_header == NULL) || (shared_cache_len == 0)) {
21a5e72196SApple OSS Distributions #if TARGET_OS_OSX
22a5e72196SApple OSS Distributions 		char *tmp_dir = (char *) dt_tmpdir();
23a5e72196SApple OSS Distributions 		T_QUIET; T_ASSERT_NOTNULL(tmp_dir, "darwintest created tmp dir");
24a5e72196SApple OSS Distributions 		// Try to invoke update_dyld_shared_cache to gather information on why we're not running with a shared cache
25a5e72196SApple OSS Distributions 		char *shared_cache_update_cmd[] = { "/usr/bin/update_dyld_shared_cache", "-debug", "-cache_dir", tmp_dir, NULL };
26a5e72196SApple OSS Distributions 		pid_t child1 = dt_launch_tool_pipe(shared_cache_update_cmd, false, NULL, ^bool (char *data, __unused size_t data_size, __unused dt_pipe_data_handler_context_t *context) {
27a5e72196SApple OSS Distributions 			T_LOG("%s", data);
28a5e72196SApple OSS Distributions 			return false;
29a5e72196SApple OSS Distributions 		}, ^bool (__unused char *data, __unused size_t data_size, __unused dt_pipe_data_handler_context_t *context) {
30a5e72196SApple OSS Distributions 			T_LOG("%s", data);
31a5e72196SApple OSS Distributions 			return false;
32a5e72196SApple OSS Distributions 		}, BUFFER_PATTERN_LINE, NULL);
33a5e72196SApple OSS Distributions 
34a5e72196SApple OSS Distributions 		int status = 0;
35a5e72196SApple OSS Distributions 		dt_waitpid(child1, &status, NULL, 0);
36a5e72196SApple OSS Distributions 
37a5e72196SApple OSS Distributions 		T_LOG("waitpid for %d returned with status %d", child1, WEXITSTATUS(status));
38a5e72196SApple OSS Distributions #endif // TARGET_OS_OSX
39a5e72196SApple OSS Distributions 		T_ASSERT_NOTNULL(cache_header, "shared cache present");
40a5e72196SApple OSS Distributions 		T_ASSERT_GT((int) shared_cache_len, 0, "shared cache has non-zero length");
41a5e72196SApple OSS Distributions 	}
42a5e72196SApple OSS Distributions 
43a5e72196SApple OSS Distributions 	T_PASS("shared cache appears to be present and valid");
44a5e72196SApple OSS Distributions }
45