rwf_ruby/
lib.rs

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
use libc::uintptr_t;
use once_cell::sync::OnceCell;
use std::ffi::{c_char, c_int, CStr, CString};
use std::fs::canonicalize;
use std::mem::MaybeUninit;
use std::path::Path;

use std::collections::HashMap;
use tracing::info;

// Make sure the Ruby VM is initialized only once.
static RUBY_INIT: OnceCell<Ruby> = OnceCell::new();

#[repr(C)]
#[derive(Debug, Clone)]
pub struct RackResponse {
    pub value: uintptr_t,
    pub code: c_int,
    pub num_headers: c_int,
    pub headers: *mut KeyValue,
    pub body: *mut c_char,
    pub is_file: c_int,
}

#[repr(C)]
#[derive(Debug)]
pub struct KeyValue {
    key: *const c_char,
    value: *const c_char,
}

#[repr(C)]
#[derive(Debug)]
pub struct RackRequest {
    env: *const KeyValue,
    length: c_int,
    body: *const c_char,
}

impl RackRequest {
    pub fn send(env: HashMap<String, String>, body: &[u8]) -> Result<RackResponse, Error> {
        // let mut c_strings = vec![];
        let mut keys = vec![];

        let (mut k, mut v) = (vec![], vec![]);

        for (key, value) in &env {
            let key = CString::new(key.as_str()).unwrap();
            let value = CString::new(value.as_str()).unwrap();
            k.push(key);
            v.push(value);

            let env_key = KeyValue {
                key: k.last().unwrap().as_ptr(),
                value: v.last().unwrap().as_ptr(),
            };

            keys.push(env_key);
        }

        let body = CString::new(body).unwrap();

        let req = RackRequest {
            length: keys.len() as c_int,
            env: keys.as_ptr(),
            body: body.as_ptr(),
        };

        // Hardcoded to Rails, but can be any other Rack app.
        let app_name = CString::new("Rails.application").unwrap();

        let mut response: RackResponse = unsafe { MaybeUninit::zeroed().assume_init() };

        let result = unsafe { rwf_app_call(req, app_name.as_ptr(), &mut response) };

        if result != 0 {
            return Err(Error::App);
        } else {
            Ok(response)
        }
    }
}

/// RackResponse with values allocated in Rust memory space.
#[derive(Debug)]
pub struct RackResponseOwned {
    code: u16,
    headers: HashMap<String, String>,
    body: Vec<u8>,
    is_file: bool,
}

impl RackResponseOwned {
    /// Request body.
    pub fn body(&self) -> &[u8] {
        &self.body
    }

    /// Request HTTP code.
    pub fn code(&self) -> u16 {
        self.code
    }

    /// Is the request a file?
    pub fn is_file(&self) -> bool {
        self.is_file
    }

    /// Request headers.
    pub fn headers(&self) -> &HashMap<String, String> {
        &self.headers
    }
}

impl From<RackResponse> for RackResponseOwned {
    /// Move all data out of C into Rust-owned memory.
    /// This also drops the reference to the Rack response array,
    /// allowing it to be garbage collected.
    fn from(response: RackResponse) -> RackResponseOwned {
        let code = response.code as u16;

        let mut headers = HashMap::new();

        for n in 0..response.num_headers {
            let env_key = unsafe { response.headers.offset(n as isize) };
            let name = unsafe { CStr::from_ptr((*env_key).key) };
            let value = unsafe { CStr::from_ptr((*env_key).value) };

            // Headers should be valid UTF-8.
            headers.insert(
                name.to_string_lossy().to_string(),
                value.to_string_lossy().to_string(),
            );
        }

        // Body can be anything.
        let body = unsafe { CStr::from_ptr(response.body) };
        let body = Vec::from(body.to_bytes());

        RackResponseOwned {
            code,
            headers,
            body,
            is_file: response.is_file == 1,
        }
    }
}

impl RackResponse {
    /// Parse the Rack response from a Ruby value.
    pub fn new(value: &Value) -> Self {
        unsafe { rwf_rack_response_new(value.raw_ptr()) }
    }
}

impl Drop for RackResponse {
    fn drop(&mut self) {
        unsafe { rwf_rack_response_drop(self) }
    }
}

#[link(name = "ruby")]
extern "C" {
    fn ruby_cleanup(code: c_int) -> c_int;
    fn rb_errinfo() -> uintptr_t;

    // Execute some Ruby code.
    fn rb_eval_string_protect(code: *const c_char, state: *mut c_int) -> uintptr_t;
    fn rb_obj_as_string(value: uintptr_t) -> uintptr_t;

    fn rb_gc_disable() -> c_int;
    fn rb_gc_enable() -> c_int;
}

