Commit Graph

72 Commits

Author SHA1 Message Date
kyren 84b009da03 A few small performance improvements
When 'debug_assertions' is not enabled, don't bother doing asserts in
stack_guard / stack_err_guard.  Also, add an optional feature not enabled by
default to disable LUA_USE_APICHECK in release mode.  Once the bugs in rlua that
allow you to trigger LUA_USE_APICHECK are fixed, this feature will be the
default behavior.
2018-02-09 01:22:34 -05:00
kyren 164250b352 Don't panic with "rlua internal error" message on panics that are not internal
It is part of the contract that only LuaRef types constructed from the same
parent Lua state are passed into Lua, so generating a panic there is not an
internal error.
2018-02-07 17:05:00 -05:00
kyren 98ee4e9492 More correct scope drop behavior
now no longer aborts if a Drop impl panics
2018-02-07 16:42:03 -05:00
kyren cb25a99f70 Lots of changes, not sure if actually safe yet.
* Make Lua Send
* Add Send bounds to (nearly) all instances where userdata and functions are
  passed to Lua
* Add a "scope" method which takes a callback that accepts a `Scope`, and give
  `Scope` the ability to create functions and userdata that are !Send, *and also
  functions that are not even 'static!*.
2018-02-06 20:53:25 -05:00
kyren fe35742026 Set the metatable of __gc'ed userdata to something more informative 2018-02-05 14:40:20 -05:00
kyren 6382baa991 Use ptr::write to initialize uninitalized memory, NOT mem::replace 2018-01-27 18:38:00 -05:00
kyren 77eb73a50c Simplify handling of userdata __gc and resurrected userdata.
Now, simply remove the userdata table immediately before dropping the userdata.
This does two things, it prevents __gc from double dropping the userdata, and
after the first call to __gc, it prevents the userdata from being identified as
any particular userdata type, so it cannot be misused after being finalized.

This change thus removes the userdata invalidation error, and simplifies a lot
of userdata handling code.

It also fixes a panic bug.  Because there is no predictable order for
finalizers, it is possible to run a userdata finalizer that does not resurrect
itself before a lua table finalizer that accesses that userdata, and this means
that there were several asserts that were possible to trigger in normal Lua code
in util.rs related to `WrappedError`.

Now, finalized userdata is simply a userdata with no methods, so any use of
finalized userdata becomes a normal script runtime error (though, with a
potentially confusing error message).  As a future improvement, we could set
a metatable on finalized userdata that provides a better error message.
2018-01-27 18:27:01 -05:00
kyren 42007260ca Add automatic Lua "user accessible registry" keys
Also, during the implementation of this, I noticed a problem with the 0.10
memory safety, which is that luaL_ref is also memory unsafe.  I attempted to
change the API to support luaL_ref potentially returning Result, but this change
will cause an enormous amount of API chaos, (just as an example, it becomes
impossible to implement Clone for LuaRef as is).  Instead, luaL_ref now is
guarded by gc_guard.
2017-12-17 00:46:22 -05:00
kyren bfb6111e0a API for registry access via string keys only (for now)
Also includes some fixes for stack usage and changes an assert_eq to lua_assert
2017-12-16 17:44:13 -05:00
kyren 447810364a extra space 2017-12-04 23:57:08 -05:00
kyren 66a4e9a8e7 Add `ExpiredUserData` error and avoid what was previously a panic
Also make sure that panic messages clearly state that they are internal errors,
so people report them as a bug.  Since the only panics left are all internal
errors, just move the internal error message into the panic / assert macros.
2017-12-04 02:50:27 -05:00
kyren a44b6b5170 Move function and thread into their own modules, auto-formatting 2017-12-04 00:57:39 -05:00
kyren 51838f3509 Include garbage collector error type, remove unnecessary setmetatable wrapper 2017-12-04 00:35:13 -05:00
kyren d76935e683 I *THINK* this might actually be it, is rlua 'm' safe now? 2017-12-04 00:15:20 -05:00
kyren e80e7d4540 missing push_string calls in util 2017-12-03 23:25:03 -05:00
kyren 67e8907f19 Couple of changes in preparation for 'm' safety:
- auto formatting
- add gc control to ffi
- add gc_guard to util functions
- use gc_guard to make util error handling functions never trigger __gc
  metamethod Lua errors even without __gc metatable wrapper
- sort of a technicality, don't call luaL_requiref outside of the Lua
  constructor, as it could trigger the garbage collector when user code has had
  a chance to set __gc metamethods.  Changes the API to load the debug table.
