@openziti/ziti-sdk-nodejs 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/.github/workflows/build.yml +220 -0
  2. package/.github/workflows/codeql-analysis.yml +71 -0
  3. package/.github/workflows/mattermost-ziti-webhook.yml +26 -0
  4. package/.gitmodules +4 -0
  5. package/.travis.yml-obsolete +99 -0
  6. package/CODE_OF_CONDUCT.md +17 -0
  7. package/CONTRIBUTING.md +6 -0
  8. package/LICENSE +201 -0
  9. package/README.md +155 -0
  10. package/appveyor.yml-obsolete +32 -0
  11. package/binding.gyp +227 -0
  12. package/lib/index.js +17 -0
  13. package/lib/ziti.js +40 -0
  14. package/package.json +56 -0
  15. package/scripts/build-appveyor.bat +198 -0
  16. package/scripts/install_node.sh +99 -0
  17. package/scripts/validate_tag.sh +24 -0
  18. package/src/Ziti_https_request.c +960 -0
  19. package/src/Ziti_https_request_data.c +250 -0
  20. package/src/Ziti_https_request_end.c +79 -0
  21. package/src/stack_traces.c +334 -0
  22. package/src/utils.c +108 -0
  23. package/src/utils.h +85 -0
  24. package/src/ziti-add-on.c +88 -0
  25. package/src/ziti-nodejs.h +209 -0
  26. package/src/ziti_close.c +79 -0
  27. package/src/ziti_dial.c +375 -0
  28. package/src/ziti_enroll.c +245 -0
  29. package/src/ziti_hello.c +52 -0
  30. package/src/ziti_init.c +315 -0
  31. package/src/ziti_service_available.c +222 -0
  32. package/src/ziti_shutdown.c +47 -0
  33. package/src/ziti_websocket_connect.c +458 -0
  34. package/src/ziti_websocket_write.c +235 -0
  35. package/src/ziti_write.c +223 -0
  36. package/tests/enroll-test.js +38 -0
  37. package/tests/hello.js +12 -0
  38. package/tests/https-test.js +206 -0
  39. package/tests/mattermost-test.js +124 -0
  40. package/tests/websocket-test.js +119 -0
  41. package/ziti.js +1 -0
  42. package/ziti.png +0 -0
