meebis 0.15.0

A fast, disposable, in-memory Redis-compatible server for ephemeral dev work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Redis-compatible Lua scripting: `EVAL` / `EVALSHA` / `SCRIPT`, plus the
//! sandboxed `redis`, `cjson`, `cmsgpack`, and `bit` libraries scripts expect.
//!
//! Scripts run on an embedded Lua 5.1 (Redis' scripting version). The keyspace
//! lock is already held by [`super::execute`] when we get here, so a whole
//! script executes atomically and each `redis.call` re-enters
//! [`super::execute_locked`] against that same locked keyspace — exactly how
//! Redis' single thread runs a script start to finish without interleaving.

mod cjson;
mod cmsgpack;
mod luabit;

use super::{parse_int, upper, wrong_args};
use crate::db::Keyspace;
use crate::resp::{format_double, Frame};
use crate::server::{ConnState, Shared};
use crate::sha1::sha1_hex;
use bytes::Bytes;
use mlua::{Function, Lua, LuaOptions, StdLib, Table, Value, Variadic};
use std::cell::RefCell;

/// Where an `EVAL`-family command gets its script body.
pub(crate) enum Source {
    /// `EVAL`: the body is inline (arg 1) and is cached under its SHA.
    Body,
    /// `EVALSHA`: arg 1 is a SHA that must already be in the cache.
    Sha,
}

/// `redis.call` is a thin Lua wrapper over `redis.pcall` that turns an error
/// reply table into a raised Lua error, matching Redis' semantics (a raised
/// `{err=...}` is catchable by `pcall` as a table).
const REDIS_CALL_WRAPPER: &str = r#"
local pcall_fn = ...
return function(...)
  local reply = pcall_fn(...)
  if type(reply) == 'table' and reply.err ~= nil then
    return error(reply)
  end
  return reply
end
"#;

/// The `SCRIPT` command family: `LOAD`, `EXISTS`, `FLUSH`.
pub(crate) fn script(shared: &Shared, args: &[Bytes]) -> Frame {
    if args.len() < 2 {
        return wrong_args("script");
    }
    match upper(&args[1]).as_str() {
        "LOAD" => {
            if args.len() != 3 {
                return unknown_subcommand("load");
            }
            let body = args[2].clone();
            if let Err(e) = compile_check(&body) {
                return Frame::err(format!("Error compiling script (new function): {e}"));
            }
            let sha = sha1_hex(&body);
            shared.scripts.lock().unwrap().insert(sha.clone(), body);
            Frame::Bulk(Bytes::from(sha))
        }
        "EXISTS" => {
            let cache = shared.scripts.lock().unwrap();
            Frame::Array(
                args[2..]
                    .iter()
                    .map(|a| {
                        let sha = String::from_utf8_lossy(a).to_ascii_lowercase();
                        Frame::Integer(cache.contains_key(&sha) as i64)
                    })
                    .collect(),
            )
        }
        "FLUSH" => {
            // An optional ASYNC | SYNC modifier is accepted and ignored.
            shared.scripts.lock().unwrap().clear();
            Frame::ok()
        }
        other => unknown_subcommand(&other.to_lowercase()),
    }
}

/// `EVAL` / `EVALSHA` (and their `_RO` aliases).
pub(crate) fn eval(
    ks: &mut Keyspace,
    shared: &Shared,
    conn: &mut ConnState,
    args: &[Bytes],
    source: Source,
) -> Frame {
    // <EVAL script | EVALSHA sha> numkeys [key ...] [arg ...]
    if args.len() < 3 {
        return wrong_args(match source {
            Source::Body => "eval",
            Source::Sha => "evalsha",
        });
    }
    let numkeys = match parse_int(&args[2]) {
        Ok(n) => n,
        Err(e) => return e,
    };
    if numkeys < 0 {
        return Frame::err("Number of keys can't be negative");
    }
    let numkeys = numkeys as usize;
    let rest = &args[3..];
    if numkeys > rest.len() {
        return Frame::err("Number of keys can't be greater than number of args");
    }
    let keys = rest[..numkeys].to_vec();
    let argv = rest[numkeys..].to_vec();

    let body: Bytes = match source {
        Source::Sha => {
            let sha = String::from_utf8_lossy(&args[1]).to_ascii_lowercase();
            match shared.scripts.lock().unwrap().get(&sha) {
                Some(b) => b.clone(),
                None => {
                    return Frame::Error("NOSCRIPT No matching script. Please use EVAL.".into())
                }
            }
        }
        Source::Body => {
            let body = args[1].clone();
            let sha = sha1_hex(&body);
            shared.scripts.lock().unwrap().insert(sha, body.clone());
            body
        }
    };

    run_script(ks, shared, conn, &body, keys, argv)
}

