[PATCH] D21218: [LibFuzzer] Avoid using std::random_swap() due to platform differences and implement our own version.

Dan Liew via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 9 22:28:22 PDT 2016


delcypher added a comment.

@kcc: Sorry it took me quite a while to figure out exactly where divergence in mutation behavior was coming from but it does seem to be the use of `std::random_shuffle()` that is causing it which is rather sad. Here's a small test cases you can run to see the problem. You should fine that the printed result is different on Linux (with libstdc++) and OSX (with libcxx).

  #include <algorithm>
  #include <iostream>
  #include <random>
  #include <stdint.h>
  
  void printArray(uint8_t* X, size_t length) {
    for (size_t index=0; index < length; ++index) {
      std::cout << "0x" << std::hex << static_cast<unsigned>(X[index]) << ",";
    }
    std::cout << std::endl;
  }
  
  class Random {
   public:
    Random(unsigned int seed) : R(seed) {}
    size_t Rand() { return R(); }
    size_t RandBool() { return Rand() % 2; }
    size_t operator()(size_t n) { return n ? Rand() % n : 0; }
    std::mt19937 &Get_mt19937() { return R; }
   private:
    std::mt19937 R;
  };
  
  
  int main() {
    Random R(0);
    uint8_t X[7] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
    printArray(X, 7);
    // Shuffle
    std::random_shuffle(X + 2, X + 2 + 4, R);
    // With libstdc++ : 0x0,0x11,0x44,0x55,0x33,0x22,0x66,
    // With libcxx    : 0x0,0x11,0x22,0x33,0x55,0x44,0x66,
    printArray(X, 7);
    return 0;
  }

I'm not entirely sure the implementation I've picked to replace `random_shuffle` is the best as it required more iterations in the unit test to hit all the mutations we want to see (previously 524288 now its 566171).


http://reviews.llvm.org/D21218





More information about the llvm-commits mailing list