Welcome to mirror list, hosted at ThFree Co, Russian Federation.

merp-crash-test.cs « tests « mono - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d534a64fae59c4fb534b8b41694a13f3ac2b28db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Web.Script.Serialization;
using Diag = System.Diagnostics;
using System.Runtime.InteropServices;

class C
{
	class CrasherClass
	{
		public struct Crasher {
			public string Name {get;}
			public Action Action {get; }

			public Action<object> Validator {get; }

			public Crasher (string name, Action action, Action<object> validator = null)
			{
				Name = name;
				Action = action;
				Validator = validator;
			}
		}

		public class ValidationException : Exception {
			public ValidationException () : base () {}
			public ValidationException (string msg) : base (msg) {}
			public ValidationException (string msg, Exception inner) : base (msg, inner) {}
		}

		public static List<Crasher> Crashers;
		public static int StresserIndex;

		static CrasherClass ()
		{
			Crashers = new List<Crasher> ();

			// Basic functionality
			Crashers.Add(new Crasher ("MerpCrashManaged", MerpCrashManaged));
			//  Run this test for stress tests
			//
			//  I've ran a burn-in with all of them of
			//  1,000 - 10,000 runs already.
			//
			//  Feel free to change by moving this line.
			StresserIndex = Crashers.Count - 1;

			Crashers.Add(new Crasher ("MerpCrashMalloc", MerpCrashMalloc));
			Crashers.Add(new Crasher ("MerpCrashFailFast", MerpCrashFailFast, ValidateFailFastMsg));

			Crashers.Add(new Crasher ("MerpCrashNullFp", MerpCrashNullFp));
			Crashers.Add(new Crasher ("MerpCrashExceptionHook", MerpCrashUnhandledExceptionHook));

			// Specific Edge Cases
			Crashers.Add(new Crasher ("MerpCrashDladdr", MerpCrashDladdr));
			Crashers.Add(new Crasher ("MerpCrashSnprintf", MerpCrashSnprintf));
			Crashers.Add(new Crasher ("MerpCrashDomainUnload", MerpCrashDomainUnload));
			Crashers.Add(new Crasher ("MerpCrashUnbalancedGCSafe", MerpCrashUnbalancedGCSafe));
			Crashers.Add(new Crasher  ("MerpCrashSignalTerm", MerpCrashSignalTerm));
			Crashers.Add(new Crasher  ("MerpCrashSignalTerm", MerpCrashSignalAbrt));
			Crashers.Add(new Crasher  ("MerpCrashSignalKill", MerpCrashSignalFpe));
			Crashers.Add(new Crasher  ("MerpCrashSignalKill", MerpCrashSignalBus));
			Crashers.Add(new Crasher  ("MerpCrashSignalSegv", MerpCrashSignalSegv));
			Crashers.Add(new Crasher  ("MerpCrashSignalIll", MerpCrashSignalIll));
			Crashers.Add(new Crasher ("MerpCrashTestBreadcrumbs", MerpCrashTestBreadcrumbs, validator: ValidateBreadcrumbs));
		}

		public static void 
		MerpCrashManaged ()
		{
			unsafe { Console.WriteLine("{0}", *(int*) -1); }
		}

		const string failfastMsg = "abcd efgh";

		public static void
		MerpCrashFailFast ()
		{
			Environment.FailFast (failfastMsg);
		}

		public static void ValidateFailFastMsg (object json)
		{
			string s = jsonGetKeys (json, "payload", "failfast_message") as string;
			if (s != failfastMsg)
				throw new ValidationException (String.Format ("incorrect fail fast message (expected: {0}, got: {1})", failfastMsg, s));
		}

