|
| 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 | +// Instruments all indirect calls so that they work even if a function |
| 19 | +// pointer was cast incorrectly. For example, if you cast an int (int, float) |
| 20 | +// to an int (int, float, int) and call it natively, on most archs it will |
| 21 | +// happen to work, ignoring the extra param, whereas in wasm it will trap. |
| 22 | +// When porting code that relies on such casts working (like e.g. Python), |
| 23 | +// this pass may be useful. It sets a new "ABI" for indirect calls, in which |
| 24 | +// they all return an i64 and they have a fixed number of i64 params, and |
| 25 | +// the pass converts everything to go through that. |
| 26 | +// |
| 27 | +// This should work even with dynamic linking, however, the number of |
| 28 | +// params must be identical, i.e., the "ABI" must match. |
| 29 | + |
| 30 | +#include <wasm.h> |
| 31 | +#include <wasm-builder.h> |
| 32 | +#include <asm_v_wasm.h> |
| 33 | +#include <pass.h> |
| 34 | +#include <wasm-emscripten.h> |
| 35 | +#include <ir/literal-utils.h> |
| 36 | + |
| 37 | +namespace wasm { |
| 38 | + |
| 39 | +// This should be enough for everybody. (As described above, we need this |
| 40 | +// to match when dynamically linking, and also dynamic linking is why we |
| 41 | +// can't just detect this automatically in the module we see.) |
| 42 | +static const int NUM_PARAMS = 15; |
| 43 | + |
| 44 | +// Converts a value to the ABI type of i64. |
| 45 | +static Expression* toABI(Expression* value, Module* module) { |
| 46 | + Builder builder(*module); |
| 47 | + switch (value->type) { |
| 48 | + case i32: { |
| 49 | + value = builder.makeUnary(ExtendUInt32, value); |
| 50 | + break; |
| 51 | + } |
| 52 | + case i64: { |
| 53 | + // already good |
| 54 | + break; |
| 55 | + } |
| 56 | + case f32: { |
| 57 | + value = builder.makeUnary( |
| 58 | + ExtendUInt32, |
| 59 | + builder.makeUnary(ReinterpretFloat32, value) |
| 60 | + ); |
| 61 | + break; |
| 62 | + } |
| 63 | + case f64: { |
| 64 | + value = builder.makeUnary(ReinterpretFloat64, value); |
| 65 | + break; |
| 66 | + } |
| 67 | + case none: { |
| 68 | + // the value is none, but we need a value here |
| 69 | + value = builder.makeSequence( |
| 70 | + value, |
| 71 | + LiteralUtils::makeZero(i64, *module) |
| 72 | + ); |
| 73 | + break; |
| 74 | + } |
| 75 | + case unreachable: { |
| 76 | + // can leave it, the call isn't taken anyhow |
| 77 | + break; |
| 78 | + } |
| 79 | + default: { |
| 80 | + // SIMD may be interesting some day |
| 81 | + WASM_UNREACHABLE(); |
| 82 | + } |
| 83 | + } |
| 84 | + return value; |
| 85 | +} |
| 86 | + |
| 87 | +// Converts a value from the ABI type of i64 to the expected type |
| 88 | +static Expression* fromABI(Expression* value, Type type, Module* module) { |
| 89 | + Builder builder(*module); |
| 90 | + switch (type) { |
| 91 | + case i32: { |
| 92 | + value = builder.makeUnary(WrapInt64, value); |
| 93 | + break; |
| 94 | + } |
| 95 | + case i64: { |
| 96 | + // already good |
| 97 | + break; |
| 98 | + } |
| 99 | + case f32: { |
| 100 | + value = builder.makeUnary( |
| 101 | + ReinterpretInt32, |
| 102 | + builder.makeUnary(WrapInt64, value) |
| 103 | + ); |
| 104 | + break; |
| 105 | + } |
| 106 | + case f64: { |
| 107 | + value = builder.makeUnary(ReinterpretInt64, value); |
| 108 | + break; |
| 109 | + } |
| 110 | + case none: { |
| 111 | + value = builder.makeDrop(value); |
| 112 | + } |
| 113 | + case unreachable: { |
| 114 | + // can leave it, the call isn't taken anyhow |
| 115 | + break; |
| 116 | + } |
| 117 | + default: { |
| 118 | + // SIMD may be interesting some day |
| 119 | + WASM_UNREACHABLE(); |
| 120 | + } |
| 121 | + } |
| 122 | + return value; |
| 123 | +} |
| 124 | + |
| 125 | +struct ParallelFuncCastEmulation : public WalkerPass<PostWalker<ParallelFuncCastEmulation>> { |
| 126 | + bool isFunctionParallel() override { return true; } |
| 127 | + |
| 128 | + Pass* create() override { return new ParallelFuncCastEmulation(ABIType); } |
| 129 | + |
| 130 | + ParallelFuncCastEmulation(Name ABIType) : ABIType(ABIType) {} |
| 131 | + |
| 132 | + void visitCallIndirect(CallIndirect* curr) { |
| 133 | + if (curr->operands.size() > NUM_PARAMS) { |
| 134 | + Fatal() << "FuncCastEmulation::NUM_PARAMS needs to be at least " << |
| 135 | + curr->operands.size(); |
| 136 | + } |
| 137 | + for (Expression*& operand : curr->operands) { |
| 138 | + operand = toABI(operand, getModule()); |
| 139 | + } |
| 140 | + // Add extra operands as needed. |
| 141 | + while (curr->operands.size() < NUM_PARAMS) { |
| 142 | + curr->operands.push_back(LiteralUtils::makeZero(i64, *getModule())); |
| 143 | + } |
| 144 | + // Set the new types |
| 145 | + auto oldType = curr->type; |
| 146 | + curr->type = i64; |
| 147 | + curr->fullType = ABIType; |
| 148 | + // Fix up return value |
| 149 | + replaceCurrent(fromABI(curr, oldType, getModule())); |
| 150 | + } |
| 151 | + |
| 152 | +private: |
| 153 | + // the name of a type for a call with the right params and return |
| 154 | + Name ABIType; |
| 155 | +}; |
| 156 | + |
| 157 | +struct FuncCastEmulation : public Pass { |
| 158 | + void run(PassRunner* runner, Module* module) override { |
| 159 | + // we just need the one ABI function type for all indirect calls |
| 160 | + std::string sig = "j"; |
| 161 | + for (Index i = 0; i < NUM_PARAMS; i++) { |
| 162 | + sig += 'j'; |
| 163 | + } |
| 164 | + ABIType = ensureFunctionType(sig, module)->name; |
| 165 | + // Add a way for JS to call into the table (as our i64 ABI means an i64 |
| 166 | + // is returned when there is a return value, which JS engines will fail on), |
| 167 | + // using dynCalls |
| 168 | + EmscriptenGlueGenerator generator(*module); |
| 169 | + generator.generateDynCallThunks(); |
| 170 | + // Add a thunk for each function in the table, and do the call through it. |
| 171 | + std::unordered_map<Name, Name> funcThunks; |
| 172 | + for (auto& segment : module->table.segments) { |
| 173 | + for (auto& name : segment.data) { |
| 174 | + auto iter = funcThunks.find(name); |
| 175 | + if (iter == funcThunks.end()) { |
| 176 | + auto thunk = makeThunk(name, module); |
| 177 | + funcThunks[name] = thunk; |
| 178 | + name = thunk; |
| 179 | + } else { |
| 180 | + name = iter->second; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + // update call_indirects |
| 185 | + PassRunner subRunner(module, runner->options); |
| 186 | + subRunner.setIsNested(true); |
| 187 | + subRunner.add<ParallelFuncCastEmulation>(ABIType); |
| 188 | + subRunner.run(); |
| 189 | + } |
| 190 | + |
| 191 | +private: |
| 192 | + // the name of a type for a call with the right params and return |
| 193 | + Name ABIType; |
| 194 | + |
| 195 | + // Creates a thunk for a function, casting args and return value as needed. |
| 196 | + Name makeThunk(Name name, Module* module) { |
| 197 | + Name thunk = std::string("byn$fpcast-emu$") + name.str; |
| 198 | + if (module->getFunctionOrNull(thunk)) { |
| 199 | + Fatal() << "FuncCastEmulation::makeThunk seems a thunk name already in use. Was the pass already run on this code?"; |
| 200 | + } |
| 201 | + // The item in the table may be a function or a function import. |
| 202 | + auto* func = module->getFunctionOrNull(name); |
| 203 | + Import* imp = nullptr; |
| 204 | + if (!func) imp = module->getImport(name); |
| 205 | + std::vector<Type>& params = func ? func->params : module->getFunctionType(imp->functionType)->params; |
| 206 | + Type type = func ? func->result : module->getFunctionType(imp->functionType)->result; |
| 207 | + Builder builder(*module); |
| 208 | + std::vector<Expression*> callOperands; |
| 209 | + for (Index i = 0; i < params.size(); i++) { |
| 210 | + callOperands.push_back(fromABI(builder.makeGetLocal(i, i64), params[i], module)); |
| 211 | + } |
| 212 | + Expression* call = func ? (Expression*)builder.makeCall(name, callOperands, type) |
| 213 | + : (Expression*)builder.makeCallImport(name, callOperands, type); |
| 214 | + std::vector<Type> thunkParams; |
| 215 | + for (Index i = 0; i < NUM_PARAMS; i++) { |
| 216 | + thunkParams.push_back(i64); |
| 217 | + } |
| 218 | + auto* thunkFunc = builder.makeFunction( |
| 219 | + thunk, |
| 220 | + std::move(thunkParams), |
| 221 | + i64, |
| 222 | + {}, // no vars |
| 223 | + toABI(call, module) |
| 224 | + ); |
| 225 | + thunkFunc->type = ABIType; |
| 226 | + module->addFunction(thunkFunc); |
| 227 | + return thunk; |
| 228 | + } |
| 229 | +}; |
| 230 | + |
| 231 | +Pass* createFuncCastEmulationPass() { |
| 232 | + return new FuncCastEmulation(); |
| 233 | +} |
| 234 | + |
| 235 | +} // namespace wasm |
0 commit comments