Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BurstStacktraces.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using AOT;
  3. using NUnit.Framework;
  4. using System.Text.RegularExpressions;
  5. using Unity.Burst;
  6. using Unity.Jobs;
  7. using UnityEngine;
  8. using UnityEngine.TestTools;
  9. using Unity.Collections.LowLevel.Unsafe;
  10. #if UNITY_2021_1_OR_NEWER
  11. namespace BurstStacktraces
  12. {
  13. [BurstCompile]
  14. class BurstStacktracesJobs
  15. {
  16. [BurstCompile(CompileSynchronously = true, Debug = true)]
  17. struct ThrowManagedExceptionDebugJob : IJob
  18. {
  19. public void Execute()
  20. {
  21. throw new ArgumentException("A");
  22. }
  23. }
  24. [BurstCompile(CompileSynchronously = true)]
  25. struct ThrowManagedExceptionJob : IJob
  26. {
  27. public void Execute()
  28. {
  29. throw new ArgumentException("A");
  30. }
  31. }
  32. [BurstCompile(CompileSynchronously = true, Debug = true)]
  33. unsafe struct ThrowNativeExceptionDebugJob : IJob
  34. {
  35. #pragma warning disable 649
  36. [NativeDisableUnsafePtrRestriction] public int* Ptr;
  37. #pragma warning restore 649
  38. public void Execute()
  39. {
  40. // Ptr is null, so this will fail with a null reference exception.
  41. *Ptr = 42;
  42. }
  43. }
  44. [BurstCompile(CompileSynchronously = true)]
  45. unsafe struct ThrowNativeExceptionJob : IJob
  46. {
  47. #pragma warning disable 649
  48. [NativeDisableUnsafePtrRestriction] public int* Ptr;
  49. #pragma warning restore 649
  50. public void Execute()
  51. {
  52. // Ptr is null, so this will fail with a null reference exception.
  53. *Ptr = 42;
  54. }
  55. }
  56. private delegate int ExceptionDelegate(int a);
  57. [BurstCompile(CompileSynchronously = true)]
  58. [MonoPInvokeCallback(typeof(ExceptionDelegate))]
  59. private static int ThrowManagedExceptionFunctionPointer(int a)
  60. {
  61. throw new ArgumentException("A");
  62. }
  63. [BurstCompile(CompileSynchronously = true)]
  64. [MonoPInvokeCallback(typeof(ExceptionDelegate))]
  65. private static int ThrowNativeExceptionFunctionPointer(int a)
  66. {
  67. // a is zero so this will fail with a divide by zero native hardware exception.
  68. return 42 / a;
  69. }
  70. [BurstCompile(CompileSynchronously = true, Debug = true)]
  71. [MonoPInvokeCallback(typeof(ExceptionDelegate))]
  72. private static int ThrowManagedExceptionDebugFunctionPointer(int a)
  73. {
  74. throw new ArgumentException("A");
  75. }
  76. [BurstCompile(CompileSynchronously = true, Debug = true)]
  77. [MonoPInvokeCallback(typeof(ExceptionDelegate))]
  78. private static int ThrowNativeExceptionDebugFunctionPointer(int a)
  79. {
  80. // a is zero so this will fail with a divide by zero native hardware exception.
  81. return 42 / a;
  82. }
  83. [Test]
  84. [UnityPlatform(RuntimePlatform.WindowsEditor)]
  85. [Description("Only Windows has built-in support for line numbers in stack traces")]
  86. public void ThrowManagedExceptionDebugWindowsFromJob()
  87. {
  88. BurstCompiler.Options.EnableBurstDebug = true;
  89. #if UNITY_2021_1_OR_NEWER
  90. // The line location was fixed in 2021.1+
  91. LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:22\\]"));
  92. #else
  93. LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:21\\]"));
  94. #endif
  95. var jobData = new ThrowManagedExceptionDebugJob();
  96. jobData.Run();
  97. }
  98. [Test]
  99. [UnityPlatform(RuntimePlatform.WindowsEditor)]
  100. [Description("Only Windows has built-in support for line numbers in stack traces")]
  101. public void ThrowNativeExceptionDebugWindowsFromJob()
  102. {
  103. BurstCompiler.Options.EnableBurstDebug = true;
  104. #if UNITY_2021_1_OR_NEWER
  105. // The line location was fixed in 2021.1+
  106. LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:45\\]"));
  107. #else
  108. LogAssert.Expect(LogType.Exception, new Regex("\\[BurstStacktraces\\.cs:44\\]"));
  109. #endif
  110. var jobData = new ThrowNativeExceptionDebugJob { Ptr = null };
  111. jobData.Run();
  112. }
  113. [Test]
  114. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  115. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  116. public void ThrowManagedExceptionDebugFromJob()
  117. {
  118. BurstCompiler.Options.EnableBurstDebug = true;
  119. LogAssert.Expect(LogType.Exception, new Regex("BurstStacktraces\\.BurstStacktracesJobs\\.ThrowManagedExceptionDebugJob"));
  120. var jobData = new ThrowManagedExceptionDebugJob();
  121. jobData.Run();
  122. }
  123. [Test]
  124. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  125. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  126. public void ThrowManagedExceptionFromJob()
  127. {
  128. BurstCompiler.Options.EnableBurstDebug = false;
  129. LogAssert.Expect(LogType.Exception, new Regex("Unity\\.Jobs\\.IJobExtensions\\.JobStruct`1<BurstStacktraces\\.BurstStacktracesJobs\\.ThrowManagedExceptionJob>\\.Execute"));
  130. var jobData = new ThrowManagedExceptionJob();
  131. jobData.Run();
  132. }
  133. [Test]
  134. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  135. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  136. public void ThrowNativeExceptionDebugFromJob()
  137. {
  138. BurstCompiler.Options.EnableBurstDebug = true;
  139. LogAssert.Expect(LogType.Exception, new Regex("BurstStacktraces\\.BurstStacktracesJobs\\.ThrowNativeExceptionDebugJob"));
  140. var jobData = new ThrowNativeExceptionDebugJob { Ptr = null };
  141. jobData.Run();
  142. }
  143. [Test]
  144. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  145. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  146. public void ThrowNativeExceptionFromJob()
  147. {
  148. BurstCompiler.Options.EnableBurstDebug = false;
  149. LogAssert.Expect(LogType.Exception, new Regex("Unity\\.Jobs\\.IJobExtensions\\.JobStruct`1<BurstStacktraces\\.BurstStacktracesJobs\\.ThrowNativeExceptionJob>\\.Execute"));
  150. var jobData = new ThrowNativeExceptionJob { Ptr = null };
  151. jobData.Run();
  152. }
  153. [Test]
  154. [UnityPlatform(RuntimePlatform.WindowsEditor)]
  155. [Description("Only Windows has built-in support for line numbers in stack traces")]
  156. public void ThrowManagedExceptionDebugWindowsFromFunctionPointer()
  157. {
  158. BurstCompiler.Options.EnableBurstDebug = true;
  159. var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowManagedExceptionDebugFunctionPointer);
  160. try
  161. {
  162. funcPtr.Invoke(0);
  163. }
  164. catch (Exception e)
  165. {
  166. #if UNITY_2021_1_OR_NEWER
  167. // The line location was fixed in 2021.1+
  168. Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:84] BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionDebugFunctionPointer"));
  169. #else
  170. Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:83] BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionDebugFunctionPointer"));
  171. #endif
  172. }
  173. }
  174. [Test]
  175. [UnityPlatform(RuntimePlatform.WindowsEditor)]
  176. [Description("Only Windows has built-in support for line numbers in stack traces")]
  177. public void ThrowNativeExceptionDebugWindowsFromFunctionPointer()
  178. {
  179. BurstCompiler.Options.EnableBurstDebug = true;
  180. var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowNativeExceptionDebugFunctionPointer);
  181. try
  182. {
  183. funcPtr.Invoke(0);
  184. }
  185. catch (Exception e)
  186. {
  187. #if UNITY_2021_1_OR_NEWER
  188. // The line location was fixed in 2021.1+
  189. Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:92] BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionDebugFunctionPointer"));
  190. #else
  191. Assert.IsTrue(e.Message.Contains("[BurstStacktraces.cs:91] BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionDebugFunctionPointer"));
  192. #endif
  193. }
  194. }
  195. [Test]
  196. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  197. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  198. public void ThrowManagedExceptionDebugFromFunctionPointer()
  199. {
  200. BurstCompiler.Options.EnableBurstDebug = true;
  201. var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowManagedExceptionDebugFunctionPointer);
  202. try
  203. {
  204. funcPtr.Invoke(0);
  205. }
  206. catch (Exception e)
  207. {
  208. Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionDebugFunctionPointer"));
  209. }
  210. }
  211. [Test]
  212. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  213. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  214. public void ThrowManagedExceptionFromFunctionPointer()
  215. {
  216. BurstCompiler.Options.EnableBurstDebug = false;
  217. var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowManagedExceptionFunctionPointer);
  218. try
  219. {
  220. funcPtr.Invoke(0);
  221. }
  222. catch (Exception e)
  223. {
  224. Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowManagedExceptionFunctionPointer"));
  225. }
  226. }
  227. [Test]
  228. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  229. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  230. public void ThrowNativeExceptionDebugFromFunctionPointer()
  231. {
  232. BurstCompiler.Options.EnableBurstDebug = true;
  233. var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowNativeExceptionDebugFunctionPointer);
  234. try
  235. {
  236. funcPtr.Invoke(0);
  237. }
  238. catch (Exception e)
  239. {
  240. Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionDebugFunctionPointer"));
  241. }
  242. }
  243. [Test]
  244. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor)]
  245. [Description("Requires stacktrace exception support which is only supported in the Editor")]
  246. public void ThrowNativeExceptionFromFunctionPointer()
  247. {
  248. BurstCompiler.Options.EnableBurstDebug = false;
  249. var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(ThrowNativeExceptionFunctionPointer);
  250. try
  251. {
  252. funcPtr.Invoke(0);
  253. }
  254. catch (Exception e)
  255. {
  256. Assert.IsTrue(e.Message.Contains("BurstStacktraces.BurstStacktracesJobs.ThrowNativeExceptionFunctionPointer"));
  257. }
  258. }
  259. }
  260. }
  261. #endif