mirror.dongdigua.github.io/org/lua_demo.org
2023-08-11 14:07:01 +08:00

3.2 KiB

Run a Lua Demo

Lua is everywhere. Neovim uses lua, Vis uses lua; when I visited Lantian's website1, it's running OpenResty; when I'm jailbreaking Kindle, it uses a lua script and Koreader uses lua; MediaWiki Modules are powered by lua.

So I want to learn a little lua, just enough to handle daily tasks.

I visited lua.org and found the demo. The idea of knowing how it works immediately came to my mind. By searching on DDG2 I fount it have a hidden demo program called demo, it's just the script for running demo (talk about that later why I'm sure about it).

I copied it and added the essential html in the print block, put it on the server, use slowcgi(8) to serve it with httpd(8). Then I got "No such file or directory". I carefully checked the path and permission, but the error still exists

Finally StackOverflow taught me that the execution environment should also be located in the chroot environment. For security, chroot is needed. Well, just statically link lua3.

MYLDFLAGS= -static

It runs well, but the -- continue HTML began in shell script line indicates it's not the only file in the CGI application. I found the rest of them at https://web.tecgraf.puc-rio.br/~lhf/ftp/lua/#demo from lua-l mailing list.

It contains 3 files

        demo            the CGI script
        demo.html       the HTML page
        demo.lua        the Lua program that runs user programs

The shell script takes QUERY_STRING and replace textarea with $QUERY_STRING.lua. So demo?demo just prints the demo.lua itself in the same directory.

I thought my single-file solution is better because it don't need to fork twice and perform file IO, and it can be even better.

I added resource limit according to PIL chapter 25 "SandBoxing"

local steplimit = 100
local memlimit  = 100

local count = 0
local function step ()
	count = count + 1
	if collectgarbage("count") > memlimit then
		error("DoSer uses too much memory")
	end
	if count > steplimit then
		error("DoSer uses too much CPU")
	end
end

debug.sethook(step, "", 100)

source code: https://github.com/dongdigua/demo.lua

Tsoding made a stream yesterday twitch: New Apartment trying to compile lua to wasm using clang. He failed, and said "Lua is not a real language, it's a toy language." The stream is really fun, though. I found some solution:

Footnotes


1

I found his page all empty so I sent him an email, he quickly fixed it.

2

DuckDuckGo, not DongDiGua

3

If make freebsd don't work, try make generic