emscripten-core / emscripten-core/emscripten

SDL2 TTF_RenderText_Solid Produces "alignment fault" when compiled with -s SAFE_HEAP=1

Open
#14,870 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

When compiling an SDL2 program that uses the SDL2_TTF library, I noticed that compiling with `-s SAFE_HEAP=1` causes the application to crash whenever a call is made to render some text.

This small repository recreates this behavior: https://github.com/benjamin-t-brown/emscripten_sdl2_ttf_example

As a summary:

```c++
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();

SDL_Window *window= SDL_CreateWindow("SDL_ttf in SDL2", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 512, 512, 0);
SDL_Renderer *renderer= SDL_CreateRenderer(window, -1, 0);
TTF_Font *font = TTF_OpenFont("assets/monofonto.ttf", 25);
SDL_Color color = {255, 255, 255};
SDL_Surface *surface = TTF_RenderText_Solid(font, "Welcome to TTF Test", color);
```

When run in a web page, the code errors at `TTF_RenderText_Solid` with an alignment fault `Uncaught (in promise) RuntimeError: abort(alignment fault) at Error...` When run with the output of a gcc compiler, no alignment errors are found.

This code was compiled with the following:

```
em++ Test.cpp -g -O0 -s USE_SDL=2 -s USE_SDL_TTF=2 -s ALLOW_MEMORY_GROWTH=1 -s SAFE_HEAP=1 -s WARN_UNALIGNED=1 -s DISABLE_EXCEPTION_CATCHING=0 -s DEMANGLE_SUPPORT=1 -s ASSERTIONS=2 -s EXPORTED_FUNCTIONS='["_main"]' -s EXPORTED_RUNTIME_METHODS=['ccall'] --preload-file assets -o .build/test.js
```

Version

```
em++ --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.27 (7e538a419c3649f3a540a57beab347aa8f6c6271)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.