|
| 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 the build with code to intercept all local reads and writes. |
| 19 | +// |
| 20 | +// gets: |
| 21 | +// |
| 22 | +// Before: |
| 23 | +// (get_local $x) |
| 24 | +// |
| 25 | +// After: |
| 26 | +// (call $get_TYPE |
| 27 | +// (i32.const n) // call id |
| 28 | +// (i32.const n) // local id |
| 29 | +// (get_local $x) |
| 30 | +// ) |
| 31 | +// |
| 32 | +// sets: |
| 33 | +// |
| 34 | +// Before: |
| 35 | +// (set_local $x (i32.const 1)) |
| 36 | +// |
| 37 | +// After: |
| 38 | +// (set_local $x |
| 39 | +// (call $set_TYPE |
| 40 | +// (i32.const n) // call id |
| 41 | +// (i32.const n) // local id |
| 42 | +// (i32.const 1) // value |
| 43 | +// ) |
| 44 | +// ) |
| 45 | + |
| 46 | +#include <wasm.h> |
| 47 | +#include <wasm-builder.h> |
| 48 | +#include <pass.h> |
| 49 | +#include "shared-constants.h" |
| 50 | +#include "asmjs/shared-constants.h" |
| 51 | +#include "asm_v_wasm.h" |
| 52 | + |
| 53 | +namespace wasm { |
| 54 | + |
| 55 | +Name get_i32("get_i32"); |
| 56 | +Name get_i64("get_i64"); |
| 57 | +Name get_f32("get_f32"); |
| 58 | +Name get_f64("get_f64"); |
| 59 | + |
| 60 | +Name set_i32("set_i32"); |
| 61 | +Name set_i64("set_i64"); |
| 62 | +Name set_f32("set_f32"); |
| 63 | +Name set_f64("set_f64"); |
| 64 | + |
| 65 | +struct InstrumentLocals : public WalkerPass<PostWalker<InstrumentLocals>> { |
| 66 | + void visitGetLocal(GetLocal* curr) { |
| 67 | + Builder builder(*getModule()); |
| 68 | + Name import; |
| 69 | + switch (curr->type) { |
| 70 | + case i32: import = get_i32; break; |
| 71 | + case i64: return; // TODO |
| 72 | + case f32: import = get_f32; break; |
| 73 | + case f64: import = get_f64; break; |
| 74 | + default: WASM_UNREACHABLE(); |
| 75 | + } |
| 76 | + replaceCurrent( |
| 77 | + builder.makeCallImport( |
| 78 | + import, |
| 79 | + { |
| 80 | + builder.makeConst(Literal(int32_t(id++))), |
| 81 | + builder.makeConst(Literal(int32_t(curr->index))), |
| 82 | + curr |
| 83 | + }, |
| 84 | + curr->type |
| 85 | + ) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + void visitSetLocal(SetLocal* curr) { |
| 90 | + Builder builder(*getModule()); |
| 91 | + Name import; |
| 92 | + switch (curr->value->type) { |
| 93 | + case i32: import = set_i32; break; |
| 94 | + case i64: return; // TODO |
| 95 | + case f32: import = set_f32; break; |
| 96 | + case f64: import = set_f64; break; |
| 97 | + case unreachable: return; // nothing to do here |
| 98 | + default: WASM_UNREACHABLE(); |
| 99 | + } |
| 100 | + curr->value = builder.makeCallImport( |
| 101 | + import, |
| 102 | + { |
| 103 | + builder.makeConst(Literal(int32_t(id++))), |
| 104 | + builder.makeConst(Literal(int32_t(curr->index))), |
| 105 | + curr->value |
| 106 | + }, |
| 107 | + curr->value->type |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + void visitModule(Module* curr) { |
| 112 | + addImport(curr, get_i32, "iiii"); |
| 113 | + addImport(curr, get_i64, "jiij"); |
| 114 | + addImport(curr, get_f32, "fiif"); |
| 115 | + addImport(curr, get_f64, "diid"); |
| 116 | + addImport(curr, set_i32, "iiii"); |
| 117 | + addImport(curr, set_i64, "jiij"); |
| 118 | + addImport(curr, set_f32, "fiif"); |
| 119 | + addImport(curr, set_f64, "diid"); |
| 120 | + } |
| 121 | + |
| 122 | +private: |
| 123 | + Index id = 0; |
| 124 | + |
| 125 | + void addImport(Module* wasm, Name name, std::string sig) { |
| 126 | + auto import = new Import; |
| 127 | + import->name = name; |
| 128 | + import->module = INSTRUMENT; |
| 129 | + import->base = name; |
| 130 | + import->functionType = ensureFunctionType(sig, wasm)->name; |
| 131 | + import->kind = ExternalKind::Function; |
| 132 | + wasm->addImport(import); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +Pass* createInstrumentLocalsPass() { |
| 137 | + return new InstrumentLocals(); |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace wasm |
0 commit comments