Skip to content

Commit

Permalink
fix: il2cpp linux runtime SIGABRT crash with nanosockets (unity 2021.…
Browse files Browse the repository at this point in the history
…3.15) (#1116)

* Update NanoSockets.cs

* Add modifications notice

* Minor restructure.

* Blank spacing

* Enable Mirage.Sockets.Udp unsafe code directive

* Fix v2 suggested by FakeByte and James

* Fix attempt v2
  • Loading branch information
SoftwareGuy committed Dec 10, 2022
1 parent f4e75ea commit ff7148b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Assets/Mirage/Runtime/Sockets/Udp/Mirage.Sockets.Udp.asmdef
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "Mirage.Sockets.Udp",
"rootNamespace": "",
"references": [
"GUID:c0b2064c294eb174c9f3f7da398eb677"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
Expand Down
3 changes: 1 addition & 2 deletions Assets/Mirage/Runtime/Sockets/Udp/NanoEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ namespace Mirage.Sockets.Udp
{
public sealed class NanoEndPoint : IEndPoint, IEquatable<NanoEndPoint>
{
public Address address;
public Address address = new Address();

public NanoEndPoint(string host, ushort port)
{
address = new Address();
address.port = port;
UDP.SetHostName(ref address, host);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/*
* Lightweight UDP sockets abstraction for rapid implementation of message-oriented protocols
* Copyright (c) 2019 Stanislav Denisov
*
*
* NanoSockets modifications made by Mirage Team
* Copyright (c) 2022 Mirage Team and contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
Expand All @@ -25,6 +28,7 @@
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using Mirage.SocketLayer;

namespace NanoSockets
{
Expand Down Expand Up @@ -85,13 +89,22 @@ public override int GetHashCode()
return hash;
}

public override string ToString()

public unsafe override string ToString()
{
var ip = new StringBuilder(64);

NanoSockets.UDP.GetIP(ref this, ip, 64);

return string.Format("IP:{0} Port:{1}", ip, this.port);
// FIX: Unity IL2CPP SIGABRT in 2021.3.15 on Linux builds
// Problem: On Linux IL2CPP builds, it seems something with
// IL2CPP and StringBuilder causes SIGABRT to be emitted due to
// a bad free of a pointer: "free(): invalid pointer". Unity will then
// commit suicide.
// Solution: Allocate 64 bytes on the stack, tell NanoSockets to put the
// IP into that, then read as string in the return function. Tested and
// confirmed working on Manjaro x64 (Unity 2021.3.15).

// Attempt v2 (2022-12-09): Use unsafe pointer for the IP string.
var ptr = stackalloc char[64];
UDP.GetIP(ref this, (IntPtr)ptr, 64);
return $"IP: {new string(ptr)} Port: {this.Port}";
}

public static Address CreateFromIpPort(string ip, ushort port)
Expand Down
1 change: 0 additions & 1 deletion Assets/Mirage/Runtime/Sockets/Udp/UdpSocketFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ private static bool CheckNanosocketsSupport()
|| Application.isEditor;
#endif
}

}

public class EndPointWrapper : IEndPoint
Expand Down

0 comments on commit ff7148b

Please sign in to comment.