Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory allocation in Webassembly #21

Open
tainnhan opened this issue Nov 10, 2023 · 1 comment
Open

Memory allocation in Webassembly #21

tainnhan opened this issue Nov 10, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@tainnhan
Copy link

tainnhan commented Nov 10, 2023

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?

@cawfree
Copy link
Owner

cawfree commented Dec 1, 2023

Hey @tainnhan thanks for raising.

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

@cawfree cawfree added the bug Something isn't working label Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants