Closed Bug 210605 Opened 22 years ago Closed 21 years ago

Script catches all Throwables when inside reflected Java method

Categories

(Rhino Graveyard :: Core, defect)

x86
Linux
defect
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED

People

(Reporter: hannesw, Assigned: igor)

References

Details

Attachments

(2 files, 1 obsolete file)

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030529 Build Identifier: If a java method is called through reflection by org.mozilla.javascript.NativeJavaMethod.call(), all instances of java.lang.Throwable thrown while executing that method are caught by a surrounding JavaScript try/catch statement. Reproducible: Always Steps to Reproduce: 1. Compile this class and add it to your classpath: public class Test { public static void stop() { Thread.currentThread().stop(); } } 2. Execute this script: try { Packages.Test.stop(); } catch (x) { java.lang.System.err.println("cought: "+x); } Actual Results: The following line is printed: cought: java.lang.ThreadDeath Expected Results: JavaScript shouldn't be able to catch the java.lang.ThreadDeath. The cause of the problem seems to be that all Errors/Throwables are caught by NativeJavaMethod.call() wrapped inside a InvocationTargetException, which is then again wrapped inside a JavaScriptException. I guess the right thing would be to unwrap and check the Throwable wrapped by the InvocationTargetException and either wrap that in the JavaScriptException or rethrow it. The following code in line 272 of NativeJavaMethod fixes the problem for me: } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (!(t instanceof Exception)) { Thread.currentThread().stop(t); } throw JavaScriptException.wrapException(cx, scope, e); } I'm not sure if this is the way to go, though, and I don't understand why NativeJavaMethod behaves differently than FunctionObject, which handles InvocationTargetExceptions in the same way.
cc'ing Igor -
Attached patch Consistent exception handling (obsolete) — Splinter Review
This big patch changes exception handling in Rhino in the following way: 1. JavaScriptException is used only to represent exceptions thrown by the script throw statement. Previously the class was used as well to wrap instances of Throwable that script can catch. 2. WrappedException is always used to wrap instances of Throwable that script can catch by the catch statement. The new method ScriptRutime.throwAsUncheckedException() that replaces WrappedException.wrapException() will either re-throw the exception wrapped as WrappedException or re-throw it as is. The method unwrap InvocationTargetExceptions and if its target is Error instance, it is re-thrown as is. In this way the script catch statement will never see instances of Error including ThreadDeath. 3. Since the change effectively means that the script catch can see instances of JavaScriptException, EcmaError and WrappedException, the new method ScriptRutime.getCatchObject is used to extract JS object that is passed as the catch argument. For JavaScriptException and EcmaError the method simply extracts the corresponding JS object. For WrappedException the method wraps the original exception as a LiveConnect object. In this way a potentially expensive LiveConnect wrapping is avoided if the exception is never caught by a script. Interpreter.interpret and optimizer/Codegen.generateCatchBlock is updated to use the new method.
Attachment #126809 - Attachment is obsolete: true
Thanks Igor. I'm running the patched version, everything looks right now.
I committed the patch.
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → FIXED
This bug has resurfaced with revision 1.173 of Context.java committed on 01/16/2004: By removing the lines if (e instanceof Error) { throw (Error)e; } from Context.throwAsUncheckedException(), java.lang.Errors are thrown as WrappedExceptions, which will be cought by JavaScript catch statements.
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
That was caused by my attempt to get script source/line number information for all exceptions including LinkageError and StackOverflow. For I now I will revert the check for Error instances and after 1.5R1.5 release implement better solution.
Status: REOPENED → ASSIGNED
Should be fixed in 1.5R5
Assignee: nboyd → igor
Blocks: 231523
Status: ASSIGNED → NEW
I committed the fix.
Status: NEW → RESOLVED
Closed: 22 years ago21 years ago
Resolution: --- → FIXED
Thanks Igor. From your comments it seems like 1.5R5 is just around the corner, which is also good to hear.
Status: RESOLVED → VERIFIED
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: