|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | +// Instruments branch hints and their targets, adding logging that allows us to |
| 19 | +// see if the hints were valid or not. We turn |
| 20 | +// |
| 21 | +// @metadata.branch.hint B |
| 22 | +// if (condition) { |
| 23 | +// X |
| 24 | +// } else { |
| 25 | +// Y |
| 26 | +// } |
| 27 | +// |
| 28 | +// into |
| 29 | +// |
| 30 | +// @metadata.branch.hint B |
| 31 | +// ;; log the ID of the condition (123), the prediction (B), and the actual |
| 32 | +// ;; runtime result (temp == condition). |
| 33 | +// if (temp = condition; log(123, B, temp); temp) { |
| 34 | +// X |
| 35 | +// } else { |
| 36 | +// Y |
| 37 | +// } |
| 38 | +// |
| 39 | +// Concretely, we emit calls to this logging function: |
| 40 | +// |
| 41 | +// (import "fuzzing-support" "log-branch" |
| 42 | +// (func $log-branch (param i32 i32 i32)) ;; ID, prediction, actual |
| 43 | +// ) |
| 44 | +// |
| 45 | +// This can be used to verify that branch hints are accurate, by implementing |
| 46 | +// the import like this for example: |
| 47 | +// |
| 48 | +// imports['fuzzing-support']['log-branch'] = (id, prediction, actual) => { |
| 49 | +// // We only care about truthiness of the expected and actual values. |
| 50 | +// expected = +!!expected; |
| 51 | +// actual = +!!actual; |
| 52 | +// // Throw if the hint said this branch would be taken, but it was not, or |
| 53 | +// // vice versa. |
| 54 | +// if (expected != actual) throw `Bad branch hint! (${id})`; |
| 55 | +// }; |
| 56 | +// |
| 57 | + |
| 58 | +#include "ir/eh-utils.h" |
| 59 | +#include "ir/names.h" |
| 60 | +#include "ir/properties.h" |
| 61 | +#include "pass.h" |
| 62 | +#include "wasm-builder.h" |
| 63 | +#include "wasm.h" |
| 64 | + |
| 65 | +namespace wasm { |
| 66 | + |
| 67 | +namespace { |
| 68 | + |
| 69 | +// The branch id, which increments as we go. |
| 70 | +int branchId = 1; |
| 71 | + |
| 72 | +struct InstrumentBranchHints |
| 73 | + : public WalkerPass<PostWalker<InstrumentBranchHints>> { |
| 74 | + |
| 75 | + using Super = WalkerPass<PostWalker<InstrumentBranchHints>>; |
| 76 | + |
| 77 | + // The module and base names of our import. |
| 78 | + const Name MODULE = "fuzzing-support"; |
| 79 | + const Name BASE = "log-branch"; |
| 80 | + |
| 81 | + // The internal name of our import. |
| 82 | + Name logBranch; |
| 83 | + |
| 84 | + void visitIf(If* curr) { processCondition(curr); } |
| 85 | + |
| 86 | + void visitBreak(Break* curr) { |
| 87 | + if (curr->condition) { |
| 88 | + processCondition(curr); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // TODO: BrOn, but the condition there is not an i32 |
| 93 | + |
| 94 | + bool addedInstrumentation = false; |
| 95 | + |
| 96 | + template<typename T> void processCondition(T* curr) { |
| 97 | + if (curr->condition->type == Type::unreachable) { |
| 98 | + // This branch is not even reached. |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + auto likely = getFunction()->codeAnnotations[curr].branchLikely; |
| 103 | + if (!likely) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + Builder builder(*getModule()); |
| 108 | + |
| 109 | + // Pick an ID for this branch. |
| 110 | + int id = branchId++; |
| 111 | + |
| 112 | + // Instrument the condition. |
| 113 | + auto tempLocal = builder.addVar(getFunction(), Type::i32); |
| 114 | + auto* set = builder.makeLocalSet(tempLocal, curr->condition); |
| 115 | + auto* idConst = builder.makeConst(Literal(int32_t(id))); |
| 116 | + auto* guess = builder.makeConst(Literal(int32_t(*likely))); |
| 117 | + auto* get1 = builder.makeLocalGet(tempLocal, Type::i32); |
| 118 | + auto* log = builder.makeCall(logBranch, {idConst, guess, get1}, Type::none); |
| 119 | + auto* get2 = builder.makeLocalGet(tempLocal, Type::i32); |
| 120 | + curr->condition = builder.makeBlock({set, log, get2}); |
| 121 | + addedInstrumentation = true; |
| 122 | + } |
| 123 | + |
| 124 | + void doWalkFunction(Function* func) { |
| 125 | + Super::doWalkFunction(func); |
| 126 | + |
| 127 | + // Our added blocks may have caused nested pops. |
| 128 | + if (addedInstrumentation) { |
| 129 | + EHUtils::handleBlockNestedPops(func, *getModule()); |
| 130 | + addedInstrumentation = false; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + void doWalkModule(Module* module) { |
| 135 | + // Find our import, if we were already run on this module. |
| 136 | + for (auto& func : module->functions) { |
| 137 | + if (func->module == MODULE && func->base == BASE) { |
| 138 | + logBranch = func->name; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + // Otherwise, add it. |
| 143 | + if (!logBranch) { |
| 144 | + auto* func = module->addFunction(Builder::makeFunction( |
| 145 | + Names::getValidFunctionName(*module, BASE), |
| 146 | + Signature({Type::i32, Type::i32, Type::i32}, Type::none), |
| 147 | + {})); |
| 148 | + func->module = MODULE; |
| 149 | + func->base = BASE; |
| 150 | + logBranch = func->name; |
| 151 | + } |
| 152 | + |
| 153 | + // Walk normally, using logBranch as we go. |
| 154 | + Super::doWalkModule(module); |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +} // anonymous namespace |
| 159 | + |
| 160 | +Pass* createInstrumentBranchHintsPass() { return new InstrumentBranchHints(); } |
| 161 | + |
| 162 | +} // namespace wasm |
0 commit comments