Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
0vercl0k opened this issue May 16, 2023 · 1 comment
Open

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

0vercl0k opened this issue May 16, 2023 · 1 comment
Assignees

Comments

@0vercl0k
Copy link
Owner

0vercl0k commented May 16, 2023

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 0vercl0k self-assigned this May 16, 2023
@0vercl0k
Copy link
Owner Author

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

  • The sequence of number it'll generate w/ the same seeded generator can differ per libc implementation; the latest Windows/msvc, Linux/gcc versions print the same one, but the latest Linux/clang version doesn't.
  • On top of the above, the number of times it gets a number off the generator is different across at least Windows & Linux; this basically also desynchronize testcase generation across platform (example)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant