Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop upon exiting a nodejs program #5323

Closed
cpitclaudel opened this issue Jun 20, 2017 · 15 comments
Closed

Infinite loop upon exiting a nodejs program #5323

cpitclaudel opened this issue Jun 20, 2017 · 15 comments

Comments

@cpitclaudel
Copy link

I don't have a simple repro yet, but I'm seeing the following in the generated javascript code:

  function _exit(status) {
      __exit(status);
    }function __Exit(status) {
      __exit(status);
    }

function __exit($0) {
 $0 = $0 | 0;
 __Exit($0 | 0);
}

This causes an infinite loop when the node.js program completes:

exception thrown: RangeError: Maximum call stack size exceeded,RangeError: Maximum call stack size exceeded
    at __Exit (/test/output.g3.js:4646:21)
    at __exit (/test/output.g3.js:4108667:2)
    at __Exit (/test/output.g3.js:4647:7)
    at __exit (/test/output.g3.js:4108667:2)
    at __Exit (/test/output.g3.js:4647:7)
    at __exit (/test/output.g3.js:4108667:2)
    at __Exit (/test/output.g3.js:4647:7)
    at __exit (/test/output.g3.js:4108667:2)
    at __Exit (/test/output.g3.js:4647:7)
    at __exit (/test/output.g3.js:4108667:2)
/test/output.g3.js:114
      throw ex;
      ^

I compiled with this:

EMCC_JS_OPTIONS=(-s OUTLINING_LIMIT=200000 # Avoid “excessive recursion” errors at parsing in web workers
                 # (But beware: too low values cause stack overflows in the program)
                 -s DISABLE_EXCEPTION_CATCHING=0 # Let CVC4 catch exceptions
                 -s ABORTING_MALLOC=0 -s ALLOW_MEMORY_GROWTH=1 # Allow dynamic memory resizing
                 -s EXPORTED_FUNCTIONS='["_main"]'
                 -s STRICT=1 -s ERROR_ON_UNDEFINED_SYMBOLS=1)

EMCC_JS_LIBLIKE_OPTIONS=(-s INVOKE_RUN=0 # Don't call main automatically
                         -s NO_EXIT_RUNTIME=1
                         -s MODULARIZE=1 -s EXPORT_NAME="'CVC4'"
                         -s 'EXTRA_EXPORTED_RUNTIME_METHODS=["FS"]')

emcc "${EMCC_JS_OPTIONS[@]}" "${EMCC_JS_LIBLIKE_OPTIONS[@]}" -O3 -g3 -l "nodefs.js" "${EMCC_JS_INPUTS[@]}" -o output.g3.js

Any idea ?

@kripken
Copy link
Member

kripken commented Jun 20, 2017

The source for _exit is in src/library.js (search for exit:). Perhaps you have a local change? Or perhaps some custom code that modifies the JS output?

@cpitclaudel
Copy link
Author

No local change, and no modifications of the JS output, AFAICT. Does it make a difference that it reads __exit, not _exit?

@kripken
Copy link
Member

kripken commented Jun 20, 2017

Oh, right. That __exit might be from your code? Then it could explain part of this, but still it's not clear why _exit differs from what is in the js library. Should be easy to figure out with a repro though.

@cpitclaudel
Copy link
Author

I'm trying to get one. I misspoke when I stated that it was only node — it also happens on a regular webpage. It started happening when I moved to incoming.

@cpitclaudel
Copy link
Author

Would it help if I sent the compiled program?

@cpitclaudel
Copy link
Author

Could this be a recent regression?

When I run on tag-1.37.13, the code looks like this:

  Module["_pthread_mutex_unlock"] = _pthread_mutex_unlock;

  
  
  function __exit(status) {
      // void _exit(int status);
      // http://pubs.opengroup.org/onlinepubs/000095399/functions/exit.html
      Module['exit'](status);
    }function _exit(status) {
      __exit(status);
    }function __Exit(status) {
      __exit(status);
    }

Whereas on incoming, I get this:

  Module["_pthread_mutex_unlock"] = _pthread_mutex_unlock;

  
  function _exit(status) {
      __exit(status);
    }function __Exit(status) {
      __exit(status);
    }

@kripken
Copy link
Member

kripken commented Jun 20, 2017

If it's a regression, please bisect, the specific commit would be useful information.

@cpitclaudel
Copy link
Author

I can try, but each build cycle takes 25 minutes, so it's going to be a fairly painful task.

@kripken
Copy link
Member

kripken commented Jun 20, 2017

Well, I'd guess it might be 7a5744d - perhaps just check that commit and the one before it?

Or if a repro is quicker to make, that's better.

@cpitclaudel
Copy link
Author

Thanks, that's very helpful :) I'll try to make a repro.

@cpitclaudel
Copy link
Author

Alright, here it is:

#include <unistd.h>

int main() {
  _exit(0);
}
$ emcc helloworld.cpp -o helloworld.js; nodejs helloworld.js
exception thrown: RangeError: Maximum call stack size exceeded,RangeError: Maximum call stack size exceeded
    at __Exit (/scratch/emscripten/helloworld.js:1734:21)
    at __exit (/scratch/emscripten/helloworld.js:2355:2)
    at asm.__exit (/scratch/emscripten/helloworld.js:5715:20)
    at __Exit (/scratch/emscripten/helloworld.js:1735:7)
    at __exit (/scratch/emscripten/helloworld.js:2355:2)
    at asm.__exit (/scratch/emscripten/helloworld.js:5715:20)
    at __Exit (/scratch/emscripten/helloworld.js:1735:7)
    at __exit (/scratch/emscripten/helloworld.js:2355:2)
    at asm.__exit (/scratch/emscripten/helloworld.js:5715:20)
    at __Exit (/scratch/emscripten/helloworld.js:1735:7)
/scratch/emscripten/helloworld.js:110
      throw ex;
      ^

It took me a while, because this works fine:

#include <cstdlib>

void _exit(int c) {
  std::exit(c);
}

int main() {
  _exit(0);
}

@cpitclaudel
Copy link
Author

And of course, it works if I return to latest:

$ emsdk activate sdk-1.37.9-64bit
…
$ source /build/emsdk-portable/emsdk_env.sh 
…
$ emcc helloworld.cpp -o helloworld.js
…
$ nodejs helloworld.js
$ 

@kripken
Copy link
Member

kripken commented Jun 20, 2017

Thanks, looks like it was a bug in our system libs, fix is in #5325 (bug was old, but it just became noticeable recently with that commit mentioned before).

@kripken
Copy link
Member

kripken commented Jun 21, 2017

Fixed by that merge.

@kripken kripken closed this as completed Jun 21, 2017
@cpitclaudel
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants