1.0.47.1: fix longstanding bug in os_get_runtime_executable_path on darwin
[sbcl.git] / src / runtime / darwin-os.c
1 /*
2  * This is the Darwin incarnation of OS-dependent routines. See also
3  * "bsd-os.c".
4  */
5
6 /*
7  * This software is part of the SBCL system. See the README file for
8  * more information.
9  *
10  * This software is derived from the CMU CL system, which was
11  * written at Carnegie Mellon University and released into the
12  * public domain. The software is in the public domain and is
13  * provided with absolutely no warranty. See the COPYING and CREDITS
14  * files for more information.
15  */
16
17 #include "thread.h"
18 #include "sbcl.h"
19 #include "globals.h"
20 #include "runtime.h"
21 #include <signal.h>
22 #include <limits.h>
23 #include <mach-o/dyld.h>
24 #include <errno.h>
25 #include <dlfcn.h>
26
27 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
28 #include <mach/mach.h>
29 #endif
30
31 char *
32 os_get_runtime_executable_path(int external)
33 {
34     char path[PATH_MAX + 1];
35     uint32_t size = sizeof(path);
36
37     if (_NSGetExecutablePath(path, &size) == -1)
38         return NULL;
39
40     return copied_string(path);
41 }
42
43 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
44
45 /* exc_server handles mach exception messages from the kernel and
46  * calls catch exception raise. We use the system-provided
47  * mach_msg_server, which, I assume, calls exc_server in a loop.
48  *
49  */
50 extern boolean_t exc_server();
51
52 void *
53 mach_exception_handler(void *port)
54 {
55   mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
56   /* mach_msg_server should never return, but it should dispatch mach
57    * exceptions to our catch_exception_raise function
58    */
59   lose("mach_msg_server returned");
60 }
61
62 /* Sets up the thread that will listen for mach exceptions. note that
63    the exception handlers will be run on this thread. This is
64    different from the BSD-style signal handling situation in which the
65    signal handlers run in the relevant thread directly. */
66
67 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
68 mach_port_t current_mach_task = MACH_PORT_NULL;
69
70 pthread_t
71 setup_mach_exception_handling_thread()
72 {
73     kern_return_t ret;
74     pthread_t mach_exception_handling_thread = NULL;
75     pthread_attr_t attr;
76
77     current_mach_task = mach_task_self();
78
79     /* allocate a mach_port for this process */
80     ret = mach_port_allocate(current_mach_task,
81                              MACH_PORT_RIGHT_PORT_SET,
82                              &mach_exception_handler_port_set);
83
84     /* create the thread that will receive the mach exceptions */
85
86     FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
87
88     pthread_attr_init(&attr);
89     pthread_create(&mach_exception_handling_thread,
90                    &attr,
91                    mach_exception_handler,
92                    (void*) mach_exception_handler_port_set);
93     pthread_attr_destroy(&attr);
94
95     return mach_exception_handling_thread;
96 }
97
98 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
99    exception port (which is being listened to do by the mach
100    exception handling thread). */
101 kern_return_t
102 mach_thread_init(mach_port_t thread_exception_port)
103 {
104     kern_return_t ret;
105     mach_port_t current_mach_thread;
106
107     /* allocate a named port for the thread */
108     FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
109     ret = mach_port_allocate_name(current_mach_task,
110                                   MACH_PORT_RIGHT_RECEIVE,
111                                   thread_exception_port);
112     if (ret) {
113         lose("mach_port_allocate_name failed with return_code %d\n", ret);
114     }
115
116     /* establish the right for the thread_exception_port to send messages */
117     ret = mach_port_insert_right(current_mach_task,
118                                  thread_exception_port,
119                                  thread_exception_port,
120                                  MACH_MSG_TYPE_MAKE_SEND);
121     if (ret) {
122         lose("mach_port_insert_right failed with return_code %d\n", ret);
123     }
124
125     current_mach_thread = mach_thread_self();
126     ret = thread_set_exception_ports(current_mach_thread,
127                                      EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
128                                      thread_exception_port,
129                                      EXCEPTION_DEFAULT,
130                                      THREAD_STATE_NONE);
131     if (ret) {
132         lose("thread_set_exception_ports failed with return_code %d\n", ret);
133     }
134
135     ret = mach_port_deallocate (current_mach_task, current_mach_thread);
136     if (ret) {
137         lose("mach_port_deallocate failed with return_code %d\n", ret);
138     }
139
140     ret = mach_port_move_member(current_mach_task,
141                                 thread_exception_port,
142                                 mach_exception_handler_port_set);
143     if (ret) {
144         lose("mach_port_move_member failed with return_code %d\n", ret);
145     }
146
147     return ret;
148 }
149
150 void
151 setup_mach_exceptions() {
152     setup_mach_exception_handling_thread();
153     mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
154 }
155
156 pid_t
157 mach_fork() {
158     pid_t pid = fork();
159     if (pid == 0) {
160         setup_mach_exceptions();
161         return pid;
162     } else {
163         return pid;
164     }
165 }
166
167 #endif
168