2017-12-03 23:01:03 -05:00
kyren 0bd676aa81 more refactoring 2017-12-03 21:19:32 -05:00
kyren c95f591935 remove specific protected functions in favor of generic protect_lua_call 2017-12-03 20:29:41 -05:00
kyren a490229f31 More refactoring towards mem error safety 2017-12-03 20:10:45 -05:00
kyren e41c72d642 more refactoring trying to find a workable path to 'm' error safety 2017-12-03 18:15:31 -05:00
kyren 5742e3f20a still making small structural changes
slowly trying to refactor things until using all the protected calls in
protected_ffi is workable
2017-12-02 19:16:57 -05:00
kyren 8a6161b16f new strategy for protected ffi calls 2017-12-02 18:37:17 -05:00
kyren b7c80ec066 make some things private 2017-12-02 17:47:00 -05:00
kyren 2b7e89e7c6 move error / panic metatable creation 2017-12-02 17:13:46 -05:00
kyren fa1703d3d1 split macros into their own file 2017-12-02 17:04:33 -05:00
kyren 8a7e03978b Experimental protected versions of all used 'm' erroring functions 2017-12-02 15:41:53 -05:00
Timidger 8ac27877e4 Only get gc on setmetatable if metatable isn't nil 2017-11-07 20:13:30 -08:00
kyren deab88a851 Fix xpcall return values, make the tests a lot better. 2017-10-25 14:51:10 -04:00
kyren 773bf3e9ba Fix some clippy lints, possible edge case API incompatibility around HashMap 2017-10-24 16:15:57 -04:00
kyren e7661a5c27 auto-formatting 2017-10-23 16:42:20 -04:00
kyren 4b7a3403bc Cleanups of userdata handling, particularly around callbacks
First, make sure that `add_methods` cannot trigger another userdata registry
insert, causing an unintended panic.  Second, remove `RefCell` surrounding
userdata hashmap, as this change makes it no longer needed.  Third, add a
`RefCell` around `Callback` because FnMut means that callbacks cannot recurse
into themselves, and panic appropriately when this happens.  This should
eventually be turned into an error.
2017-10-14 18:26:09 -04:00
kyren 2553623b65 Provide custom allocators that ensure that OOM results in an abort
(closes unsafety hole)
2017-08-02 16:33:48 -04:00
kyren 16f57d18e5 Merge pull request #34 from jonas-schievink/better-error
[WIP] Enhanced errors
2017-08-02 16:00:08 -04:00
kyren c3c7d8c093 setmetatable should return the table 2017-08-02 15:56:16 -04:00
Jonas Schievink dd1d335cee Handle LUA_ERRERR 2017-08-02 21:41:35 +02:00
kyren 9c34d4b99f Fix soundness problems with rlua
setmetatable now wraps a __gc method in a cclosure that aborts on error, also
'debug' library is no longer provided.  We could provide just the subset of the
debug library that is sound, though.
2017-08-02 14:36:54 -04:00
kyren f9f3d05804 Fix argument bugs with pcall / xpcall, add tests for it 2017-08-02 10:42:18 -04:00
Jonas Schievink 70f05ac068 Remove ErrorError 2017-08-02 14:01:00 +02:00
Jonas Schievink a1626399c4 Fix compilation 2017-08-01 23:43:51 +02:00
Jonas Schievink cb61d53ba5 Fix error message and shorten code 2017-08-01 23:42:42 +02:00
Jonas Schievink ed6e7ed5e7 Don't dispose whole traceback when it's not utf-8 2017-08-01 23:38:12 +02:00
Jonas Schievink bf76e41487 Merge IncompleteStatement into SyntaxError
Both are a form of syntax error, this reflects that better. No
functionality is lost, incomplete inputs are moved to a bool field of
SyntaxError.
2017-08-01 23:23:31 +02:00
Jonas Schievink ed0565c176 Fix handling of `CallbackError`
Previously, the traceback would be printed, but not the actual error.

I've removed traceback printing completely, not sure if that's a good
idea. A `Display` impl that outputs multiple lines feels weird.
2017-08-01 23:23:31 +02:00
Jonas Schievink 7e250dacce Enhance error messages 2017-08-01 23:23:31 +02:00
kyren d415455ccb Fix several bugs with error handling in xxx_with_traceback functions
In resume_with_traceback, always use the coroutine stack for error handling so
we don't miss panics, in both _with_traceback functions remove the temporary
traceback entry from the stack.
2017-07-27 17:16:40 -04:00
kyren a2b77f37a2 'main_state' fix
Dont' confuse the state we're pushing the registry value for the main state to
with the main state itself, pop from the correct state.
2017-07-27 16:47:58 -04:00
kyren 69fa01df45 auto formatting 2017-07-24 10:40:00 -04:00
kyren 698785df64 Merge remote-tracking branch 'base/master' into remove-lua-prefix 2017-07-24 07:21:54 -04:00
kyren 44c99ea1b9 Remove error_guard
Replace with custom protected versions of lua ffi functions.
2017-07-23 13:41:46 -04:00
Jonas Schievink 9df7727eaa Remove the `Lua*` prefix from most types
cc #15