/// The mutable state a script's `redis.call`/`pcall` needs, shared through a
/// `RefCell` so a single scoped Lua closure can reach it across calls.
struct Ctx<'a> {
    ks: &'a mut Keyspace,
    conn: &'a mut ConnState,
}

fn run_script(
    ks: &mut Keyspace,
    shared: &Shared,
    conn: &mut ConnState,
    body: &[u8],
    keys: Vec<Bytes>,
    argv: Vec<Bytes>,
) -> Frame {
    let lua = match new_sandbox() {
        Ok(l) => l,
        Err(e) => return Frame::err(format!("Error creating Lua environment: {e}")),
    };
    if let Err(e) = set_keys_argv(&lua, &keys, &argv) {
        return Frame::err(format!("Error preparing script: {e}"));
    }

    // A script may `redis.call('select', n)`. Redis scopes that to the script
    // and hands the caller back the database it started on, so remember it.
    let caller_db = conn.db_index;
    let ctx = RefCell::new(Ctx { ks, conn });

    let result: mlua::Result<Frame> = lua.scope(|scope| {
        let pcall = scope.create_function_mut(|lua, cmd: Variadic<Value>| {
            let mut guard = ctx.borrow_mut();
            let c: &mut Ctx = &mut guard;
            redis_pcall(lua, shared, &mut *c.ks, &mut *c.conn, cmd)
        })?;

        let redis: Table = lua.globals().get("redis")?;
        let call: Function = lua
            .load(REDIS_CALL_WRAPPER)
            .set_name("@redis.call")
            .call(pcall.clone())?;
        redis.set("pcall", pcall)?;
        redis.set("call", call)?;

        let user_fn = lua.load(body).set_name("@user_script").into_function()?;
        // Run under pcall so we can recover the error value (table or string).
        let runner: Function = lua
            .load("local f = ...\nlocal ok, res = pcall(f)\nreturn ok, res")
            .set_name("@__run")
            .into_function()?;
        let (ok, value): (bool, Value) = runner.call(user_fn)?;
        Ok(if ok {
            lua_to_frame(value)
        } else {
            error_value_to_frame(value)
        })
    });

    // Undo any `select` the script performed, however it exited.
    let conn = ctx.into_inner().conn;
    if conn.db_index != caller_db {
        conn.db_index = caller_db;
        super::clientcmd::sync_registry_db(shared, conn);
    }

    match result {
        Ok(frame) => frame,
        // The user script is compiled before it runs, so a syntax error lands
        // here; runtime errors are caught by the pcall trampoline above.
        Err(e) => Frame::err(format!("Error compiling script (new function): {e}")),
    }
}

/// A fresh sandbox: base + table/string/math (Redis' subset — no `os`/`io`/
/// `debug`; on Lua 5.1 `coroutine` comes with the base library), with `redis`,
/// `cjson`, `cmsgpack`, and `bit` registered.
/// `redis.call`/`pcall` are added per-run inside the scope, since they borrow
/// the keyspace.
fn new_sandbox() -> mlua::Result<Lua> {
    let lua = Lua::new_with(
        StdLib::TABLE | StdLib::STRING | StdLib::MATH,
        LuaOptions::default(),
    )?;
    let redis = lua.create_table()?;
    register_redis_helpers(&lua, &redis)?;
    lua.globals().set("redis", redis)?;
    cjson::register(&lua)?;
    cmsgpack::register(&lua)?;
    luabit::register(&lua)?;
    Ok(lua)
}

fn compile_check(body: &[u8]) -> Result<(), String> {
    let lua = Lua::new_with(
        StdLib::TABLE | StdLib::STRING | StdLib::MATH,
        LuaOptions::default(),
    )
    .map_err(|e| e.to_string())?;
    lua.load(body)
        .set_name("@user_script")
        .into_function()
        .map(|_| ())
        .map_err(|e| e.to_string())
}

