[merp] Use macOS version not Darwin version in MERP reports (#17130)
[mono-project.git] / mono / utils / mono-merp.c
blob3438a232f407fbe9173aee32eecc644e9a3fb25d
1 /**
2 * \file
3 * Support for interop with the Microsoft Error Reporting tool
5 * Author:
6 * Alexander Kyte (alkyte@microsoft.com)
8 * (C) 2018 Microsoft, Inc.
12 #include <config.h>
13 #include <glib.h>
15 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
16 #include "mono-merp.h"
18 #include <unistd.h>
19 #include <spawn.h>
21 // OSX OS stuff now, for merpGUI interop
22 #include <mach/mach.h>
23 #include <mach/task_info.h>
24 #include <mach/mach_types.h>
25 #include <mach/mach_traps.h>
26 #include <servers/bootstrap.h>
28 #include <metadata/locales.h>
29 #include <mini/jit.h>
31 #if defined(HAVE_SYS_UTSNAME_H)
32 #include <sys/utsname.h>
33 #endif
35 // To get the apple machine model
36 #include <sys/param.h>
37 #include <sys/sysctl.h>
38 #include <fcntl.h>
40 #include <mono/utils/json.h>
41 #include <mono/utils/mono-state.h>
42 #include <utils/mono-threads-debug.h>
44 static const char *
45 kernel_version_string (void)
47 #ifdef HAVE_SYS_UTSNAME_H
48 static struct utsname name;
49 static const char *version_string;
51 if (!version_string) {
52 // WSL does not always nul terminate. WSL was fixed February 2018.
53 // Restore memset if variable made non-static.
54 //memset (&name, 0, sizeof (name));
56 if (uname (&name) >= 0)
57 version_string = name.release;
59 if (!version_string)
60 version_string = "";
62 return version_string;
63 #endif
64 return "";
67 static gboolean
68 starts_with (const char *pre, size_t pre_sz, const char *str)
70 return strncmp (pre, str, pre_sz) == 0;
73 static const char *
74 macos_version_string (void)
76 /* Can't allocate in here, could be called from a signal handler in a
77 * crashed process.
79 static const char *version_string;
80 static char buf[256];
81 static const size_t buf_size = sizeof (buf);
83 if (version_string)
84 return version_string;
86 /* macOS 10.13.6 or later */
87 if (!version_string) {
88 size_t size = 0;
89 if (sysctlbyname ("kern.osproductversion", NULL, &size, NULL, 0) < 0 || size >= buf_size) {
90 /* if we couldn't get the size or if it needs more space that we have in buf, leave it empty */
91 version_string = "";
92 return version_string;
95 if (sysctlbyname ("kern.osproductversion", (char*)buf, &size, NULL, 0) >= 0)
96 version_string = &buf[0];
98 /* macOS 10.13.5 or older */
99 if (!version_string) {
100 const char *kv_string = kernel_version_string ();
101 if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("17"), kv_string))
102 version_string = "10.13"; // High Sierra
103 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("16"), kv_string))
104 version_string = "10.12"; // Sierra
105 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("15"), kv_string))
106 version_string = "10.11"; // El Capitan
107 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("14"), kv_string))
108 version_string = "10.10"; // Yosemite
109 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("13"), kv_string))
110 version_string = "10.9"; // Mavericks
111 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("12"), kv_string))
112 version_string = "10.8"; // Mountain Lion
113 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("11"), kv_string))
114 version_string = "10.7"; // Lion
116 if (!version_string)
117 version_string = "";
119 return version_string;
122 // To get the path of the running process
123 #include <libproc.h>
125 typedef enum {
126 MerpArchInvalid = 0,
128 MerpArchx86_64 = 1,
129 MerpArchx86 = 2,
130 MerpArchPPC = 3,
131 MerpArchPPC64 = 4
132 } MerpArch;
134 typedef enum
136 MERP_EXC_NONE = 0,
138 MERP_EXC_FORCE_QUIT = 1,
139 MERP_EXC_SIGSEGV = 2,
140 MERP_EXC_SIGABRT = 3,
141 MERP_EXC_SIGSYS = 4,
142 MERP_EXC_SIGILL = 5,
143 MERP_EXC_SIGBUS = 6,
144 MERP_EXC_SIGFPE = 7 ,
145 MERP_EXC_SIGTRAP = 8,
146 MERP_EXC_SIGKILL = 9,
147 MERP_EXC_HANG = 10
148 } MERPExcType;
150 typedef struct {
151 const char *merpFilePath;
152 const char *crashLogPath;
153 const char *werXmlPath;
155 const char *bundleIDArg; // App Bundle ID (required for bucketization)
156 const char *versionArg; // App Version (required for bucketization)
158 MerpArch archArg; // Arch, MacOS only, bails out if not found also required for bucketization. (required)
159 MERPExcType exceptionArg; // Exception type (refer to merpcommon.h and mach/exception_types.h for more info (optional)
161 const char *serviceNameArg; // This is the Bootstrap service name that MERP GUI will create to receive mach_task_self on a port created. Bails out if MERP GUI fails to receive mach_task_self from the crashed app. (Required for crash log generation)
162 const char *servicePathArg; // The path to the executable, used to relaunch the crashed app.
164 const char *moduleName;
165 const char *moduleVersion;
166 size_t moduleOffset;
168 const char *osVersion;
169 int uiLidArg; // MONO_LOCALE_INVARIANT 0x007F
171 char systemModel [100];
172 const char *systemManufacturer;
174 const char *eventType;
176 MonoStackHash hashes;
177 GSList *annotations;
178 } MERPStruct;
180 typedef struct {
181 gboolean enable_merp;
183 const char *appBundleID;
184 const char *appPath;
185 const char *appSignature;
186 const char *appVersion;
187 const char *merpGUIPath;
188 const char *eventType;
189 const char *merpFilePath;
190 const char *crashLogPath;
191 const char *werXmlPath;
192 const char *moduleVersion;
194 gboolean log;
195 GSList *annotations;
196 } MerpOptions;
198 static MerpOptions config;
200 typedef struct {
201 char *key;
202 char *value;
203 } MonoMerpAnnotationEntry;
205 static const char *
206 get_merp_bitness (MerpArch arch)
208 switch (arch) {
209 case MerpArchx86_64:
210 return "x64";
211 case MerpArchx86:
212 return "x32";
213 default:
214 g_assert_not_reached ();
218 static MerpArch
219 get_merp_arch (void)
221 #ifdef TARGET_X86
222 return MerpArchx86;
223 #elif defined(TARGET_AMD64)
224 return MerpArchx86_64;
225 #elif defined(TARGET_POWERPC)
226 return MerpArchPPC;
227 #elif defined(TARGET_POWERPC64)
228 return MerpArchPPC64;
229 #else
230 g_assert_not_reached ();
231 #endif
234 static const char *
235 get_merp_exctype (MERPExcType exc)
237 switch (exc) {
238 case MERP_EXC_FORCE_QUIT:
239 return "0x10000000";
240 case MERP_EXC_SIGSEGV:
241 return "0x20000000";
242 case MERP_EXC_SIGABRT:
243 return "0x30000000";
244 case MERP_EXC_SIGSYS:
245 return "0x40000000";
246 case MERP_EXC_SIGILL:
247 return "0x50000000";
248 case MERP_EXC_SIGBUS:
249 return "0x60000000";
250 case MERP_EXC_SIGFPE:
251 return "0x70000000";
252 case MERP_EXC_SIGTRAP:
253 return "0x03000000";
254 case MERP_EXC_SIGKILL:
255 return "0x04000000";
256 case MERP_EXC_HANG:
257 return "0x02000000";
258 case MERP_EXC_NONE:
259 // Exception type documented as optional, not optional
260 g_assert_not_reached ();
261 default:
262 g_assert_not_reached ();
266 static MERPExcType
267 parse_exception_type (const char *signal)
269 if (!strcmp (signal, "SIGSEGV"))
270 return MERP_EXC_SIGSEGV;
272 if (!strcmp (signal, "SIGFPE"))
273 return MERP_EXC_SIGFPE;
275 if (!strcmp (signal, "SIGILL"))
276 return MERP_EXC_SIGILL;
278 if (!strcmp (signal, "SIGABRT"))
279 return MERP_EXC_SIGABRT;
281 // Force quit == hang?
282 // We need a default for this
283 if (!strcmp (signal, "SIGTERM"))
284 return MERP_EXC_HANG;
286 // FIXME: There are no other such signal
287 // strings passed to mono_handle_native_crash at the
288 // time of writing this
289 g_error ("Merp doesn't know how to handle %s\n", signal);
292 static int merp_file_permissions = S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH;
294 static gboolean
295 mono_merp_write_params (MERPStruct *merp)
297 int handle = g_open (merp->merpFilePath, O_TRUNC | O_WRONLY | O_CREAT, merp_file_permissions);
298 g_assertf (handle != -1, "Could not open MERP file at %s", merp->merpFilePath);
300 g_async_safe_fprintf(handle, "ApplicationBundleId: %s\n", merp->bundleIDArg);
301 g_async_safe_fprintf(handle, "ApplicationVersion: %s\n", merp->versionArg);
302 g_async_safe_fprintf(handle, "ApplicationBitness: %s\n", get_merp_bitness (merp->archArg));
304 g_async_safe_fprintf(handle, "ApplicationName: %s\n", merp->serviceNameArg);
305 g_async_safe_fprintf(handle, "ApplicationPath: %s\n", merp->servicePathArg);
306 g_async_safe_fprintf(handle, "BlameModuleName: %s\n", merp->moduleName);
307 g_async_safe_fprintf(handle, "BlameModuleVersion: %s\n", merp->moduleVersion);
308 g_async_safe_fprintf(handle, "BlameModuleOffset: 0x%llx\n", (unsigned long long)merp->moduleOffset);
309 g_async_safe_fprintf(handle, "ExceptionType: %s\n", get_merp_exctype (merp->exceptionArg));
310 g_async_safe_fprintf(handle, "StackChecksum: 0x%llx\n", merp->hashes.offset_free_hash);
311 g_async_safe_fprintf(handle, "StackHash: 0x%llx\n", merp->hashes.offset_rich_hash);
313 // Provided by icall
314 g_async_safe_fprintf(handle, "OSVersion: %s\n", merp->osVersion);
315 g_async_safe_fprintf(handle, "LanguageID: 0x%x\n", merp->uiLidArg);
316 g_async_safe_fprintf(handle, "SystemManufacturer: %s\n", merp->systemManufacturer);
317 g_async_safe_fprintf(handle, "SystemModel: %s\n", merp->systemModel);
318 g_async_safe_fprintf(handle, "EventType: %s\n", merp->eventType);
320 close (handle);
321 return TRUE;
324 static gboolean
325 mono_merp_send (MERPStruct *merp)
327 gboolean invoke_success = FALSE;
329 #if defined(HAVE_EXECV) && defined(HAVE_FORK)
330 pid_t pid = (pid_t) fork ();
332 // Only one we define on OSX
333 if (pid == 0) {
334 const char *open_path = "/usr/bin/open";
335 const char *argvOpen[] = {open_path, "-a", config.merpGUIPath, NULL};
336 execv (open_path, (char**)argvOpen);
337 exit (-1);
338 } else {
339 int status;
340 waitpid (pid, &status, 0);
341 gboolean exit_success = FALSE;
342 int exit_status = FALSE;
344 while (TRUE) {
345 if (waitpid(pid, &status, WUNTRACED | WCONTINUED) == -1)
346 break;
348 if (WIFEXITED(status)) {
349 exit_status = WEXITSTATUS(status);
350 exit_success = TRUE;
351 invoke_success = exit_status == TRUE;
352 break;
353 } else if (WIFSIGNALED(status)) {
354 break;
359 // // Create process to launch merp gui application
360 #endif
362 return invoke_success;
365 static void
366 get_apple_model (char *buffer, size_t max_length)
368 size_t sz = 0;
370 // Get the number of bytes to copy
371 sysctlbyname("hw.model", NULL, &sz, NULL, 0);
373 if (sz > max_length) {
374 buffer[0] = '\0';
375 return;
378 sysctlbyname("hw.model", buffer, &sz, NULL, 0);
381 static void
382 mono_init_merp (const intptr_t crashed_pid, const char *signal, MonoStackHash *hashes, MERPStruct *merp)
384 mono_memory_barrier ();
385 g_assert (mono_merp_enabled ());
387 merp->merpFilePath = config.merpFilePath;
388 merp->crashLogPath = config.crashLogPath;
389 merp->werXmlPath = config.werXmlPath;
391 // If these aren't set, icall wasn't made
392 // don't do merp? / don't set the variable to use merp;
393 g_assert (config.appBundleID);
394 g_assert (config.appVersion);
395 merp->bundleIDArg = config.appSignature;
396 merp->versionArg = config.appVersion;
398 merp->archArg = get_merp_arch ();
399 merp->exceptionArg = parse_exception_type (signal);
401 merp->serviceNameArg = config.appBundleID;
402 merp->servicePathArg = config.appPath;
404 merp->moduleName = "Mono Exception";
405 merp->moduleVersion = config.moduleVersion;
407 merp->moduleOffset = 0;
409 merp->uiLidArg = MONO_LOCALE_INVARIANT;
410 #if defined (TARGET_OSX)
411 merp->osVersion = macos_version_string ();
412 #else
413 merp->osVersion = kernel_version_string ();
414 #endif
416 // FIXME: THis is apple-only for now
417 merp->systemManufacturer = "apple";
418 get_apple_model ((char *) merp->systemModel, sizeof (merp->systemModel));
420 merp->eventType = config.eventType;
422 merp->hashes = *hashes;
424 merp->annotations = config.annotations;
427 static gboolean
428 mono_merp_write_fingerprint_payload (const char *non_param_data, const MERPStruct *merp)
430 int handle = g_open (merp->crashLogPath, O_TRUNC | O_WRONLY | O_CREAT, merp_file_permissions);
431 g_assertf (handle != -1, "Could not open crash log file at %s", merp->crashLogPath);
433 g_async_safe_fprintf(handle, "{\n");
434 g_async_safe_fprintf(handle, "\t\"payload\" : \n");
435 g_write (handle, non_param_data, (guint32)strlen (non_param_data));
436 g_async_safe_fprintf(handle, ",\n");
438 g_async_safe_fprintf(handle, "\t\"parameters\" : \n{\n");
439 g_async_safe_fprintf(handle, "\t\t\"ApplicationBundleId\" : \"%s\",\n", merp->bundleIDArg);
440 g_async_safe_fprintf(handle, "\t\t\"ApplicationVersion\" : \"%s\",\n", merp->versionArg);
441 g_async_safe_fprintf(handle, "\t\t\"ApplicationBitness\" : \"%s\",\n", get_merp_bitness (merp->archArg));
442 g_async_safe_fprintf(handle, "\t\t\"ApplicationName\" : \"%s\",\n", merp->serviceNameArg);
443 g_async_safe_fprintf(handle, "\t\t\"BlameModuleName\" : \"%s\",\n", merp->moduleName);
444 g_async_safe_fprintf(handle, "\t\t\"BlameModuleVersion\" : \"%s\",\n", merp->moduleVersion);
445 g_async_safe_fprintf(handle, "\t\t\"BlameModuleOffset\" : \"0x%lx\",\n", merp->moduleOffset);
446 g_async_safe_fprintf(handle, "\t\t\"ExceptionType\" : \"%s\",\n", get_merp_exctype (merp->exceptionArg));
447 g_async_safe_fprintf(handle, "\t\t\"StackChecksum\" : \"0x%llx\",\n", merp->hashes.offset_free_hash);
448 g_async_safe_fprintf(handle, "\t\t\"StackHash\" : \"0x%llx\",\n", merp->hashes.offset_rich_hash);
449 g_async_safe_fprintf(handle, "\t\t\"Extra\" : \n\t\t{\n");
451 for (GSList *cursor = merp->annotations; cursor; cursor = cursor->next) {
452 MonoMerpAnnotationEntry *iter = (MonoMerpAnnotationEntry *) cursor->data;
453 g_async_safe_fprintf(handle, "\t\t\t\"%s\" : \"%s\"\n", iter->key, iter->value);
456 g_async_safe_fprintf(handle, "\t\t},\n");
458 // Provided by icall
459 g_async_safe_fprintf(handle, "\t\t\"OSVersion\" : \"%s\",\n", merp->osVersion);
460 g_async_safe_fprintf(handle, "\t\t\"LanguageID\" : \"0x%x\",\n", merp->uiLidArg);
461 g_async_safe_fprintf(handle, "\t\t\"SystemManufacturer\" : \"%s\",\n", merp->systemManufacturer);
462 g_async_safe_fprintf(handle, "\t\t\"SystemModel\" : \"%s\",\n", merp->systemModel);
463 g_async_safe_fprintf(handle, "\t\t\"EventType\" : \"%s\"\n", merp->eventType);
465 // End of parameters
466 g_async_safe_fprintf(handle, "\t}\n");
467 g_async_safe_fprintf(handle, "}\n");
469 // End of object
470 close (handle);
472 return TRUE;
475 static gboolean
476 mono_write_wer_template (MERPStruct *merp)
478 // Note about missing ProcessInformation block: we have no PID that makes sense
479 // and when mono is embedded and used to run functions without an entry point,
480 // there is no image that would make any semantic sense to send either.
481 // It's a nuanced problem, each way we can run mono would need a separate fix.
483 int handle = g_open (merp->werXmlPath, O_WRONLY | O_CREAT | O_TRUNC, merp_file_permissions);
484 g_assertf (handle != -1, "Could not open WER XML file at %s", merp->werXmlPath);
486 // Provided by icall
487 g_async_safe_fprintf(handle, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
488 g_async_safe_fprintf(handle, "<WERReportMetadata>\n");
489 g_async_safe_fprintf(handle, "<ProblemSignatures>\n");
490 g_async_safe_fprintf(handle, "<EventType>%s</EventType>\n", merp->eventType);
492 int i=0;
493 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->bundleIDArg, i);
494 i++;
495 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->versionArg, i);
496 i++;
497 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, get_merp_bitness (merp->archArg), i);
498 i++;
499 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->serviceNameArg, i);
500 i++;
501 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->moduleName, i);
502 i++;
503 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->moduleVersion, i);
504 i++;
505 g_async_safe_fprintf(handle, "<Parameter%d>0x%zx</Parameter%d>\n", i, merp->moduleOffset, i);
506 i++;
507 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, get_merp_exctype (merp->exceptionArg), i);
508 i++;
509 g_async_safe_fprintf(handle, "<Parameter%d>0x%llx</Parameter%d>\n", i, merp->hashes.offset_free_hash, i);
510 i++;
511 g_async_safe_fprintf(handle, "<Parameter%d>0x%llx</Parameter%d>\n", i, merp->hashes.offset_rich_hash, i);
512 i++;
513 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->osVersion, i);
514 i++;
515 g_async_safe_fprintf(handle, "<Parameter%d>0x%x</Parameter%d>\n", i, merp->uiLidArg, i);
516 i++;
517 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->systemManufacturer, i);
518 i++;
519 g_async_safe_fprintf(handle, "<Parameter%d>%s</Parameter%d>\n", i, merp->systemModel, i);
520 i++;
522 g_async_safe_fprintf(handle, "</ProblemSignatures>\n");
523 g_async_safe_fprintf(handle, "</WERReportMetadata>\n");
525 close (handle);
527 return TRUE;
530 // Returns success
531 gboolean
532 mono_merp_invoke (const intptr_t crashed_pid, const char *signal, const char *non_param_data, MonoStackHash *hashes)
534 MonoStateMem mem;
535 int merp_tmp_file_tag = 2;
536 gboolean alloc_success = mono_state_alloc_mem (&mem, merp_tmp_file_tag, sizeof (MERPStruct));
537 if (!alloc_success)
538 return FALSE;
540 MERPStruct *merp = (MERPStruct *) mem.mem;
541 memset (merp, 0, sizeof (*merp));
543 mono_summarize_timeline_phase_log (MonoSummaryMerpWriter);
545 mono_init_merp (crashed_pid, signal, hashes, merp);
547 if (!mono_merp_write_params (merp))
548 return FALSE;
550 if (!mono_merp_write_fingerprint_payload (non_param_data, merp))
551 return FALSE;
553 if (!mono_write_wer_template (merp))
554 return FALSE;
556 // Start program
557 mono_summarize_timeline_phase_log (MonoSummaryMerpInvoke);
558 gboolean success = mono_merp_send (merp);
560 if (success)
561 mono_summarize_timeline_phase_log (MonoSummaryCleanup);
563 mono_state_free_mem (&mem);
565 return success;
568 void
569 mono_merp_add_annotation (const char *key, const char *value)
571 MonoMerpAnnotationEntry *entry = g_new0 (MonoMerpAnnotationEntry, 1);
572 entry->key = g_strdup (key);
573 entry->value = g_strdup (value);
574 config.annotations = g_slist_prepend (config.annotations, entry);
577 void
578 mono_merp_disable (void)
580 mono_memory_barrier ();
582 if (!config.enable_merp)
583 return;
585 g_free ((char*)config.appBundleID); // cast away const
586 g_free ((char*)config.appSignature);
587 g_free ((char*)config.appVersion);
588 g_free ((char*)config.merpGUIPath);
589 g_free ((char*)config.eventType);
590 g_free ((char*)config.appPath);
591 g_free ((char*)config.moduleVersion);
592 g_slist_free (config.annotations);
593 memset (&config, 0, sizeof (config));
595 mono_memory_barrier ();
598 void
599 mono_merp_enable (const char *appBundleID, const char *appSignature, const char *appVersion, const char *merpGUIPath, const char *eventType, const char *appPath, const char *configDir)
601 mono_memory_barrier ();
603 g_assert (!config.enable_merp);
605 char *prefix = NULL;
607 if (!configDir) {
608 const char *home = g_get_home_dir ();
609 prefix = g_strdup_printf ("%s/Library/Group Containers/UBF8T346G9.ms/", home);
610 } else {
611 prefix = g_strdup (configDir);
613 config.merpFilePath = g_strdup_printf ("%s%s", prefix, "MERP.uploadparams.txt");
614 config.crashLogPath = g_strdup_printf ("%s%s", prefix, "lastcrashlog.txt");
615 config.werXmlPath = g_strdup_printf ("%s%s", prefix, "CustomLogsMetadata.xml");
616 g_free (prefix);
618 config.moduleVersion = mono_get_runtime_callbacks ()->get_runtime_build_info ();
620 config.appBundleID = g_strdup (appBundleID);
621 config.appSignature = g_strdup (appSignature);
622 config.appVersion = g_strdup (appVersion);
623 config.merpGUIPath = g_strdup (merpGUIPath);
624 config.eventType = g_strdup (eventType);
625 config.appPath = g_strdup (appPath);
627 config.log = g_getenv ("MONO_MERP_VERBOSE") != NULL;
629 config.enable_merp = TRUE;
631 mono_memory_barrier ();
634 gboolean
635 mono_merp_enabled (void)
637 return config.enable_merp;
640 #endif // TARGET_OSX