Skip to content

Conversation

kevin-montrose
Copy link
Contributor

@kevin-montrose kevin-montrose commented Mar 7, 2025

Changes:

  • Allowlist for imported functions (LuaAllowedFunctions)
  • cjson.encode and cjson.decode
  • bit.* functions
  • math.* functions
  • cmsgpack.pack and cmsgpack.unpack
  • struct.pack, struct.unpack, and struct.size
  • Removing a bunch of default included functions that are in Lua 5.4 but not Lua 5.1

TODOs:

  • Tests for the weirder math.* functions
  • Test that configs cannot include unintended functions
  • Tests for deeply recursive (de)serialization
  • Test for Invalid lua can trigger assertion and affect future command #1079
  • Fix various TODOs where we allocate List<byte> or byte[] instances and instead use ScratchBufferManager
  • Crawl though issues for everything this closes
  • Benchmarks

While large, this is basically rote as it's just filling it a bunch of missing functions.

Note that getfenv and setfenv remain unimplemented, as they're Lua 5.1-isms that would be very difficult to implement and are rarely used. Users can adapt their scripts to use load instead as a workaround.

LuaAllowedFunctions

This controls the set of global functions available to a Lua script, including functions grouped under redis.

This is mostly a defense in depth thing for when Garnet is hosted as a service, allowing problematic functions to be disabled without code changes.

cjson

Serialization is bespoke because of the need to handle data passed on the Lua stack.
Deserialization uses .NET's built-in JsonNode.

cmsgpack

Rather than pull in a dependency, I implemented both serialization and deserialization.

The format isn't too complicated and serialization would be bespoke for the same reasons as cjson.encode, so it seemed like a reasonable decisions.

Linux & .NET 9+ test skips

Some of these tests are finally reproducing the longjmp issue the .NET folks warned about, and failing pretty reliably. For affected tests, I'm skipping the exception parts on with a TODO for the next phase.

Benchmarks

main is as of 225b2ae13c37e9583b752e068a4058b603f69c0a
luaRuntimeLibraries is as of 225b2ae13c37e9583b752e068a4058b603f69c0a

Just reporting LuaRunnerOperations (and just Native memory management) as that's where the impact is expected to be largest.

TL;DR - Construction costs are up, as expected, since the loader block is much larger now. Biggest relative hit is in ConstructSmall where the loader block dominates, and is ~34%. Compilation is somewhat improved (~20%), as more of the loader runs at construction time.

I can picture a way to speed this up (basically, keeping some LuaStateWrappers pre-initialized and having sessions obtain them on demand), but I'm not sure it's worth the complexity.
main

Method Params Mean Error StdDev Median Allocated
ResetParametersSmall Native,None 6.660 us 0.5793 us 1.653 us 6.450 us 688 B
ResetParametersLarge Native,None 6.585 us 0.6188 us 1.785 us 6.200 us 1024 B
ConstructSmall Native,None 297.882 us 16.8782 us 48.427 us 282.100 us 856 B
ConstructLarge Native,None 295.097 us 14.0580 us 40.560 us 276.750 us 4256 B
CompileForSessionSmall Native,None 38.819 us 1.7545 us 4.891 us 37.700 us 736 B
CompileForSessionLarge Native,None 122.918 us 7.1262 us 20.331 us 115.650 us 688 B

luaRuntimeLibraries

Method Params Mean Error StdDev Median Allocated
ResetParametersSmall Native,None 6.839 us 0.3603 us 1.051 us 6.950 us 688 B
ResetParametersLarge Native,None 6.181 us 0.5141 us 1.475 us 5.850 us 688 B
ConstructSmall Native,None 399.046 us 23.0923 us 66.627 us 369.500 us 1152 B
ConstructLarge Native,None 361.200 us 17.1312 us 47.470 us 349.900 us 3928 B
CompileForSessionSmall Native,None 30.435 us 1.1521 us 3.287 us 30.600 us 400 B
CompileForSessionLarge Native,None 109.432 us 5.9090 us 17.049 us 102.150 us 1024 B

Related Issues

@badrishc badrishc linked an issue Mar 8, 2025 that may be closed by this pull request
@kevin-montrose kevin-montrose marked this pull request as ready for review March 10, 2025 20:36
@badrishc badrishc requested a review from Copilot March 12, 2025 19:56
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements the remaining Lua runtime libraries and extends the allowlist configuration for imported Lua functions. Key changes include:

  • Adding support for various Lua library functions (cjson, cmsgpack, bit, math, struct).
  • Extending the configuration options (LuaAllowedFunctions) and updating corresponding tests.
  • Updating native and managed Lua APIs and associated benchmarks.

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/Garnet.test/GarnetServerConfigTests.cs Updated test comments to reflect JSON args usage
test/Garnet.test/LuaScriptRunnerTests.cs Adjusted error message expectations and added tests for allowed functions
libs/server/ArgSlice/ScratchBufferManager.cs Added a UTF8EncodeString method to better handle string encoding in the scratch buffer
libs/host/Configuration/Options.cs Extended Options to support allowed Lua functions via a new HashSet property
libs/server/Lua/LuaStateWrapper.cs Introduced a Remove() method and improved documentation on load functions
libs/server/Lua/LuaOptions.cs Updated the constructor to accept allowed functions and initialize them appropriately
benchmark/BDN.benchmark/Operations/OperationsBase.cs Updated LuaOptions instantiation to include allowedFunctions parameter
libs/server/Lua/SessionScriptCache.cs Passed allowedFunctions to LuaRunner to enforce function restrictions
benchmark/BDN.benchmark/Lua/LuaParams.cs Revised options creation for benchmarks with allowed functions
test/Garnet.test/TestUtils.cs Modified test helpers to accept allowed Lua functions
libs/server/Lua/NativeMethods.cs Enhanced native method documentation and updated methods to reflect execution changes in load functions

@badrishc badrishc linked an issue Mar 18, 2025 that may be closed by this pull request
@kevin-montrose kevin-montrose merged commit b5135c6 into main Mar 18, 2025
26 checks passed
@kevin-montrose kevin-montrose deleted the luaRuntimeLibraries branch March 18, 2025 14:41
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

2 participants