9 #include <mach/machine.h>
11 #include <System/sys/reason.h>
12 #include <System/sys/proc_info.h>
13 #include <System/kern/kern_cdata.h>
15 #include <mach-o/dyld_priv.h>
18 // arm64e uses chained binds which does not support lazy binding
19 #define SUPPORTS_LAZY_BINDING 0
21 #define SUPPORTS_LAZY_BINDING 1
25 static bool sSignalCaught
= false;
26 static bool sChildAbortInDyld
= false;
27 static pid_t sChildPid
= 0;
30 static void childDied(int sig
)
33 //printf("sigchld for pid=%d\n", sChildPid);
35 struct proc_exitreasoninfo info
;
36 bzero(&info
, sizeof(info
));
37 uint8_t packReasonData
[OS_REASON_BUFFER_MAX_SIZE
];
38 bzero(packReasonData
, OS_REASON_BUFFER_MAX_SIZE
);
39 info
.eri_reason_buf_size
= OS_REASON_BUFFER_MAX_SIZE
;
40 info
.eri_kcd_buf
= (user_addr_t
)packReasonData
;
41 //fprintf(stderr, "info=%p\n", &info);
42 int procResult
= proc_pidinfo(sChildPid
, PROC_PIDEXITREASONINFO
, 1, &info
, PROC_PIDEXITREASONINFO_SIZE
);
43 if ( procResult
!= sizeof(struct proc_exitreasoninfo
) ) {
44 printf("bad return size from proc_pidinfo(), %d expected %lu\n", procResult
, PROC_PIDEXITREASONINFO_SIZE
);
47 if ( info
.eri_namespace
!= OS_REASON_DYLD
) {
48 printf("eri_namespace (%d) != OS_REASON_DYLD\n", info
.eri_namespace
);
52 sChildAbortInDyld
= true;
56 bool runTest(const char* prog
)
58 sSignalCaught
= false;
59 sChildAbortInDyld
= false;
61 // fork and exec child
64 err(EXIT_FAILURE
, "fork");
65 if ( sChildPid
== 0 ) {
67 char* childArgv
[] = { (char*)prog
, NULL
};
68 int result
= execvp(prog
, childArgv
);
69 err(EXIT_FAILURE
, "exec(\"%s\",...)", prog
);
71 for(int i
=0; i
< 10; ++i
) {
77 return sChildAbortInDyld
;
81 int main(int argc
, const char* argv
[])
83 printf("[BEGIN] lazy-symbol-missing and called\n");
85 #if SUPPORTS_LAZY_BINDING
86 // set up signal handler for catching child terminations
87 signal(SIGCHLD
, childDied
);
89 // test launch program with missing library
90 runTest(RUN_DIR
"/lazy-symbol-missing-called.exe");
92 if ( sSignalCaught
&& sChildAbortInDyld
)
93 printf("[PASS] lazy-symbol-missing and called\n");
95 printf("[FAIL] lazy-symbol-missing and called\n");
97 printf("[PASS] lazy-symbol-missing and called\n");