Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // build with luajit: g++ -Wall lua_limit_test.cpp -o lua_limit_test -Iinclude/luajit-2.0 -Llib -lluajit-5.1 -lpthread
- // build with lua: g++ -Wall lua_limit_test.cpp -o lua_limit_test -Ilua-5.1.5/src -Llua-5.1.5/src -llua -lpthread -ldl
- #include <stdio.h>
- extern "C" {
- #include "lua.h"
- #include "lauxlib.h"
- #include "lualib.h"
- }
- #include <unistd.h>
- #include <pthread.h>
- lua_State *L;
- pthread_t tid;
- void hook(lua_State *L, lua_Debug *ar) {
- printf("hook: raising error...\n");
- luaL_error(L, "interrupted by hook");
- }
- void *thread_func(void *arg) {
- printf("thread: start\n");
- usleep(1000 * 1000);
- printf("thread: setting hook...\n");
- int res = 0;
- res = lua_sethook(L, &hook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT, 0);
- printf("thread: hook set done for lua_State, res: %d\n", res);
- printf("thread: done\n");
- return NULL;
- }
- int main(int argc, char **argv) {
- printf("main: begin\n");
- /* */
- if (pthread_create(&tid, NULL, &thread_func, NULL)) {
- printf("main: failed to create thread\n");
- return 0;
- } else {
- printf("main: started thread %ld\n", tid);
- }
- /* */
- L = luaL_newstate();
- luaL_openlibs(L);
- // thread_func(NULL);
- printf("main: executing LUA code...\n");
- int res = luaL_dostring(L, "local x = 0; for v = 0, 1e+15, 1 do x = x + 1; end;");
- printf("main: LUA code done, res: %d\n", res);
- if (res) {
- const char *err = lua_tostring(L, -1);
- printf("main: LUA error: %s\n", err);
- }
- printf("main: waiting for thread to complete...\n");
- pthread_join(tid, NULL);
- printf("main: done\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement