emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.67k stars 3.29k forks source link

LinkError: WebAssembly.instantiate(): Import #310 module="env" function="segfault" error: function import requires a callable #14131

Open vaaneeunbnd opened 3 years ago

vaaneeunbnd commented 3 years ago

Hello,

I am trying to port a c++ library to wasm/js. However when I try to do so the file it created however I get segmentation fault and link error. Below is the code that I use to port the library -

BUILD  = debug
OUTDIR = bin
TMPDIR = tmp
SRCDIR = src

INCLUDES  = -I../3rdparty/mylib/include -I../3rdparty/ABC/include -I../3rdparty/mylib/3rdparty -I../3rdparty/mylib/3rdparty/khronos -I../3rdparty/XYZ/include
DEFINES   =
LINK_OPTS =
EMOPTS    = \
    -s RESERVED_FUNCTION_POINTERS=20 \
    -s NO_EXIT_RUNTIME=1 \
    -s MODULARIZE=1 \
    -s EXPORT_NAME="_mylib" \
    -s EXPORTED_FUNCTIONS=@$(SRCDIR)/functions.json \
    -s ERROR_ON_UNDEFINED_SYMBOLS=0 \
    -s ALLOW_MEMORY_GROWTH=1 \
    --memory-init-file 0 

ifeq ($(BUILD), debug)
    LINK_OPTS += -O0 -Wall --llvm-lto 0 -s ASSERTIONS=2 --closure 0 -s DEMANGLE_SUPPORT=1 -s WASM=1
    # DEFINES   += -DBGFX_CONFIG_DEBUG=1
else ifeq ($(BUILD), release)
    LINK_OPTS = -O3 --llvm-lto 1 --closure 0
endif

all: library
    @echo $(OUTDIR)/mylib_$(BUILD).js complete

library:
    @mkdir -p $(OUTDIR) $(TMPDIR)
    $(CXX) $(LINK_OPTS) $(INCLUDES) $(DEFINES) $(EMOPTS) -o $(TMPDIR)/mylib_$(BUILD).raw.js ../3rdparty/mylib/src/amalgamated.cpp
    @cat $(SRCDIR)/header.js            >  $(OUTDIR)/mylib_$(BUILD).js
    @cat $(TMPDIR)/mylib_$(BUILD).raw.js >> $(OUTDIR)/mylib_$(BUILD).js
    @cat $(SRCDIR)/footer.js            >> $(OUTDIR)/mylib_$(BUILD).js

clean:
    rm -rf $(OUTDIR) $(TMPDIR)

I would be grateful if someone can please assist me with this.

Screen Shot 2021-05-10 at 3 37 41 pm

Thanks, Vaanee

vaaneeunbnd commented 3 years ago

Hello,

Hope your day is going well. I would be grateful if someone can please review this issue.

thanks

kripken commented 3 years ago

"function import requires a callable" means that there is an import that expects a function, but the JS did not provide one.

segfault is an import that is added when using SAFE_HEAP. Emscripten will provide it from JS when that is set at compile time.

My theory is that your wasm and JS files are out of sync. That is, the wasm was built with SAFE_HEAP, and then the JS was built without it. Perhaps at the build system level, or perhaps when you moved the files to a testing environment, a mismatch happened and one file was updated but not the other.

I suggest rebuilding entirely from scratch. If that does not help, check the dates on the files to see that they are indeed updated.