Skip to content

Commit cbc2d72

Browse files
fix: 32bit-ARMv7 crashes on android (#2654)
* fix Word alignment applied to batch message. Adjusted MaximumTransmissionUnitSize setter to round down to nearest word aligned size Adjusted DefaultNonFragmentedMessageMaxSize to round down to nearest word aligned size * Fixed tests The tools bytes measured tests were slightly modified --------- Co-authored-by: Kitty Draper <kitty.draper@unity3d.com>
1 parent 7ae8296 commit cbc2d72

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ private void OnEnable()
586586
/// <param name="size"></param>
587587
public int MaximumTransmissionUnitSize
588588
{
589-
set => MessageManager.NonFragmentedMessageMaxSize = value;
589+
set => MessageManager.NonFragmentedMessageMaxSize = value & ~7; // Round down to nearest word aligned size
590590
get => MessageManager.NonFragmentedMessageMaxSize;
591591
}
592592

com.unity.netcode.gameobjects/Runtime/Messaging/NetworkBatchHeader.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ internal struct NetworkBatchHeader : INetworkSerializeByMemcpy
1212
/// </summary>
1313
public ushort Magic;
1414

15+
/// <summary>
16+
/// Total number of messages in the batch.
17+
/// </summary>
18+
public ushort BatchCount;
19+
1520
/// <summary>
1621
/// Total number of bytes in the batch.
1722
/// </summary>
@@ -22,9 +27,5 @@ internal struct NetworkBatchHeader : INetworkSerializeByMemcpy
2227
/// </summary>
2328
public ulong BatchHash;
2429

25-
/// <summary>
26-
/// Total number of messages in the batch.
27-
/// </summary>
28-
public ushort BatchCount;
2930
}
3031
}

com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ internal uint GetMessageType(Type t)
9595
return m_MessageTypes[t];
9696
}
9797

98-
public const int DefaultNonFragmentedMessageMaxSize = 1300;
98+
public const int DefaultNonFragmentedMessageMaxSize = 1300 & ~7; // Round down to nearest word aligned size (1296)
9999
public int NonFragmentedMessageMaxSize = DefaultNonFragmentedMessageMaxSize;
100100
public int FragmentedMessageMaxSize = int.MaxValue;
101101

@@ -829,11 +829,17 @@ internal unsafe void ProcessSendQueues()
829829
// Skipping the Verify and sneaking the write mark in because we know it's fine.
830830
queueItem.Writer.Handle->AllowedWriteMark = sizeof(NetworkBatchHeader);
831831
#endif
832-
queueItem.BatchHeader.BatchHash = XXHash.Hash64(queueItem.Writer.GetUnsafePtr() + sizeof(NetworkBatchHeader), queueItem.Writer.Length - sizeof(NetworkBatchHeader));
833832

834-
queueItem.BatchHeader.BatchSize = queueItem.Writer.Length;
833+
834+
var alignedLength = (queueItem.Writer.Length + 7) & ~7;
835+
queueItem.Writer.TryBeginWrite(alignedLength);
836+
837+
queueItem.BatchHeader.BatchHash = XXHash.Hash64(queueItem.Writer.GetUnsafePtr() + sizeof(NetworkBatchHeader), alignedLength - sizeof(NetworkBatchHeader));
838+
839+
queueItem.BatchHeader.BatchSize = alignedLength;
835840

836841
queueItem.Writer.WriteValue(queueItem.BatchHeader);
842+
queueItem.Writer.Seek(alignedLength);
837843

838844

839845
try

com.unity.netcode.gameobjects/Tests/Runtime/Metrics/TransportBytesMetricsTests.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public IEnumerator TrackTotalNumberOfBytesSent()
4141
}
4242

4343
Assert.True(observer.Found);
44-
Assert.AreEqual(FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead, observer.Value);
44+
Assert.AreEqual(((FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead) + 7) & ~7, observer.Value);
4545
}
4646

4747
[UnityTest]
@@ -61,8 +61,6 @@ public IEnumerator TrackTotalNumberOfBytesReceived()
6161
writer.Dispose();
6262
}
6363

64-
65-
6664
var nbFrames = 0;
6765
while (!observer.Found || nbFrames < 10)
6866
{
@@ -71,7 +69,7 @@ public IEnumerator TrackTotalNumberOfBytesReceived()
7169
}
7270

7371
Assert.True(observer.Found);
74-
Assert.AreEqual(FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead, observer.Value);
72+
Assert.AreEqual(((FastBufferWriter.GetWriteSize(messageName) + k_MessageOverhead) + 7) & ~7, observer.Value);
7573
}
7674

7775
private class TotalBytesObserver : IMetricObserver
@@ -89,12 +87,22 @@ public TotalBytesObserver(IMetricDispatcher dispatcher, DirectionalMetricInfo me
8987

9088
public long Value { get; private set; }
9189

90+
private int m_BytesFoundCounter;
91+
private long m_TotalBytes;
92+
9293
public void Observe(MetricCollection collection)
9394
{
9495
if (collection.TryGetCounter(m_MetricInfo.Id, out var counter) && counter.Value > 0)
9596
{
96-
Found = true;
97-
Value = counter.Value;
97+
// Don't assign another observed value once one is already observed
98+
if (!Found)
99+
{
100+
Found = true;
101+
Value = counter.Value;
102+
m_TotalBytes += ((counter.Value + 7) & ~7);
103+
m_BytesFoundCounter++;
104+
UnityEngine.Debug.Log($"[{m_BytesFoundCounter}] Bytes Observed {counter.Value} | Total Bytes Observed: {m_TotalBytes}");
105+
}
98106
}
99107
}
100108
}

0 commit comments

Comments
 (0)