Skip to content

Commit

Permalink
validate incoming payload before resuming epoch.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshmadan committed Apr 28, 2021
1 parent a31b52b commit 5ac18c2
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cs/src/core/Index/FasterLog/FasterLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ public long Enqueue(IReadOnlySpanBatch readOnlySpanBatch)
public unsafe bool TryEnqueue(byte[] entry, out long logicalAddress)
{
logicalAddress = 0;
var length = entry.Length;
int allocatedLength = headerSize + Align(length);
ValidateAllocatedLength(allocatedLength);

epoch.Resume();

var length = entry.Length;
logicalAddress = allocator.TryAllocateRetryNow(headerSize + Align(length));
logicalAddress = allocator.TryAllocateRetryNow(allocatedLength);
if (logicalAddress == 0)
{
epoch.Suspend();
Expand All @@ -274,11 +276,13 @@ public unsafe bool TryEnqueue(byte[] entry, out long logicalAddress)
public unsafe bool TryEnqueue(ReadOnlySpan<byte> entry, out long logicalAddress)
{
logicalAddress = 0;
var length = entry.Length;
int allocatedLength = headerSize + Align(length);
ValidateAllocatedLength(allocatedLength);

epoch.Resume();

var length = entry.Length;
logicalAddress = allocator.TryAllocateRetryNow(headerSize + Align(length));
logicalAddress = allocator.TryAllocateRetryNow(allocatedLength);
if (logicalAddress == 0)
{
epoch.Suspend();
Expand Down Expand Up @@ -1111,8 +1115,9 @@ private unsafe bool TryAppend(IReadOnlySpanBatch readOnlySpanBatch, out long log
allocatedLength += Align(readOnlySpanBatch.Get(i).Length) + headerSize;
}

epoch.Resume();
ValidateAllocatedLength(allocatedLength);

epoch.Resume();
logicalAddress = allocator.TryAllocateRetryNow(allocatedLength);
if (logicalAddress == 0)
{
Expand Down Expand Up @@ -1303,5 +1308,12 @@ private unsafe void SetHeader(int length, byte* dest)
*(ulong*)dest = Utility.XorBytes(dest + 8, length + 4);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ValidateAllocatedLength(int numSlots)
{
if (numSlots > allocator.PageSize)
throw new FasterException("Entry does not fit on page");
}
}
}

0 comments on commit 5ac18c2

Please sign in to comment.