Skip to content

Commit

Permalink
Handle sessionId as ulong instead of long in EventPipeSession.cs. (#4345
Browse files Browse the repository at this point in the history
)

Session id is a uint64 in runtime as well as specified as a uint64 in
IPC protocol,
https://github.com/dotnet/diagnostics/blob/main/documentation/design-docs/ipc-protocol.md#returns-as-an-ipc-message-payload.

EventPipeSession.cs however handled it as a long instead of an ulong,
currently that doesn't affect release builds since it doesn't do much
with the id except passing it back to stop the session, but in debug
builds there is an assert that validates that session id > 0. On Android
physical devices it is not uncommon to get code and memory allocated at
high addresses, including having the high order bit set and when that
happens, EventPipeSession.cs will see a negative session id and assert
on debug builds.

Fix adjust session id as ulong inline with IPC protocol, it also makes
sure keyword serialized when starting a session is handled according to
IPC specification, but only when serialized into the payload, it will
still be typed as long inside EventPipeProvider since it is a public
property.
  • Loading branch information
lateralusX authored Oct 23, 2023
1 parent 093a0ea commit 6f9f639
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ namespace Microsoft.Diagnostics.NETCore.Client
{
public class EventPipeSession : IDisposable
{
private long _sessionId;
private ulong _sessionId;
private IpcEndpoint _endpoint;
private bool _disposedValue; // To detect redundant calls
private bool _stopped; // To detect redundant calls
private readonly IpcResponse _response;

private EventPipeSession(IpcEndpoint endpoint, IpcResponse response, long sessionId)
private EventPipeSession(IpcEndpoint endpoint, IpcResponse response, ulong sessionId)
{
_endpoint = endpoint;
_response = response;
Expand Down Expand Up @@ -93,7 +93,7 @@ private static EventPipeSession CreateSessionFromResponse(IpcEndpoint endpoint,
{
DiagnosticsClient.ValidateResponseMessage(response.Value.Message, operationName);

long sessionId = BinaryPrimitives.ReadInt64LittleEndian(new ReadOnlySpan<byte>(response.Value.Message.Payload, 0, 8));
ulong sessionId = BinaryPrimitives.ReadUInt64LittleEndian(new ReadOnlySpan<byte>(response.Value.Message.Payload, 0, 8));

EventPipeSession session = new(endpoint, response.Value, sessionId);
response = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public byte[] SerializeV2()
writer.Write(Providers.Count);
foreach (EventPipeProvider provider in Providers)
{
writer.Write(provider.Keywords);
writer.Write(unchecked((ulong)provider.Keywords));
writer.Write((uint)provider.EventLevel);

writer.WriteString(provider.Name);
Expand Down

0 comments on commit 6f9f639

Please sign in to comment.