|
1 | 1 | # rlua -- High level bindings between Rust and Lua |
2 | 2 |
|
3 | | -[](https://circleci.com/gh/amethyst/rlua) |
4 | | -[](https://crates.io/crates/rlua) |
5 | | -[](https://docs.rs/rlua) |
| 3 | +*rlua is now deprecated in favour of mlua: see below for migration information* |
6 | 4 |
|
7 | | -[Guided Tour](examples/guided_tour.rs) |
8 | | - |
9 | | -This library is a high level interface between Rust and Lua. Its goal is to be |
10 | | -an easy to use, practical, flexible, and *safe* API between Rust and Lua. |
11 | | - |
12 | | -`rlua` is NOT designed to be a perfect zero cost wrapper over the Lua C API, |
13 | | -because such a wrapper cannot maintain the safety guarantees that `rlua` is |
14 | | -designed to have. Every place where the Lua C API may trigger an error longjmp |
15 | | -in any way is protected by `lua_pcall`, and the user of the library is protected |
16 | | -from directly interacting with unsafe things like the Lua stack, and there is |
17 | | -overhead associated with this safety. However, performance *is* a focus of the |
18 | | -library to the extent possible while maintaining safety, so if you encounter |
19 | | -something that is egregiously worse than using the Lua C API directly, or simply |
20 | | -something you feel could perform better, feel free to file a bug report. |
21 | | - |
22 | | -## API stability |
23 | | - |
24 | | -Currently, this library follows a pre-1.0 semver, so all API changes should be |
25 | | -accompanied by 0.x version bumps. See the [Version 1.0 |
26 | | -milestone](https://github.com/amethyst/rlua/milestone/1) for the work planned |
27 | | -to be done before a more stable 1.0 release. There may be breaking changes as |
28 | | -these issues are dealt with on the way (the version number will be bumped as |
29 | | -needed). |
30 | | - |
31 | | -## Lua versions supported |
32 | | - |
33 | | -As of release 0.18, the version of Lua can be configured at build time using |
34 | | -Cargo features. Lua 5.4 is the default. The rlua API stays the same with |
35 | | -different Lua versions, though there are a small number of limitations. Lua |
36 | | -code may, of course, behave a little differently between the versions. |
37 | | - |
38 | | -Only one can be selected at a time, so to select anything |
39 | | -other than the default (built-in Lua 5.4) you will need to disable default |
| 5 | +`rlua` is now a thin transitional wrapper around |
| 6 | +[`mlua`](https://github.com/mlua-rs/mlua); it is recommended to use mlua |
| 7 | +directly for new projects and to migrate to it when convenient. `mlua` was a |
| 8 | +fork of `rlua` which has recently seen more development activity and new |
40 | 9 | features. |
41 | 10 |
|
42 | | -The available features are: |
43 | | - |
44 | | -| Cargo feature | Lua version | Notes | |
45 | | -| ------------- | ----------- | ----- | |
46 | | -| builtin-lua54 | Lua 5.4 (source included in package, default) | | |
47 | | -| builtin-lua53 | Lua 5.3 (source included in package) | | |
48 | | -| builtin-lua51 | Lua 5.1 (source included in package) | | |
49 | | -| system-lua54 | Lua 5.4 (installed on host system, found using pkg-config) | | |
50 | | -| system-lua53 | Lua 5.3 (installed on host system, found using pkg-config) | | |
51 | | -| system-lua51 | Lua 5.1 (installed on host system, found using pkg-config) | | |
52 | | -| system-luajit | LuaJIT 2.x (installed on host system, found using pkg-config) | Memory limits not available | |
53 | | - |
54 | | -## Loading external C (or other compiled) modules |
55 | | - |
56 | | -By default rlua blocks Lua from loading of external modules written in C (or |
57 | | -other compiled language) using the Lua C API. To allow this, a couple of |
58 | | -things are needed: |
59 | | - |
60 | | -1. Initialise Lua using `unsafe_new_with_flags()`, and pass non-default flags |
61 | | - not including `LOAD_WRAPPERS` or `REMOVE_LOADLIB`. This will remove |
62 | | - wrappers which block native libraries. |
63 | | - |
64 | | -2. Export the Lua C API symbols so that the library can call Lua C API |
65 | | - functions. There are a few options: |
66 | | - |
67 | | - * At time of writing, Rust has an unstable (nightly-only) option to export |
68 | | - symbols from an executable: |
69 | | - [export-executable-symbols](https://github.com/rust-lang/rfcs/pull/2841) |
70 | | - |
71 | | - * Another option is to do it by adding linker flags. For example, on Linux, |
72 | | - adding this to a `build.rs` asks the linker to export symbols: |
73 | | - `println!("cargo:rustc-link-arg-examples=-Wl,-export-dynamic");` |
74 | | - |
75 | | - * Using the system-lua54 (or similar) feature as described above may link |
76 | | - against a system shared Lua library instead of building the interpreter |
77 | | - into the rlua, making the symbols available. |
78 | | - |
79 | | -### Other Lua features |
80 | | - |
81 | | -Some other features affect how Lua is built (for the builtin versions): |
82 | | - |
83 | | -| Cargo feature | Effect | |
84 | | -| ------------- | ------ | |
85 | | -| lua-no-oslib | Don't compile the Lua `os` library at all (builtin Lua libraries). | |
86 | | - |
87 | | -## Safety and Panics |
88 | | - |
89 | | -The goal of this library is complete safety by default: it should not be |
90 | | -possible to cause undefined behavior with the safe API, even in edge cases. |
91 | | -Unsoundness is considered the most serious kind of bug, so if you find the |
92 | | -ability to cause UB with this API without `unsafe`, please file a bug report. |
93 | | - |
94 | | -This includes calling functions in the Lua standard library; some unsafe |
95 | | -functions are wrapped by default (for example to prevent loading binary |
96 | | -modules), but these wrappers can be disabled using one of the `unsafe` |
97 | | -constructors for the `Lua` object if required for the application. |
98 | | - |
99 | | -Another goal of this library is complete protection from panics: currently, it |
100 | | -should not be possible for a script to trigger a panic. There ARE however |
101 | | -several internal panics in the library, but triggering them is considered a bug. |
102 | | -If you find a way to trigger these internal panics, please file a bug report. |
103 | | - |
104 | | -Yet another goal of the library is to, in all cases, safely handle panics that |
105 | | -are generated inside Rust callbacks. Panic unwinds in Rust callbacks should |
106 | | -currently be handled correctly -- the unwind is caught and carried across the |
107 | | -Lua API boundary as a regular Lua error in a way that prevents Lua from catching |
108 | | -it. This is done by overriding the normal Lua 'pcall' and 'xpcall' functions |
109 | | -with custom versions that cannot catch errors that are actually from Rust |
110 | | -panics, and by handling panic errors on the receiving Rust side by resuming the |
111 | | -panic. |
112 | | - |
113 | | -`rlua` should also be panic safe in another way as well, which is that any `Lua` |
114 | | -instances or handles should remain usable after a user generated panic, and such |
115 | | -panics should not break internal invariants or leak Lua stack space. This is |
116 | | -mostly important to safely use `rlua` types in Drop impls, as you should not be |
117 | | -using panics for general error handling. |
118 | | - |
119 | | -In summary, here is a list of `rlua` behaviors that should be considered a bug. |
120 | | -If you encounter them, a bug report would be very welcome: |
121 | | - |
122 | | - * If you can cause UB with `rlua` without typing the word "unsafe", this is a |
123 | | - bug. |
124 | | - * If your program panics with a message that contains the string "rlua |
125 | | - internal error", this is a bug. |
126 | | - * The above is true even for the internal panic about running out of stack |
127 | | - space! There are a few ways to generate normal script errors by running out |
128 | | - of stack, but if you encounter a *panic* based on running out of stack, this |
129 | | - is a bug. |
130 | | - * When the internal version of Lua is built using the `cc` crate, and |
131 | | - `cfg!(debug_assertions)` is true, Lua is built with the `LUA_USE_APICHECK` |
132 | | - define set. Any abort caused by this internal Lua API checking is |
133 | | - definitely a bug, and is likely to be a soundness bug because without |
134 | | - `LUA_USE_APICHECK` it would likely instead be UB. |
135 | | - * Lua C API errors are handled by longjmp. All instances where the Lua C API |
136 | | - would otherwise longjmp over calling stack frames should be guarded against, |
137 | | - except in internal callbacks where this is intentional. If you detect that |
138 | | - `rlua` is triggering a longjmp over your Rust stack frames, this is a bug! |
139 | | - * If you can somehow handle a panic triggered from a Rust callback in Lua, |
140 | | - this is a bug. |
141 | | - * If you detect that, after catching a panic or during a Drop triggered from a |
142 | | - panic, a `Lua` or handle method is triggering other bugs or there is a Lua |
143 | | - stack space leak, this is a bug. `rlua` instances are supposed to remain |
144 | | - fully usable in the face of user generated panics. This guarantee does not |
145 | | - extend to panics marked with "rlua internal error" simply because that is |
146 | | - already indicative of a separate bug. |
147 | | - |
148 | | -## Sandboxing and Untrusted Scripts |
149 | | - |
150 | | -The API now contains the pieces necessary to implement simple, limited |
151 | | -"sandboxing" of Lua scripts by controlling their environment, limiting their |
152 | | -allotted VM instructions, and limiting the amount of memory they may allocate. |
153 | | - |
154 | | -These features deserve a few words of warning: **Do not use them to run |
155 | | -untrusted scripts unless you really Know What You Are Doing (tm)** (and even |
156 | | -then, you probably should not do this). |
157 | | - |
158 | | -First, this library contains a huge amount of unsafe code, and I currently |
159 | | -*would not trust it in a truly security sensitive context*. There are almost |
160 | | -certainly bugs still lurking in this library! It is surprisingly, fiendishly |
161 | | -difficult to use the Lua C API without the potential for unsafety. |
| 11 | +## Migration |
162 | 12 |
|
163 | | -Second, properly sandboxing Lua scripts can be quite difficult, much of the |
164 | | -stdlib is unsafe, and sometimes in surprising ways. Some information on this |
165 | | -can be found [here](http://lua-users.org/wiki/SandBoxes). |
| 13 | +`rlua` 0.20 includes some utilities to help transition to `mlua`, but is otherwise |
| 14 | +just re-exporting `mlua` directly. |
166 | 15 |
|
167 | | -Third, PUC-Rio Lua is a C library not *really* designed to be used with |
168 | | -untrusted scripts. Please understand that though PUC-Rio Lua is an extremely |
169 | | -well written language runtime, it is still quite a lot of C code, and it is not |
170 | | -commonly used with truly malicious scripts. Take a look |
171 | | -[here](https://www.lua.org/bugs.html) and count how many bugs resulted in memory |
172 | | -unsafety in the interpreter. Another small example: did you know there is a way |
173 | | -to attack Lua tables to cause linear complexity in the table length operator? |
174 | | -That this still counts as one VM instruction? |
| 16 | +The main changes are: |
175 | 17 |
|
176 | | -Fourth, if you provide a callback API to scripts, it can be very difficult to |
177 | | -secure that API. Do all of your API functions have some maximum runtime? Do |
178 | | -any of your API functions allow the script to allocate via Rust? Are there |
179 | | -limits on how much they can allocate this way? All callback functions still |
180 | | -count as a single VM instruction! |
| 18 | +* In `mlua`, `Lua::context()` is no longer necessary. The methods previously on |
| 19 | + `Context` can now be called directly on the `Lua` object. `rlua` 0.20 includes |
| 20 | + an `RluaCompat` extension trait which adds a `context()` method which can be used |
| 21 | + to avoid having to update code all at once. |
181 | 22 |
|
182 | | -In any case, sandboxing in this way may still be useful to protect against buggy |
183 | | -(but non-malicious) scripts, and may even serve as a single *layer* of a larger |
184 | | -security strategy, but **please think twice before relying on this to protect |
185 | | -you from untrusted Lua code**. |
| 23 | +* The `ToLua` trait has been renamed to `IntoLua`, and its conversion method `to_lua` |
| 24 | + is now `into_lua`. `rlua` 0.20 includes `ToLua` as an alias for `IntoLua` and an |
| 25 | + extension `ToLuaCompat` which adds a `to_lua` method as a temporary convenience. |
186 | 26 |
|
187 | | -## License |
| 27 | +A few other changes which should be less disruptive: |
188 | 28 |
|
189 | | -This project is licensed under the [MIT license](LICENSE) |
| 29 | +* `mlua` has different defaults and options for blocking loading C libraries or |
| 30 | + compiled modules from Lua code or catching Rust panics. Check the `Lua::new_with` |
| 31 | + and unsafe variants for the new options. |
0 commit comments