summaryrefslogtreecommitdiff
path: root/src/tools/fuzzing/random.cpp
diff options
context:
space:
mode:
authorThomas Lively <7121787+tlively@users.noreply.github.com>2021-11-04 11:05:01 -0700
committerGitHub <noreply@github.com>2021-11-04 11:05:01 -0700
commitf1947db456df4576ac857a535f16cba3ffbfdc37 (patch)
treee8b9cb213dc3cfc1babce105abb7802958efd370 /src/tools/fuzzing/random.cpp
parentab66e9ab1210a87d1db8ebe93cf8463eafe34e33 (diff)
downloadbinaryen-f1947db456df4576ac857a535f16cba3ffbfdc37.tar.gz
binaryen-f1947db456df4576ac857a535f16cba3ffbfdc37.tar.bz2
binaryen-f1947db456df4576ac857a535f16cba3ffbfdc37.zip
[NFC] Factor fuzzer randomness into a separate utility (#4304)
In preparation for using it from a separate file specifically for generating random HeapTypes that has no need to depend on all of fuzzing.h.
Diffstat (limited to 'src/tools/fuzzing/random.cpp')
-rw-r--r--src/tools/fuzzing/random.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/tools/fuzzing/random.cpp b/src/tools/fuzzing/random.cpp
new file mode 100644
index 000000000..38a86924e
--- /dev/null
+++ b/src/tools/fuzzing/random.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2021 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "literal.h"
+
+#include "random.h"
+
+namespace wasm {
+
+Random::Random(std::vector<char>&& bytes) : bytes(std::move(bytes)) {
+ // Ensure there is *some* input to be read.
+ if (bytes.empty()) {
+ bytes.push_back(0);
+ }
+}
+
+int8_t Random::get() {
+ if (pos == bytes.size()) {
+ // We ran out of input; go back to the start for more.
+ finishedInput = true;
+ pos = 0;
+ xorFactor++;
+ }
+ return bytes[pos++] ^ xorFactor;
+}
+
+int16_t Random::get16() {
+ auto temp = uint16_t(get()) << 8;
+ return temp | uint16_t(get());
+}
+
+int32_t Random::get32() {
+ auto temp = uint32_t(get16()) << 16;
+ return temp | uint32_t(get16());
+}
+
+int64_t Random::get64() {
+ auto temp = uint64_t(get32()) << 32;
+ return temp | uint64_t(get32());
+}
+
+float Random::getFloat() { return Literal(get32()).reinterpretf32(); }
+
+double Random::getDouble() { return Literal(get64()).reinterpretf64(); }
+
+uint32_t Random::upTo(uint32_t x) {
+ if (x == 0) {
+ return 0;
+ }
+ uint32_t raw;
+ if (x <= 255) {
+ raw = get();
+ } else if (x <= 65535) {
+ raw = get16();
+ } else {
+ raw = get32();
+ }
+ auto ret = raw % x;
+ // use extra bits as "noise" for later
+ xorFactor += raw / x;
+ return ret;
+}
+
+} // namespace wasm