dyld-832.7.1.tar.gz
[apple/dyld.git] / testing / test-cases / lazy-symbol-missing.dtest / runner.cpp
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <dlfcn.h>
5 #include <unistd.h>
6 #include <signal.h>
7 #include <errno.h>
8 #include <mach/mach.h>
9 #include <mach/machine.h>
10 #include <err.h>
11 #include <System/sys/reason.h>
12 #include <System/sys/proc_info.h>
13 #include <System/kern/kern_cdata.h>
14 #include <libproc.h>
15 #include <mach-o/dyld_priv.h>
16
17 #if __arm64e__
18 // arm64e uses chained binds which does not support lazy binding
19 #define SUPPORTS_LAZY_BINDING 0
20 #else
21 #define SUPPORTS_LAZY_BINDING 1
22 #endif
23
24 #include "test_support.h"
25
26 static const char* getUserDescription(struct proc_exitreasoninfo& info) {
27 kcdata_iter_t iter = kcdata_iter((void*)info.eri_kcd_buf, info.eri_reason_buf_size);
28 if ( !kcdata_iter_valid(iter) )
29 return NULL;
30 if ( kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_OS_REASON )
31 return NULL;
32 iter = kcdata_iter_find_type(iter, EXIT_REASON_USER_DESC);
33 if ( !kcdata_iter_valid(iter) ) {
34 return NULL;
35 }
36 return kcdata_iter_string(iter, 0);
37 }
38
39 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
40 #if SUPPORTS_LAZY_BINDING
41 _process process;
42 #ifdef WEAK
43 process.set_executable_path(RUN_DIR "/lazy-symbol-missing-called-weak-lib.exe");
44 #else
45 process.set_executable_path(RUN_DIR "/lazy-symbol-missing-called.exe");
46 #endif
47 const char* env[] = { "TEST_OUTPUT=None", NULL};
48 process.set_env(env);
49 process.set_exit_handler(^(pid_t pid) {
50 LOG("Child exited pid=%d", pid);
51
52 struct proc_exitreasoninfo info;
53 bzero(&info, sizeof(info));
54 uint8_t packReasonData[OS_REASON_BUFFER_MAX_SIZE];
55 bzero(packReasonData, OS_REASON_BUFFER_MAX_SIZE);
56 info.eri_reason_buf_size = OS_REASON_BUFFER_MAX_SIZE;
57 info.eri_kcd_buf = (user_addr_t)packReasonData;
58 LOG("info=%p", &info);
59 int procResult = proc_pidinfo(pid, PROC_PIDEXITREASONINFO, 1, &info, PROC_PIDEXITREASONINFO_SIZE);
60 if ( procResult != sizeof(struct proc_exitreasoninfo) ) {
61 FAIL("bad return size from proc_pidinfo(), %d expected %lu", procResult, PROC_PIDEXITREASONINFO_SIZE);
62 }
63 if ( info.eri_namespace != OS_REASON_DYLD ) {
64 FAIL("eri_namespace (%d) != OS_REASON_DYLD", info.eri_namespace);
65 }
66 const char* userDesc = getUserDescription(info);
67 if ( userDesc != NULL ) {
68 LOG("user desc=%s", (const char*)userDesc);
69 }
70 #ifdef WEAK
71 // Make sure we print a dylib name, not a dependency library # when referencing a symbol from a missing library
72 if ( userDesc != NULL ) {
73 if ( strstr(userDesc, "libbar-missing.dylib") == NULL ) {
74 FAIL("Expected name of missing dylib");
75 }
76 }
77 #endif
78 PASS("Success");
79 });
80 (void)process.launch();
81 #endif
82 PASS("Success");
83 }
84