|
| 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 the build with code to intercept all memory.grow operations. |
| 19 | +// |
| 20 | +// Before: |
| 21 | +// (memory.grow (const.i32 6)) |
| 22 | +// |
| 23 | +// After: |
| 24 | +// (call $post_memory_grow |
| 25 | +// (i32.const 1) // ID |
| 26 | +// (memory.grow |
| 27 | +// (call $pre_memory_grow |
| 28 | +// (i32.const 1) // ID |
| 29 | +// (i32.const 6) // number of pages |
| 30 | +// ) |
| 31 | +// ) |
| 32 | +// ) |
| 33 | +// |
| 34 | + |
| 35 | +#include "asmjs/shared-constants.h" |
| 36 | +#include <pass.h> |
| 37 | +#include <wasm-builder.h> |
| 38 | + |
| 39 | +namespace wasm { |
| 40 | + |
| 41 | +static Name pre_memory_grow("pre_memory_grow"); |
| 42 | +static Name post_memory_grow("post_memory_grow"); |
| 43 | + |
| 44 | +struct InstrumentMemoryGrow |
| 45 | + : public WalkerPass<PostWalker<InstrumentMemoryGrow>> { |
| 46 | + // Adds calls to new imports. |
| 47 | + bool addsEffects() override { return true; } |
| 48 | + |
| 49 | + void visitMemoryGrow(MemoryGrow* curr) { |
| 50 | + id++; |
| 51 | + Builder builder(*getModule()); |
| 52 | + auto addressType = getModule()->getMemory(curr->memory)->addressType; |
| 53 | + curr->delta = |
| 54 | + builder.makeCall(pre_memory_grow, |
| 55 | + {builder.makeConst(int32_t(id)), curr->delta}, |
| 56 | + addressType); |
| 57 | + replaceCurrent(builder.makeCall( |
| 58 | + post_memory_grow, {builder.makeConst(int32_t(id)), curr}, addressType)); |
| 59 | + } |
| 60 | + |
| 61 | + void visitModule(Module* curr) { |
| 62 | + auto addressType = |
| 63 | + curr->memories.empty() ? Type::i32 : curr->memories[0]->addressType; |
| 64 | + addImport(curr, pre_memory_grow, {Type::i32, addressType}, addressType); |
| 65 | + addImport(curr, post_memory_grow, {Type::i32, addressType}, addressType); |
| 66 | + } |
| 67 | + |
| 68 | +private: |
| 69 | + Index id; |
| 70 | + |
| 71 | + void addImport(Module* curr, Name name, Type params, Type results) { |
| 72 | + auto import = Builder::makeFunction(name, Signature(params, results), {}); |
| 73 | + import->module = ENV; |
| 74 | + import->base = name; |
| 75 | + curr->addFunction(std::move(import)); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +Pass* createInstrumentMemoryGrowPass() { return new InstrumentMemoryGrow(); } |
| 80 | + |
| 81 | +} // namespace wasm |
0 commit comments