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 14KB

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