Path: blob/main/crates/c-api/tests/component/utils.h
1692 views
#pragma once1#include <string_view>23#define CHECK_ERR(err) \4do { \5if (err) { \6auto msg = wasm_name_t{}; \7wasmtime_error_message(err, &msg); \8EXPECT_EQ(err, nullptr) << std::string_view{msg.data, msg.size}; \9} \10} while (false)1112// From crates/component-util/src/lib.rs13inline constexpr std::string_view REALLOC_AND_FREE =14R"END(15(global $last (mut i32) (i32.const 8))16(func $realloc (export "realloc")17(param $old_ptr i32)18(param $old_size i32)19(param $align i32)20(param $new_size i32)21(result i32)2223(local $ret i32)2425;; Test if the old pointer is non-null26local.get $old_ptr27if28;; If the old size is bigger than the new size then29;; this is a shrink and transparently allow it30local.get $old_size31local.get $new_size32i32.gt_u33if34local.get $old_ptr35return36end3738;; otherwise fall through to allocate a new chunk which will later39;; copy data over40end4142;; align up `$last`43(global.set $last44(i32.and45(i32.add46(global.get $last)47(i32.add48(local.get $align)49(i32.const -1)))50(i32.xor51(i32.add52(local.get $align)53(i32.const -1))54(i32.const -1))))5556;; save the current value of `$last` as the return value57global.get $last58local.set $ret5960;; bump our pointer61(global.set $last62(i32.add63(global.get $last)64(local.get $new_size)))6566;; while `memory.size` is less than `$last`, grow memory67;; by one page68(loop $loop69(if70(i32.lt_u71(i32.mul (memory.size) (i32.const 65536))72(global.get $last))73(then74i32.const 175memory.grow76;; test to make sure growth succeeded77i32.const -178i32.eq79if unreachable end8081br $loop)))828384;; ensure anything necessary is set to valid data by spraying a bit85;; pattern that is invalid86local.get $ret87i32.const 0xde88local.get $new_size89memory.fill9091;; If the old pointer is present then that means this was a reallocation92;; of an existing chunk which means the existing data must be copied.93local.get $old_ptr94if95local.get $ret ;; destination96local.get $old_ptr ;; source97local.get $old_size ;; size98memory.copy99end100101local.get $ret102)103)END";104105106