1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 /*
6  * This custom library loading code is only meant to be called
7  * during initialization. As a result, it takes no special
8  * precautions to be threadsafe. Any of the library loading functions
9  * like mozload should not be available to other code.
10  */
11 
12 #include <jni.h>
13 #include <android/log.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/mman.h>
17 #include <sys/limits.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <zlib.h>
25 #include "dlfcn.h"
26 #include "APKOpen.h"
27 #include <sys/time.h>
28 #include <sys/syscall.h>
29 #include <sys/resource.h>
30 #include <sys/prctl.h>
31 #include "sqlite3.h"
32 #include "SQLiteBridge.h"
33 #include "NSSBridge.h"
34 #include "ElfLoader.h"
35 #include "application.ini.h"
36 
37 #include "mozilla/arm.h"
38 #include "mozilla/Bootstrap.h"
39 #include "mozilla/TimeStamp.h"
40 #include "mozilla/UniquePtr.h"
41 #include "XREChildData.h"
42 
43 /* Android headers don't define RUSAGE_THREAD */
44 #ifndef RUSAGE_THREAD
45 #define RUSAGE_THREAD 1
46 #endif
47 
48 #ifndef RELEASE_OR_BETA
49 /* Official builds have the debuggable flag set to false, which disables
50  * the backtrace dumper from bionic. However, as it is useful for native
51  * crashes happening before the crash reporter is registered, re-enable
52  * it on non release builds (i.e. nightly and aurora).
53  * Using a constructor so that it is re-enabled as soon as libmozglue.so
54  * is loaded.
55  */
make_dumpable()56 __attribute__((constructor)) void make_dumpable() { prctl(PR_SET_DUMPABLE, 1); }
57 #endif
58 
59 typedef int mozglueresult;
60 
61 enum StartupEvent {
62 #define mozilla_StartupTimeline_Event(ev, z) ev,
63 #include "StartupTimeline.h"
64 #undef mozilla_StartupTimeline_Event
65   MAX_STARTUP_EVENT_ID
66 };
67 
68 using namespace mozilla;
69 
70 static const int MAX_MAPPING_INFO = 32;
71 static mapping_info lib_mapping[MAX_MAPPING_INFO];
72 
getLibraryMapping()73 APKOPEN_EXPORT const struct mapping_info* getLibraryMapping() {
74   return lib_mapping;
75 }
76 
JNI_Throw(JNIEnv * jenv,const char * classname,const char * msg)77 void JNI_Throw(JNIEnv* jenv, const char* classname, const char* msg) {
78   __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Throw\n");
79   jclass cls = jenv->FindClass(classname);
80   if (cls == nullptr) {
81     __android_log_print(
82         ANDROID_LOG_ERROR, "GeckoLibLoad",
83         "Couldn't find exception class (or exception pending) %s\n", classname);
84     exit(FAILURE);
85   }
86   int rc = jenv->ThrowNew(cls, msg);
87   if (rc < 0) {
88     __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad",
89                         "Error throwing exception %s\n", msg);
90     exit(FAILURE);
91   }
92   jenv->DeleteLocalRef(cls);
93 }
94 
95 namespace {
96 JavaVM* sJavaVM;
97 }
98 
abortThroughJava(const char * msg)99 void abortThroughJava(const char* msg) {
100   struct sigaction sigact = {};
101   if (SEGVHandler::__wrap_sigaction(SIGSEGV, nullptr, &sigact)) {
102     return;  // sigaction call failed.
103   }
104 
105   Dl_info info = {};
106   if ((sigact.sa_flags & SA_SIGINFO) &&
107       __wrap_dladdr(reinterpret_cast<void*>(sigact.sa_sigaction), &info) &&
108       info.dli_fname && strstr(info.dli_fname, "libxul.so")) {
109     return;  // Existing signal handler is in libxul (i.e. we have crash
110              // reporter).
111   }
112 
113   JNIEnv* env = nullptr;
114   if (!sJavaVM ||
115       sJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) {
116     return;
117   }
118 
119   if (!env || env->PushLocalFrame(2) != JNI_OK) {
120     return;
121   }
122 
123   jclass loader = env->FindClass("org/mozilla/gecko/mozglue/GeckoLoader");
124   if (!loader) {
125     return;
126   }
127 
128   jmethodID method =
129       env->GetStaticMethodID(loader, "abort", "(Ljava/lang/String;)V");
130   jstring str = env->NewStringUTF(msg);
131 
132   if (method && str) {
133     env->CallStaticVoidMethod(loader, method, str);
134   }
135 
136   env->PopLocalFrame(nullptr);
137 }
138 
139 Bootstrap::UniquePtr gBootstrap;
140 #ifndef MOZ_FOLD_LIBS
141 static void* sqlite_handle = nullptr;
142 static void* nspr_handle = nullptr;
143 static void* plc_handle = nullptr;
144 #else
145 #define sqlite_handle nss_handle
146 #define nspr_handle nss_handle
147 #define plc_handle nss_handle
148 #endif
149 static void* nss_handle = nullptr;
150 
151 static int mapping_count = 0;
152 
report_mapping(char * name,void * base,uint32_t len,uint32_t offset)153 extern "C" void report_mapping(char* name, void* base, uint32_t len,
154                                uint32_t offset) {
155   if (mapping_count >= MAX_MAPPING_INFO) return;
156 
157   struct mapping_info* info = &lib_mapping[mapping_count++];
158   info->name = strdup(name);
159   info->base = (uintptr_t)base;
160   info->len = len;
161   info->offset = offset;
162 }
163 
delete_mapping(const char * name)164 extern "C" void delete_mapping(const char* name) {
165   for (int pos = 0; pos < mapping_count; ++pos) {
166     struct mapping_info* info = &lib_mapping[pos];
167     if (!strcmp(info->name, name)) {
168       struct mapping_info* last = &lib_mapping[mapping_count - 1];
169       free(info->name);
170       *info = *last;
171       --mapping_count;
172       break;
173     }
174   }
175 }
176 
getAPKLibraryName(const char * apkName,const char * libraryName)177 static UniquePtr<char[]> getAPKLibraryName(const char* apkName,
178                                            const char* libraryName) {
179 #define APK_ASSETS_PATH "!/assets/" ANDROID_CPU_ARCH "/"
180   size_t filenameLength = strlen(apkName) +
181                           sizeof(APK_ASSETS_PATH) +  // includes \0 terminator
182                           strlen(libraryName);
183   auto file = MakeUnique<char[]>(filenameLength);
184   snprintf(file.get(), filenameLength, "%s" APK_ASSETS_PATH "%s", apkName,
185            libraryName);
186   return file;
187 #undef APK_ASSETS_PATH
188 }
189 
dlopenAPKLibrary(const char * apkName,const char * libraryName)190 static void* dlopenAPKLibrary(const char* apkName, const char* libraryName) {
191   return __wrap_dlopen(getAPKLibraryName(apkName, libraryName).get(),
192                        RTLD_GLOBAL | RTLD_LAZY);
193 }
194 
loadGeckoLibs(const char * apkName)195 static mozglueresult loadGeckoLibs(const char* apkName) {
196   TimeStamp t0 = TimeStamp::Now();
197   struct rusage usage1_thread, usage1;
198   getrusage(RUSAGE_THREAD, &usage1_thread);
199   getrusage(RUSAGE_SELF, &usage1);
200 
201   gBootstrap = GetBootstrap(getAPKLibraryName(apkName, "libxul.so").get());
202   if (!gBootstrap) {
203     __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad",
204                         "Couldn't get a handle to libxul!");
205     return FAILURE;
206   }
207 
208   TimeStamp t1 = TimeStamp::Now();
209   struct rusage usage2_thread, usage2;
210   getrusage(RUSAGE_THREAD, &usage2_thread);
211   getrusage(RUSAGE_SELF, &usage2);
212 
213 #define RUSAGE_TIMEDIFF(u1, u2, field)                    \
214   ((u2.ru_##field.tv_sec - u1.ru_##field.tv_sec) * 1000 + \
215    (u2.ru_##field.tv_usec - u1.ru_##field.tv_usec) / 1000)
216 
217   __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad",
218                       "Loaded libs in %fms total, %ldms(%ldms) user, "
219                       "%ldms(%ldms) system, %ld(%ld) faults",
220                       (t1 - t0).ToMilliseconds(),
221                       RUSAGE_TIMEDIFF(usage1_thread, usage2_thread, utime),
222                       RUSAGE_TIMEDIFF(usage1, usage2, utime),
223                       RUSAGE_TIMEDIFF(usage1_thread, usage2_thread, stime),
224                       RUSAGE_TIMEDIFF(usage1, usage2, stime),
225                       usage2_thread.ru_majflt - usage1_thread.ru_majflt,
226                       usage2.ru_majflt - usage1.ru_majflt);
227 
228   gBootstrap->XRE_StartupTimelineRecord(LINKER_INITIALIZED, t0);
229   gBootstrap->XRE_StartupTimelineRecord(LIBRARIES_LOADED, t1);
230   return SUCCESS;
231 }
232 
233 static mozglueresult loadNSSLibs(const char* apkName);
234 
loadSQLiteLibs(const char * apkName)235 static mozglueresult loadSQLiteLibs(const char* apkName) {
236   if (sqlite_handle) return SUCCESS;
237 
238 #ifdef MOZ_FOLD_LIBS
239   if (loadNSSLibs(apkName) != SUCCESS) return FAILURE;
240 #else
241 
242   sqlite_handle = dlopenAPKLibrary(apkName, "libmozsqlite3.so");
243   if (!sqlite_handle) {
244     __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad",
245                         "Couldn't get a handle to libmozsqlite3!");
246     return FAILURE;
247   }
248 #endif
249 
250   setup_sqlite_functions(sqlite_handle);
251   return SUCCESS;
252 }
253 
loadNSSLibs(const char * apkName)254 static mozglueresult loadNSSLibs(const char* apkName) {
255   if (nss_handle && nspr_handle && plc_handle) return SUCCESS;
256 
257   nss_handle = dlopenAPKLibrary(apkName, "libnss3.so");
258 
259 #ifndef MOZ_FOLD_LIBS
260   nspr_handle = dlopenAPKLibrary(apkName, "libnspr4.so");
261 
262   plc_handle = dlopenAPKLibrary(apkName, "libplc4.so");
263 #endif
264 
265   if (!nss_handle) {
266     __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad",
267                         "Couldn't get a handle to libnss3!");
268     return FAILURE;
269   }
270 
271 #ifndef MOZ_FOLD_LIBS
272   if (!nspr_handle) {
273     __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad",
274                         "Couldn't get a handle to libnspr4!");
275     return FAILURE;
276   }
277 
278   if (!plc_handle) {
279     __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad",
280                         "Couldn't get a handle to libplc4!");
281     return FAILURE;
282   }
283 #endif
284 
285   return setup_nss_functions(nss_handle, nspr_handle, plc_handle);
286 }
287 
288 extern "C" APKOPEN_EXPORT void MOZ_JNICALL
Java_org_mozilla_gecko_mozglue_GeckoLoader_loadGeckoLibsNative(JNIEnv * jenv,jclass jGeckoAppShellClass,jstring jApkName)289 Java_org_mozilla_gecko_mozglue_GeckoLoader_loadGeckoLibsNative(
290     JNIEnv* jenv, jclass jGeckoAppShellClass, jstring jApkName) {
291   jenv->GetJavaVM(&sJavaVM);
292 
293   const char* str;
294   // XXX: java doesn't give us true UTF8, we should figure out something
295   // better to do here
296   str = jenv->GetStringUTFChars(jApkName, nullptr);
297   if (str == nullptr) return;
298 
299   int res = loadGeckoLibs(str);
300   if (res != SUCCESS) {
301     JNI_Throw(jenv, "java/lang/Exception", "Error loading gecko libraries");
302   }
303   jenv->ReleaseStringUTFChars(jApkName, str);
304 }
305 
306 extern "C" APKOPEN_EXPORT void MOZ_JNICALL
Java_org_mozilla_gecko_mozglue_GeckoLoader_loadSQLiteLibsNative(JNIEnv * jenv,jclass jGeckoAppShellClass,jstring jApkName)307 Java_org_mozilla_gecko_mozglue_GeckoLoader_loadSQLiteLibsNative(
308     JNIEnv* jenv, jclass jGeckoAppShellClass, jstring jApkName) {
309   const char* str;
310   // XXX: java doesn't give us true UTF8, we should figure out something
311   // better to do here
312   str = jenv->GetStringUTFChars(jApkName, nullptr);
313   if (str == nullptr) return;
314 
315   __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load sqlite start\n");
316   mozglueresult rv = loadSQLiteLibs(str);
317   if (rv != SUCCESS) {
318     JNI_Throw(jenv, "java/lang/Exception", "Error loading sqlite libraries");
319   }
320   __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load sqlite done\n");
321   jenv->ReleaseStringUTFChars(jApkName, str);
322 }
323 
324 extern "C" APKOPEN_EXPORT void MOZ_JNICALL
Java_org_mozilla_gecko_mozglue_GeckoLoader_loadNSSLibsNative(JNIEnv * jenv,jclass jGeckoAppShellClass,jstring jApkName)325 Java_org_mozilla_gecko_mozglue_GeckoLoader_loadNSSLibsNative(
326     JNIEnv* jenv, jclass jGeckoAppShellClass, jstring jApkName) {
327   const char* str;
328   // XXX: java doesn't give us true UTF8, we should figure out something
329   // better to do here
330   str = jenv->GetStringUTFChars(jApkName, nullptr);
331   if (str == nullptr) return;
332 
333   __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load nss start\n");
334   mozglueresult rv = loadNSSLibs(str);
335   if (rv != SUCCESS) {
336     JNI_Throw(jenv, "java/lang/Exception", "Error loading nss libraries");
337   }
338   __android_log_print(ANDROID_LOG_ERROR, "GeckoLibLoad", "Load nss done\n");
339   jenv->ReleaseStringUTFChars(jApkName, str);
340 }
341 
CreateArgvFromObjectArray(JNIEnv * jenv,jobjectArray jargs,int * length)342 static char** CreateArgvFromObjectArray(JNIEnv* jenv, jobjectArray jargs,
343                                         int* length) {
344   size_t stringCount = jenv->GetArrayLength(jargs);
345 
346   if (length) {
347     *length = stringCount;
348   }
349 
350   if (!stringCount) {
351     return nullptr;
352   }
353 
354   char** argv = new char*[stringCount + 1];
355 
356   argv[stringCount] = nullptr;
357 
358   for (size_t ix = 0; ix < stringCount; ix++) {
359     jstring string = (jstring)(jenv->GetObjectArrayElement(jargs, ix));
360     const char* rawString = jenv->GetStringUTFChars(string, nullptr);
361     const int strLength = jenv->GetStringUTFLength(string);
362     argv[ix] = strndup(rawString, strLength);
363     jenv->ReleaseStringUTFChars(string, rawString);
364     jenv->DeleteLocalRef(string);
365   }
366 
367   return argv;
368 }
369 
FreeArgv(char ** argv,int argc)370 static void FreeArgv(char** argv, int argc) {
371   for (int ix = 0; ix < argc; ix++) {
372     // String was allocated with strndup, so need to use free to deallocate.
373     free(argv[ix]);
374   }
375   delete[](argv);
376 }
377 
378 extern "C" APKOPEN_EXPORT void MOZ_JNICALL
Java_org_mozilla_gecko_mozglue_GeckoLoader_nativeRun(JNIEnv * jenv,jclass jc,jobjectArray jargs,int ipcFd,int crashFd,int crashAnnotationFd)379 Java_org_mozilla_gecko_mozglue_GeckoLoader_nativeRun(JNIEnv* jenv, jclass jc,
380                                                      jobjectArray jargs,
381                                                      int ipcFd, int crashFd,
382                                                      int crashAnnotationFd) {
383   int argc = 0;
384   char** argv = CreateArgvFromObjectArray(jenv, jargs, &argc);
385 
386   if (ipcFd < 0) {
387     if (gBootstrap == nullptr) {
388       FreeArgv(argv, argc);
389       return;
390     }
391 
392     ElfLoader::Singleton.ExpectShutdown(false);
393     gBootstrap->GeckoStart(jenv, argv, argc, sAppData);
394     ElfLoader::Singleton.ExpectShutdown(true);
395   } else {
396     gBootstrap->XRE_SetAndroidChildFds(jenv, ipcFd, crashFd, crashAnnotationFd);
397     gBootstrap->XRE_SetProcessType(argv[argc - 1]);
398 
399     XREChildData childData;
400     gBootstrap->XRE_InitChildProcess(argc - 1, argv, &childData);
401   }
402 
403   gBootstrap.reset();
404   FreeArgv(argv, argc);
405 }
406 
ChildProcessInit(int argc,char * argv[])407 extern "C" APKOPEN_EXPORT mozglueresult ChildProcessInit(int argc,
408                                                          char* argv[]) {
409   int i;
410   for (i = 0; i < (argc - 1); i++) {
411     if (strcmp(argv[i], "-greomni")) continue;
412 
413     i = i + 1;
414     break;
415   }
416 
417   if (loadNSSLibs(argv[i]) != SUCCESS) {
418     return FAILURE;
419   }
420   if (loadSQLiteLibs(argv[i]) != SUCCESS) {
421     return FAILURE;
422   }
423   if (loadGeckoLibs(argv[i]) != SUCCESS) {
424     return FAILURE;
425   }
426 
427   gBootstrap->XRE_SetProcessType(argv[--argc]);
428 
429   XREChildData childData;
430   return NS_FAILED(gBootstrap->XRE_InitChildProcess(argc, argv, &childData));
431 }
432 
433 extern "C" APKOPEN_EXPORT jboolean MOZ_JNICALL
Java_org_mozilla_gecko_mozglue_GeckoLoader_neonCompatible(JNIEnv * jenv,jclass jc)434 Java_org_mozilla_gecko_mozglue_GeckoLoader_neonCompatible(JNIEnv* jenv,
435                                                           jclass jc) {
436 #ifdef __ARM_EABI__
437   return mozilla::supports_neon();
438 #else
439   return true;
440 #endif  // __ARM_EABI__
441 }
442 
443 // Does current process name end with ':media'?
IsMediaProcess()444 static bool IsMediaProcess() {
445   pid_t pid = getpid();
446   char str[256];
447   snprintf(str, sizeof(str), "/proc/%d/cmdline", pid);
448   FILE* f = fopen(str, "r");
449   if (f) {
450     fgets(str, sizeof(str), f);
451     fclose(f);
452     const size_t strLen = strlen(str);
453     const char suffix[] = ":media";
454     const size_t suffixLen = sizeof(suffix) - 1;
455     if (strLen >= suffixLen &&
456         !strncmp(str + strLen - suffixLen, suffix, suffixLen)) {
457       return true;
458     }
459   }
460   return false;
461 }
462 
463 #ifndef SYS_rt_tgsigqueueinfo
464 #define SYS_rt_tgsigqueueinfo __NR_rt_tgsigqueueinfo
465 #endif
466 /* Copy of http://androidxref.com/7.1.1_r6/xref/bionic/linker/debugger.cpp#262,
467  * with debuggerd related code stripped.
468  *
469  * Copyright (C) 2008 The Android Open Source Project
470  * All rights reserved.
471  *
472  * Redistribution and use in source and binary forms, with or without
473  * modification, are permitted provided that the following conditions
474  * are met:
475  *  * Redistributions of source code must retain the above copyright
476  *    notice, this list of conditions and the following disclaimer.
477  *  * Redistributions in binary form must reproduce the above copyright
478  *    notice, this list of conditions and the following disclaimer in
479  *    the documentation and/or other materials provided with the
480  *    distribution.
481  *
482  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
483  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
484  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
485  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
486  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
487  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
488  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
489  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
490  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
491  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
492  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
493  * SUCH DAMAGE.
494  */
CatchFatalSignals(int num,siginfo_t * info,void * context)495 static void CatchFatalSignals(int num, siginfo_t* info, void* context) {
496   // It's possible somebody cleared the SA_SIGINFO flag, which would mean
497   // our "info" arg holds an undefined value.
498   struct sigaction action = {};
499   if ((sigaction(num, nullptr, &action) < 0) ||
500       !(action.sa_flags & SA_SIGINFO)) {
501     info = nullptr;
502   }
503 
504   // We need to return from the signal handler so that debuggerd can dump the
505   // thread that crashed, but returning here does not guarantee that the signal
506   // will be thrown again, even for SIGSEGV and friends, since the signal could
507   // have been sent manually. Resend the signal with rt_tgsigqueueinfo(2) to
508   // preserve the SA_SIGINFO contents.
509   signal(num, SIG_DFL);
510 
511   struct siginfo si;
512   if (!info) {
513     memset(&si, 0, sizeof(si));
514     si.si_code = SI_USER;
515     si.si_pid = getpid();
516     si.si_uid = getuid();
517     info = &si;
518   } else if (info->si_code >= 0 || info->si_code == SI_TKILL) {
519     // rt_tgsigqueueinfo(2)'s documentation appears to be incorrect on kernels
520     // that contain commit 66dd34a (3.9+). The manpage claims to only allow
521     // negative si_code values that are not SI_TKILL, but 66dd34a changed the
522     // check to allow all si_code values in calls coming from inside the house.
523   }
524 
525   int rc = syscall(SYS_rt_tgsigqueueinfo, getpid(), gettid(), num, info);
526   if (rc != 0) {
527     __android_log_print(ANDROID_LOG_FATAL, "mozglue",
528                         "failed to resend signal during crash: %s",
529                         strerror(errno));
530     _exit(0);
531   }
532 }
533 
534 extern "C" APKOPEN_EXPORT void MOZ_JNICALL
Java_org_mozilla_gecko_mozglue_GeckoLoader_suppressCrashDialog(JNIEnv * jenv,jclass jc)535 Java_org_mozilla_gecko_mozglue_GeckoLoader_suppressCrashDialog(JNIEnv* jenv,
536                                                                jclass jc) {
537   MOZ_RELEASE_ASSERT(IsMediaProcess(),
538                      "Suppress crash dialog only for media process");
539   // Restoring to SIG_DFL will crash on x86/Android M devices (see bug 1374556)
540   // so copy Android code
541   // (http://androidxref.com/7.1.1_r6/xref/bionic/linker/debugger.cpp#302). See
542   // comments above CatchFatalSignals() for copyright notice.
543   struct sigaction action;
544   memset(&action, 0, sizeof(action));
545   sigemptyset(&action.sa_mask);
546   action.sa_sigaction = &CatchFatalSignals;
547   action.sa_flags = SA_RESTART | SA_SIGINFO;
548 
549   // Use the alternate signal stack if available so we can catch stack
550   // overflows.
551   action.sa_flags |= SA_ONSTACK;
552 
553   sigaction(SIGABRT, &action, nullptr);
554   sigaction(SIGBUS, &action, nullptr);
555   sigaction(SIGFPE, &action, nullptr);
556   sigaction(SIGILL, &action, nullptr);
557   sigaction(SIGSEGV, &action, nullptr);
558 #if defined(SIGSTKFLT)
559   sigaction(SIGSTKFLT, &action, nullptr);
560 #endif
561   sigaction(SIGTRAP, &action, nullptr);
562 }
563