Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty byte[] bug in EventSource #52602

Merged
merged 6 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
Expand All @@ -18,7 +19,7 @@ public partial class TestsWriteEventToListener
{
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/21569", TargetFrameworkMonikers.NetFramework)]
public void Test_WriteEvent_ArgsBasicTypes()
public unsafe void Test_WriteEvent_ArgsBasicTypes()
{
TestUtilities.CheckNoEventSourcesRunning("Start");

Expand Down Expand Up @@ -104,11 +105,42 @@ public void Test_WriteEvent_ArgsBasicTypes()

#region Validate byte array arguments

var rng = new Random(42);

byte[] arr = new byte[20];
rng.NextBytes(arr);
log.EventWithByteArray(arr);
Assert.Equal(52, LoudListener.t_lastEvent.EventId);
Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(arr.Length, ((byte[])LoudListener.t_lastEvent.Payload[0]).Length);
Assert.Equal(arr, (byte[])LoudListener.t_lastEvent.Payload[0]);

arr = new byte[20];
rng.NextBytes(arr);
log.EventWithByteArrayCustom(arr);
Assert.Equal(53, LoudListener.t_lastEvent.EventId);
Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(arr, (byte[])LoudListener.t_lastEvent.Payload[0]);

arr = new byte[0];
log.EventWithByteArrayCustom(arr);
Assert.Equal(53, LoudListener.t_lastEvent.EventId);
Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(arr, (byte[])LoudListener.t_lastEvent.Payload[0]);

arr = new byte[20];
rng.NextBytes(arr);
fixed (byte* arrPtr = arr)
{
log.EventWithBytePointer(arrPtr, arr.Length);
}
Assert.Equal(54, LoudListener.t_lastEvent.EventId);
Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(arr, (byte[])LoudListener.t_lastEvent.Payload[0]);

log.EventWithBytePointer((byte*)IntPtr.Zero, 0);
Assert.Equal(54, LoudListener.t_lastEvent.EventId);
Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(Array.Empty<byte>(), (byte[])LoudListener.t_lastEvent.Payload[0]);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,46 @@ public void EventWithByteArray(byte[] arr)
this.WriteEvent(52, arr);
}

[Event(53)]
public unsafe void EventWithByteArrayCustom(byte[] arr)
{
// This implementation does not guard against passing null as DataPointer for Length == 0 case
arr ??= Array.Empty<byte>();

fixed (byte* arg1Ptr = arr)
{
int bufferLength = arr.Length;

const int NumEventDatas = 2;
var descrs = stackalloc EventData[NumEventDatas];

descrs[0] = new EventData
{
DataPointer = (IntPtr)(&bufferLength),
Size = sizeof(int)
};
descrs[1] = new EventData
{
DataPointer = (IntPtr)arg1Ptr,
Size = arr.Length
};

WriteEventCore(53, 2, descrs);
}
}

[Event(54)]
public unsafe void EventWithBytePointer(byte* ptr, int length)
{
var data = new EventData
{
DataPointer = (IntPtr)ptr,
Size = length
};

WriteEventCore(54, 1, &data);
}

#region Keywords / Tasks /Opcodes / Channels
public class Keywords
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
Expand Down Expand Up @@ -34,32 +33,31 @@ public static void EventSource_ExistsWithCorrectId()

[OuterLoop]
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50639")]
public void EventSource_EventsRaisedAsExpected()
{
RemoteExecutor.Invoke(() =>
RemoteExecutor.Invoke(async () =>
{
using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.Sockets", EventLevel.Verbose))
{
var events = new ConcurrentQueue<EventWrittenEventArgs>();
listener.RunWithCallback(events.Enqueue, () =>
await listener.RunWithCallbackAsync(events.Enqueue, async () =>
{
// Invoke several tests to execute code paths while tracing is enabled

new SendReceive_Sync(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter();
new SendReceive_Sync(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter();
await new SendReceive_Sync(null).SendRecv_Stream_TCP(IPAddress.Loopback, false);
await new SendReceive_Sync(null).SendRecv_Stream_TCP(IPAddress.Loopback, true);

new SendReceive_Task(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter();
new SendReceive_Task(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter();
await new SendReceive_Task(null).SendRecv_Stream_TCP(IPAddress.Loopback, false);
await new SendReceive_Task(null).SendRecv_Stream_TCP(IPAddress.Loopback, true);

new SendReceive_Eap(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter();
new SendReceive_Eap(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter();
await new SendReceive_Eap(null).SendRecv_Stream_TCP(IPAddress.Loopback, false);
await new SendReceive_Eap(null).SendRecv_Stream_TCP(IPAddress.Loopback, true);

new SendReceive_Apm(null).SendRecv_Stream_TCP(IPAddress.Loopback, false).GetAwaiter();
new SendReceive_Apm(null).SendRecv_Stream_TCP(IPAddress.Loopback, true).GetAwaiter();
await new SendReceive_Apm(null).SendRecv_Stream_TCP(IPAddress.Loopback, false);
await new SendReceive_Apm(null).SendRecv_Stream_TCP(IPAddress.Loopback, true);

new NetworkStreamTest().CopyToAsync_AllDataCopied(4096, true).GetAwaiter().GetResult();
new NetworkStreamTest().Timeout_Roundtrips().GetAwaiter().GetResult();
await new NetworkStreamTest().CopyToAsync_AllDataCopied(4096, true);
await new NetworkStreamTest().Timeout_Roundtrips();
});
Assert.DoesNotContain(events, ev => ev.EventId == 0); // errors from the EventSource itself
Assert.InRange(events.Count, 1, int.MaxValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,10 @@ private static unsafe void DecodeObjects(object?[] decodedObjects, ParameterInfo

BytePtr:
var blob = new byte[data->Size];
Marshal.Copy(dataPointer, blob, 0, blob.Length);
if (blob.Length != 0)
{
Marshal.Copy(dataPointer, blob, 0, blob.Length);
}
decoded = blob;
goto Store;

Expand Down