|
| 1 | +/* |
| 2 | + * Copyright 2017 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// |
| 18 | +// Shared execution result checking code |
| 19 | +// |
| 20 | + |
| 21 | +#include "wasm.h" |
| 22 | +#include "shell-interface.h" |
| 23 | + |
| 24 | +namespace wasm { |
| 25 | + |
| 26 | +static bool areBitwiseEqual(Literal a, Literal b) { |
| 27 | + if (a == b) return true; |
| 28 | + // accept equal nans if equal in all bits |
| 29 | + if (a.type != b.type) return false; |
| 30 | + if (a.type == f32) { |
| 31 | + return a.reinterpreti32() == b.reinterpreti32(); |
| 32 | + } else if (a.type == f64) { |
| 33 | + return a.reinterpreti64() == b.reinterpreti64(); |
| 34 | + } |
| 35 | + return false; |
| 36 | +} |
| 37 | + |
| 38 | +// gets execution results from a wasm module. this is useful for fuzzing |
| 39 | +// |
| 40 | +// we can only get results when there are no imports. we then call each method |
| 41 | +// that has a result, with some values |
| 42 | +struct ExecutionResults { |
| 43 | + std::map<Name, Literal> results; |
| 44 | + |
| 45 | + // get results of execution |
| 46 | + void get(Module& wasm) { |
| 47 | + if (wasm.imports.size() > 0) { |
| 48 | + std::cout << "[fuzz-exec] imports, so quitting\n"; |
| 49 | + return; |
| 50 | + } |
| 51 | + for (auto& func : wasm.functions) { |
| 52 | + if (func->result != none) { |
| 53 | + // this has a result |
| 54 | + results[func->name] = run(func.get(), wasm); |
| 55 | + std::cout << "[fuzz-exec] note result: " << func->name.str << " => " << results[func->name] << '\n'; |
| 56 | + } else { |
| 57 | + // no result, run it anyhow (it might modify memory etc.) |
| 58 | + run(func.get(), wasm); |
| 59 | + std::cout << "[fuzz-exec] no result for void func: " << func->name.str << '\n'; |
| 60 | + } |
| 61 | + } |
| 62 | + std::cout << "[fuzz-exec] " << results.size() << " results noted\n"; |
| 63 | + } |
| 64 | + |
| 65 | + // get current results and check them against previous ones |
| 66 | + void check(Module& wasm) { |
| 67 | + ExecutionResults optimizedResults; |
| 68 | + optimizedResults.get(wasm); |
| 69 | + if (optimizedResults != *this) { |
| 70 | + std::cout << "[fuzz-exec] optimization passes changed execution results"; |
| 71 | + abort(); |
| 72 | + } |
| 73 | + std::cout << "[fuzz-exec] " << results.size() << " results match\n"; |
| 74 | + } |
| 75 | + |
| 76 | + bool operator==(ExecutionResults& other) { |
| 77 | + for (auto& iter : results) { |
| 78 | + auto name = iter.first; |
| 79 | + if (other.results.find(name) != other.results.end()) { |
| 80 | + std::cout << "[fuzz-exec] comparing " << name << '\n'; |
| 81 | + if (!areBitwiseEqual(results[name], other.results[name])) { |
| 82 | + std::cout << "not identical!\n"; |
| 83 | + abort(); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + bool operator!=(ExecutionResults& other) { |
| 91 | + return !((*this) == other); |
| 92 | + } |
| 93 | + |
| 94 | + Literal run(Function* func, Module& wasm) { |
| 95 | + ShellExternalInterface interface; |
| 96 | + try { |
| 97 | + ModuleInstance instance(wasm, &interface); |
| 98 | + LiteralList arguments; |
| 99 | + // init hang support, if present |
| 100 | + if (wasm.getFunctionOrNull("hangLimitInitializer")) { |
| 101 | + instance.callFunction("hangLimitInitializer", arguments); |
| 102 | + } |
| 103 | + // call the method |
| 104 | + for (WasmType param : func->params) { |
| 105 | + // zeros in arguments TODO: more? |
| 106 | + arguments.push_back(Literal(param)); |
| 107 | + } |
| 108 | + return instance.callFunction(func->name, arguments); |
| 109 | + } catch (const TrapException&) { |
| 110 | + // may throw in instance creation (init of offsets) or call itself |
| 111 | + return Literal(); |
| 112 | + } |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +} // namespace wasm |
| 117 | + |
0 commit comments