防止Lua的无限循环 [英] Prevent Lua infinite loop

查看:404
本文介绍了防止Lua的无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用LUA接口来获得LUA支持我的C#程序,如果用户提交这样的代码工作线程将冻结

I use lua interfaces to get lua support in my C# program, the worker thread will freeze if the user submits code like this

while true do end

我有一个办法,如果一个无限循环运行检测,但我需要在退出从工作线程DoString方法的一个很好的方式。 ?任何想法

I have a way to detect if a infinite loop is running, but I need a nice way of exiting the DoString method from the Worker thread. Any ideas?

编辑:@kikito,是林deteting它类似的东西。我的问题是,我无法找到杀死DoString方法的干净的方式,它看起来像Lua的接口主类(LUA)有一些静态依赖,因为如果我这样做 lua.Close(); 在我的实例,它将中止DoString方法,但下一次我实例的LUA类新的Lua(); 就会死机说着有关保护内存

edit: @kikito, Yes Im deteting it something like that. The problem I have is that I cant find a clean way of killing the DoString method, it looks like the Lua interfaces main class (Lua) has some static dependencies, because if I do lua.Close(); on my instance it will abort the DoString method, but the next time I instance an lua class new Lua(); it will crash saying something about protective memory

编辑:一个特性分支显示我的.Close代码
https://github.com/AndersMalmgren/FreePIE/tree/detect-and-recover-infite-lua-loop

edit: A feature branch showing my .Close code https://github.com/AndersMalmgren/FreePIE/tree/detect-and-recover-infite-lua-loop

推荐答案

设置挂钩是不充分的所有,以防止资源浪费意外,更不用说abuse-这里有一个简单的例子(字符串模式中的时间都花在匹配 - 没有调试钩子被调用):

Sandboxing Lua

Setting hooks is not sufficient at all to prevent unintended waste of resources, let alone abuse- here's a simple example (the time is spent during string pattern matching- no debug hooks get called):

S =('A'):代表(20000):比赛('.- b')

< STRONG>的唯一可靠方法来强制在一块Lua代码时间/内存限制是在其自己的进程中运行Lua解释,使您的OS监控过程。

和Lua的好处是,你不会需要任何沙箱复杂,依赖于操作系统的权限设定:您刚才限制时间和内存(合理;在Windows上有招聘对象,Unix有相关ulimits- : Linux的资源限​​制),然后让事情像os.execute,在IO库,并从编剧(很容易)的距离就像luasocket模块的一半。

The nice thing with Lua is that you won't need any complicated, OS-dependent permission setup for sandboxing: you just limit time and memory (reasonable; on windows there are Job Objects, Unix has ulimits- relevant: Linux resource limitation) and then keep things like os.execute, half the io-library and modules like luasocket away from the scripter (pretty easy).

您可以在不捣毁你的Lua解释处理几乎一切(除了违反了时间/内存限制):只是包装用户提供的代码的pcall ;如果你调用可能会失败,任何个人的Lua-API函数,你需要用他们,你可以PCALL,太(或设置一个Lua恐慌功能,并从那里处理它)函数内。

You can handle almost everything (except violation of time/memory limits) without trashing your Lua interpreter: Just wrap the execution of user-supplied code inside a pcall; if you call any Lua-API functions that might fail yourself, you need to wrap them inside a function that you can pcall, too (or set a Lua panic function and handle it from there).

[我不想让人们在这个线程一眼假设debug.sethook是足够的沙盒和计算器不会让我评论(还)]

[I didn't want people glancing at this thread to assume that debug.sethook is adequate for sandboxing, and stackoverflow would not let me comment (yet)]

这篇关于防止Lua的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