Skip to content

Instantly share code, notes, and snippets.

@lelandbatey
Last active August 29, 2015 14:18
Show Gist options
  • Save lelandbatey/db23db32e30ac7dbbeca to your computer and use it in GitHub Desktop.
Save lelandbatey/db23db32e30ac7dbbeca to your computer and use it in GitHub Desktop.
Emscripten problem

Problem

When compiling this[0] C++ file with Emscripten, the compiled javascript file produces incorrect output when run with node (using node v0.12.0 on all platforms). This is not "serious" code, it is a toy minified raytracer, as found here[1]. I have tried to find any information on the Emscripten website regarding less-than-compatible behavior this source file might be doing which makes it run incorrectly, but I have had no luck.

In attempting to narrow the problem, I also constructed a small program which seems to break in a similar way, which can be found here[6].

I come here hoping that it is simply an oversight by me in how this code is written or how I'm compiling it.

Steps to reproduce:

Assuming you've downloaded the C++ file here[0], the following is the script I'm using to test compilation(it can also be found here[5]:

#!/bin/bash

# Compile with normal C++ compiler
clang break_emscripten_minimal.cpp -lm -o c_minimal
./c_minimal > c_minimal_raw.ppm
convert c_minimal_raw.ppm c_minimal_black.png
# rm c_minimal

clang break_emscripten_raytracer.cpp -lm -o c_raytracer
./c_raytracer > c_ray_raw.ppm
convert c_ray_raw.ppm c_raytraced_img.png
# rm c_raytracer


# Compile with Emscripten
emcc break_emscripten_minimal.cpp -lm -o js_minimal.js
node js_minimal.js > js_minimal_raw.ppm
convert js_minimal_raw.ppm js_minimal_black.png
# rm js_minimal.js

emcc break_emscripten_raytracer.cpp -lm -o js_raytracer.js
node js_raytracer.js > js_ray_raw.ppm
convert js_ray_raw.ppm js_raytraced_img.png
# rm js_raytracer.js

This script will attempt to compile and run both source files (the raytracer and the simple black image generator) and have them output PPM files. It then tries to convert those images to PNG (for easy viewing).

I've had intermittent results. On all the platforms I've tested this on, Emscripten compiles the code fine, but the data written to stdout varies. For example, on OS X the generated image is a valid image, it's just very corrupted[2]. On Ubuntu 14.04, the data ouput is not even a valid image.

Additionally, on OS X the progress written to stderr has corrupted characters:

��.41 complete
97.46 complete
��.51 complete
97.56 complete
��.61 complete
��.66 complete
��71 complete
��.75 complete
97.80 complete
��.85 complete
97.90 complete
��7.95 complete
��.00 complete
��8.05 complete
��8.10 complete
��.14 complete
��19 complete

Platform Information:

The OS X information, as reported by emcc -v:

emcc (Emscripten GCC-like replacement + linker emulating GNU ld ) 1.29.0
clang version 3.4 
Target: x86_64-apple-darwin14.1.0
Thread model: posix
INFO     root: (Emscripten: Running sanity checks)

The Ubuntu 14.04 information, as reported by emcc -v:

emcc (Emscripten GCC-like replacement + linker emulating GNU ld ) 1.30.0
clang version 3.5.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.0
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Candidate multilib: .;@m64
Selected multilib: .;@m64
INFO     root: (Emscripten: Running sanity checks)

The compiled javascript from OS X is here[3], while the compiled JS from Ubuntu is here[4]

[0] - http://nacr.us/media/text/emscripten_broken/break_emscripten_raytracer.cpp
[1] - http://fabiensanglard.net/rayTracing_back_of_business_card/
[2] - http://nacr.us/media/text/emscripten_broken/osx_js_raytracer_broken_output.png
[3] - http://nacr.us/media/text/emscripten_broken/osx_raytracer.js
[4] - http://nacr.us/media/text/emscripten_broken/ubuntu_ray.js
[5] - http://nacr.us/media/text/emscripten_broken/compile_broken_sources.sh
[6] - http://nacr.us/media/text/emscripten_broken/break_emscripten_minimal.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment