123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- using System;
- using AOT;
- using NUnit.Framework;
- using System.Text.RegularExpressions;
- using Unity.Burst;
- using Unity.Jobs;
- using UnityEngine;
- using UnityEngine.TestTools;
- using Unity.Collections.LowLevel.Unsafe;
-
- #if UNITY_2021_1_OR_NEWER
- namespace BurstStacktraces
- {
- [BurstCompile]
- class BurstStacktracesJobs
- {
- [BurstCompile(CompileSynchronously = true, Debug = true)]
- struct ThrowManagedExceptionDebugJob : IJob
- {
- public void Execute()
- {
- throw new ArgumentException("A");
- }
- }
-
- [BurstCompile(CompileSynchronously = true)]
- struct ThrowManagedExceptionJob : IJob
- {
- public void Execute()
- {
- throw new ArgumentException("A");
- }
- }
-
- [BurstCompile(CompileSynchronously = true, Debug = true)]
- unsafe struct ThrowNativeExceptionDebugJob : IJob
- {
- #pragma warning disable 649
- [NativeDisableUnsafePtrRestriction] public int* Ptr;
- #pragma warning restore 649
-
- public void Execute()
- {
- // Ptr is null, so this will fail with a null reference exception.
- *Ptr = 42;
- }
- }
-
- [BurstCompile(CompileSynchronously = true)]
- unsafe struct ThrowNativeExceptionJob : IJob
- {
- #pragma warning disable 649
- [NativeDisableUnsafePtrRestriction] public int* Ptr;
- #pragma warning restore 649
-
- public void Execute()
- {
- // Ptr is null, so this will fail with a null reference exception.
- *Ptr = 42;
- }
- }
-
- private delegate int ExceptionDelegate(int a);
-
- [BurstCompile(CompileSynchronously = true)]
- [MonoPInvokeCallback(typeof(ExceptionDelegate))]
- private static int ThrowManagedExceptionFunctionPointer(int a)
- {
- throw new ArgumentException("A");
- }
-
- [BurstCompile(CompileSynchronously = true)]
- [MonoPInvokeCallback(typeof(ExceptionDelegate))]
- private static int ThrowNativeExceptionFunctionPointer(int a)
- {
- // a is zero so this will fail with a divide by zero native hardware exception.
- return 42 / a;
- }
-
- [BurstCompile(CompileSynchronously = true, Debug = true)]
- [MonoPInvokeCallback(typeof(ExceptionDelegate))]
- private static int ThrowManagedExceptionDebugFunctionPointer(int a)
- {
- throw new ArgumentException("A");
- }
-
- [BurstCompile(CompileSynchronously = true, Debug = true)]
- [MonoPInvokeCallback(typeof(ExceptionDelegate))]
- private static int ThrowNativeExceptionDebugFunctionPointer(int a)
- {
- // a is zero so this will fail with a divide by zero native hardware exception.
- return 42 / a;
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor)]
- [Description("Only Windows has built-in support for line numbers in stack traces")]
- public void ThrowManagedExceptionDebugWindowsFromJob()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- #if UNITY_2021_1_OR_NEWER
- // The line location was fixed in 2021.1+
- LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:22\\]"));
- #else
- LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:21\\]"));
- #endif
-
- var jobData = new ThrowManagedExceptionDebugJob();
- jobData.Run();
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor)]
- [Description("Only Windows has built-in support for line numbers in stack traces")]
- public void ThrowNativeExceptionDebugWindowsFromJob()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- #if UNITY_2021_1_OR_NEWER
- // The line location was fixed in 2021.1+
- LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:45\\]"));
- #else
- LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:44\\]"));
- #endif
-
- var jobData = new ThrowNativeExceptionDebugJob { Ptr = null };
- jobData.Run();
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowManagedExceptionDebugFromJob()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- LogAssert.Expect(LogType.Exception, new Regex("BurstStacktraces\\.BurstStacktracesJobs\\.ThrowManagedExceptionDebugJob"));
-
- var jobData = new ThrowManagedExceptionDebugJob();
- jobData.Run();
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowManagedExceptionFromJob()
- {
- BurstCompiler.Options.EnableBurstDebug = false;
-
- LogAssert.Expect(LogType.Exception, new Regex("Unity\\.Jobs\\.IJobExtensions\\.JobStruct`1<BurstStacktraces\\.BurstStacktracesJobs\\.ThrowManagedExceptionJob>\\.Execute"));
-
- var jobData = new ThrowManagedExceptionJob();
- jobData.Run();
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowNativeExceptionDebugFromJob()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- LogAssert.Expect(LogType.Exception, new Regex("BurstStacktraces\\.BurstStacktracesJobs\\.ThrowNativeExceptionDebugJob"));
-
- var jobData = new ThrowNativeExceptionDebugJob { Ptr = null };
- jobData.Run();
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowNativeExceptionFromJob()
- {
- BurstCompiler.Options.EnableBurstDebug = false;
-
- LogAssert.Expect(LogType.Exception, new Regex("Unity\\.Jobs\\.IJobExtensions\\.JobStruct`1<BurstStacktraces\\.BurstStacktracesJobs\\.ThrowNativeExceptionJob>\\.Execute"));
-
- var jobData = new ThrowNativeExceptionJob { Ptr = null };
- jobData.Run();
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor)]
- [Description("Only Windows has built-in support for line numbers in stack traces")]
- public void ThrowManagedExceptionDebugWindowsFromFunctionPointer()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowManagedExceptionDebugFunctionPointer);
-
- try
- {
- funcPtr.Invoke(0);
- }
- catch (Exception e)
- {
- #if UNITY_2021_1_OR_NEWER
- // The line location was fixed in 2021.1+
- Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:84] BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionDebugFunctionPointer"));
- #else
- Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:83] BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionDebugFunctionPointer"));
- #endif
- }
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor)]
- [Description("Only Windows has built-in support for line numbers in stack traces")]
- public void ThrowNativeExceptionDebugWindowsFromFunctionPointer()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowNativeExceptionDebugFunctionPointer);
-
- try
- {
- funcPtr.Invoke(0);
- }
- catch (Exception e)
- {
- #if UNITY_2021_1_OR_NEWER
- // The line location was fixed in 2021.1+
- Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:92] BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionDebugFunctionPointer"));
- #else
- Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:91] BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionDebugFunctionPointer"));
- #endif
- }
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowManagedExceptionDebugFromFunctionPointer()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowManagedExceptionDebugFunctionPointer);
-
- try
- {
- funcPtr.Invoke(0);
- }
- catch (Exception e)
- {
- Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionDebugFunctionPointer"));
- }
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowManagedExceptionFromFunctionPointer()
- {
- BurstCompiler.Options.EnableBurstDebug = false;
-
- var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowManagedExceptionFunctionPointer);
-
- try
- {
- funcPtr.Invoke(0);
- }
- catch (Exception e)
- {
- Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionFunctionPointer"));
- }
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowNativeExceptionDebugFromFunctionPointer()
- {
- BurstCompiler.Options.EnableBurstDebug = true;
-
- var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowNativeExceptionDebugFunctionPointer);
-
- try
- {
- funcPtr.Invoke(0);
- }
- catch (Exception e)
- {
- Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionDebugFunctionPointer"));
- }
- }
-
- [Test]
- [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
- [Description("Requires stacktrace exception support which is only supported in the Editor")]
- public void ThrowNativeExceptionFromFunctionPointer()
- {
- BurstCompiler.Options.EnableBurstDebug = false;
-
- var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowNativeExceptionFunctionPointer);
-
- try
- {
- funcPtr.Invoke(0);
- }
- catch (Exception e)
- {
- Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionFunctionPointer"));
- }
- }
- }
- }
- #endif
|