@@ -0,0 +1,250 @@
1
+ /*
2
+ Copyright 2019-2020 Netfoundry, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ #include "ziti-nodejs.h"
18
+ #include <ziti/ziti_src.h>
19
+
20
+
21
+ /**
22
+ * This function is responsible for calling the JavaScript on_resp_data callback function
23
+ * that was specified when the Ziti_https_request_data(...) was called from JavaScript.
24
+ */
25
+ static void CallJs_on_req_body(napi_env env, napi_value js_cb, void* context, void* data) {
26
+
27
+ ZITI_NODEJS_LOG(DEBUG, "entered");
28
+
29
+ // This parameter is not used.
30
+ (void) context;
31
+
32
+ // Retrieve the HttpsRespBodyItem created by the worker thread.
33
+ HttpsReqBodyItem* item = (HttpsReqBodyItem*)data;
34
+
35
+ // env and js_cb may both be NULL if Node.js is in its cleanup phase, and
36
+ // items are left over from earlier thread-safe calls from the worker thread.
37
+ // When env is NULL, we simply skip over the call into Javascript
38
+ if (env != NULL) {
39
+
40
+ napi_value undefined;
41
+
42
+ // Retrieve the JavaScript `undefined` value so we can use it as the `this`
43
+ // value of the JavaScript function call.
44
+ napi_get_undefined(env, &undefined);
45
+
46
+ // const obj = {}
47
+ napi_value js_http_item, js_req, js_status, js_body;
48
+ int rc = napi_create_object(env, &js_http_item);
49
+ if (rc != napi_ok) {
50
+ napi_throw_error(env, "EINVAL", "failure to create object");
51
+ }
52
+
53
+ // obj.req = req
54
+ napi_create_int64(env, (int64_t)item->req, &js_req);
55
+ if (rc != napi_ok) {
56
+ napi_throw_error(env, "EINVAL", "failure to create resp.req");
57
+ }
58
+ rc = napi_set_named_property(env, js_http_item, "req", js_req);
59
+ if (rc != napi_ok) {
60
+ napi_throw_error(env, "EINVAL", "failure to set named property req");
61
+ }
62
+ ZITI_NODEJS_LOG(DEBUG, "js_req: %p", item->req);
63
+
64
+ // obj.code = status
65
+ rc = napi_create_int32(env, item->status, &js_status);
66
+ if (rc != napi_ok) {
67
+ napi_throw_error(env, "EINVAL", "failure to create resp.status");
68
+ }
69
+ rc = napi_set_named_property(env, js_http_item, "status", js_status);
70
+ if (rc != napi_ok) {
71
+ napi_throw_error(env, "EINVAL", "failure to set named property status");
72
+ }
73
+ ZITI_NODEJS_LOG(DEBUG, "status: %zd", item->status);
74
+
75
+ // obj.body = body
76
+ rc = napi_create_int32(env, (int32_t)item->body, &js_body);
77
+ if (rc != napi_ok) {
78
+ napi_throw_error(env, "EINVAL", "failure to create resp.body");
79
+ }
80
+ rc = napi_set_named_property(env, js_http_item, "body", js_body);
81
+ if (rc != napi_ok) {
82
+ napi_throw_error(env, "EINVAL", "failure to set named property body");
83
+ }
84
+
85
+ // Call the JavaScript function and pass it the HttpsRespItem
86
+ rc = napi_call_function(
87
+ env,
88
+ undefined,
89
+ js_cb,
90
+ 1,
91
+ &js_http_item,
92
+ NULL
93
+ );
94
+ if (rc != napi_ok) {
95
+ napi_throw_error(env, "EINVAL", "failure to invoke JS callback");
96
+ }
97
+
98
+ }
99
+ }
100
+
101
+
102
+
103
+ /**
104
+ *
105
+ */
106
+ void on_req_body(um_http_req_t *req, const char *body, ssize_t status) {
107
+
108
+ ZITI_NODEJS_LOG(DEBUG, "status: %zd, body: %p", status, body);
109
+
110
+ HttpsAddonData* addon_data = (HttpsAddonData*) req->data;
111
+ ZITI_NODEJS_LOG(DEBUG, "addon_data is: %p", addon_data);
112
+
113
+ HttpsReqBodyItem* item = calloc(1, sizeof(*item));
114
+ ZITI_NODEJS_LOG(DEBUG, "new HttpsReqBodyItem is: %p", item);
115
+
116
+ // Grab everything off the um_http_resp_t that we need to eventually pass on to the JS on_resp_body callback.
117
+ // If we wait until CallJs_on_resp_body is invoked to do that work, the um_http_resp_t may have already been free'd by the C-SDK
118
+
119
+ item->req = req;
120
+ item->body = (void*)body;
121
+ item->status = status;
122
+
123
+ ZITI_NODEJS_LOG(DEBUG, "calling tsfn_on_req_body: %p", addon_data->tsfn_on_req_body);
124
+
125
+ // Initiate the call into the JavaScript callback.
126
+ int rc = napi_call_threadsafe_function(
127
+ addon_data->tsfn_on_req_body,
128
+ item,
129
+ napi_tsfn_blocking);
130
+ if (rc != napi_ok) {
131
+ napi_throw_error(addon_data->env, "EINVAL", "failure to invoke JS callback");
132
+ }
133
+ }
134
+
135
+
136
+
137
+ /**
138
+ * Send Body data over active HTTPS request
139
+ *
140
+ * @param {string} [0] req
141
+ * @param {string} [1] data (we expect a Buffer)
142
+ * @param {func} [2] JS on_write callback; This is invoked from 'on_req_body' function above
143
+ *
144
+ * @returns NULL
145
+ */
146
+ napi_value _Ziti_http_request_data(napi_env env, const napi_callback_info info) {
147
+ napi_status status;
148
+ int rc;
149
+ size_t argc = 3;
150
+ napi_value args[3];
151
+ status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
152
+ if (status != napi_ok) {
153
+ napi_throw_error(env, NULL, "Failed to parse arguments");
154
+ }
155
+
156
+ if (argc < 3) {
157
+ napi_throw_error(env, "EINVAL", "Too few arguments");
158
+ return NULL;
159
+ }
160
+
161
+ // Obtain um_http_req_t
162
+ int64_t js_req;
163
+ status = napi_get_value_int64(env, args[0], &js_req);
164
+ if (status != napi_ok) {
165
+ napi_throw_error(env, NULL, "Failed to get Req");
166
+ }
167
+ // um_http_req_t *r = (um_http_req_t*)js_req;
168
+ HttpsReq* httpsReq = (HttpsReq*)js_req;
169
+ um_http_req_t *r = httpsReq->req;
170
+
171
+ ZITI_NODEJS_LOG(DEBUG, "req: %p", r);
172
+
173
+ HttpsAddonData* addon_data = httpsReq->addon_data;
174
+ ZITI_NODEJS_LOG(DEBUG, "addon_data is: %p", addon_data);
175
+
176
+ // If some kind of Ziti error previously occured on this request, then short-circuit now
177
+ if (httpsReq->on_resp_has_fired && (httpsReq->respCode < 0)) {
178
+ ZITI_NODEJS_LOG(DEBUG, "aborting due to previous error: %d", httpsReq->respCode);
179
+ return NULL;
180
+ }
181
+
182
+ // Obtain data to write (we expect a Buffer)
183
+ void* buffer;
184
+ size_t bufferLength;
185
+ status = napi_get_buffer_info(env, args[1], &buffer, &bufferLength);
186
+ if (status != napi_ok) {
187
+ napi_throw_error(env, NULL, "Failed to get Buffer info");
188
+ }
189
+ ZITI_NODEJS_LOG(DEBUG, "bufferLength: %zd", bufferLength);
190
+
191
+ // Since the underlying Buffer's lifetime is not guaranteed if it's managed by the VM, we will copy the chunk into our heap
192
+ void* chunk = calloc(1, bufferLength + 1);
193
+ memcpy(chunk, buffer, bufferLength);
194
+
195
+ // Obtain ptr to JS 'on_write' callback function
196
+ napi_value js_write_cb = args[2];
197
+ napi_value work_name;
198
+
199
+ // Create a string to describe this asynchronous operation.
200
+ status = napi_create_string_utf8(
201
+ env,
202
+ "N-API on_write",
203
+ NAPI_AUTO_LENGTH,
204
+ &work_name);
205
+ if (status != napi_ok) {
206
+ napi_throw_error(env, NULL, "Failed to napi_create_string_utf8");
207
+ }
208
+
209
+ // Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
210
+ // which we can call from a worker thread.
211
+ rc = napi_create_threadsafe_function(
212
+ env,
213
+ js_write_cb,
214
+ NULL,
215
+ work_name,
216
+ 0,
217
+ 1,
218
+ NULL,
219
+ NULL,
220
+ NULL,
221
+ CallJs_on_req_body,
222
+ &(addon_data->tsfn_on_req_body)
223
+ );
224
+ if (rc != napi_ok) {
225
+ napi_throw_error(env, "EINVAL", "Failed to create threadsafe_function");
226
+ }
227
+ ZITI_NODEJS_LOG(DEBUG, "napi_create_threadsafe_function addon_data->tsfn_on_req_body() : %p", addon_data->tsfn_on_req_body);
228
+
229
+ // Now, call the C-SDK to actually write the data over to the service
230
+ um_http_req_data(r, chunk, bufferLength, on_req_body );
231
+
232
+ return NULL;
233
+ }
234
+
235
+
236
+ void expose_ziti_https_request_data(napi_env env, napi_value exports) {
237
+ napi_status status;
238
+ napi_value fn;
239
+
240
+ status = napi_create_function(env, NULL, 0, _Ziti_http_request_data, NULL, &fn);
241
+ if (status != napi_ok) {
242
+ napi_throw_error(env, NULL, "Unable to wrap native function '_Ziti_http_request_data");
243
+ }
244
+
245
+ status = napi_set_named_property(env, exports, "Ziti_http_request_data", fn);
246
+ if (status != napi_ok) {
247
+ napi_throw_error(env, NULL, "Unable to populate exports for 'Ziti_http_request_data");
248
+ }
249
+
250
+ }
@@ -0,0 +1,79 @@
1
+ /*
2
+ Copyright 2019-2020 Netfoundry, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ #include "ziti-nodejs.h"
18
+ #include <ziti/ziti_src.h>
19
+
20
+
21
+ /**
22
+ * Indicate that an active HTTPS request is now complete
23
+ *
24
+ * @param {string} [0] req
25
+ *
26
+ * @returns NULL
27
+ */
28
+ napi_value _Ziti_http_request_end(napi_env env, const napi_callback_info info) {
29
+ napi_status status;
30
+ size_t argc = 1;
31
+ napi_value args[1];
32
+ status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
33
+ if (status != napi_ok) {
34
+ napi_throw_error(env, NULL, "Failed to parse arguments");
35
+ }
36
+
37
+ if (argc != 1) {
38
+ napi_throw_error(env, "EINVAL", "Invalid argument count");
39
+ return NULL;
40
+ }
41
+
42
+ // Obtain um_http_req_t
43
+ int64_t js_req;
44
+ status = napi_get_value_int64(env, args[0], &js_req);
45
+ if (status != napi_ok) {
46
+ napi_throw_error(env, NULL, "Failed to get Req");
47
+ }
48
+ HttpsReq* httpsReq = (HttpsReq*)js_req;
49
+ um_http_req_t *r = httpsReq->req;
50
+
51
+ ZITI_NODEJS_LOG(DEBUG, "req: %p", r);
52
+
53
+ // TEMP hack to work around an issue still being debugged
54
+ ZITI_NODEJS_LOG(DEBUG, "httpsReq->on_resp_has_fired: %o", httpsReq->on_resp_has_fired);
55
+ if (httpsReq->on_resp_has_fired) {
56
+ ZITI_NODEJS_LOG(DEBUG, "seems as though on_resp has previously fired... skipping call to um_http_req_end");
57
+ } else {
58
+ um_http_req_end(r);
59
+ }
60
+
61
+ return NULL;
62
+ }
63
+
64
+
65
+ void expose_ziti_https_request_end(napi_env env, napi_value exports) {
66
+ napi_status status;
67
+ napi_value fn;
68
+
69
+ status = napi_create_function(env, NULL, 0, _Ziti_http_request_end, NULL, &fn);
70
+ if (status != napi_ok) {
71
+ napi_throw_error(env, NULL, "Unable to wrap native function '_Ziti_http_request_end");
72
+ }
73
+
74
+ status = napi_set_named_property(env, exports, "Ziti_http_request_end", fn);
75
+ if (status != napi_ok) {
76
+ napi_throw_error(env, NULL, "Unable to populate exports for 'Ziti_http_request_end");
77
+ }
78
+
79
+ }
@@ -0,0 +1,334 @@
1
+ /*
2
+ Copyright 2019-2020 Netfoundry, Inc.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ /* compile with:
18
+ on linux: gcc -g stack_traces.c
19
+ on OS X: gcc -g -fno-pie stack_traces.c
20
+ on windows: gcc -g stack_traces.c -limagehlp
21
+ */
22
+
23
+ #include <signal.h>
24
+ #include <stdio.h>
25
+ #include <assert.h>
26
+ #include <stdlib.h>
27
+ #include <stdbool.h>
28
+ #include <stdint.h>
29
+ #include <stdbool.h>
30
+ #include <errno.h>
31
+ // #include <pthread.h>
32
+ #include <node_api.h>
33
+
34
+ #ifdef _WIN32
35
+ #include <windows.h>
36
+ #include <imagehlp.h>
37
+ #else
38
+ #include <err.h>
39
+ #include <execinfo.h>
40
+ #endif
41
+
42
+ #include <ziti/ziti.h>
43
+
44
+ static char const * icky_global_program_name = "ziti-nodejs";
45
+
46
+
47
+ /**
48
+ * Resolve symbol name and source location given the path to the executable and an address
49
+ */
50
+ int addr2line(char const * const program_name, void const * const addr)
51
+ {
52
+ char addr2line_cmd[512] = {0};
53
+
54
+ /* have addr2line map the address to the relent line in the code */
55
+ #ifdef __APPLE__
56
+ /* apple does things differently... */
57
+ sprintf(addr2line_cmd,"atos -o %.256s %p", program_name, addr);
58
+ #else
59
+ sprintf(addr2line_cmd,"addr2line -f -p -e %.256s %p", program_name, addr);
60
+ #endif
61
+
62
+ return system(addr2line_cmd);
63
+ }
64
+
65
+ #ifdef _WIN32
66
+ void windows_print_stacktrace(CONTEXT* context)
67
+ {
68
+ SymInitialize(GetCurrentProcess(), 0, true);
69
+
70
+ STACKFRAME frame = { 0 };
71
+
72
+ /* setup initial stack frame */
73
+ frame.AddrPC.Offset = context->Eip;
74
+ frame.AddrPC.Mode = AddrModeFlat;
75
+ frame.AddrStack.Offset = context->Esp;
76
+ frame.AddrStack.Mode = AddrModeFlat;
77
+ frame.AddrFrame.Offset = context->Ebp;
78
+ frame.AddrFrame.Mode = AddrModeFlat;
79
+
80
+ while (StackWalk(IMAGE_FILE_MACHINE_I386 ,
81
+ GetCurrentProcess(),
82
+ GetCurrentThread(),
83
+ &frame,
84
+ context,
85
+ 0,
86
+ SymFunctionTableAccess,
87
+ SymGetModuleBase,
88
+ 0 ) )
89
+ {
90
+ addr2line(icky_global_program_name, (void*)frame.AddrPC.Offset);
91
+ }
92
+
93
+ SymCleanup( GetCurrentProcess() );
94
+ }
95
+
96
+ LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS * ExceptionInfo)
97
+ {
98
+ switch(ExceptionInfo->ExceptionRecord->ExceptionCode)
99
+ {
100
+ case EXCEPTION_ACCESS_VIOLATION:
101
+ fputs("Error: EXCEPTION_ACCESS_VIOLATION\n", stderr);
102
+ break;
103
+ case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
104
+ fputs("Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED\n", stderr);
105
+ break;
106
+ case EXCEPTION_BREAKPOINT:
107
+ fputs("Error: EXCEPTION_BREAKPOINT\n", stderr);
108
+ break;
109
+ case EXCEPTION_DATATYPE_MISALIGNMENT:
110
+ fputs("Error: EXCEPTION_DATATYPE_MISALIGNMENT\n", stderr);
111
+ break;
112
+ case EXCEPTION_FLT_DENORMAL_OPERAND:
113
+ fputs("Error: EXCEPTION_FLT_DENORMAL_OPERAND\n", stderr);
114
+ break;
115
+ case EXCEPTION_FLT_DIVIDE_BY_ZERO:
116
+ fputs("Error: EXCEPTION_FLT_DIVIDE_BY_ZERO\n", stderr);
117
+ break;
118
+ case EXCEPTION_FLT_INEXACT_RESULT:
119
+ fputs("Error: EXCEPTION_FLT_INEXACT_RESULT\n", stderr);
120
+ break;
121
+ case EXCEPTION_FLT_INVALID_OPERATION:
122
+ fputs("Error: EXCEPTION_FLT_INVALID_OPERATION\n", stderr);
123
+ break;
124
+ case EXCEPTION_FLT_OVERFLOW:
125
+ fputs("Error: EXCEPTION_FLT_OVERFLOW\n", stderr);
126
+ break;
127
+ case EXCEPTION_FLT_STACK_CHECK:
128
+ fputs("Error: EXCEPTION_FLT_STACK_CHECK\n", stderr);
129
+ break;
130
+ case EXCEPTION_FLT_UNDERFLOW:
131
+ fputs("Error: EXCEPTION_FLT_UNDERFLOW\n", stderr);
132
+ break;
133
+ case EXCEPTION_ILLEGAL_INSTRUCTION:
134
+ fputs("Error: EXCEPTION_ILLEGAL_INSTRUCTION\n", stderr);
135
+ break;
136
+ case EXCEPTION_IN_PAGE_ERROR:
137
+ fputs("Error: EXCEPTION_IN_PAGE_ERROR\n", stderr);
138
+ break;
139
+ case EXCEPTION_INT_DIVIDE_BY_ZERO:
140
+ fputs("Error: EXCEPTION_INT_DIVIDE_BY_ZERO\n", stderr);
141
+ break;
142
+ case EXCEPTION_INT_OVERFLOW:
143
+ fputs("Error: EXCEPTION_INT_OVERFLOW\n", stderr);
144
+ break;
145
+ case EXCEPTION_INVALID_DISPOSITION:
146
+ fputs("Error: EXCEPTION_INVALID_DISPOSITION\n", stderr);
147
+ break;
148
+ case EXCEPTION_NONCONTINUABLE_EXCEPTION:
149
+ fputs("Error: EXCEPTION_NONCONTINUABLE_EXCEPTION\n", stderr);
150
+ break;
151
+ case EXCEPTION_PRIV_INSTRUCTION:
152
+ fputs("Error: EXCEPTION_PRIV_INSTRUCTION\n", stderr);
153
+ break;
154
+ case EXCEPTION_SINGLE_STEP:
155
+ fputs("Error: EXCEPTION_SINGLE_STEP\n", stderr);
156
+ break;
157
+ case EXCEPTION_STACK_OVERFLOW:
158
+ fputs("Error: EXCEPTION_STACK_OVERFLOW\n", stderr);
159
+ break;
160
+ default:
161
+ fputs("Error: Unrecognized Exception\n", stderr);
162
+ break;
163
+ }
164
+ fflush(stderr);
165
+ /* If this is a stack overflow then we can't walk the stack, so just show
166
+ where the error happened */
167
+ if (EXCEPTION_STACK_OVERFLOW != ExceptionInfo->ExceptionRecord->ExceptionCode)
168
+ {
169
+ windows_print_stacktrace(ExceptionInfo->ContextRecord);
170
+ }
171
+ else
172
+ {
173
+ addr2line(icky_global_program_name, (void*)ExceptionInfo->ContextRecord->Eip);
174
+ }
175
+
176
+ return EXCEPTION_EXECUTE_HANDLER;
177
+ }
178
+
179
+ void set_signal_handler()
180
+ {
181
+ SetUnhandledExceptionFilter(windows_exception_handler);
182
+ }
183
+ #else
184
+
185
+ #define MAX_STACK_FRAMES 64
186
+ static void *stack_traces[MAX_STACK_FRAMES];
187
+ void posix_print_stack_trace()
188
+ {
189
+ int i, trace_size = 0;
190
+ char **messages = (char **)NULL;
191
+
192
+ trace_size = backtrace(stack_traces, MAX_STACK_FRAMES);
193
+ messages = backtrace_symbols(stack_traces, trace_size);
194
+
195
+ /* skip the first couple stack frames (as they are this function and
196
+ our handler) and also skip the last frame as it's (always?) junk. */
197
+ for (i = 3; i < (trace_size - 1); ++i)
198
+ // for (i = 4; i < (trace_size - 1); ++i)
199
+ // for (i = 0; i < trace_size; ++i) // we'll use this for now so you can see what's going on
200
+ {
201
+ if (addr2line(icky_global_program_name, stack_traces[i]) != 0)
202
+ {
203
+ printf(" error determining line # for: %s\n", messages[i]);
204
+ }
205
+
206
+ }
207
+ if (messages) { free(messages); }
208
+ }
209
+
210
+ void posix_signal_handler(int sig, siginfo_t *siginfo, void *context)
211
+ {
212
+ (void)context;
213
+ switch(sig)
214
+ {
215
+ case SIGSEGV:
216
+ fputs("Caught SIGSEGV: Segmentation Fault\n", stderr);
217
+ break;
218
+ case SIGINT:
219
+ fputs("Caught SIGINT: Interactive attention signal, (usually ctrl+c)\n", stderr);
220
+ break;
221
+ case SIGFPE:
222
+ switch(siginfo->si_code)
223
+ {
224
+ case FPE_INTDIV:
225
+ fputs("Caught SIGFPE: (integer divide by zero)\n", stderr);
226
+ break;
227
+ case FPE_INTOVF:
228
+ fputs("Caught SIGFPE: (integer overflow)\n", stderr);
229
+ break;
230
+ case FPE_FLTDIV:
231
+ fputs("Caught SIGFPE: (floating-point divide by zero)\n", stderr);
232
+ break;
233
+ case FPE_FLTOVF:
234
+ fputs("Caught SIGFPE: (floating-point overflow)\n", stderr);
235
+ break;
236
+ case FPE_FLTUND:
237
+ fputs("Caught SIGFPE: (floating-point underflow)\n", stderr);
238
+ break;
239
+ case FPE_FLTRES:
240
+ fputs("Caught SIGFPE: (floating-point inexact result)\n", stderr);
241
+ break;
242
+ case FPE_FLTINV:
243
+ fputs("Caught SIGFPE: (floating-point invalid operation)\n", stderr);
244
+ break;
245
+ case FPE_FLTSUB:
246
+ fputs("Caught SIGFPE: (subscript out of range)\n", stderr);
247
+ break;
248
+ default:
249
+ fputs("Caught SIGFPE: Arithmetic Exception\n", stderr);
250
+ break;
251
+ }
252
+ case SIGILL:
253
+ switch(siginfo->si_code)
254
+ {
255
+ case ILL_ILLOPC:
256
+ fputs("Caught SIGILL: (illegal opcode)\n", stderr);
257
+ break;
258
+ case ILL_ILLOPN:
259
+ fputs("Caught SIGILL: (illegal operand)\n", stderr);
260
+ break;
261
+ case ILL_ILLADR:
262
+ fputs("Caught SIGILL: (illegal addressing mode)\n", stderr);
263
+ break;
264
+ case ILL_ILLTRP:
265
+ fputs("Caught SIGILL: (illegal trap)\n", stderr);
266
+ break;
267
+ case ILL_PRVOPC:
268
+ fputs("Caught SIGILL: (privileged opcode)\n", stderr);
269
+ break;
270
+ case ILL_PRVREG:
271
+ fputs("Caught SIGILL: (privileged register)\n", stderr);
272
+ break;
273
+ case ILL_COPROC:
274
+ fputs("Caught SIGILL: (coprocessor error)\n", stderr);
275
+ break;
276
+ case ILL_BADSTK:
277
+ fputs("Caught SIGILL: (internal stack error)\n", stderr);
278
+ break;
279
+ default:
280
+ fputs("Caught SIGILL: Illegal Instruction\n", stderr);
281
+ break;
282
+ }
283
+ break;
284
+ case SIGTERM:
285
+ fputs("Caught SIGTERM: a termination request was sent to the program\n", stderr);
286
+ break;
287
+ case SIGABRT:
288
+ fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr);
289
+ break;
290
+ default:
291
+ break;
292
+ }
293
+ posix_print_stack_trace();
294
+ _Exit(1);
295
+ }
296
+
297
+ static uint8_t alternate_stack[SIGSTKSZ];
298
+ void set_signal_handler()
299
+ {
300
+ /* setup alternate stack */
301
+ {
302
+ stack_t ss = {};
303
+ /* malloc is usually used here, I'm not 100% sure my static allocation
304
+ is valid but it seems to work just fine. */
305
+ ss.ss_sp = (void*)alternate_stack;
306
+ ss.ss_size = SIGSTKSZ;
307
+ ss.ss_flags = 0;
308
+
309
+ if (sigaltstack(&ss, NULL) != 0) { err(1, "sigaltstack"); }
310
+ }
311
+
312
+ /* register our signal handlers */
313
+ {
314
+ struct sigaction sig_action = {};
315
+ sig_action.sa_sigaction = posix_signal_handler;
316
+ sigemptyset(&sig_action.sa_mask);
317
+
318
+ #ifdef __APPLE__
319
+ /* for some reason we backtrace() doesn't work on osx
320
+ when we use an alternate stack */
321
+ sig_action.sa_flags = SA_SIGINFO;
322
+ #else
323
+ sig_action.sa_flags = SA_SIGINFO | SA_ONSTACK;
324
+ #endif
325
+
326
+ if (sigaction(SIGSEGV, &sig_action, NULL) != 0) { err(1, "sigaction"); }
327
+ if (sigaction(SIGFPE, &sig_action, NULL) != 0) { err(1, "sigaction"); }
328
+ if (sigaction(SIGINT, &sig_action, NULL) != 0) { err(1, "sigaction"); }
329
+ if (sigaction(SIGILL, &sig_action, NULL) != 0) { err(1, "sigaction"); }
330
+ if (sigaction(SIGTERM, &sig_action, NULL) != 0) { err(1, "sigaction"); }
331
+ if (sigaction(SIGABRT, &sig_action, NULL) != 0) { err(1, "sigaction"); }
332
+ }
333
+ }
334
+ #endif