#[link(name = "rwf_ruby")]
extern "C" {
    /// Get the type of the object.
    fn rwf_rb_type(value: uintptr_t) -> c_int;

    /// Get the CStr value. Careful with this one,
    /// if the object isn't a string, this will segfault.
    fn rwf_value_cstr(value: uintptr_t) -> *mut c_char;

    /// Clear error state after handling an exception.
    fn rwf_clear_error_state();

    /// Convert the Rack response to a struct we can work with.
    /// The Rack response is an array of three elements:
    /// - HTTP code (int)
    /// - headers (Hash)
    /// - body (String)
    fn rwf_rack_response_new(value: uintptr_t) -> RackResponse;

    /// Deallocate memory allocated for converting the Rack response
    /// from Ruby to Rust.
    fn rwf_rack_response_drop(response: &RackResponse);

    /// Load an app into the VM.
    fn rwf_load_app(path: *const c_char) -> c_int;

    /// Initialize Ruby correctly.
    fn rwf_init_ruby();

    fn rwf_app_call(
        request: RackRequest,
        app_name: *const c_char,
        response: *mut RackResponse,
    ) -> c_int;
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Ruby VM did not start")]
    VmInit,

    #[error("{err}")]
    Eval { err: String },

    #[error("Ruby app failed to load")]
    App,
}

#[derive(Debug)]
pub struct Value {
    ptr: uintptr_t,
}

#[derive(Debug, PartialEq)]
#[repr(C)]
pub enum Type {
    None = 0x00,

    Object = 0x01,
    Class = 0x02,
    Module = 0x03,
    Float = 0x04,
    RString = 0x05,
    Regexp = 0x06,
    Array = 0x07,
    Hash = 0x08,
    Struct = 0x09,
    Bignum = 0x0a,
    File = 0x0b,
    Data = 0x0c,
    Match = 0x0d,
    Complex = 0x0e,
    Rational = 0x0f,

    Nil = 0x11,
    True = 0x12,
    False = 0x13,
    Symbol = 0x14,
    Fixnum = 0x15,
    Undef = 0x16,

    IMemo = 0x1a,
    Node = 0x1b,
    IClass = 0x1c,
    Zombie = 0x1d,

    Mask = 0x1f,
}

impl Value {
    pub fn to_string(&self) -> String {
        if self.ty() == Type::RString {
            unsafe {
                let cstr = rwf_value_cstr(self.ptr);
                CStr::from_ptr(cstr).to_string_lossy().to_string()
            }
        } else {
            String::new()
        }
    }

    pub fn ty(&self) -> Type {
        let ty = unsafe { rwf_rb_type(self.ptr) };
        match ty {
            0x05 => Type::RString,
            _ => Type::Nil,
        }
    }

    pub fn raw_ptr(&self) -> uintptr_t {
        self.ptr
    }
}

impl From<uintptr_t> for Value {
    fn from(ptr: uintptr_t) -> Value {
        Value { ptr }
    }
}

pub struct Ruby;

impl Ruby {
    pub fn init() -> Result<(), Error> {
        RUBY_INIT.get_or_try_init(move || Ruby::new())?;

        Ok(())
    }

    fn new() -> Result<Self, Error> {
        unsafe {
            rwf_init_ruby();
            Ok(Ruby {})
        }
    }

    /// Preload the Rack app into memory. Run this before trying to run anything else.
    pub fn load_app(path: impl AsRef<Path> + Copy) -> Result<(), Error> {
        Self::init()?;
        let path = path.as_ref();

        let version = Self::eval("RUBY_VERSION").unwrap().to_string();
        info!("Using {}", version);

        if path.exists() {
            // We use `require`, which only works with abslute paths.
            let absolute = canonicalize(path).unwrap();
            let s = absolute.display().to_string();
            let cs = CString::new(s).unwrap();

            unsafe {
                if rwf_load_app(cs.as_ptr()) != 0 {
                    return Err(Error::App);
                }
            }
        }

        Ok(())
    }

    /// Run some Ruby code. If an exception is thrown, return the error.
    pub fn eval(code: &str) -> Result<Value, Error> {
        Self::init()?;

        unsafe {
            let mut state: c_int = 0;
            let c_string = CString::new(code).unwrap();
            let value = rb_eval_string_protect(c_string.as_ptr(), &mut state);

            if state != 0 {
                let err = rb_errinfo();
                let err = Value::from(rb_obj_as_string(err)).to_string();
                rwf_clear_error_state();

                Err(Error::Eval { err })
            } else {
                Ok(Value { ptr: value })
            }
        }
    }

    pub fn gc_disable() {
        unsafe {
            rb_gc_disable();
        }
    }

    pub fn gc_enable() {
        unsafe {
            rb_gc_enable();
        }
    }
}

impl Drop for Ruby {
    fn drop(&mut self) {
        unsafe {
            ruby_cleanup(0);
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_rack_response() {
        let response = Ruby::eval(r#"[200, {"hello": "world", "the year is 2024": "linux desktop is coming"}, ["apples and oranges"]]"#).unwrap();
        let response = RackResponse::new(&response);

        assert_eq!(response.code, 200);
        assert_eq!(response.num_headers, 2);

        let owned = RackResponseOwned::from(response);
        assert_eq!(
            owned.headers.get("the year is 2024"),
            Some(&String::from("linux desktop is coming"))
        );
        assert_eq!(
            String::from_utf8_lossy(&owned.body),
            "apples and oranges".to_string()
        );
    }

    #[test]
    fn test_load_rails() {
        Ruby::load_app(&Path::new("tests/todo/config/environment.rb")).unwrap();
        let response = Ruby::eval("Rails.application.call({})").unwrap();
        let response = RackResponse::new(&response);
        let owned = RackResponseOwned::from(response);
        assert_eq!(owned.code, 403);
    }
}