Search Unity

Bug Span.Slice not working in Bursted Job when safety checks is on

Discussion in 'Burst' started by iamarugin, Jun 24, 2022.

  1. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Burst 1.6.6
    Unity 2021.3.4

    So I have a job:

    Code (CSharp):
    1. [BurstCompile]
    2.   public struct SDGridJob : IJobParallelFor {
    3.     [ReadOnly] public NativeArray<float3> hexagons;
    4.     [ReadOnly] public NativeArray<float3> positions;
    5.     [ReadOnly] public NativeArray<float3> prevPositions;
    6.  
    7.     [WriteOnly] public NativeList<GridImpact>.ParallelWriter gridImpacts;
    8.  
    9.     public float outerRadius, innerRadius;
    10.     public int2 gridSize;
    11.     public float3 gridPosition;
    12.  
    13.     private const int searchRange = 1;
    14.  
    15.     public void Execute(int index) {
    16.       var position = positions[index];
    17.       var hashedPos = HC.CubeFromLocalPosition(position - gridPosition, outerRadius, innerRadius);
    18.    
    19.       Span<int3> neighbors = stackalloc int3[HC.HexagonsInRange(searchRange)];
    20.       HC.GetSpiralHexagons(hashedPos, 1, neighbors);
    21.       for(int i = 0; i < neighbors.Length; i++) {
    22.         var neighbor = neighbors[i];
    23.         var idx = HC.IndexFromCube(neighbor, gridSize);
    24.         if(idx > 0 && idx < hexagons.Length) {
    25.           var distance = math.sdHexPrism(hexagons[idx], new float2(outerRadius, 0), position);
    26.           if(distance < 0) {
    27.             gridImpacts.AddNoResize(new GridImpact {
    28.               index = index,
    29.               coords = neighbor,
    30.               position = position + math.normalize(prevPositions[index] - position) * math.abs(distance)
    31.             });
    32.           }
    33.         }
    34.       }
    35.     }
    36.   }
    Which fails in the function GetSpiralHexagons on hexagons.Slice

    Code (CSharp):
    1.     public static void GetSpiralHexagons(int3 center, int radius, Span<int3> hexagons) {
    2.       hexagons[0] = center;
    3.       for(int i = 1; i <= radius; i++) {
    4.         var index = HexagonsInRange(i - 1);
    5.         var slice = hexagons.Slice(index, HexagonsInRange(i) - index);
    6.         GetRingHexagons(center, i, slice);
    7.       }
    8.     }
    With the exception:

    Code (CSharp):
    1. System.ArgumentOutOfRangeException: start + length is less than zero or greater than Length..
    2. This Exception was thrown from a job compiled with Burst, which has limited exception support.
    3. 0x00007ff88f9f1bb7 (592d9ce4b8e13077f6d22783dbc5cc4) [HexCoordinates.cs:100] Model.HexCoordinates.GetSpiralHexagons
    4. 0x00007ff88f9f13fb (592d9ce4b8e13077f6d22783dbc5cc4) [unknown:0] Unity.Jobs.IJobParallelForExtensions.ParallelForJobStruct`1<Model.SDGridJob>.Execute
    5. 0x00007ff88f9f1011 (592d9ce4b8e13077f6d22783dbc5cc4) A2BB5B4F582043E0
    6. 0x00007ff76b606cc0 (Unity) ExecuteJob
    7. 0x00007ff76b606f75 (Unity) ExecuteJobCopyData
    8. 0x00007ff76b607bcf (Unity) ForwardJobForEachToManaged
    9. 0x00007ff76b60319c (Unity) JobQueue::Exec
    10. 0x00007ff76b60345a (Unity) JobQueue::ExecuteJobFromHighPriorityStack
    11. 0x00007ff76b6039e9 (Unity) JobQueue::ProcessJobs
    12. 0x00007ff76b605a4f (Unity) JobQueue::WorkLoop
    13. 0x00007ff76b7fdd07 (Unity) Thread::RunThreadWrapper
    14. 0x00007ff8c81b7034 (KERNEL32) BaseThreadInitThunk
    15. 0x00007ff8c9282651 (ntdll) RtlUserThreadStart
    16.  
    After that console is full of errors of unfreed ullocation, recompiling is not helping, only Unity restart solves it.
    If I turn off collection checks or disable Burst - everything is working as expected. I've double checked Span.Slice receives correct values. Maybe I am missing something here?
     
  2. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    The same behaviour for Burst 1.7.3
     
  3. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    Can you give me the Length of hexagons as you enter that function, and the start and length you are using when taking a slice within the function please?
     
  4. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    Oh and does HexagonsInRange return an int?
     
  5. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Hey, here is a HexagonsInRange method:

    Code (CSharp):
    1.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
    2.     public static int HexagonsInRange(int range) {
    3.       return 1 + 3 * range * (range + 1);
    4.     }
    The values of start and length before exception:

    index: 1, length 6

    upload_2022-6-27_14-58-49.png

    upload_2022-6-27_14-59-2.png
     
  6. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    And the length of hexagons is 7
     
  7. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    I can repro - guess who introduced an off by one bug in the slice-with-length safety check :D

    The bug is that your start + length == the original Length (so 1 + 6 == 7), and I did a less than check instead of less than equal for the safety check.

    I'll do some PRs and we'll get a fix out.
     
    TheOtherMonarch and iamarugin like this.
  8. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    This is great, thank you!
     
  9. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Hey, will be this fix released for Burst 1.7.x or it will be done for 1.8.x only?
     
  10. fabrizio_unity

    fabrizio_unity

    Unity Technologies

    Joined:
    May 3, 2018
    Posts:
    47
    Hello,

    the fix will also be part of 1.7.4 release, which we are testing right now so it should be out this week unless we hit further road blockers.
    Hopefully our ETA works for you.

    The Burst Team
     
  11. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    This is great, thank you!