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

Safer handling of the Lua stack #188

Merged
merged 8 commits into from
Jul 22, 2021
Merged

Conversation

guidanoli
Copy link
Contributor

@guidanoli guidanoli commented Jul 16, 2021

Some places don't lock the LuaRuntime before fiddling with the Lua stack, and many places don't ensure extra slots either.
This PR also involves other changes:

  • Add preconditions and postconditions to important functions/methods
  • Change behaviour _LuaObject.__str__
    • If __tostring doesn't exist, use _LuaObject.__repr__
    • If __tostring returns non-string value, raise a TypeError
    • If __tostring raises an error, raise a LuaError
  • lua_pushglobaltable instead of pushing _G, which may not point to the global table.
  • Make lock_runtime return boolean and not raise error directly
  • Use LUA_NOREF and LUA_REFNIL constants for effective handling of Lua references
  • Add important assertions (checks if LuaRuntime._state is not NULL, and if _LuaObject._runtime is None, etc...)

@guidanoli
Copy link
Contributor Author

Can you review this, @scoder ?

* Make lock_runtime return boolean and not raise error directly
* If check_lua_stack fails on _LuaRuntimeStack.__enter__, unlocks
LuaRuntime, avoiding the resource to be acquired forever
* Use _LuaRuntimeStack in py_to_lua_custom (unrolls stack on Python error)
* Use _LuaRuntimeStack in py_to_lua_overflow (catches error on py_to_lua_custom)
* Add preconditions and postconditions to important functions/methods
* Add assertion for extra >= 0 on check_lua_stack (would violate Lua C
API precondition and probably cause undefined behaviour)
* Lock LuaRuntime instance on _fix_args_kwargs and ensure 1 Lua stack slot
@scoder
Copy link
Owner

scoder commented Jul 16, 2021

Did you test if this has any performance impact? (I.e., can you prove that it doesn't?)

@guidanoli
Copy link
Contributor Author

Did you test if this has any performance impact? (I.e., can you prove that it doesn't?)

Previously, with the context handler it was 50% the speed.
Now we use inline functions getstacktop and setstacktop, which is now 94% the speed, which I think it's pretty acceptable since the gains in readability and maintainability are greater.

@guidanoli
Copy link
Contributor Author

guidanoli commented Jul 17, 2021

Note that getstacktop unlocks the runtime if check_lua_stack raises an error, which is quite important. Otherwise, this resource would not be properly released.

@guidanoli
Copy link
Contributor Author

I change my mind -- I think it's fine the way it is, explicit and efficient.
However, I still check for extra stack space wherever needed.

@guidanoli guidanoli changed the title Context Handler for Lua stack Safer handling of the Lua stack Jul 18, 2021
@guidanoli guidanoli requested a review from scoder July 19, 2021 17:32
@guidanoli
Copy link
Contributor Author

Could you review this, @scoder? It makes Lupa more reliable, in that we guarantee we can push new values to the Lua stack.

@scoder
Copy link
Owner

scoder commented Jul 21, 2021

Yes, this looks very clean and helpful.

@guidanoli
Copy link
Contributor Author

guidanoli commented Jul 22, 2021

Let's get this merged, @scoder ? I depend on it for future PRs.

@scoder scoder merged commit 7fe8768 into scoder:master Jul 22, 2021
@scoder
Copy link
Owner

scoder commented Jul 22, 2021

Works for me. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants