react-native-webassembly icon indicating copy to clipboard operation
react-native-webassembly copied to clipboard

Memory allocation in Webassembly

Open tainnhan opened this issue 1 year ago • 1 comments

Hello,

I'm currently trying to integrate rust code into my expo app. After compiling my rust code into wasm I have a sign_bg.wasm.d.ts. The function signature look like this:

/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function add(a: number, b: number): number;
export function greet(a: number, b: number, c: number): void;
export function generate_eddsa_signature(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
export function __wbindgen_exn_store(a: number): void;

        const module = await WebAssembly.instantiate<{
          memory: WebAssembly.Memory;
          __wbindgen_add_to_stack_pointer: (a: number) => number;
          __wbindgen_malloc: (a: number, b: number) => number;
          __wbindgen_realloc: (
            a: number,
            b: number,
            c: number,
            d: number
          ) => number;
          __wbindgen_free: (a: number, b: number, c: number) => void;
        }>(wasmBytes);

After this initializitation I use

let ptr = module.instance.exports. __wbindgen_malloc(5,1);
console.log(ptr); // gives me 1114120 

I checked my size of memory

let length = module.instance.exports.memory.byteLength;
console.log(length) // gives me 1114112

How can the allocated memory of my pointer be outside of the boundary of my Assembly Memory? Do I misunderstand here something?

tainnhan avatar Nov 10 '23 18:11 tainnhan

Hey @tainnhan thanks for raising.

Did you manage to determine where the extra two bytes are coming from?

cawfree avatar Dec 01 '23 11:12 cawfree