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 using function .call() #175

Open
enricoscoda opened this issue Apr 21, 2015 · 2 comments
Open

Infinite Loop using function .call() #175

enricoscoda opened this issue Apr 21, 2015 · 2 comments
Labels
docs Issues containing stuff that ought to be documented Triage Issues yet to be triaged

Comments

@enricoscoda
Copy link

The following code:

var c = function() {
    return c.call();
}
c();

doesn't stop even if I limit recursion and block code after 10 seconds using instruction observer:

cx.setInstructionObserverThreshold(10000);
// limit recursion...
cx.setMaximumInterpreterStackDepth(1000);
@gbrail
Copy link
Collaborator

gbrail commented Apr 21, 2015

When you call "setInstructionObserverThreshold," it just inserts code that
calls Context.observeInstructionCount() every N instructions. The default
implementation of that function does nothing.

If you want to actually do something like throw an exception, then you need
to create a subclass of Context and override that method:

http://mozilla.github.io/rhino/javadoc/org/mozilla/javascript/Context.html#observeInstructionCount(int)

(Typically you do this by overriding ContextFactory to create your own
subclass of Context.)

Then, whatever exception you throw will percolate up to the caller.

Finally, I am pretty sure that "MaximumInterpreterStackDepth" only affects
the code if you are running in interpreted mode, which would only happen if
you explicitly called "setOptimizationLevel" to set the level to -1. The
default is 0, so the code is compiled.

On Tue, Apr 21, 2015 at 9:12 AM, EnricoScoda [email protected]
wrote:

The following code:
var c = function() {
return c.call();
}
c();
doesn't stop even if I limit recursion and block code after 10 seconds
using instruction observer:
cx.setInstructionObserverThreshold(10000);
// limit recursion...
cx.setMaximumInterpreterStackDepth(1000);


Reply to this email directly or view it on GitHub
#175.

greg brail | apigee https://apigee.com/ | twitter @gbrail
http://twitter.com/gbrail

@enricoscoda
Copy link
Author

Hi Greg,
what you describe is implemented in my code and works pretty well in common cases (long evaluations or deep recursions). I played more with it. The following throws "Exceeded maximum stack depth":

var c = function() {
   c();
}
c();

instead this one goes on forever:

var c = function() {
   return c();
}
c();

I can provide you my extended ContextFactory and my extended Context, I you like.
Thanks a lot for the quick response,
Enrico

@p-bakker p-bakker added docs Issues containing stuff that ought to be documented Triage Issues yet to be triaged labels Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Issues containing stuff that ought to be documented Triage Issues yet to be triaged
Projects
None yet
Development

No branches or pull requests

3 participants