Skip to main content

Libs

Struct Libs 

Source
pub struct Libs(/* private fields */);
Expand description

The libraries a script gets: its capabilities. Combine with |.

Implementations§

Source§

impl Libs

Source

pub const NONE: Libs

Nothing.

Source

pub const BASE: Libs

The base functions (type, pairs, pcall, setmetatable, tostring…).

Source

pub const STRING: Libs

string, and the string metatable (s:upper()).

Source

pub const TABLE: Libs

table.

Source

pub const MATH: Libs

math.

Source

pub const UTF8: Libs

utf8.

Source

pub const COROUTINE: Libs

coroutine.

Source

pub const OS: Libs

os.time, os.clock, os.date, os.difftime, os.setlocale (the "C" locale only; os.getenv is ENV’s).

Source

pub const LOAD: Libs

load (source text only, never bytecode).

Source

pub const PRINT: Libs

print (to the host’s sink or Lua::take_output).

Source

pub const GC: Libs

collectgarbage (count, step, collect; steps are paid for in fuel).

Source

pub const DEBUG_INFO: Libs

debug.traceback and a read-only debug.getinfo (no hooks, locals, upvalues, registry or functions handed out). Not in SAFE, STANDARD or ALL: a host opts in.

Source

pub const REQUIRE: Libs

require and package (loaded, preload, searchers), with modules only from the host (Lua::add_module, Lua::set_module_loader): no search path, no files. Not in SAFE, STANDARD or ALL: a host opts in.

Source

pub const ENV: Libs

os.getenv, reading only what the host put in the state’s map (Lua::set_env), never the process environment. Not in SAFE, STANDARD or ALL: a host opts in.

Source

pub const IO: Libs

io over streams the host gives: io.stdin, io.stdout and io.stderr are the host’s (Lua::set_stdin, Lua::set_stdout, Lua::set_stderr; by default stdin is empty and the others write where print writes), and io.open asks the host’s opener (Lua::set_file_opener): no file system, no process (io.popen and io.tmpfile are absent). Not in SAFE, STANDARD or ALL: a host opts in.

Source

pub const SAFE: Libs

Everything but load, print and collectgarbage: what untrusted code gets.

Source

pub const STANDARD: Libs

The safe set plus print and collectgarbage.

Source

pub const ALL: Libs

Every library of the standard set, load included (not the opt-in DEBUG_INFO, REQUIRE, ENV and IO).

Source

pub const fn union(self, other: Libs) -> Libs

Both sets (| in a const).

Examples found in repository?
examples/secprobe.rs (line 20)
19fn state(mem: usize, fast: bool) -> Lua {
20    let libs = Libs::ALL.union(Libs::DEBUG_INFO).union(Libs::REQUIRE);
21    let mut lua = Lua::new(Limits::default().memory(mem).string_len(32 << 20).fuel(1 << 34).hash_seed(7), libs);
22    lua.add_module("hostmod", "local M = {n = 1} for i = 1, 20 do M[i] = function() return i end end return M");
23    lua.set_fast_loop(fast);
24    lua.set_print(|_| {});
25    lua
26}
More examples
Hide additional examples
examples/secmem.rs (line 13)
8fn main() {
9    let a: Vec<String> = std::env::args().collect();
10    let src = a.get(1).cloned().unwrap_or_default();
11    let mem: usize = a.get(2).and_then(|s| s.parse().ok()).unwrap_or(64) << 20;
12    let fuel: u64 = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(1 << 32);
13    let libs = Libs::ALL.union(Libs::DEBUG_INFO).union(Libs::REQUIRE);
14    let mut lua = Lua::new(Limits::default().memory(mem).fuel(fuel).hash_seed(7), libs);
15    lua.set_print(|_| {});
16    let h0 = hwm();
17    let t = std::time::Instant::now();
18    let r = lua.exec(src, "=s");
19    let r = match r { Ok(v) => format!("ok {:?}", v.first().and_then(|x| lua.to_string_lossy(*x))), Err(e) => format!("err {e}") };
20    let now = std::fs::read_to_string("/proc/self/statm").ok().and_then(|s| s.split_whitespace().nth(1)?.parse::<usize>().ok()).unwrap_or(0) * 4096;
21    println!(
22        "peak rss {} MiB (from {} MiB) under a {} MiB limit, rss now {} MiB with {} MiB counted, {:?}, fuel {}, {}",
23        hwm() >> 10,
24        h0 >> 10,
25        mem >> 20,
26        now >> 20,
27        lua.memory_used() >> 20,
28        t.elapsed(),
29        fuel - lua.fuel_left(),
30        &r[..r.len().min(200)]
31    );
32}
examples/seclat.rs (line 16)
10fn main() {
11    let a: Vec<String> = std::env::args().collect();
12    let setup = a.get(1).cloned().unwrap_or_default();
13    let body = a.get(2).cloned().unwrap_or_default();
14    let ms: u64 = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(10);
15    let mem: usize = a.get(4).and_then(|s| s.parse().ok()).unwrap_or(512) << 20;
16    let libs = Libs::ALL.union(Libs::DEBUG_INFO).union(Libs::REQUIRE);
17    let mut worst = Duration::ZERO;
18    let mut last = String::new();
19    for _ in 0..5 {
20        let mut lua = Lua::new(Limits::default().memory(mem).string_len(64 << 20).fuel(1 << 40).hash_seed(7), libs);
21        lua.set_print(|_| {});
22        lua.exec(&setup, "=setup").unwrap();
23        let t0 = Instant::now();
24        let clock: remedy_lua::Clock = Arc::new(move || t0.elapsed());
25        lua.set_time_limit(Some((clock, Duration::from_millis(ms))));
26        let f = lua.load(&body, "=body").unwrap();
27        let start = Instant::now();
28        let r = lua.call(f.value(), &[]);
29        let el = start.elapsed();
30        worst = worst.max(el.saturating_sub(Duration::from_millis(ms)));
31        last = format!("{:?}", r.err());
32    }
33    println!("worst overrun {:?} past {ms} ms: {}", worst, &last[..last.len().min(120)]);
34}
Source

pub fn contains(self, other: Libs) -> bool

Whether other is all included.

Trait Implementations§

Source§

impl BitOr for Libs

Source§

type Output = Libs

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Libs) -> Libs

Performs the | operation. Read more
Source§

impl Clone for Libs

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Libs

Source§

impl Debug for Libs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Libs

Source§

impl Hash for Libs

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Libs

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Libs

Source§

impl Sub for Libs

Source§

type Output = Libs

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Libs) -> Libs

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Forget for Libs

§

impl Freeze for Libs

§

impl Move for Libs

§

impl RefUnwindSafe for Libs

§

impl Send for Libs

§

impl Sync for Libs

§

impl Unpin for Libs

§

impl UnsafeUnpin for Libs

§

impl UnwindSafe for Libs

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.