Skip to content

Commit

Permalink
Expose lua os.clock() api (redis#12971)
Browse files Browse the repository at this point in the history
Implement redis#12699

This PR exposing Lua os.clock() api for getting the elapsed time of Lua
code execution.

Using:
```lua
local start = os.clock()
...
do something
...
local elpased = os.clock() - start
```

---------

Co-authored-by: Meir Shpilraien (Spielrein) <[email protected]>
Co-authored-by: Madelyn Olson <[email protected]>
  • Loading branch information
3 people authored Feb 22, 2024
1 parent 165afc5 commit 4a26555
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
9 changes: 8 additions & 1 deletion deps/lua/src/loslib.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,17 @@ static const luaL_Reg syslib[] = {

/* }====================================================== */

#define UNUSED(V) ((void) V)

/* Only a subset is loaded currently, for sandboxing concerns. */
static const luaL_Reg sandbox_syslib[] = {
{"clock", os_clock},
{NULL, NULL}
};

LUALIB_API int luaopen_os (lua_State *L) {
luaL_register(L, LUA_OSLIBNAME, syslib);
UNUSED(syslib);
luaL_register(L, LUA_OSLIBNAME, sandbox_syslib);
return 1;
}

3 changes: 2 additions & 1 deletion src/script_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static char *libraries_allow_list[] = {
"math",
"table",
"struct",
"os",
NULL,
};

Expand Down Expand Up @@ -1232,14 +1233,14 @@ static void luaLoadLibraries(lua_State *lua) {
luaLoadLib(lua, LUA_STRLIBNAME, luaopen_string);
luaLoadLib(lua, LUA_MATHLIBNAME, luaopen_math);
luaLoadLib(lua, LUA_DBLIBNAME, luaopen_debug);
luaLoadLib(lua, LUA_OSLIBNAME, luaopen_os);
luaLoadLib(lua, "cjson", luaopen_cjson);
luaLoadLib(lua, "struct", luaopen_struct);
luaLoadLib(lua, "cmsgpack", luaopen_cmsgpack);
luaLoadLib(lua, "bit", luaopen_bit);

#if 0 /* Stuff that we don't load currently, for sandboxing concerns. */
luaLoadLib(lua, LUA_LOADLIBNAME, luaopen_package);
luaLoadLib(lua, LUA_OSLIBNAME, luaopen_os);
#endif
}

Expand Down
46 changes: 46 additions & 0 deletions tests/unit/scripting.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,52 @@ start_server {tags {"scripting"}} {
[run_script {return redis.sha1hex('Pizza & Mandolino')} 0]
} {da39a3ee5e6b4b0d3255bfef95601890afd80709 74822d82031af7493c20eefa13bd07ec4fada82f}

test "Measures elapsed time os.clock()" {
set escaped [run_script {
local start = os.clock()
while os.clock() - start < 1 do end
return {double = os.clock() - start}
} 0]
assert_morethan_equal $escaped 1 ;# 1 second
}

test "Prohibit dangerous lua methods in sandbox" {
assert_equal "" [run_script {
local allowed_methods = {"clock"}
-- Find a value from a tuple and return the position.
local indexOf = function(tuple, value)
for i, v in ipairs(tuple) do
if v == value then return i end
end
return nil
end
-- Check for disallowed methods and verify all allowed methods exist.
-- If an allowed method is found, it's removed from 'allowed_methods'.
-- If 'allowed_methods' is empty at the end, all allowed methods were found.
for key, value in pairs(os) do
local index = indexOf(allowed_methods, key)
if index == nil or type(value) ~= "function" then
return "Disallowed "..type(value)..":"..key
end
table.remove(allowed_methods, index)
end
if #allowed_methods ~= 0 then
return "Expected method not found: "..table.concat(allowed_methods, ",")
end
return ""
} 0]
}

test "Verify execution of prohibit dangerous Lua methods will fail" {
assert_error {ERR *attempt to call field 'execute'*} {run_script {os.execute()} 0}
assert_error {ERR *attempt to call field 'exit'*} {run_script {os.exit()} 0}
assert_error {ERR *attempt to call field 'getenv'*} {run_script {os.getenv()} 0}
assert_error {ERR *attempt to call field 'remove'*} {run_script {os.remove()} 0}
assert_error {ERR *attempt to call field 'rename'*} {run_script {os.rename()} 0}
assert_error {ERR *attempt to call field 'setlocale'*} {run_script {os.setlocale()} 0}
assert_error {ERR *attempt to call field 'tmpname'*} {run_script {os.tmpname()} 0}
}

test {Globals protection reading an undeclared global variable} {
catch {run_script {return a} 0} e
set e
Expand Down

0 comments on commit 4a26555

Please sign in to comment.