Doesn't touch `LuaString` mainly because that's a *lot* of renaming work
and the code looks weird. Also I want feedback before I proceed.
2017-07-23 18:36:50 +02:00
kyren 2bd7a2ee8c Reduce error_guard code to as little as possible
Also ensure that on error in error_guard the stack is in a predictable place.
2017-07-23 02:08:32 -04:00
kyren 36134e6373 Userdata can have __gc metamethods called multiple times
Lua 5.3 has the ability for scripts to define __gc metamethods on
tables, which gives them the ability to "resurrect" userdata after __gc
has been called.  This means, __gc can be called multiple times on
userdata.  This commit protects against this by simply panicking on
access after resurrection.  This is possibly not the best approach?
2017-07-23 01:00:33 -04:00
kyren 6b8a4240e2 format fix, fixes rustfmt warning 2017-06-30 15:17:53 -04:00
kyren d3b311fe49 Another major API change, out of stack space is not an Err
It, ahem "should not" be possible to exhaust lua stack space in normal usage,
and causing stack errors to be Err is slightly obnoxious.  I have been wanting
to make this change for a while, and removing the callback API from tables makes
this sensible *I think*.

I can think of a couple of ways that this is not technically true, but I think
that they are acceptable, or should be handled differently.

One, you can make arbitrarily sized LuaVariadic values.  I think this is maybe a
bug already, because there is an argument limit in Lua which is lower than the
stack limit.  I'm not sure what happens there, but if it is a stack based panic,
(or any panic?) it is a bug.

Two, I believe that if you recurse over and over between lua -> rust -> lua ->
rust etc, and call rlua API functions, you might get a stack panic.  I think for
trusted lua code, this is morally equivalent to a regular stack overflow in
plain rust, which is already.. well it's not a panic but it's some kind of safe
crash I'm not sure, so I think this is acceptable.  For *untrusted* lua code,
this could theoretically be a problem if the API provided a callback that would
call back into lua, then some lua script could force a stack based panic.  There
are so many concerns with untrusted lua code, and this library is NOT safe
enough yet for untrusted code (it doesn't even provide an option to limit lua to
the safe API subset yet!), so this is not currently an issue.  When the library
provides support for "safe lua", it should come with big warnings anyway, and
being able to force a stack panic is pretty minor in comparison.

I think if there are other ways to cause unbounded stack usage, that it is a
bug, or there can be an error just for that situation, like argument count
limits.

This commit also fixes several stupid bugs with tests, stack checking, and
panics.
2017-06-25 17:15:11 -04:00
kyren bf9bf849c2 Simplification of error types
The multi-level error types were a mistake.  Probably should have waited on the
cargo version bump, oh well.
2017-06-25 04:25:48 -04:00
kyren 7dba280a4b Tests for LuaError conversion, Important pcall / xpcall bugfixes. 2017-06-25 02:40:09 -04:00
kyren 2c439f8097 Not sure I like everything about this approach yet 2017-06-25 01:47:55 -04:00
kyren a609f709ee A lot of corner case bugfixes 2017-06-25 01:10:26 -04:00
kyren b59b8cc23b Different strategy.. 2017-06-24 22:26:35 -04:00
kyren 1fda34225e Small improvements to WrappedError
Currently trying to figure out how to make LuaError a proper LuaUserDataType
2017-06-24 21:44:27 -04:00
kyren 3deb6df525 Lots of LuaError changes
It is possible that I have gone too far here into error discrimination and
should scale it back, not sure yet.
2017-06-24 20:57:04 -04:00
kyren 47db72cac4 Big API incompatible error change, remove dependency on error_chain
The current situation with error_chain is less than ideal, and there are lots of
conflicting interests that are impossible to meet at once.  Here is an
unorganized brain dump of the current situation, stay awhile and listen!

This change was triggered ultimately by the desire to make LuaError implement
Clone, and this is currently impossible with error_chain.  LuaError must
implement Clone to be a proper lua citizen that can live as userdata within a
lua runtime, because there is no way to limit what the lua runtime can do with a
received error.  Currently, this is solved by there being a rule that the error
will "expire" if the error is passed back into rust, and this is very
sub-optimal.  In fact, one could easily imagine a scenario where lua is for
example memoizing some function, and if the function has ever errored in the
past the function should continue returning the same error, and this situation
immediately fails with this restriciton in place.

Additionally, there are other more minor problems with error_chain which make
the API less good than it could be, or limit how we can use error_chain.  This
change has already solved a small bug in a Chucklefish project, where the
conversion from an external error type (Borrow[Mut]Error) was allowed but not
intended for user code, and was accidentally used.  Additionally, pattern
matching on error_chain errors, which should be common when dealing with Lua, is
less convenient than a hand rolled error type.