fn register_redis_helpers(lua: &Lua, redis: &Table) -> mlua::Result<()> {
    redis.set(
        "error_reply",
        lua.create_function(|lua, msg: mlua::String| {
            let t = lua.create_table()?;
            t.set("err", msg)?;
            Ok(t)
        })?,
    )?;
    redis.set(
        "status_reply",
        lua.create_function(|lua, msg: mlua::String| {
            let t = lua.create_table()?;
            t.set("ok", msg)?;
            Ok(t)
        })?,
    )?;
    redis.set(
        "sha1hex",
        lua.create_function(|_, s: mlua::String| Ok(sha1_hex(&s.as_bytes())))?,
    )?;
    // Logging and replication controls are no-ops in a single-node dev server.
    redis.set("log", lua.create_function(|_, _: Variadic<Value>| Ok(()))?)?;
    redis.set("replicate_commands", lua.create_function(|_, ()| Ok(true))?)?;
    redis.set("set_repl", lua.create_function(|_, _: Value| Ok(()))?)?;
    redis.set("setresp", lua.create_function(|_, _: Value| Ok(()))?)?;
    redis.set(
        "breakpoint",
        lua.create_function(|_, _: Variadic<Value>| Ok(false))?,
    )?;
    redis.set(
        "debug",
        lua.create_function(|_, _: Variadic<Value>| Ok(()))?,
    )?;

    for (name, level) in [
        ("LOG_DEBUG", 0),
        ("LOG_VERBOSE", 1),
        ("LOG_NOTICE", 2),
        ("LOG_WARNING", 3),
    ] {
        redis.set(name, level)?;
    }
    for (name, val) in [
        ("REPL_NONE", 0),
        ("REPL_AOF", 1),
        ("REPL_SLAVE", 2),
        ("REPL_REPLICA", 2),
        ("REPL_ALL", 3),
    ] {
        redis.set(name, val)?;
    }
    Ok(())
}

fn set_keys_argv(lua: &Lua, keys: &[Bytes], argv: &[Bytes]) -> mlua::Result<()> {
    let kt = lua.create_table()?;
    for (i, k) in keys.iter().enumerate() {
        kt.set(i + 1, lua.create_string(&k[..])?)?;
    }
    let at = lua.create_table()?;
    for (i, a) in argv.iter().enumerate() {
        at.set(i + 1, lua.create_string(&a[..])?)?;
    }
    lua.globals().set("KEYS", kt)?;
    lua.globals().set("ARGV", at)?;
    Ok(())
}

/// Execute one `redis.call`/`pcall`. Command errors come back as a `{err=...}`
/// table (never a raised Lua error); `redis.call`'s Lua wrapper is what turns
/// that into a raise. Malformed calls (non-string args, blocked commands) do
/// raise, as Redis does.
fn redis_pcall(
    lua: &Lua,
    shared: &Shared,
    ks: &mut Keyspace,
    conn: &mut ConnState,
    cmd: Variadic<Value>,
) -> mlua::Result<Value> {
    if cmd.is_empty() {
        return Err(mlua::Error::RuntimeError(
            "Please specify at least one argument for this redis lib call".into(),
        ));
    }
    let mut argv: Vec<Bytes> = Vec::with_capacity(cmd.len());
    for v in cmd.iter() {
        match value_to_arg(v) {
            Some(b) => argv.push(b),
            None => {
                return Err(mlua::Error::RuntimeError(
                    "Lua redis lib command arguments must be strings or integers".into(),
                ))
            }
        }
    }
    let name = String::from_utf8_lossy(&argv[0]).to_ascii_uppercase();
    if is_blocked(&name) {
        return Err(mlua::Error::RuntimeError(
            "This Redis command is not allowed from script".into(),
        ));
    }
    let started = crate::log::script_cmd(shared, conn, &argv);
    let frame = super::execute_locked(ks, shared, conn, &name, &argv);
    crate::log::script_reply(shared, conn, &frame, started);
    frame_to_lua(lua, frame)
}

/// Commands Redis refuses to run from inside a script.
fn is_blocked(name: &str) -> bool {
    matches!(
        name,
        "EVAL"
            | "EVAL_RO"
            | "EVALSHA"
            | "EVALSHA_RO"
            | "SCRIPT"
            | "FUNCTION"
            | "FCALL"
            | "FCALL_RO"
            | "MULTI"
            | "EXEC"
            | "DISCARD"
            | "WATCH"
            | "UNWATCH"
            | "SUBSCRIBE"
            | "UNSUBSCRIBE"
            | "PSUBSCRIBE"
            | "PUNSUBSCRIBE"
            | "SSUBSCRIBE"
            | "SUNSUBSCRIBE"
    )
}

fn value_to_arg(v: &Value) -> Option<Bytes> {
    match v {
        Value::String(s) => Some(Bytes::copy_from_slice(&s.as_bytes())),
        Value::Integer(i) => Some(Bytes::from(i.to_string())),
        Value::Number(n) => Some(Bytes::from(number_to_arg(*n))),
        _ => None,
    }
}

fn number_to_arg(n: f64) -> String {
    if n.is_finite() && n == n.trunc() && n.abs() < 1e15 {
        (n as i64).to_string()
    } else {
        format!("{n}")
    }
}

