Lack of return causes abort() in Emscripten

I received the following Javascript exception from some code compiled with Emscripten. The code was working correctly as a standalone C++ program. This confused me for a while:

uncaught exception: abort() at jsStackTrace@file:///home/new/gitcode/repeatanalysis/progs/r.js:1049:7
stackTrace@file:///home/new/gitcode/progs/r.js:1066:3
abort@file:///home/new/gitcode/progs/r.js:8368:3
_llvm_trap@file:///home/new/gitcode/progs/r.js:4960:7
__Z15test_badret_auxiii [test_badret_aux(int, int, int)]@file:///home/new/gitcode/repeatanalysis/progs/r.js:8079:2
_test_badret@file:///home/new/gitcode/progs/r.js:8089:2
asm._test_badret@file:///home/new/gitcode/progs/r.js:8148:1
ccallFunc@file:///home/new/gitcode/progs/r.js:534:9
@file:///home/new/gitcode/progs/r.html:15:4

The cause was a non-void function failing to return a value. Here’s the complete example:

#include <iostream>
#include <fstream>
#include <vector>
#include "malloc.h"

using namespace std;

int test_badret_aux(int arg1,int arg2,int arg3) {
// return 0;
}

extern "C" {
int test_badret(char *inputstring);
}

int test_badret(char *inputstring) {
test_badret_aux(1,100,100);

return 0;
}

Compiled with (I tried a bunch of options):

 emcc ./r.cpp -o r.js -s EXPORTED_FUNCTIONS="['_test_badret']" -s SAFE_HEAP=1 -g -s ASSERTIONS=1

And driven with the following html:

 <!DOCTYPE html>
<html>
<body>
<script src="r.js"></script>

<script>

var mystring = "test";
var strptr = Module._malloc(mystring.length);
Module.writeAsciiToMemory(mystring, strptr);

var ret = Module.ccall('test_badret', // name of C function
  'number', // return type
  ['number'], // argument types
  [strptr]); // arguments

</script>


</body>
</html>

If I uncomment the return in the above C program, the code no longer aborts. Understandable perhaps. But seemed unusual to me. This was using Firefox (Iceweasel 31.2.0 on Debian Jessie).

Comments are closed.