WebAssembly / wasm-c-api

Wasm C API prototype
Apache License 2.0
534 stars 77 forks source link

AddressSanitizer: allocation-size-too-big #177

Closed try-agaaain closed 1 year ago

try-agaaain commented 1 year ago

When I execute the make run-hello-cc command, everything seems to be fine in the terminal output:

$ make run-hello-cc
mkdir -p out/example
clang++ -c -std=c++11 -DWASM_API_DEBUG   -Wall -Werror -ggdb -O -fsanitize=address -I. -Iv8/v8/include -I./include example/hello.cc -o out/example/hello-cc.o
mkdir -p out/.
clang++ -c -std=c++11 -DWASM_API_DEBUG   -Wall -Werror -ggdb -O -fsanitize=address -I. -Iv8/v8/include -I./include -I./src src/wasm-v8.cc -o out/./wasm-v8.o
clang++ -std=c++11 -DWASM_API_DEBUG   -Wall -Werror -ggdb -O -fsanitize=address -fsanitize-memory-track-origins -fsanitize-memory-use-after-dtor out/example/hello-cc.o -o out/example/hello-cc \
        out/./wasm-bin.o out/./wasm-v8.o \
         \
        v8/v8/out.gn/x64.release/obj/libv8_monolith.a \
         \
        -ldl -pthread
==== C++ hello ====
Initializing...
Loading binary...
Validating module...
Compiling module...
Creating callback...
Instantiating module...
Extracting export...
Calling export...
Calling back...
> Hello world!
Shutting down...
Done.
==== Done ====

However, when I run the executable output from this command, I get an address assignment error:

$ ./out/example/hello-cc
Initializing...
Loading binary...
=================================================================
==1587==ERROR: AddressSanitizer: requested allocation size 0xffffffffffffffff (0x800 after adjustments for alignment, red zones etc.) exceeds maximum supported size of 0x10000000000 (thread T0)
    #0 0x5643f7d89f0d in operator new[](unsigned long, std::nothrow_t const&) (/home/todo/internship/wasm-c-api/out/example/hello-cc+0x17cf0d) (BuildId: 1df0ba2bc5fdbe7a65681cc1a78a0196c90072f1)
    #1 0x5643f7d8e771 in wasm::vec<char>::vec(unsigned long) /home/todo/internship/wasm-c-api/./include/wasm.hh:57:39
    #2 0x5643f7d8e771 in wasm::vec<char>::make_uninitialized(unsigned long) /home/todo/internship/wasm-c-api/./include/wasm.hh:139:12
    #3 0x5643f7d8e595 in main /home/todo/internship/wasm-c-api/example/hello.cc:96:3

==1587==HINT: if you don't care about these errors you may set allocator_may_return_null=1
SUMMARY: AddressSanitizer: allocation-size-too-big (/home/todo/internship/wasm-c-api/out/example/hello-cc+0x17cf0d) (BuildId: 1df0ba2bc5fdbe7a65681cc1a78a0196c90072f1) in operator new[](unsigned long, std::nothrow_t const&)
==1587==ABORTING

Here is some information about my machine:

$ uname -a
Linux team317 5.15.79.1-microsoft-standard-WSL2 #1 SMP Wed Nov 23 01:01:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ arch
x86_64
try-agaaain commented 1 year ago

I found that this problem is caused by hello.cc not being able to find hello.wasm, after changing the hello.wasm path in hello.cc to the correct one:

  std::ifstream file("hello.wasm");

However, there is another problem, when executing make run-hello-c, the following error will occur:

$make run-hello-c
Initializing...
Loading binary...
Validating module...
Compiling module...
Creating callback...
Instantiating module...
Extracting export...
Calling export...
Calling back...
> Hello World!
Shutting down...
Done.
Leaked 1 instances of wasm::Engine, made 1, freed 0!
Leaked 1 instances of wasm::Store, made 1, freed 0!
Leaked 1 instances of wasm::FuncType, made 10, freed 9!
Leaked 1 instances of wasm::Module, made 3, freed 2!
Leaked 1 instances of wasm::Instance, made 1, freed 0!
Leaked 1 instances of wasm::Func, made 2, freed 1!

...

    #21 0x558dcbabceac in wasm_module_new /home/todo/internship/wasm-c-api/src/wasm-c.cc:740:25
    #22 0x558dcba90334 in main /home/todo/internship/wasm-c-api/example/hello.c:53:31
    #23 0x7f797475dd8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

Indirect leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x558dcba8db4d in operator new(unsigned long) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x17cb4d) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #1 0x558dcbc2e0f3 in v8::internal::CodeObjectRegistry::RegisterNewlyAllocatedCodeObject(unsigned long) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x31d0f3) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #2 0x558dcc1bf6cd in v8::internal::DeserializerAllocator::AllocateRaw(v8::internal::AllocationSpace, int) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x8ae6cd) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #3 0x558dcc1c3042 in v8::internal::Deserializer::ReadObject(int) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x8b2042) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #4 0x558dcc1c03ce in bool v8::internal::Deserializer::ReadData<v8::internal::FullMaybeObjectSlot>(v8::internal::FullMaybeObjectSlot, v8::internal::FullMaybeObjectSlot, int, unsigned long) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x8af3ce) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #5 0x558dcbbbb4c7 in v8::internal::Heap::IterateStrongRoots(v8::internal::RootVisitor*, v8::internal::VisitMode) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x2aa4c7) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #6 0x558dcbe32c8c in v8::internal::StartupDeserializer::DeserializeInto(v8::internal::Isolate*) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x521c8c) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #7 0x558dcbb72f1a in v8::internal::Isolate::Init(v8::internal::ReadOnlyDeserializer*, v8::internal::StartupDeserializer*) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x261f1a) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #8 0x558dcbb731b8 in v8::internal::Isolate::InitWithSnapshot(v8::internal::ReadOnlyDeserializer*, v8::internal::StartupDeserializer*) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x2621b8) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #9 0x558dcbe304f0 in v8::internal::Snapshot::Initialize(v8::internal::Isolate*) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x51f4f0) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #10 0x558dcbafaac8 in v8::Isolate::Initialize(v8::Isolate*, v8::Isolate::CreateParams const&) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x1e9ac8) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #11 0x558dcbafabce in v8::Isolate::New(v8::Isolate::CreateParams const&) (/home/todo/internship/wasm-c-api/out/example/hello-c+0x1e9bce) (BuildId: 21d447bafe28612d21dcccbf8ef47369b70ae682)
    #12 0x558dcba9cb44 in wasm::Store::make(wasm::Engine*) /home/todo/internship/wasm-c-api/./src/wasm-v8.cc:431:18
    #13 0x558dcbaaf4ec in wasm_store_new /home/todo/internship/wasm-c-api/src/wasm-c.cc:233:24
    #14 0x558dcba9023b in main /home/todo/internship/wasm-c-api/example/hello.c:24:25
    #15 0x7f797475dd8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

SUMMARY: AddressSanitizer: 107552 byte(s) leaked in 1632 allocation(s).
make: *** [Makefile:120: run-hello-c] Error 1
rossberg commented 1 year ago

Hm, that is odd. I cannot reproduce that, so I'm not sure what to advise. All I could do would be running it in a debugger and trying to find out why the deallocations don't do anything.

try-agaaain commented 1 year ago

Thank you for your suggestion, I am unable to resolve the issue at the moment, it might be related to my environment, I plan to temporarily close this issue.