HouraiTeahouse / HouraiNetworking

Transport level library for peer-to-peer networking with multiple backends for the Unity.
MIT License
100 stars 16 forks source link

Investigate LZF Compressor memory access issues #6

Open james7132 opened 3 years ago

james7132 commented 3 years ago

As mentioned in #5, there are memory access issues with the LZFCompressor implementation that are causing Unity to crash. This should not be happening on any input.

With #5's merge, it will be disabled by default, but it is key in reducing bandwidth usage and ensuring messages are below max message size limits.

This should be investigated and reenabled once this has been resolved.

toipi commented 3 years ago

Hey! I'm pretty sure the crash in question happens when you try to decompress invalid data (e.g. uncompressed data as input, or input that got corrupted during transport despite having a valid checksum). This causes a StackOverflowException to be thrown and crashes Unity (possibly because of the unsafe context?) since the Decompress function stackallocs more memory upon failure and tries again until it succeeds.

The solution (on my end at least) was to add a sizeLimit parameter as an upper bound for the size of the decompression buffer. A reasonable value could be some constant depending on the use case, or a multiple of inputLength as an estimate for the highest possible decompressed size, but this kind of thinking might not cover all cases. The ideal value would be the available stack size since stackalloc is obviously stack-bound, but I couldn't find any resources on how that could be done. I'm also not sure if such an approach would play nice with async code. The C# reference page for stackalloc recommends setting a conservative size limit.

Some ideas for workarounds:

I don't currently use this library but you can test this with the following change to the Decompress function:

    /// <summary>
    /// Decompress input bytes.
    /// </summary>
    /// <param name="input">Bytes to decompress.</param>
    /// <param name="outputBuffer">Output/work buffer. Upon completion, will contain the output.</param>
    /// <param name="inputLength">Length of data in inputBytes.</param>
    /// <param name="startOffset">The offset into the input buffer to start decompressing from.</param>
    /// <param name="sizeLimit">The upper bound for the uncompressed output length in bytes. </param>
    /// <returns>Length of output. -1 if decompression fails.</returns>
    public static unsafe int Decompress(byte[] input, ref byte[] output, int inputLength, uint startOffset = 0, uint sizeLimit = 0)
    {
        if (sizeLimit == 0) sizeLimit = uint.MaxValue; // should be set to a reasonable upper bound that won't cause a stack overflow

        // If outputSize is 0, increase buffer size and try again.
        int outputSize = input.Length;
        fixed (byte* inputPtr = input)
        {
            while (true)
            {
                byte* buffer = stackalloc byte[outputSize];
                int count = TryDecompress(inputPtr + startOffset, buffer, inputLength, outputSize);
                outputSize *= 2;

                // enforce size limit to avoid stack overflow
                if (outputSize > sizeLimit)
                {
                    return -1;
                }

                if (count == 0) continue;
                CopyBuffer(buffer, ref output, count);
                return count;
            }
        }
    }

I don't definitively know if this is the only thing that would cause such a crash of course, but in my case it seemed to be. If the crash goes away and you start getting -1 as the return value, you can then log the offending bits of data to see what the underlying error is.

I'd send a proper fix but I'm not sure how one could decide a reasonable upper bound outside my own use case. Hope this is of some help anyway. Thank you for your work on the library btw! :)