emscripten-core/emscripten

assert - Undefined variable

Closed this issue · 3 comments

Hi.
I'm trying to compile stockfish 17.1 to webassembly.
I want to include some files so I need to use the FS.

Trying with wasmFS, when I set the compiler to:
-s WASMFS -s MALLOC=dlmalloc (or emmmalloc) -s FORCE_FILESYSTEM

I get the error:

building:ERROR: Closure compiler run failed:

building:ERROR: /tmp/emscripten_temp_o3qyyes2/stockfish.jso5.js:477:1780: ERROR - [JSC_UNDEFINED_VARIABLE] variable assert is undeclared
  477| var exists=!!FS.findObject(path);return{exists,object:{contents:exists?FS.readFile(path):null}}},mkdir:
...
,msync:(stream,bufferPtr,offset,length,mmapFlags)=>{assert(offset===0);// TODO: assert that stream has the fd corresponding to the mapped buffer (bufferPtr).

This error comes from line 231 in "src/lib/libwasmfs.js"

Could you check why does this happen?

Thanks, MateoLa.

Ah yes, it looks like that line is missing and #if ASSERTIONS wrapper.

Can you share what other flags you are passing ? In particular can you share full link command with all the flags?

@sbc100

Hi Sam.
This is the beginning of /src/emscripten/Makefile at: https://github.com/MateoLa/stockfish-nnue.wasm I'm working on.

EXE = stockfish.js
COMP = em++
CXX = em++
comp = clang
arch = wasm
bits = 64

# Build options
wasm_simd = yes
embedded_nnue = no
minify_js = yes
assertion = no

VPATH := $(VPATH):emscripten
EM_CXXFLAGS += -pthread
EM_LDFLAGS += \
	-s EXPORT_ES6=1 -s ENVIRONMENT=worker -s STRICT=1 \
	-s ASYNCIFY=1 -s 'ASYNCIFY_IMPORTS=["emscripten_utils_getline_impl"]' \
	-s PROXY_TO_PTHREAD=1 \
	-s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=$$((1 << 27)) -s MAXIMUM_MEMORY=$$((1 << 31)) \
	-s FORCE_FILESYSTEM -s EXPORTED_RUNTIME_METHODS=["FS"] \
	-s ALLOW_UNIMPLEMENTED_SYSCALLS
# emscripten.org/docs/tools_reference/settings_reference.html

ifeq ($(minify_js),yes)
	EM_LDFLAGS += --closure 1
else
	EM_LDFLAGS += --closure 0 --minify 0
endif

ifeq ($(assertion),yes)
	EM_LDFLAGS += -s ASSERTIONS=1
else
	EM_LDFLAGS += -s ASSERTIONS=0
endif

ifeq ($(wasm_simd),yes)
	EM_CXXFLAGS += -DUSE_WASM_SIMD -msimd128
	SRCS += emscripten/wasm_simd.cpp
endif

ifeq ($(embedded_nnue),yes)
	SRCS += emscripten/embedded_nnue.cpp
else
	EM_CXXFLAGS += -DNNUE_EMBEDDING_OFF
endif

EM_EMSCRIPTEN = $(shell emsdk list | grep INSTALLED | head -n 1 | awk '{ print $$1 }')

EXTRACXXFLAGS += \
	-DUSE_POPCNT $(EM_CXXFLAGS) \
	-DEM_EMSCRIPTEN=$(EM_EMSCRIPTEN)
EXTRALDFLAGS += $(EM_LDFLAGS)

The build comes from the file: "/src/Makefile" but compiling with the option ARCH=wasm` ends in what is shown here.

The reported issue occurs when I have tried:

EM_LDFLAGS += \
	-s EXPORT_ES6=1 -s ENVIRONMENT=worker -s STRICT=1 \
	-s ASYNCIFY=1 -s 'ASYNCIFY_IMPORTS=["emscripten_utils_getline_impl"]' \
	-s PROXY_TO_PTHREAD=1 \
	-s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=$$((1 << 27)) -s MAXIMUM_MEMORY=$$((1 << 31)) \
	-s WASMFS -s MALLOC=dlmalloc -s FORCE_FILESYSTEM \
	-s EXPORTED_RUNTIME_METHODS=["FS"] \
	-s ALLOW_UNIMPLEMENTED_SYSCALLS

Thanks, MateoLa.

The bug relates to -sSTRICT=1 which means assert is not available in release builds. You can remove that or wait for my fix which is in #25106