		public static void ValidateBreadcrumbs (object json)
		{
			var monoType = Type.GetType ("Mono.Runtime", false);
			var m = monoType.GetMethod ("CheckCrashReportReason", BindingFlags.NonPublic | BindingFlags.Static);
			var m_params = new object [] { "./", false };
			string o = (string)m.Invoke(null, m_params);
			if (o != "segv")
				throw new Exception ("Crash report reason should be 'segv'");

			m = monoType.GetMethod ("CheckCrashReportHash", BindingFlags.NonPublic | BindingFlags.Static);
			long hash = (long)m.Invoke (null, m_params);

			if (hash == 0)
				throw new Exception ("Crash hash should not be zero");
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashSnprintf ();

		// This test tries to test the writer's reentrancy
		public static void 
		MerpCrashSnprintf ()
		{
			mono_test_MerpCrashSnprintf ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashDladdr ();

		public static void 
		MerpCrashDladdr ()
		{
			mono_test_MerpCrashDladdr ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashMalloc ();

		public static void 
		MerpCrashMalloc ()
		{
			mono_test_MerpCrashMalloc ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashLoaderLock ();

		public static void 
		MerpCrashLoaderLock ()
		{
			mono_test_MerpCrashLoaderLock ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashDomainUnload ();

		public static void 
		MerpCrashDomainUnload ()
		{
			mono_test_MerpCrashDomainUnload ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashUnbalancedGCSafe ();

		public static void 
		MerpCrashUnbalancedGCSafe ()
		{
			mono_test_MerpCrashUnbalancedGCSafe ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashNullFp ();

		public static void 
		MerpCrashNullFp ()
		{
			mono_test_MerpCrashNullFp ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashUnhandledExceptionHook ();

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashSignalTerm ();

		public static void
		MerpCrashSignalTerm ()
		{
			mono_test_MerpCrashSignalTerm ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashSignalAbrt ();

		public static void
		MerpCrashSignalAbrt ()
		{
			mono_test_MerpCrashSignalAbrt ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashSignalFpe ();

		public static void
		MerpCrashSignalFpe ()
		{
			mono_test_MerpCrashSignalFpe ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashSignalBus ();

		public static void
		MerpCrashSignalBus ()
		{
			mono_test_MerpCrashSignalBus ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashSignalSegv ();

		public static void
		MerpCrashSignalSegv ()
		{
			mono_test_MerpCrashSignalSegv ();
		}

		[DllImport("libtest")]
		public static extern void mono_test_MerpCrashSignalIll ();

		public static void
		MerpCrashSignalIll ()
		{
			mono_test_MerpCrashSignalIll ();
		}

		public static void 
		MerpCrashUnhandledExceptionHook ()
		{
			AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleException);
			throw new Exception ("This is Unhandled");
		}

		public static void HandleException (object sender, UnhandledExceptionEventArgs e)
		{
			Console.WriteLine ("And now to crash inside the hook");
			mono_test_MerpCrashUnhandledExceptionHook ();
		}

		public static void
		MerpCrashTestBreadcrumbs ()
		{
			mono_test_MerpCrashSignalSegv ();
		}


		private static object jsonGetKey (object o, string key) => (o as Dictionary<string,object>)[key];
		private static object jsonGetKeys (object o, params string[] keys) {
			try {
				foreach (var key in keys) {
					o = jsonGetKey (o, key);
				}
				return o;
			} catch (KeyNotFoundException e) {
				throw new ValidationException (String.Format ("{0}, key not found, looking for key path [{1}]", e.ToString(), String.Join (", ", keys)));
			}
		}

	}

	static string configDir = "./merp-crash-test/";

	public static void 
	CrashWithMerp (int testNum)
	{
		SetupCrash (configDir);
		CrasherClass.Crashers [Convert.ToInt32 (testNum)].Action ();
	}

	public static string env = Environment.GetEnvironmentVariable ("MONO_PATH");
	public static string this_assembly_path = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);

	public static void 
	SetupCrash (string configDir)
	{
		var monoType = Type.GetType ("Mono.Runtime", false);
		var m = monoType.GetMethod("EnableMicrosoftTelemetry", BindingFlags.NonPublic | BindingFlags.Static);

		// This leads to open -a /bin/cat, which errors out, but errors
		// in invoking merp are only logged errors, not fatal assertions.
		var merpGUIPath = "/bin/cat";
		var appBundleId = "com.xam.Minimal";
		var appSignature = "Test.Xam.Minimal";
		var appVersion = "123456";
		var eventType = "AppleAppCrash";
		var appPath = "/where/mono/lives";
		var m_params = new object[] { appBundleId, appSignature, appVersion, merpGUIPath, eventType, appPath, configDir };

		m.Invoke(null, m_params);	

		DumpLogSet ();
	}

	public static void 
	TestValidate (string configDir, bool silent, Action<object> validator = null)
	{
		var xmlFilePath = String.Format("{0}CustomLogsMetadata.xml", configDir);
		var paramsFilePath = String.Format("{0}MERP.uploadparams.txt", configDir);
		var crashFilePath = String.Format("{0}lastcrashlog.txt", configDir);

		// Fixme: Maybe parse these json files rather than
		// just checking they exist
		var xmlFileExists = File.Exists (xmlFilePath);
		var paramsFileExists = File.Exists (paramsFilePath);
		var crashFileExists = File.Exists (crashFilePath);

		if (xmlFileExists) {
			var text = File.ReadAllText (xmlFilePath);
			if (!silent)
				Console.WriteLine ("Xml file {0}", text);
			File.Delete (xmlFilePath);
		} else {
			Console.WriteLine ("Xml file {0} missing", xmlFilePath);
		}

		if (paramsFileExists) {
			var text = File.ReadAllText (paramsFilePath);
			if (!silent)
				Console.WriteLine ("Params file {0}", text);
			File.Delete (paramsFilePath);
		} else {
			Console.WriteLine ("Params file {0} missing", paramsFilePath);
		}

		if (crashFileExists) {
			var crashFile = File.ReadAllText (crashFilePath);
			File.Delete (crashFilePath);

			var checker = new JavaScriptSerializer ();

			// Throws if invalid json
			if (!silent)
				Console.WriteLine("Validating: {0}",  crashFile);
			try {
				var obj = checker.DeserializeObject (crashFile);
				if (validator is object)
					validator (obj);
			} catch (CrasherClass.ValidationException e) {
				throw new Exception (String.Format ("Validation failed '{0}', json: {1}", e.Message, crashFile));
			} catch (Exception e) {
				throw new Exception (String.Format ("Invalid json  ({0}:{1}): {2}", e.GetType(), e.Message, crashFile));
			}

			File.Delete (crashFilePath);
			// Assert it has the required merp fields
		} else {
			Console.WriteLine ("Crash file {0} missing", crashFilePath);
		}

		DumpLogCheck (expected_level: "MerpInvoke"); // we are expecting merp invoke to fail

		if (!xmlFileExists)
			throw new Exception (String.Format ("Did not produce {0}", xmlFilePath));

		if (!paramsFileExists)
			throw new Exception (String.Format ("Did not produce {0}", paramsFilePath));

		if (!crashFileExists)
			throw new Exception (String.Format ("Did not produce {0}", crashFilePath));
	}

	public static void
	Cleanup (string configDir)
	{
		Directory.Delete (configDir, true);
	}

	static void DumpLogSet ()
	{
		var monoType = Type.GetType ("Mono.Runtime", false);
		var convert = monoType.GetMethod("EnableCrashReportLog", BindingFlags.NonPublic | BindingFlags.Static);
		convert.Invoke(null, new object[] { "./" });
	}

	static void DumpLogUnset ()
	{
		var monoType = Type.GetType ("Mono.Runtime", false);
		var convert = monoType.GetMethod("EnableCrashReportLog", BindingFlags.NonPublic | BindingFlags.Static);
		convert.Invoke(null, new object[] { null });
	}

	static void DumpLogCheck (string expected_level = "Done")
	{
		var monoType = Type.GetType ("Mono.Runtime", false);
		var convert = monoType.GetMethod("CheckCrashReportLog", BindingFlags.NonPublic | BindingFlags.Static);
		var result = (int) convert.Invoke(null, new object[] { "./", true });
		// Value of enum
		string [] levels = new string [] { "None", "Setup", "SuspendHandshake", "UnmanagedStacks", "ManagedStacks", "StateWriter", "StateWriterDone", "MerpWriter", "MerpInvoke", "Cleanup", "Done", "DoubleFault" };

		if (expected_level != levels [result])
			throw new Exception (String.Format ("Crash level {0} does not match expected {1}", levels [result], expected_level));

		// also clear hash and reason breadcrumbs
		convert = monoType.GetMethod("CheckCrashReportHash", BindingFlags.NonPublic | BindingFlags.Static);
		var hash_result = (long) convert.Invoke(null, new object[] { "./", true });
		convert = monoType.GetMethod("CheckCrashReportReason", BindingFlags.NonPublic | BindingFlags.Static);
		var reason_result = (string) convert.Invoke(null, new object[] { "./", true });

		if (reason_result == string.Empty)
			throw new Exception("Crash reason should not be an empty string");
	}


	public static void 
	SpawnCrashingRuntime (string runtime, int testNum, bool silent)
	{
		var asm = "merp-crash-test.exe";
		var pi = new Diag.ProcessStartInfo ();
		pi.UseShellExecute = false;
		pi.FileName = runtime;
		pi.Arguments = String.Format ("{0} {1}", asm, testNum);;
		pi.Environment ["MONO_PATH"] = env;

		if (!silent) {
			Console.WriteLine ("Running {0}", CrasherClass.Crashers [testNum].Name);
			Console.WriteLine ("MONO_PATH={0} {1} {2} {3}", env, runtime, asm, testNum);
		}

		if (Directory.Exists (configDir)) {
			Console.WriteLine ("Cleaning up left over configDir {0}", configDir);
			Cleanup (configDir);
		}

		Directory.CreateDirectory (configDir);

		try {
			var process = Diag.Process.Start (pi);
			process.WaitForExit ();

			TestValidate (configDir, silent, CrasherClass.Crashers [testNum].Validator);
		} finally {
			Cleanup (configDir);
		}
	}

	public static void TestManagedException ()
	{
		if (Directory.Exists (configDir)) {
			Console.WriteLine ("Cleaning up left over configDir {0}", configDir);
			Cleanup (configDir);
		}
		Directory.CreateDirectory (configDir);

		SetupCrash (configDir);
		var monoType = Type.GetType ("Mono.Runtime", false);
		var m = monoType.GetMethod ("ExceptionToState", BindingFlags.NonPublic | BindingFlags.Static);
		var exception = new Exception ("test managed exception");
		var m_params = new object[] { exception };

		var result = m.Invoke (null, m_params) as Tuple<String, ulong, ulong>;
		DumpLogCheck (expected_level: "StateWriterDone");
		Cleanup (configDir);
	}

	public static Exception RunManagedExceptionTest ()
	{
			Console.WriteLine ("Testing ExceptionToState()...");
			Exception exception_test_failure = null;

			try {
				TestManagedException();
			}
			catch (Exception e)
			{
				return e;
			}
			return null;
	}

	public static int Main (string [] args)
	{
		if (args.Length == 0) {
			string processExe = Diag.Process.GetCurrentProcess ().MainModule.FileName;
			if (processExe == null)
				throw new ArgumentException ("Couldn't get name of running file");
			else if (string.IsNullOrEmpty (processExe))
				throw new ArgumentException ("Couldn't find mono runtime.");
			else if (!Path.GetFileName (processExe).StartsWith ("mono"))
				throw new ArgumentException (String.Format("Running native app {0}  isn't 'mono'"));

			var failures = new Exception [CrasherClass.Crashers.Count];
			int failure_count = 0;
			for (int i=0; i < CrasherClass.Crashers.Count; i++) {
				try {
					SpawnCrashingRuntime (processExe, i, false);
				} catch (Exception e) {
					failures [i] = e;
					if (e.InnerException != null)
						failures [i] = e.InnerException;
					failure_count++;
				}
			}

			// Also test sending a managed exception
			Exception exception_test_failure = RunManagedExceptionTest ();

			Console.WriteLine ("\n\n##################");
			Console.WriteLine ("Merp Test Results:");
			Console.WriteLine ("##################\n\n");

			if (exception_test_failure != null)
			{
				Console.WriteLine ("Sending managed exception to MERP failed: {0}\n{1}\n", exception_test_failure.Message, exception_test_failure.StackTrace);
			}

			if (failure_count > 0) {
				for (int i=0; i < CrasherClass.Crashers.Count; i++) {
					if (failures [i] != null) {
						Console.WriteLine ("Crash reporter failed test {0}", CrasherClass.Crashers [i].Name);
						Console.WriteLine ("Cause: {0}\n{1}\n", failures [i].Message, failures [i].StackTrace);
					}
				}
			}

			if (failure_count > 0 || exception_test_failure != null)
				return 1;

			Console.WriteLine ("\n\n##################");
			Console.WriteLine ("Merp Stress Test:");
			Console.WriteLine ("##################\n\n");

			Console.WriteLine ("Starting crash stress test\n");
			int iter = 0;
			for (iter=0; iter < 20; iter++) {
				Console.WriteLine ("\n#############################################");
				Console.WriteLine ("\tMerp Stress Test Iteration {0}", iter);
				Console.WriteLine ("#############################################\n");
				try {
					SpawnCrashingRuntime (processExe, CrasherClass.StresserIndex, true);
				} catch (Exception e) {
					Console.WriteLine ("Stress test caught failure. Shutting down after {1} iterations.\n {0} \n\n", e.InnerException, iter);
					throw;
				}
			}
			Console.WriteLine ("Ending crash stress test. No failures caught.\n");

			return 0;
		} else {
			CrashWithMerp (Convert.ToInt32 (args [0]));
			return 0;
		}
	}
}