Nessuna descrizione
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.

ManagedExceptionsBurstJobs.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using NUnit.Framework;
  5. using System.Text.RegularExpressions;
  6. using Unity.Burst;
  7. using Unity.Collections.LowLevel.Unsafe;
  8. using Unity.Jobs;
  9. using UnityEngine;
  10. using UnityEngine.TestTools;
  11. using System.Runtime.CompilerServices;
  12. namespace ExceptionsFromBurstJobs
  13. {
  14. [BurstCompile]
  15. class ManagedExceptionsBurstJobs
  16. {
  17. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  18. private static void ThrowNewArgumentException()
  19. {
  20. throw new ArgumentException("A");
  21. }
  22. [BurstCompile(CompileSynchronously = true)]
  23. struct ThrowArgumentExceptionJob : IJob
  24. {
  25. public void Execute()
  26. {
  27. ThrowNewArgumentException();
  28. }
  29. }
  30. [Test]
  31. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  32. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  33. public void ThrowArgumentException()
  34. {
  35. LogAssert.Expect(LogType.Exception, new Regex("ArgumentException: A"));
  36. var jobData = new ThrowArgumentExceptionJob();
  37. jobData.Run();
  38. }
  39. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  40. private static void ThrowNewArgumentNullException()
  41. {
  42. throw new ArgumentNullException("N");
  43. }
  44. [BurstCompile(CompileSynchronously = true)]
  45. struct ThrowArgumentNullExceptionJob : IJob
  46. {
  47. public void Execute()
  48. {
  49. ThrowNewArgumentNullException();
  50. }
  51. }
  52. [Test]
  53. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  54. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  55. public void ThrowArgumentNullException()
  56. {
  57. LogAssert.Expect(LogType.Exception, new Regex("System.ArgumentNullException: N"));
  58. var jobData = new ThrowArgumentNullExceptionJob();
  59. jobData.Run();
  60. }
  61. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  62. private static void ThrowNewNullReferenceException()
  63. {
  64. throw new NullReferenceException("N");
  65. }
  66. [BurstCompile(CompileSynchronously = true)]
  67. struct ThrowNullReferenceExceptionJob : IJob
  68. {
  69. public void Execute()
  70. {
  71. ThrowNewNullReferenceException();
  72. }
  73. }
  74. [Test]
  75. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  76. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  77. public void ThrowNullReferenceException()
  78. {
  79. LogAssert.Expect(LogType.Exception, new Regex("NullReferenceException: N"));
  80. var jobData = new ThrowNullReferenceExceptionJob();
  81. jobData.Run();
  82. }
  83. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  84. private static void ThrowNewInvalidOperationException()
  85. {
  86. throw new InvalidOperationException("IO");
  87. }
  88. [BurstCompile(CompileSynchronously = true)]
  89. struct ThrowInvalidOperationExceptionJob : IJob
  90. {
  91. public void Execute()
  92. {
  93. ThrowNewInvalidOperationException();
  94. }
  95. }
  96. [Test]
  97. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  98. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  99. public void ThrowInvalidOperationException()
  100. {
  101. LogAssert.Expect(LogType.Exception, new Regex("InvalidOperationException: IO"));
  102. var jobData = new ThrowInvalidOperationExceptionJob();
  103. jobData.Run();
  104. }
  105. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  106. private static void ThrowNewNotSupportedException()
  107. {
  108. throw new NotSupportedException("NS");
  109. }
  110. [BurstCompile(CompileSynchronously = true)]
  111. struct ThrowNotSupportedExceptionJob : IJob
  112. {
  113. public void Execute()
  114. {
  115. ThrowNewNotSupportedException();
  116. }
  117. }
  118. [Test]
  119. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  120. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  121. public void ThrowNotSupportedException()
  122. {
  123. LogAssert.Expect(LogType.Exception, new Regex("NotSupportedException: NS"));
  124. var jobData = new ThrowNotSupportedExceptionJob();
  125. jobData.Run();
  126. }
  127. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  128. private static void ThrowNewUnityException()
  129. {
  130. throw new UnityException("UE");
  131. }
  132. [BurstCompile(CompileSynchronously = true)]
  133. struct ThrowUnityExceptionJob : IJob
  134. {
  135. public void Execute()
  136. {
  137. ThrowNewUnityException();
  138. }
  139. }
  140. [Test]
  141. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  142. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  143. public void ThrowUnityException()
  144. {
  145. LogAssert.Expect(LogType.Exception, new Regex("UnityException: UE"));
  146. var jobData = new ThrowUnityExceptionJob();
  147. jobData.Run();
  148. }
  149. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  150. private static void ThrowNewIndexOutOfRangeException()
  151. {
  152. throw new IndexOutOfRangeException("IOOR");
  153. }
  154. [BurstCompile(CompileSynchronously = true)]
  155. struct ThrowIndexOutOfRangeExceptionJob : IJob
  156. {
  157. public void Execute()
  158. {
  159. ThrowNewIndexOutOfRangeException();
  160. }
  161. }
  162. [Test]
  163. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  164. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  165. public void ThrowIndexOutOfRange()
  166. {
  167. LogAssert.Expect(LogType.Exception, new Regex("IndexOutOfRangeException: IOOR"));
  168. var jobData = new ThrowIndexOutOfRangeExceptionJob();
  169. jobData.Run();
  170. }
  171. [BurstCompile(CompileSynchronously = true)]
  172. private unsafe struct ThrowFromDereferenceNullJob : IJob
  173. {
  174. [NativeDisableUnsafePtrRestriction]
  175. public int* Ptr;
  176. public void Execute()
  177. {
  178. *Ptr = 42;
  179. }
  180. }
  181. [Test]
  182. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  183. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  184. public void ThrowFromDereferenceNull()
  185. {
  186. LogAssert.Expect(LogType.Exception, new Regex("NullReferenceException: Object reference not set to an instance of an object"));
  187. var jobData = new ThrowFromDereferenceNullJob() { Ptr = null };
  188. jobData.Run();
  189. }
  190. [BurstCompile(CompileSynchronously = true)]
  191. private unsafe struct ThrowFromDivideByZeroJob : IJob
  192. {
  193. public int Int;
  194. public void Execute()
  195. {
  196. Int = 42 / Int;
  197. }
  198. }
  199. [Test]
  200. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  201. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  202. public void ThrowFromDivideByZero()
  203. {
  204. if (RuntimeInformation.OSArchitecture == Architecture.Arm64)
  205. {
  206. // Arm64 does not throw a divide-by-zero exception, instead it flushes the result to zero.
  207. return;
  208. }
  209. LogAssert.Expect(LogType.Exception, new Regex("DivideByZeroException: Attempted to divide by zero"));
  210. var jobData = new ThrowFromDivideByZeroJob() { Int = 0 };
  211. jobData.Run();
  212. }
  213. private unsafe delegate void ExceptionDelegate(int* a);
  214. [BurstCompile(CompileSynchronously = true)]
  215. private static unsafe void DereferenceNull(int* a)
  216. {
  217. *a = 42;
  218. }
  219. [BurstCompile(CompileSynchronously = true)]
  220. unsafe struct ThrowFromFunctionPointerJob : IJob
  221. {
  222. #pragma warning disable 649
  223. [NativeDisableUnsafePtrRestriction] public IntPtr FuncPtr;
  224. [NativeDisableUnsafePtrRestriction] public int* Ptr;
  225. #pragma warning restore 649
  226. public void Execute()
  227. {
  228. new FunctionPointer<ExceptionDelegate>(FuncPtr).Invoke(Ptr);
  229. // Set Ptr to non null which should never be hit because the above will throw.
  230. Ptr = (int*)0x42;
  231. }
  232. }
  233. [Test]
  234. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  235. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  236. public unsafe void ThrowFromFunctionPointer()
  237. {
  238. var funcPtr = BurstCompiler.CompileFunctionPointer<ExceptionDelegate>(DereferenceNull);
  239. LogAssert.Expect(LogType.Exception, new Regex("NullReferenceException: Object reference not set to an instance of an object"));
  240. var job = new ThrowFromFunctionPointerJob { FuncPtr = funcPtr.Value, Ptr = null };
  241. job.Run();
  242. Assert.AreEqual((IntPtr)job.Ptr, (IntPtr)0);
  243. }
  244. [BurstCompile(CompileSynchronously = true)]
  245. private unsafe struct ThrowFromDereferenceNullParallelJob : IJobParallelFor
  246. {
  247. [NativeDisableUnsafePtrRestriction]
  248. public int* Ptr;
  249. public void Execute(int index)
  250. {
  251. *Ptr = index;
  252. }
  253. }
  254. [Test]
  255. // No RuntimePlatform.OSXEditor in this list because of a subtle Mojave only bug.
  256. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.LinuxEditor)]
  257. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  258. public void ThrowFromDereferenceNullParallel()
  259. {
  260. var messageCount = 0;
  261. void OnMessage(string message, string stackTrace, LogType type)
  262. {
  263. Assert.AreEqual(LogType.Exception, type);
  264. StringAssert.Contains("NullReferenceException: Object reference not set to an instance of an object", message);
  265. messageCount++;
  266. }
  267. LogAssert.ignoreFailingMessages = true;
  268. Application.logMessageReceivedThreaded += OnMessage;
  269. try
  270. {
  271. var jobData = new ThrowFromDereferenceNullParallelJob() { Ptr = null };
  272. jobData.Schedule(128, 1).Complete();
  273. Assert.GreaterOrEqual(messageCount, 1);
  274. }
  275. finally
  276. {
  277. Application.logMessageReceivedThreaded -= OnMessage;
  278. LogAssert.ignoreFailingMessages = false;
  279. }
  280. }
  281. private unsafe struct ThrowFromDereferenceNullManagedJob : IJob
  282. {
  283. [NativeDisableUnsafePtrRestriction]
  284. public int* Ptr;
  285. public void Execute()
  286. {
  287. *Ptr = 42;
  288. }
  289. }
  290. [Test]
  291. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  292. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  293. public void ThrowFromDereferenceNullManaged()
  294. {
  295. LogAssert.Expect(LogType.Exception, new Regex("NullReferenceException: Object reference not set to an instance of an object"));
  296. var jobData = new ThrowFromDereferenceNullManagedJob() { Ptr = null };
  297. jobData.Run();
  298. }
  299. [Test]
  300. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  301. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  302. public void ThrowFromDereferenceNullBurstDisabled()
  303. {
  304. var previous = BurstCompiler.Options.EnableBurstCompilation;
  305. BurstCompiler.Options.EnableBurstCompilation = false;
  306. LogAssert.Expect(LogType.Exception, new Regex("NullReferenceException: Object reference not set to an instance of an object"));
  307. var jobData = new ThrowFromDereferenceNullJob() { Ptr = null };
  308. jobData.Run();
  309. BurstCompiler.Options.EnableBurstCompilation = previous;
  310. }
  311. [BurstCompile]
  312. struct Thrower : IJob
  313. {
  314. public int X;
  315. [BurstCompile(CompileSynchronously = true)]
  316. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  317. [MethodImpl(MethodImplOptions.NoInlining)]
  318. public static void ConditionalThrowWithSideEffect(int x)
  319. {
  320. if (x == -1)
  321. throw new InvalidOperationException();
  322. UnityEngine.Debug.Log("wow");
  323. throw new InvalidOperationException();
  324. }
  325. public void Execute()
  326. {
  327. ConditionalThrowWithSideEffect(X);
  328. }
  329. }
  330. [Test]
  331. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  332. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  333. public void TestConditionalThrowWithSideEffect()
  334. {
  335. LogAssert.Expect(LogType.Log, "wow");
  336. LogAssert.Expect(LogType.Exception, new Regex(".+InvalidOperation.+"));
  337. new Thrower() { X = 0 }.Run();
  338. }
  339. private unsafe struct ThrowFromManagedStackOverflowJob : IJob
  340. {
  341. [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
  342. private static int DoStackOverflow(ref int x)
  343. {
  344. // Copy just to make the stack grow.
  345. var copy = x;
  346. return copy + DoStackOverflow(ref x);
  347. }
  348. public int Int;
  349. public void Execute()
  350. {
  351. Int = DoStackOverflow(ref Int);
  352. }
  353. }
  354. //[Test]
  355. //[UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  356. public void ThrowFromManagedStackOverflow()
  357. {
  358. LogAssert.Expect(LogType.Exception, new Regex("StackOverflowException: The requested operation caused a stack overflow"));
  359. var jobData = new ThrowFromManagedStackOverflowJob() { Int = 1 };
  360. jobData.Run();
  361. }
  362. }
  363. }