So, if we decide not to use error_chain, we now have a new set of problems if we
decide interoperability with error_chain is important.  The first problem we run
into is that there are two natural bounds for wrapped errors that we would
pick, (Error + Send + Sync), or just Error, and neither of them will
interoperate well with error_chain.  (Error + Send + Sync) means we can't wrap
error chain errors into LuaError::ExternalError (they're missing the Sync
bound), and having the bounds be just Error means the opposite, that we can't
hold a LuaError inside an error_chain error.

We could just decide that interoperability with error_chain is the most
important qualification, and pick (Error + Send), but this causes a DIFFERENT
set of problems.  The rust ecosystem has the two primary error bounds as Error
or (Error + Send + Sync), and there are Into impls from &str / String to
Box<Error + Send + Sync> for example, but NOT (Error + Send).  This means that
we are forced to manually recreate the conversions from &str / String to
LuaError rather than relying on a single Into<Box<Error + Send + Sync>> bound,
but this means that string conversions have a different set of methods than
other error types for external error conversion.  I have not been able to figure
out an API that I am happy with that uses the (Error + Send) bound.  Box<Error>
is obnoxious because not having errors implement Send causes needless problems
in a multithreaded context, so that leaves (Error + Send + Sync).  This is
actually a completely reasonable bound for external errors, and has the nice
String Into impls that we would want, the ONLY problem is that it is a pain to
interoperate with the current version of error_chain.

It would be nice to be able to specify the traits that an error generated by the
error_chain macro would implement, and this is apparently in progress in the
error_chain library.  This would solve both the problem with not being able to
implement Clone and the problems with (Error + Send) bounds.  I am not convinced
that this library should go back to using error_chain when that functionality is
in stable error_chain though, because of the other minor usability problems with
using error_chain.

In that theoretical situation, the downside of NOT using error_chain is simply
that there would not be automatic stacktraces of LuaError.  This is not a huge
problem, because stack traces of lua errors are not extremely useful, and for
external errors it is not too hard to create a different version of the
LuaExternalResult / LuaExternalError traits and do conversion from an
error_chain type into a type that will print the stacktrace on display, or
use downcasting in the error causes.

So in summary, this library is no longer using error_chain, and probably will
not use it again in the future.  Currently this means that to interoperate with
error_chain, you should use error_chain 0.8.1, which derives Sync on errors, or
wait for a version that supports user defined trait derives.  In the future
when error_chain supports user defined trait derives, users may have to take an
extra step to make wrapped external errors print the stacktrace that they
capture.

This change works, but is not entirely complete.  There is no error
documentation yet, and the change brought to a head an ugly module organization
problem.  There will be more commits for documentation and reorganization, then
a new stable version of rlua.
2017-06-24 18:11:56 -04:00
kyren 802d5467c1 Change LuaError ToLua approach
Lua should be consistent independent of the way they are produced.  pcall in lua
should produce the same sort of error that would be returned by a LuaError
conversion.

The situation is not currently great, because LuaError is not Clone, so passing
a LuaError back into rust will result in the error being "consumed".
2017-06-23 19:41:08 -04:00
kyren 0022057058 rustfmt changes 2017-06-15 10:26:39 -04:00
kyren 8203414b76 Use LUA_RIDX_MAINTHREAD instead of weird bespoke method 2017-06-05 05:03:18 -04:00
kyren cb7c55f742 Don't need pub on internal function 2017-06-05 01:51:48 -04:00
kyren b3218137e1 Somewhat smarter strategy for error_guard calls, less ungodly slow.
Also add raw_length table function
2017-06-05 00:41:48 -04:00
kyren 47d4ea62ff Handle unprotected lua errors SOMEWHAT more elegantly
There should be drastically less ways to cause unprotected lua errors now, as
the LuaTable functions which were trivial to cause unprotected errors are now
protected. Unfortunately, they are protected in a pretty slow, terrible way
right now, but it at least works.

Also, set the atpanic function in lua to call a proper rust panic instead.
2017-06-05 00:03:39 -04:00
kyren 0111625d17 Basic coroutine support, currently very slightly broken
There is no longer any protection in passing LuaValue between
independent states, it is being re-added
2017-05-24 23:29:57 -04:00
kyren 5c0f183a52 Improved error handling and formatting
Now prints lua backtraces on callback errors as well.  This could be
controlled with LUA_BACKTRACE or just RUST_BACKTRACE or similar.
2017-05-22 14:25:36 -04:00
kyren b15ee9053e Update to use hlist_macro for hlist macros.
You will type hlist! hlist_pat! and HList! so often that every character
counts.  Apologize for the API churn in the README.
2017-05-22 11:16:34 -04:00
kyren 065c69894a Initial import 2017-05-21 19:50:59 -04:00