Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using UnityBenchShared;
  8. using static Unity.Burst.CompilerServices.Aliasing;
  9. namespace Burst.Compiler.IL.Tests
  10. {
  11. internal partial class Aliasing
  12. {
  13. public unsafe struct NoAliasField
  14. {
  15. [NoAlias]
  16. public int* ptr1;
  17. [NoAlias]
  18. public int* ptr2;
  19. public void Compare(ref NoAliasField other)
  20. {
  21. // Check that we can definitely alias with another struct of the same type as us.
  22. ExpectAliased(in this, in other);
  23. }
  24. public void Compare(ref ContainerOfManyNoAliasFields other)
  25. {
  26. // Check that we can definitely alias with another struct which contains the same type as ourself.
  27. ExpectAliased(in this, in other);
  28. }
  29. public class Provider : IArgumentProvider
  30. {
  31. public object Value => new NoAliasField { ptr1 = null, ptr2 = null };
  32. }
  33. }
  34. public unsafe struct ContainerOfManyNoAliasFields
  35. {
  36. public NoAliasField s0;
  37. public NoAliasField s1;
  38. [NoAlias]
  39. public NoAliasField s2;
  40. [NoAlias]
  41. public NoAliasField s3;
  42. public class Provider : IArgumentProvider
  43. {
  44. public object Value => new ContainerOfManyNoAliasFields { s0 = new NoAliasField { ptr1 = null, ptr2 = null }, s1 = new NoAliasField { ptr1 = null, ptr2 = null }, s2 = new NoAliasField { ptr1 = null, ptr2 = null }, s3 = new NoAliasField { ptr1 = null, ptr2 = null } };
  45. }
  46. }
  47. [StructLayout(LayoutKind.Explicit)]
  48. public struct Union
  49. {
  50. [FieldOffset(0)]
  51. public ulong a;
  52. [FieldOffset(1)]
  53. public int b;
  54. [FieldOffset(5)]
  55. public float c;
  56. public class Provider : IArgumentProvider
  57. {
  58. public object Value => new Union { a = 4242424242424242, b = 13131313, c = 42.0f };
  59. }
  60. }
  61. public unsafe struct LinkedList
  62. {
  63. public LinkedList* next;
  64. public class Provider : IArgumentProvider
  65. {
  66. public object Value => new LinkedList { next = null };
  67. }
  68. }
  69. [NoAlias]
  70. public unsafe struct NoAliasWithContentsStruct
  71. {
  72. public void* ptr0;
  73. public void* ptr1;
  74. public class Provider : IArgumentProvider
  75. {
  76. public object Value => new NoAliasWithContentsStruct { ptr0 = null, ptr1 = null };
  77. }
  78. }
  79. [NoAlias]
  80. public unsafe struct DoesAliasWithSubStructPointersStruct : IDisposable
  81. {
  82. public NoAliasWithContentsStruct* s;
  83. public void* ptr;
  84. public class Provider : IArgumentProvider
  85. {
  86. public object Value
  87. {
  88. get
  89. {
  90. var noAliasSubStruct = (NoAliasWithContentsStruct*)UnsafeUtility.Malloc(UnsafeUtility.SizeOf<NoAliasWithContentsStruct>(), UnsafeUtility.AlignOf<NoAliasWithContentsStruct>(), Allocator.Temp);
  91. noAliasSubStruct->ptr0 = null;
  92. noAliasSubStruct->ptr1 = null;
  93. var s = new DoesAliasWithSubStructPointersStruct { s = noAliasSubStruct, ptr = null };
  94. return s;
  95. }
  96. }
  97. }
  98. public void Dispose()
  99. {
  100. UnsafeUtility.Free(s, Allocator.Temp);
  101. }
  102. }
  103. [TestCompiler(typeof(NoAliasField.Provider))]
  104. public static unsafe void CheckNoAliasFieldWithItself(ref NoAliasField s)
  105. {
  106. // Check that they correctly alias with themselves.
  107. ExpectAliased(s.ptr1, s.ptr1);
  108. ExpectAliased(s.ptr2, s.ptr2);
  109. }
  110. [TestCompiler(typeof(NoAliasField.Provider))]
  111. public static unsafe void CheckNoAliasFieldWithAnotherPointer(ref NoAliasField s)
  112. {
  113. // Check that they do not alias each other because of the [NoAlias] on the ptr1 field above.
  114. ExpectNotAliased(s.ptr1, s.ptr2);
  115. }
  116. [TestCompiler(typeof(NoAliasField.Provider))]
  117. public static unsafe void CheckNoAliasFieldWithNull(ref NoAliasField s)
  118. {
  119. // Check that comparing a pointer with null is no alias.
  120. ExpectNotAliased(s.ptr1, null);
  121. }
  122. [TestCompiler(typeof(NoAliasField.Provider))]
  123. public static unsafe void CheckAliasFieldWithNull(ref NoAliasField s)
  124. {
  125. // Check that comparing a pointer with null is no alias.
  126. ExpectNotAliased(s.ptr2, null);
  127. }
  128. [MethodImpl(MethodImplOptions.NoInlining)]
  129. private static unsafe void NoAliasInfoSubFunctionAlias(int* a, int* b)
  130. {
  131. ExpectAliased(a, b);
  132. }
  133. [TestCompiler(typeof(NoAliasField.Provider))]
  134. public static unsafe void CheckNoAliasFieldSubFunctionAlias(ref NoAliasField s)
  135. {
  136. NoAliasInfoSubFunctionAlias(s.ptr1, s.ptr1);
  137. }
  138. [TestCompiler(typeof(NoAliasField.Provider))]
  139. public static unsafe void CheckCompareWithItself(ref NoAliasField s)
  140. {
  141. s.Compare(ref s);
  142. }
  143. [MethodImpl(MethodImplOptions.NoInlining)]
  144. private static unsafe void AliasInfoSubFunctionNoAlias([NoAlias] int* a, int* b)
  145. {
  146. ExpectNotAliased(a, b);
  147. }
  148. [TestCompiler(typeof(NoAliasField.Provider))]
  149. public static unsafe void CheckNoAliasFieldSubFunctionWithNoAliasParameter(ref NoAliasField s)
  150. {
  151. AliasInfoSubFunctionNoAlias(s.ptr1, s.ptr1);
  152. }
  153. [MethodImpl(MethodImplOptions.NoInlining)]
  154. private static unsafe void AliasInfoSubFunctionTwoSameTypedStructs(ref NoAliasField s0, ref NoAliasField s1)
  155. {
  156. // Check that they do not alias within their own structs.
  157. ExpectNotAliased(s0.ptr1, s0.ptr2);
  158. ExpectNotAliased(s1.ptr1, s1.ptr2);
  159. // But that they do alias across structs.
  160. ExpectAliased(s0.ptr1, s1.ptr1);
  161. ExpectAliased(s0.ptr1, s1.ptr2);
  162. ExpectAliased(s0.ptr2, s1.ptr1);
  163. ExpectAliased(s0.ptr2, s1.ptr2);
  164. }
  165. [TestCompiler(typeof(NoAliasField.Provider), typeof(NoAliasField.Provider))]
  166. public static unsafe void CheckNoAliasFieldAcrossTwoSameTypedStructs(ref NoAliasField s0, ref NoAliasField s1)
  167. {
  168. AliasInfoSubFunctionTwoSameTypedStructs(ref s0, ref s1);
  169. }
  170. [TestCompiler(4, 13)]
  171. public static void CheckNoAliasRefs([NoAlias] ref int a, ref int b)
  172. {
  173. ExpectAliased(in a, in a);
  174. ExpectAliased(in b, in b);
  175. ExpectNotAliased(in a, in b);
  176. }
  177. [TestCompiler(4, 13.53f)]
  178. public static void CheckNoAliasRefsAcrossTypes([NoAlias] ref int a, ref float b)
  179. {
  180. ExpectNotAliased(in a, in b);
  181. }
  182. [TestCompiler(typeof(Union.Provider))]
  183. public static void CheckNoAliasRefsInUnion(ref Union u)
  184. {
  185. ExpectAliased(in u.a, in u.b);
  186. ExpectAliased(in u.a, in u.c);
  187. ExpectNotAliased(in u.b, in u.c);
  188. }
  189. [TestCompiler(typeof(ContainerOfManyNoAliasFields.Provider))]
  190. public static unsafe void CheckNoAliasOfSubStructs(ref ContainerOfManyNoAliasFields s)
  191. {
  192. // Since ptr1 and ptr2 have [NoAlias], they do not alias within the same struct instance.
  193. ExpectNotAliased(s.s0.ptr1, s.s0.ptr2);
  194. ExpectNotAliased(s.s1.ptr1, s.s1.ptr2);
  195. ExpectNotAliased(s.s2.ptr1, s.s2.ptr2);
  196. ExpectNotAliased(s.s3.ptr1, s.s3.ptr2);
  197. // Across s0 and s1 their pointers can alias each other though.
  198. ExpectAliased(s.s0.ptr1, s.s1.ptr1);
  199. ExpectAliased(s.s0.ptr1, s.s1.ptr2);
  200. ExpectAliased(s.s0.ptr2, s.s1.ptr1);
  201. ExpectAliased(s.s0.ptr2, s.s1.ptr2);
  202. // Also s2 can alias with s0 and s1 (because they do not have [NoAlias]).
  203. ExpectAliased(s.s2.ptr1, s.s0.ptr1);
  204. ExpectAliased(s.s2.ptr1, s.s0.ptr2);
  205. ExpectAliased(s.s2.ptr2, s.s1.ptr1);
  206. ExpectAliased(s.s2.ptr2, s.s1.ptr2);
  207. // Also s3 can alias with s0 and s1 (because they do not have [NoAlias]).
  208. ExpectAliased(s.s3.ptr1, s.s0.ptr1);
  209. ExpectAliased(s.s3.ptr1, s.s0.ptr2);
  210. ExpectAliased(s.s3.ptr2, s.s1.ptr1);
  211. ExpectAliased(s.s3.ptr2, s.s1.ptr2);
  212. // But s2 and s3 cannot alias each other (they both have [NoAlias]).
  213. ExpectNotAliased(s.s2.ptr1, s.s3.ptr1);
  214. ExpectNotAliased(s.s2.ptr1, s.s3.ptr2);
  215. ExpectNotAliased(s.s2.ptr2, s.s3.ptr1);
  216. ExpectNotAliased(s.s2.ptr2, s.s3.ptr2);
  217. }
  218. [TestCompiler(typeof(ContainerOfManyNoAliasFields.Provider))]
  219. public static unsafe void CheckNoAliasFieldCompareWithParentStruct(ref ContainerOfManyNoAliasFields s)
  220. {
  221. s.s0.Compare(ref s);
  222. s.s1.Compare(ref s);
  223. s.s2.Compare(ref s);
  224. s.s3.Compare(ref s);
  225. }
  226. [TestCompiler(typeof(LinkedList.Provider))]
  227. public static unsafe void CheckStructPointerOfSameTypeInStruct(ref LinkedList l)
  228. {
  229. ExpectAliased(in l, l.next);
  230. }
  231. [TestCompiler(typeof(NoAliasWithContentsStruct.Provider))]
  232. public static unsafe void CheckStructWithNoAlias(ref NoAliasWithContentsStruct s)
  233. {
  234. // Since NoAliasWithContentsStruct has [NoAlias] on the struct definition, it cannot alias with any pointers within the struct.
  235. ExpectNotAliased(in s, s.ptr0);
  236. ExpectNotAliased(in s, s.ptr1);
  237. }
  238. [TestCompiler(typeof(DoesAliasWithSubStructPointersStruct.Provider))]
  239. public static unsafe void CheckStructWithNoAliasAndSubStructs(ref DoesAliasWithSubStructPointersStruct s)
  240. {
  241. // Since DoesAliasWithSubStructPointersStruct has [NoAlias] on the struct definition, it cannot alias with any pointers within the struct.
  242. ExpectNotAliased(in s, s.s);
  243. ExpectNotAliased(in s, s.ptr);
  244. // s.s is a [NoAlias] struct, so it shouldn't alias with pointers within it.
  245. ExpectNotAliased(s.s, s.s->ptr0);
  246. ExpectNotAliased(s.s, s.s->ptr1);
  247. // But we don't know whether s.s and s.ptr alias.
  248. ExpectAliased(s.s, s.ptr);
  249. // And we cannot assume that s does not alias with the sub-pointers of s.s.
  250. ExpectAliased(in s, s.s->ptr0);
  251. ExpectAliased(in s, s.s->ptr1);
  252. }
  253. private unsafe struct AliasingWithSelf
  254. {
  255. public AliasingWithSelf* ptr;
  256. [MethodImpl(MethodImplOptions.NoInlining)]
  257. public void CheckAlias()
  258. {
  259. ExpectAliased(in this, ptr);
  260. }
  261. }
  262. [TestCompiler]
  263. public static unsafe void CheckAliasingWithSelf()
  264. {
  265. var s = new AliasingWithSelf { ptr = null };
  266. s.ptr = (AliasingWithSelf*) &s;
  267. s.CheckAlias();
  268. }
  269. private unsafe struct AliasingWithHiddenSelf
  270. {
  271. public void* ptr;
  272. [MethodImpl(MethodImplOptions.NoInlining)]
  273. public void CheckAlias()
  274. {
  275. ExpectAliased(in this, ptr);
  276. }
  277. }
  278. [TestCompiler]
  279. public static unsafe void CheckAliasingWithHiddenSelf()
  280. {
  281. var s = new AliasingWithHiddenSelf { ptr = null };
  282. s.ptr = &s;
  283. s.CheckAlias();
  284. }
  285. [MethodImpl(MethodImplOptions.NoInlining)]
  286. [return: NoAlias]
  287. private static unsafe int* NoAliasReturn(int size)
  288. {
  289. return (int*)UnsafeUtility.Malloc(size, 16, Allocator.Temp);
  290. }
  291. [TestCompiler(typeof(NoAliasField.Provider))]
  292. public static unsafe void CheckNoAliasReturn(ref NoAliasField s)
  293. {
  294. int* ptr1 = NoAliasReturn(40);
  295. int* ptr2 = NoAliasReturn(4);
  296. int* ptr3 = ptr2 + 4;
  297. byte* ptr4 = (byte*)ptr3 + 1;
  298. // Obviously it still aliases with itself even it we bitcast.
  299. ExpectAliased((char*)ptr1 + 4, ptr1 + 1);
  300. // We know that both allocations can't point to the same memory as
  301. // they are derived from Malloc!).
  302. ExpectNotAliased(ptr1, ptr2);
  303. // Since ptr3 derives from ptr2 it cannot alias with ptr1.
  304. ExpectNotAliased(ptr3, ptr1);
  305. // And the derefenced memory locations at ptr3 and ptr2 cannot alias
  306. // since ptr3 does not overlap the allocation in ptr2.
  307. ExpectNotAliased(in *ptr3, in *ptr2);
  308. // The pointers pt4 and ptr3 have overlapping ranges so they do alias.
  309. ExpectAliased(in *ptr4, in *ptr3);
  310. // The pointers cannot alias with anything else too!
  311. ExpectNotAliased(ptr1, in s);
  312. ExpectNotAliased(ptr1, s.ptr1);
  313. ExpectNotAliased(ptr1, s.ptr2);
  314. ExpectNotAliased(ptr2, in s);
  315. ExpectNotAliased(ptr2, s.ptr1);
  316. ExpectNotAliased(ptr2, s.ptr2);
  317. ExpectNotAliased(ptr3, in s);
  318. ExpectNotAliased(ptr3, s.ptr1);
  319. ExpectNotAliased(ptr3, s.ptr2);
  320. ExpectNotAliased(ptr4, in s);
  321. ExpectNotAliased(ptr4, s.ptr1);
  322. ExpectNotAliased(ptr4, s.ptr2);
  323. UnsafeUtility.Free(ptr1, Allocator.Temp);
  324. UnsafeUtility.Free(ptr2, Allocator.Temp);
  325. }
  326. [TestCompiler]
  327. public static unsafe void CheckMallocIsNoAlias()
  328. {
  329. int* ptr1 = (int*)UnsafeUtility.Malloc(sizeof(int) * 4, 16, Allocator.Temp);
  330. int* ptr2 = (int*)UnsafeUtility.Malloc(sizeof(int), 16, Allocator.Temp);
  331. ExpectNotAliased(ptr1, ptr2);
  332. UnsafeUtility.Free(ptr1, Allocator.Temp);
  333. UnsafeUtility.Free(ptr2, Allocator.Temp);
  334. }
  335. [MethodImpl(MethodImplOptions.NoInlining)]
  336. [return: NoAlias]
  337. private static unsafe int* BumpAlloc(int* alloca)
  338. {
  339. int location = alloca[0]++;
  340. return alloca + location;
  341. }
  342. [TestCompiler]
  343. public static unsafe void CheckBumpAllocIsNoAlias()
  344. {
  345. int* alloca = stackalloc int[128];
  346. // Store our size at the start of the alloca.
  347. alloca[0] = 1;
  348. int* ptr1 = BumpAlloc(alloca);
  349. int* ptr2 = BumpAlloc(alloca);
  350. // Our bump allocator will never return the same address twice.
  351. ExpectNotAliased(ptr1, ptr2);
  352. }
  353. [TestCompiler(42, 13, 56)]
  354. public static unsafe void CheckInRefOut(in int a, ref int b, out int c)
  355. {
  356. c = 42;
  357. // They obviously alias with themselves.
  358. ExpectAliased(in a, in a);
  359. ExpectAliased(in b, in b);
  360. ExpectAliased(in c, in c);
  361. // And alias with each other too.
  362. ExpectAliased(in a, in b);
  363. ExpectAliased(in a, in c);
  364. ExpectAliased(in b, in c);
  365. }
  366. [TestCompiler(42, 13)]
  367. public static unsafe void CheckOutOut(out int a, out int b)
  368. {
  369. a = 56;
  370. b = -4;
  371. ExpectAliased(in a, in b);
  372. }
  373. private struct SomeData
  374. {
  375. public int A;
  376. }
  377. [MethodImpl(MethodImplOptions.NoInlining)]
  378. private static unsafe void OutOfBoundsGEPNoAlias(SomeData* someData)
  379. {
  380. ExpectNotAliased(in someData[0], in someData[1]);
  381. }
  382. [TestCompiler]
  383. public static unsafe void CheckOutOfBoundsGEPNoAlias()
  384. {
  385. var someData = stackalloc SomeData[2];
  386. someData[0].A = 42;
  387. someData[1].A = 13;
  388. ExpectNotAliased(in someData[0], in someData[1]);
  389. OutOfBoundsGEPNoAlias(someData);
  390. }
  391. [StructLayout(LayoutKind.Explicit)]
  392. internal unsafe struct StructWithPadding
  393. {
  394. [NoAlias]
  395. [FieldOffset(0)]
  396. public int* A;
  397. [NoAlias]
  398. [FieldOffset(16)]
  399. public int* B;
  400. [FieldOffset(32)]
  401. public int* C;
  402. public class Provider : IArgumentProvider
  403. {
  404. public object Value => new StructWithPadding { A = null, B = null, C = null };
  405. }
  406. }
  407. [TestCompiler(typeof(StructWithPadding.Provider))]
  408. public static unsafe void CheckAliasingOfStructWithPadding(ref StructWithPadding x)
  409. {
  410. ExpectNotAliased(x.A, x.B);
  411. ExpectNotAliased(x.B, x.A);
  412. ExpectAliased(x.A, x.C);
  413. ExpectAliased(x.C, x.B);
  414. ExpectAliased(x.C, x.A);
  415. ExpectAliased(x.B, x.C);
  416. }
  417. }
  418. }