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

C API: wasm_module_imports leaks memory #2469

Closed
tudor opened this issue Jul 15, 2021 · 3 comments · Fixed by #2683
Closed

C API: wasm_module_imports leaks memory #2469

tudor opened this issue Jul 15, 2021 · 3 comments · Fixed by #2683
Assignees
Labels
🛑 blocked bug Something isn't working 📦 lib-c-api About wasmer-c-api priority-high High priority issue
Milestone

Comments

@tudor
Copy link

tudor commented Jul 15, 2021

wasmer 2.0.0

To reproduce, compile the following program with clang's (or gcc's) address sanitizer. /tmp/e2.wasm has 2 imports.

C code:

#include <wasm.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
    wasm_engine_t* engine = wasm_engine_new();
    wasm_store_t* store = wasm_store_new(engine);

    FILE* fp = fopen("/tmp/e2.wasm", "rb");
    fseek(fp, 0, SEEK_END);
    size_t size = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    wasm_byte_vec_t data;
    wasm_byte_vec_new_uninitialized(&data, size);
    fread(data.data, size, 1, fp);
    fclose(fp);

    wasm_module_t* module = wasm_module_new(store, &data);

    wasm_importtype_vec_t importTypes;
    wasm_module_imports(module, &importTypes);

    wasm_importtype_vec_delete(&importTypes);
    wasm_module_delete(module);
    wasm_byte_vec_delete(&data);
    wasm_store_delete(store);
    wasm_engine_delete(engine);
}

Compilation line:

clang-12 -g -fsanitize=address -o /tmp/a /tmp/a.c -lwasmer

Errors:

=================================================================
==10573==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x49669d in malloc (/tmp/a+0x49669d)
    #1 0x7f27ef81e392 in _$LT$alloc..vec..Vec$LT$T$GT$$u20$as$u20$alloc..vec..spec_from_iter..SpecFromIter$LT$T$C$I$GT$$GT$::from_iter::h0a4e053b7d1f2fc8 (/usr/local/lib/libwasmer.so+0x92392)

Objects leaked above:
0x602000000d30 (8 bytes)

Indirect leak of 1 byte(s) in 1 object(s) allocated from:
    #0 0x49669d in malloc (/tmp/a+0x49669d)
    #1 0x7f27ef81fb47 in _$LT$core..iter..adapters..map..Map$LT$I$C$F$GT$$u20$as$u20$core..iter..traits..iterator..Iterator$GT$::fold::hd77c1645fbe20366 (/usr/local/lib/libwasmer.so+0x93b47)

Objects leaked above:
0x602000000d50 (1 bytes)

SUMMARY: AddressSanitizer: 9 byte(s) leaked in 2 allocation(s).

C code compiled to wasm using wasi-sdk:

__attribute__((import_module("foo"), import_name("foo"))) void foo(void);
int main() {
    foo();
}

Resulting wasm:

(module
  (type (;0;) (func (param i32)))
  (type (;1;) (func))
  (type (;2;) (func (result i32)))
  (import "wasi_snapshot_preview1" "proc_exit" (func $__wasi_proc_exit (type 0)))
  (import "foo" "foo" (func $foo (type 1)))
  (func $__wasm_call_ctors (type 1))
  (func $_start (type 1)
    (local i32)
    call $__wasm_call_ctors
    call $__original_main
    local.set 0
    call $__wasm_call_dtors
    block  ;; label = @1
      local.get 0
      i32.eqz
      br_if 0 (;@1;)
      local.get 0
      call $__wasi_proc_exit
      unreachable
    end)
  (func $__original_main (type 2) (result i32)
    (local i32)
    i32.const 0
    local.set 0
    call $foo
    local.get 0
    return)
  (func $dummy (type 1))
  (func $__wasm_call_dtors (type 1)
    call $dummy
    call $dummy)
  (table (;0;) 1 1 funcref)
  (memory (;0;) 2)
  (global (;0;) (mut i32) (i32.const 66560))
  (export "memory" (memory 0))
  (export "_start" (func $_start)))
@tudor tudor added the bug Something isn't working label Jul 15, 2021
@tudor
Copy link
Author

tudor commented Jul 15, 2021

Commenting out various parts of the code does indicate that the leak comes from wasm_module_imports

@syrusakbary
Copy link
Member

Thanks for the detailed report @tudor

@syrusakbary syrusakbary added the priority-high High priority issue label Oct 20, 2021
@wchaudry wchaudry added this to the Wasmer Runtime 2.x milestone Oct 21, 2021
@wchaudry wchaudry added the 🕵️ needs investigation The issue/PR needs further investigation label Nov 3, 2021
@Amanieu Amanieu self-assigned this Nov 3, 2021
@Amanieu Amanieu removed the 🕵️ needs investigation The issue/PR needs further investigation label Nov 8, 2021
@wchaudry
Copy link

@syrusakbary to look into a blocking PR that has not been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛑 blocked bug Something isn't working 📦 lib-c-api About wasmer-c-api priority-high High priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants