Skip to content

Why does this simple program trigger an alignment fault? #18096

@debevv

Description

@debevv

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.24 (68a9f990429e0bcfb63b1cde68bad792554350a5)
clang version 16.0.0 (https://github.com/llvm/llvm-project 277c382760bf9575cfa2eac73d5ad1db91466d3f)
Target: wasm32-unknown-emscripten
Thread model: posix

In the recent days I'm fighting with some strange behavior while trying to implement a simple messaging system between wasm workers. After a while I managed to isolate what I think should be the root cause of all the random crashes I'm getting on WASM (and not on Linux-x64).
This simple program crashes with Aborted(alignment fault):

#include <emscripten/wasm_worker.h>

#include <vector>

class Lock {
public:
    Lock() = default;

    Lock(const Lock&) = delete;
    Lock& operator=(const Lock&) = delete;

    Lock(Lock&&) = default;
    Lock& operator=(Lock&&) = default;

    ~Lock() {
        this->release();
    }

    void acquire() {      
        emscripten_lock_waitinf_acquire(&this->mutex);
    }

    void release() {
        emscripten_lock_release(&this->mutex);
    }

private:
    emscripten_lock_t mutex = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER;
};

int main() {
    std::vector<uint8_t> v;
    v.resize(65536 * 16);

    Lock l;

    std::vector<uint8_t> v2;
    v2.resize(65536 * 16);

    return 0;
}

I'm building it with these flags:

em++ -O0 -g -std=gnu++20 -fno-exceptions -sWASM=1 -sSTRICT=1 \
-sWASM_WORKERS -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=65536000 -sUSE_SDL=0 -sUSE_PTHREADS=0 \
-sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFORCE_FILESYSTEM -sSTACK_OVERFLOW_CHECK=2 \
-sASSERTIONS=2 -sSAFE_HEAP=1 -fsanitize=undefined alignment.cpp -o alignment.html

Full error log (screenshot because i can't paste from chromium console):
Schermata del 2022-10-24 21-49-29

It's super weird because this does not happen in Linux builds, not even with all the various -fsanitize= flags. Moreover, removing the line with Lock l; or replacing it with emscripten_lock_t mutex = EMSCRIPTEN_LOCK_T_STATIC_INITIALIZER; (which I suppose should have the same sizeof) makes the crash go away.

Again, am I missing something here? Maye some WASM quirk I'm not taking into consideration?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions