0vercl0k / wtf

wtf is a distributed, code-coverage guided, customizable, cross-platform snapshot-based fuzzer designed for attacking user and / or kernel-mode targets running on Microsoft Windows and Linux user-mode (experimental!).
MIT License
1.45k stars 128 forks source link

std::shuffle leads to different behavior on Windows & Linux #170

Open 0vercl0k opened 1 year ago

0vercl0k commented 1 year ago

I just discovered that std::shuffle's implementation is not defined by the standard so its behavior might differ per libc implementation.

Note that the implementation is not dictated by the standard, so even if you use exactly the same RandomFunc or URBG (Uniform Random Number Generator) you may get different results with different standard library implementations.

I am definitely observing different behaviors w/ the same seeds on Windows / Ubuntu. MutationDispatcher::Mutate_ShuffleBytes from libfuzzer uses it which leads to the generation of different testcases even w/ the same seeds, ugh:

    size_t MutationDispatcher::Mutate_ShuffleBytes(uint8_t* Data, size_t Size,
        size_t MaxSize) {
        if (Size > MaxSize || Size == 0) return 0;
        size_t ShuffleAmount =
            Rand(std::min(Size, (size_t)8)) + 1; // [1,8] and <= Size.
        size_t ShuffleStart = Rand(Size - ShuffleAmount);
        assert(ShuffleStart + ShuffleAmount <= Size);
        std::shuffle(Data + ShuffleStart, Data + ShuffleStart + ShuffleAmount, Rand);
        return Size;
    }
0vercl0k commented 1 year ago

Actually, I also found that std::uniform_int_distribution has annoying behaviors I wasn't aware of (sigh):