/// Convert a command reply to a Lua value, applying RESP2 rules (the mode
/// scripts see by default): status → `{ok=}`, error → `{err=}`, nil → false,
/// maps/pairs/sets flatten to arrays, doubles become strings.
fn frame_to_lua(lua: &Lua, frame: Frame) -> mlua::Result<Value> {
    Ok(match frame {
        Frame::Integer(n) => Value::Integer(n),
        Frame::Simple(s) => {
            let t = lua.create_table()?;
            t.set("ok", s)?;
            Value::Table(t)
        }
        Frame::Error(s) => {
            let t = lua.create_table()?;
            t.set("err", s)?;
            Value::Table(t)
        }
        Frame::Bulk(b) => Value::String(lua.create_string(&b[..])?),
        Frame::Null | Frame::NullArray => Value::Boolean(false),
        Frame::Double(d) => Value::String(lua.create_string(format_double(d))?),
        Frame::Array(items) | Frame::Set(items) | Frame::Push(items) => {
            let t = lua.create_table()?;
            for (i, item) in items.into_iter().enumerate() {
                t.set(i + 1, frame_to_lua(lua, item)?)?;
            }
            Value::Table(t)
        }
        Frame::Map(pairs) | Frame::Pairs(pairs) => {
            let t = lua.create_table()?;
            let mut idx = 1;
            for (k, v) in pairs {
                t.set(idx, frame_to_lua(lua, k)?)?;
                idx += 1;
                t.set(idx, frame_to_lua(lua, v)?)?;
                idx += 1;
            }
            Value::Table(t)
        }
        Frame::XReadReply(pairs) => {
            // Scripts see the RESP2 shape: `[[key, entries], ...]`.
            let t = lua.create_table()?;
            for (i, (k, v)) in pairs.into_iter().enumerate() {
                let inner = lua.create_table()?;
                inner.set(1, frame_to_lua(lua, k)?)?;
                inner.set(2, frame_to_lua(lua, v)?)?;
                t.set(i + 1, inner)?;
            }
            Value::Table(t)
        }
    })
}

/// Convert a script's return value to a reply, applying Redis' rules: numbers
/// truncate to integers, `false`/`nil` → nil, tables become arrays up to the
/// first nil, and `{ok=}`/`{err=}` become status/error replies.
fn lua_to_frame(value: Value) -> Frame {
    match value {
        Value::Nil => Frame::Null,
        Value::Boolean(true) => Frame::Integer(1),
        Value::Boolean(false) => Frame::Null,
        Value::Integer(i) => Frame::Integer(i),
        Value::Number(n) => Frame::Integer(n as i64),
        Value::String(s) => Frame::Bulk(Bytes::copy_from_slice(&s.as_bytes())),
        Value::Table(t) => table_to_frame(t),
        _ => Frame::Null,
    }
}

fn table_to_frame(t: Table) -> Frame {
    if let Ok(Value::String(e)) = t.get::<Value>("err") {
        return Frame::Error(bytes_to_string(&e));
    }
    if let Ok(Value::String(s)) = t.get::<Value>("ok") {
        return Frame::Simple(bytes_to_string(&s));
    }
    let mut items = Vec::new();
    let mut i = 1i64;
    loop {
        match t.get::<Value>(i) {
            Ok(Value::Nil) | Err(_) => break,
            Ok(v) => items.push(lua_to_frame(v)),
        }
        i += 1;
    }
    Frame::Array(items)
}

fn error_value_to_frame(value: Value) -> Frame {
    match value {
        Value::Table(t) => match t.get::<Value>("err") {
            Ok(Value::String(e)) => Frame::Error(bytes_to_string(&e)),
            _ => Frame::err("script raised a table without an 'err' field"),
        },
        Value::String(s) => {
            let msg = bytes_to_string(&s);
            // A raised code (e.g. "WRONGTYPE ...") is surfaced verbatim;
            // a bare message gets the generic ERR prefix, as Redis does.
            if has_error_code(&msg) {
                Frame::Error(msg)
            } else {
                Frame::err(msg)
            }
        }
        _ => Frame::err("script raised a non-string error"),
    }
}

fn has_error_code(msg: &str) -> bool {
    let first = msg.split(' ').next().unwrap_or("");
    !first.is_empty() && first.chars().all(|c| c.is_ascii_uppercase())
}

fn bytes_to_string(s: &mlua::String) -> String {
    String::from_utf8_lossy(&s.as_bytes()).into_owned()
}

fn unknown_subcommand(sub: &str) -> Frame {
    Frame::err(format!(
        "Unknown SCRIPT subcommand or wrong number of arguments for '{sub}'"
    ))
}