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

When memory is exhausted, an exception 0xC0000005 is triggered #1004

Closed
ruidong007 opened this issue May 30, 2023 · 4 comments
Closed

When memory is exhausted, an exception 0xC0000005 is triggered #1004

ruidong007 opened this issue May 30, 2023 · 4 comments

Comments

@ruidong007
Copy link

Thank you for your excellent work, luajit is an excellent tool.

So,we wrote a script interpreter based on luajit, and it is possible for our users to write out of memory scripts.

Therefore, we want to prompt memory exhaustion, but our test code will cause memory access errors.

The test code is the simplest example that has been put together after repeated verification.

The test code is as follows:

This code works correct,the vm will return "not enough memory" as follow picture:
Array.push_back function return not enough memory information

static int userd(lua_State* State){
	auto obj=lua_newuserdata(State,10);
	return 1;
}

int _tmain(int argc, char* argv[]){
	LuaState=luaL_newstate();
	luaL_openlibs(LuaState);
	lua_pushcfunction(LuaState,userd);
	lua_setglobal(LuaState,"userd");
	auto buf=R"(
		local Array={} 
		Array.push_back=function(a,val) 
			table.insert(a,val) 
		end 
		local e={} 
		while true do 
			Array.push_back(e,1)	//it works correct, return "not enough memory" information
		end 
	)";
	luaL_loadstring(LuaState,buf);
	lua_pcall(LuaState,0,0,1);
	lua_close(LuaState);
	return 0;
}

This code causes a exception as follow picture:
Array.push_back function cause exception information

static int userd(lua_State* State){
	auto obj=lua_newuserdata(State,10);
	return 1;
}

int _tmain(int argc, char* argv[]){
	LuaState=luaL_newstate();
	luaL_openlibs(LuaState);
	lua_pushcfunction(LuaState,userd);
	lua_setglobal(LuaState,"userd");
	auto buf=R"(
		local Array={} 
		Array.push_back=function(a,val) 
			table.insert(a,val) 
		end 
		local e={} 
		while true do 
			Array.push_back(e,userd())	//cause a exception
		end 
	)";
	luaL_loadstring(LuaState,buf);
	lua_pcall(LuaState,0,0,1);
	lua_close(LuaState);
	return 0;
}

This code works correct,the vm will return "not enough memory" as follow picture:
table.insert function return not enough memory information

static int userd(lua_State* State){
	auto obj=lua_newuserdata(State,10);
	return 1;
}

int _tmain(int argc, char* argv[]){
	LuaState=luaL_newstate();
	luaL_openlibs(LuaState);
	lua_pushcfunction(LuaState,userd);
	lua_setglobal(LuaState,"userd");
	auto buf=R"(
		local Array={} 
		Array.push_back=function(a,val) 
			table.insert(a,val) 
		end 
		local e={} 
		while true do 
			--Array.push_back(e,userd())
			table.insert(e,userd())	  //it works correct, return "not enough memory" information
		end 
	)";
	luaL_loadstring(LuaState,buf);
	lua_pcall(LuaState,0,0,1);
	lua_close(LuaState);
	return 0;
}

@MikePall
Copy link
Member

Always use the latest v2.1 git version before reporting issues. Also read the section on Exception Interoperability in the v2.1 documentation and what this means for Windows compilation settings.

@ruidong007
Copy link
Author

Always use the latest v2.1 git version before reporting issues. Also read the section on Exception Interoperability in the v2.1 documentation and what this means for Windows compilation settings.

Thank you for taking the time to reply.
We are using the latest version of Luajit 2.1 in 32-bit mode.
We did follow-up tests and came to the following conclusions:
All versions of luajit 2.1, enable jit and call c function, when the memory is exhausted, a memory conflict will be triggered, and close jit is normal.
Luajit version 2.0.5 has the same test conditions, and it can run normally regardless of whether JIT is enabled or not.
Looking forward to your help, thank you very much!

@MikePall
Copy link
Member

MikePall commented Jun 2, 2023

Ah, I didn't see you're using Windows 32 bit.
I've commited a fix. Thanks!

@MikePall MikePall closed this as completed Jun 2, 2023
@ruidong007
Copy link
Author

Please restore the operation permissions of the csbinghu account that filed issue 1003, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants