Comments (6)
I've been asked this before. The problem is that there is only one reliable way to execute untrusted code, and that is to run it inside it's own process. A timeout will not save you; for example, the script could allocate arbitrary amounts of memory, or it could cause a stack overflow exception by recursively calling a function a million times. (The stack overflow exception, in particular, will inevitably result in your app crashing).
I do actually need to do something similar to run the unit test suite reliably. If I can make it generic enough, I'll consider pulling this code out into a couple of helper classes.
from jurassic.
I'm wondering: what's the problem with using Thread.Abort()
. I know it's not ideal, but there are no real alternatives when compiling to MSIL. In e.g. Jint since it's an interpreter, it's easy to do something after a/any statement. With MSIL the only way to do that is through Thread.Abort()
. How that method works is that there are certain points where the .NET runtime checks whether there is a Thread.Abort()
pending and inserts an exception there. This kind of looks like exactly what you'd want to happen. Any implementation in Jurassic itself to accomplish this would be implemented in a similar method, i.e. have a bool
somewhere that it checks at e.g. back branches and function call's/returns. In the end the effect is roughly the same.
I wouldn't shy back from using Thread.Abort()
for this.
from jurassic.
I want to let js call stuff in my .net code.
That code takes locks, Thread.Abort will not release them again.
There are a few other cases when im writing to my logfile (some messages are up to 3k characters).
The logfile stuff i could find other non-ideal solutions for. But releasing locks? Not that I'm aware of.
from jurassic.
Thread.Abort() is fine if you just want to implement a time-out. It's not fine if you are trying to safely execute untrusted code. My belief is that people who ask for the former generally actually need the latter.
from jurassic.
Locks should not be a problem if you implement them correctly. Thread.Abort
will let finalizers run. The documentation specifically states this. This means that if you use finally
, using
or lock
, this will work correctly.
About untrusted code. Generally C# is supposed to be written exception safe. This means that you're supposed to always clean up in a finally
(or using
or lock
). If the untrusted code is not, you have a bigger problem. As for pinvoke's ([DllImport]
), the documentation for Thread.Abort
specifically states that it won't abort while inside a pinvoke. How Thread.Abort
works is that there are certain places in the jitted code that .NET checks for some work. These are points that e.g. the garbage collector runs. Thread.Abort
will insert an exception in those points.
I know that Thread.Abort
is being frowned upon, but there are valid use cases for it. And, don't forget, ASP.NET uses it a lot to recycle AppDomain's.
from jurassic.
That is very interesting. Thank you for that writeup.
I was not aware of how exactly Thread.Abort worked, I just knew that it was frowned upon (which is pretty dumb when you frown upon stuff just because you dont fully understand it).
So thanks again for clearing that up.
I will test Thread.Abort in a few usage scenarios then and see if it really works well for my case.
from jurassic.
Related Issues (20)
- HTML DOM parsing HOT 1
- Return value of CompiledScript HOT 5
- 3.2.1 backward compatibility issues HOT 4
- PropertyAccessorValue not visible HOT 1
- How to catch CLR Exception? HOT 7
- System.ArgumentNullException: Value cannot be null. (Parameter 'value') after update from 3.1.0 to 3.2.2 HOT 3
- JSONObject.Parse() fails when number is in scientific notation HOT 4
- ScriptEngine.EnableDebugging HOT 3
- Template of literals is slow
- JSON.stringify([].length) errors HOT 2
- JSONObject.Parse consumes more and more memory. Possible memory leak? HOT 11
- Scoping and CompiledScript.Execute vs ScriptEngine.Evaluate HOT 5
- Catching script errors at compile time HOT 2
- Currently JSONObject.Stringify returns object instead of string after upgrading from 3.1.0 to 3.2.6 HOT 3
- ScriptEngine.GetGlobalValue HOT 1
- Bug in Evaluate: Loop and variable handling HOT 2
- PM - Customer side scripting for Channels HOT 1
- Date.UTC() doesn't interpret two-digit years correctly HOT 1
- Possibility to create 'sessions' HOT 4
- Linux compability ? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jurassic.