mini_racer_rs 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/COMPATIBILITY.md +45 -48
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +80 -37
- data/crates/mini_racer_engine/src/context.rs +45 -22
- data/crates/mini_racer_engine/src/control.rs +70 -1
- data/crates/mini_racer_engine/src/error.rs +66 -9
- data/crates/mini_racer_engine/src/lib.rs +3 -2
- data/crates/mini_racer_engine/src/process/platform.rs +360 -2
- data/crates/mini_racer_engine/src/process/watchdog.rs +63 -14
- data/crates/mini_racer_engine/src/runner.rs +274 -50
- data/crates/mini_racer_engine/src/snapshot.rs +259 -41
- data/crates/mini_racer_engine/src/sys.rs +26 -0
- data/crates/mini_racer_sourcemap/src/map.rs +3 -2
- data/crates/mini_racer_sync/src/termination.rs +183 -45
- data/ext/mini_racer_rs/src/context.rs +41 -23
- data/ext/mini_racer_rs/src/decode.rs +10 -2
- data/ext/mini_racer_rs/src/encode.rs +72 -27
- data/ext/mini_racer_rs/src/errors.rs +21 -6
- data/ext/mini_racer_rs/src/gc_root.rs +31 -1
- data/ext/mini_racer_rs/src/heap_limit.rs +42 -0
- data/ext/mini_racer_rs/src/host.rs +1 -1
- data/ext/mini_racer_rs/src/lib.rs +6 -0
- data/ext/mini_racer_rs/src/options.rs +90 -8
- data/ext/mini_racer_rs/src/pool.rs +811 -0
- data/ext/mini_racer_rs/src/rb_raw.rs +186 -1
- data/ext/mini_racer_rs/src/snapshot.rs +6 -10
- data/ext/mini_racer_rs/src/timeout.rs +132 -0
- data/lib/mini_racer/version.rb +1 -1
- data/lib/mini_racer_rs/heap_limit_error.rb +7 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- data/lib/mini_racer_rs.rb +1 -0
- metadata +7 -3
|
@@ -16,6 +16,9 @@ use crate::gc_root::{OnceRoot, Pins, StaticRoot};
|
|
|
16
16
|
use crate::rb_raw::{self, RubyType};
|
|
17
17
|
|
|
18
18
|
const BIGINT_MAX_WORDS: usize = (16 << 20) / 8;
|
|
19
|
+
// mini_racer refuses a value nested deeper than this below the one it sends,
|
|
20
|
+
// counting a call's arguments one level down.
|
|
21
|
+
const MAX_DEPTH: u32 = 512;
|
|
19
22
|
// mini_racer formats these messages into a 64-byte buffer.
|
|
20
23
|
const MESSAGE_MAX_BYTES: usize = 63;
|
|
21
24
|
|
|
@@ -61,12 +64,22 @@ impl From<Error> for EncodeError {
|
|
|
61
64
|
|
|
62
65
|
enum Item {
|
|
63
66
|
// A pinned value, by its index in the holder.
|
|
64
|
-
Value
|
|
67
|
+
Value {
|
|
68
|
+
index: usize,
|
|
69
|
+
depth: u32,
|
|
70
|
+
},
|
|
65
71
|
// A pinned key of a hash.
|
|
66
|
-
Key
|
|
72
|
+
Key {
|
|
73
|
+
index: usize,
|
|
74
|
+
depth: u32,
|
|
75
|
+
},
|
|
67
76
|
// An element read when its turn comes, as serialize1 reads them, so a
|
|
68
77
|
// to_time that changes the array is seen.
|
|
69
|
-
Element {
|
|
78
|
+
Element {
|
|
79
|
+
array: usize,
|
|
80
|
+
index: isize,
|
|
81
|
+
depth: u32,
|
|
82
|
+
},
|
|
70
83
|
EndArray(u32),
|
|
71
84
|
EndObject(u32),
|
|
72
85
|
}
|
|
@@ -93,7 +106,7 @@ impl Hasher for Address {
|
|
|
93
106
|
}
|
|
94
107
|
|
|
95
108
|
// Iterative so a deeply nested argument cannot overflow the Ruby thread's
|
|
96
|
-
// stack
|
|
109
|
+
// stack.
|
|
97
110
|
struct Encoder<'r, 'o> {
|
|
98
111
|
ruby: &'r Ruby,
|
|
99
112
|
// Made for the first value that holds others or can run Ruby code, before
|
|
@@ -122,7 +135,7 @@ pub fn encode(
|
|
|
122
135
|
) -> Result<Vec<u8>, Error> {
|
|
123
136
|
let mut out = Vec::new();
|
|
124
137
|
Encoder::new(ruby, &mut out)
|
|
125
|
-
.top(value)
|
|
138
|
+
.top(value, 0)
|
|
126
139
|
.map_err(|error| error.raise(unsupported))?;
|
|
127
140
|
Ok(out)
|
|
128
141
|
}
|
|
@@ -142,7 +155,7 @@ pub fn call(
|
|
|
142
155
|
encoder.writer.begin_array(count);
|
|
143
156
|
encoder.string(name, Text::Name);
|
|
144
157
|
args.iter()
|
|
145
|
-
.try_for_each(|&arg| encoder.top(arg))
|
|
158
|
+
.try_for_each(|&arg| encoder.top(arg, 1))
|
|
146
159
|
.map_err(|error| error.raise(unsupported))?;
|
|
147
160
|
encoder.writer.end_array(count);
|
|
148
161
|
let (objects, properties) = (encoder.objects, encoder.properties);
|
|
@@ -182,6 +195,13 @@ fn length(len: usize) -> u32 {
|
|
|
182
195
|
u32::try_from(len).unwrap_or(u32::MAX)
|
|
183
196
|
}
|
|
184
197
|
|
|
198
|
+
fn within(depth: u32) -> Result<(), EncodeError> {
|
|
199
|
+
if depth > MAX_DEPTH {
|
|
200
|
+
return Err(EncodeError::unsupported("max depth exceeded"));
|
|
201
|
+
}
|
|
202
|
+
Ok(())
|
|
203
|
+
}
|
|
204
|
+
|
|
185
205
|
// The values whose encoding runs no Ruby code and holds no other value.
|
|
186
206
|
const fn immediate(kind: RubyType) -> bool {
|
|
187
207
|
matches!(
|
|
@@ -225,12 +245,12 @@ impl Encoder<'_, '_> {
|
|
|
225
245
|
}
|
|
226
246
|
|
|
227
247
|
// Writes an immediate at once; anything else waits, pinned, for `run`.
|
|
228
|
-
fn top(&mut self, value: Value) -> Result<(), EncodeError> {
|
|
248
|
+
fn top(&mut self, value: Value, depth: u32) -> Result<(), EncodeError> {
|
|
229
249
|
if self.immediate(value, rb_raw::ruby_type(self.ruby, value)) {
|
|
230
250
|
return Ok(());
|
|
231
251
|
}
|
|
232
|
-
let
|
|
233
|
-
self.work.push(Item::Value
|
|
252
|
+
let index = self.pin(value);
|
|
253
|
+
self.work.push(Item::Value { index, depth });
|
|
234
254
|
self.run()
|
|
235
255
|
}
|
|
236
256
|
|
|
@@ -239,17 +259,22 @@ impl Encoder<'_, '_> {
|
|
|
239
259
|
match item {
|
|
240
260
|
Item::EndArray(length) => self.writer.end_array(length),
|
|
241
261
|
Item::EndObject(count) => self.writer.end_object(count),
|
|
242
|
-
Item::Value
|
|
243
|
-
Item::Key
|
|
262
|
+
Item::Value { index, depth } => self.value(self.pinned(index), depth)?,
|
|
263
|
+
Item::Key { index, depth } => {
|
|
264
|
+
within(depth)?;
|
|
244
265
|
let key = self.pinned(index);
|
|
245
266
|
self.key(key, rb_raw::ruby_type(self.ruby, key));
|
|
246
267
|
}
|
|
247
|
-
Item::Element {
|
|
268
|
+
Item::Element {
|
|
269
|
+
array,
|
|
270
|
+
index,
|
|
271
|
+
depth,
|
|
272
|
+
} => {
|
|
248
273
|
let element = RArray::from_value(self.pinned(array))
|
|
249
274
|
.map(|array| array.entry::<Value>(index))
|
|
250
275
|
.transpose()?
|
|
251
276
|
.unwrap_or_else(|| self.ruby.qnil().as_value());
|
|
252
|
-
self.value(element)?;
|
|
277
|
+
self.value(element, depth)?;
|
|
253
278
|
}
|
|
254
279
|
}
|
|
255
280
|
}
|
|
@@ -293,17 +318,15 @@ impl Encoder<'_, '_> {
|
|
|
293
318
|
}
|
|
294
319
|
}
|
|
295
320
|
|
|
296
|
-
fn value(&mut self, value: Value) -> Result<(), EncodeError> {
|
|
321
|
+
fn value(&mut self, value: Value, depth: u32) -> Result<(), EncodeError> {
|
|
322
|
+
within(depth)?;
|
|
297
323
|
let kind = rb_raw::ruby_type(self.ruby, value);
|
|
298
324
|
if self.immediate(value, kind) {
|
|
299
325
|
return Ok(());
|
|
300
326
|
}
|
|
301
327
|
match kind {
|
|
302
|
-
RubyType::Array =>
|
|
303
|
-
|
|
304
|
-
Ok(())
|
|
305
|
-
}
|
|
306
|
-
RubyType::Hash => self.hash(value),
|
|
328
|
+
RubyType::Array => self.array(value, depth),
|
|
329
|
+
RubyType::Hash => self.hash(value, depth),
|
|
307
330
|
RubyType::Data => self.data(value),
|
|
308
331
|
RubyType::Bignum => self.bigint(value),
|
|
309
332
|
RubyType::Object => self.object(value),
|
|
@@ -322,13 +345,16 @@ impl Encoder<'_, '_> {
|
|
|
322
345
|
|
|
323
346
|
// The leading immediates are written at once: reading one runs no code
|
|
324
347
|
// that could change the array before its turn. The rest wait their turns.
|
|
325
|
-
fn array(&mut self, value: Value) {
|
|
348
|
+
fn array(&mut self, value: Value, depth: u32) -> Result<(), EncodeError> {
|
|
326
349
|
if let Some(&id) = self.seen.get(&rb_raw::identity(value)) {
|
|
327
350
|
self.writer.object_ref(id);
|
|
328
|
-
return;
|
|
351
|
+
return Ok(());
|
|
329
352
|
}
|
|
330
353
|
let array = RArray::from_value(value);
|
|
331
354
|
let len = array.map_or(0, RArray::len);
|
|
355
|
+
if len > 0 {
|
|
356
|
+
within(depth.saturating_add(1))?;
|
|
357
|
+
}
|
|
332
358
|
let count = length(len);
|
|
333
359
|
let id = self.writer.begin_array(count);
|
|
334
360
|
let pinned = self.pin(value);
|
|
@@ -348,21 +374,25 @@ impl Encoder<'_, '_> {
|
|
|
348
374
|
}
|
|
349
375
|
if next >= end {
|
|
350
376
|
self.writer.end_array(count);
|
|
351
|
-
return;
|
|
377
|
+
return Ok(());
|
|
352
378
|
}
|
|
353
379
|
self.work.push(Item::EndArray(count));
|
|
354
380
|
for index in (next..end).rev() {
|
|
355
381
|
self.work.push(Item::Element {
|
|
356
382
|
array: pinned,
|
|
357
383
|
index,
|
|
384
|
+
depth: depth.saturating_add(1),
|
|
358
385
|
});
|
|
359
386
|
}
|
|
387
|
+
Ok(())
|
|
360
388
|
}
|
|
361
389
|
|
|
362
390
|
// mini_racer collects every pair before it writes one. Until a value that
|
|
363
391
|
// is not an immediate, each pair is written as it is collected, which no
|
|
364
|
-
// Ruby code can tell apart; from there on, the pairs wait their turns.
|
|
365
|
-
|
|
392
|
+
// Ruby code can tell apart; from there on, the pairs wait their turns. It
|
|
393
|
+
// refuses a key that is not a String, a Symbol or an Integer before a key
|
|
394
|
+
// too deep.
|
|
395
|
+
fn hash(&mut self, value: Value, depth: u32) -> Result<(), EncodeError> {
|
|
366
396
|
if let Some(&id) = self.seen.get(&rb_raw::identity(value)) {
|
|
367
397
|
self.writer.object_ref(id);
|
|
368
398
|
return Ok(());
|
|
@@ -376,6 +406,8 @@ impl Encoder<'_, '_> {
|
|
|
376
406
|
let mut pairs = Vec::new();
|
|
377
407
|
let mut keys_fit = true;
|
|
378
408
|
let mut written = 0u32;
|
|
409
|
+
let inner = depth.saturating_add(1);
|
|
410
|
+
let too_deep = within(inner).is_err();
|
|
379
411
|
hash.foreach(|key: Value, item: Value| {
|
|
380
412
|
let key_kind = rb_raw::ruby_type(self.ruby, key);
|
|
381
413
|
keys_fit &= matches!(
|
|
@@ -383,7 +415,11 @@ impl Encoder<'_, '_> {
|
|
|
383
415
|
RubyType::Fixnum | RubyType::String | RubyType::Symbol
|
|
384
416
|
);
|
|
385
417
|
let item_kind = rb_raw::ruby_type(self.ruby, item);
|
|
386
|
-
if
|
|
418
|
+
if too_deep {
|
|
419
|
+
if !keys_fit {
|
|
420
|
+
return Ok(ForEach::Stop);
|
|
421
|
+
}
|
|
422
|
+
} else if keys_fit && pairs.is_empty() && immediate(item_kind) {
|
|
387
423
|
self.key(key, key_kind);
|
|
388
424
|
self.immediate(item, item_kind);
|
|
389
425
|
written = written.saturating_add(1);
|
|
@@ -395,6 +431,9 @@ impl Encoder<'_, '_> {
|
|
|
395
431
|
if !keys_fit {
|
|
396
432
|
return Err(EncodeError::unsupported("TODO serialize as Map"));
|
|
397
433
|
}
|
|
434
|
+
if too_deep && !hash.is_empty() {
|
|
435
|
+
within(inner)?;
|
|
436
|
+
}
|
|
398
437
|
let count = written.saturating_add(length(pairs.len()));
|
|
399
438
|
if pairs.is_empty() {
|
|
400
439
|
self.writer.end_object(count);
|
|
@@ -402,8 +441,14 @@ impl Encoder<'_, '_> {
|
|
|
402
441
|
}
|
|
403
442
|
self.work.push(Item::EndObject(count));
|
|
404
443
|
for (key, item) in pairs.into_iter().rev() {
|
|
405
|
-
self.work.push(Item::Value
|
|
406
|
-
|
|
444
|
+
self.work.push(Item::Value {
|
|
445
|
+
index: item,
|
|
446
|
+
depth: inner,
|
|
447
|
+
});
|
|
448
|
+
self.work.push(Item::Key {
|
|
449
|
+
index: key,
|
|
450
|
+
depth: inner,
|
|
451
|
+
});
|
|
407
452
|
}
|
|
408
453
|
Ok(())
|
|
409
454
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
use magnus::value::ReprValue;
|
|
2
|
-
use magnus::{
|
|
3
|
-
|
|
2
|
+
use magnus::{
|
|
3
|
+
Class, Error, Exception, ExceptionClass, Module, Object, RModule, RObject, Ruby, Value,
|
|
4
|
+
};
|
|
5
|
+
use mini_racer_engine::{EngineError, FlagError, HeapLimitError, JsErrorKind, TerminationCause};
|
|
4
6
|
|
|
5
7
|
use crate::export;
|
|
6
8
|
use crate::gc_root::StaticRoot;
|
|
@@ -58,6 +60,7 @@ roots! {
|
|
|
58
60
|
PLATFORM_INITIALIZED = "MiniRacer"::"PlatformAlreadyInitialized";
|
|
59
61
|
INTERNAL = "MiniRacer"::"InternalError";
|
|
60
62
|
SNAPSHOT = "MiniRacer"::"SnapshotError";
|
|
63
|
+
HEAP_LIMIT = "MiniRacerRs"::"HeapLimitError";
|
|
61
64
|
}
|
|
62
65
|
|
|
63
66
|
pub fn engine(ruby: &Ruby, error: EngineError) -> Error {
|
|
@@ -72,9 +75,8 @@ pub fn engine(ruby: &Ruby, error: EngineError) -> Error {
|
|
|
72
75
|
| TerminationCause::Timeout
|
|
73
76
|
| TerminationCause::Cancelled,
|
|
74
77
|
) => &TERMINATED,
|
|
75
|
-
JsErrorKind::Terminated(
|
|
76
|
-
|
|
77
|
-
) => &OUT_OF_MEMORY,
|
|
78
|
+
JsErrorKind::Terminated(TerminationCause::MaxMemory) => &OUT_OF_MEMORY,
|
|
79
|
+
JsErrorKind::Terminated(TerminationCause::HeapExhausted) => &HEAP_LIMIT,
|
|
78
80
|
// mini_racer raises its disposed error when a callback's
|
|
79
81
|
// exception escapes but none is kept for it.
|
|
80
82
|
JsErrorKind::HostException => {
|
|
@@ -91,12 +93,15 @@ pub fn engine(ruby: &Ruby, error: EngineError) -> Error {
|
|
|
91
93
|
EngineError::Internal(message) => raised(ruby, INTERNAL.get(ruby), &message),
|
|
92
94
|
EngineError::Snapshot(message) => snapshot(ruby, &message),
|
|
93
95
|
EngineError::InvalidSnapshot(_) => raised(ruby, SNAPSHOT.get(ruby), error.to_string()),
|
|
96
|
+
EngineError::HeapLimitOverridden => {
|
|
97
|
+
Error::new(ruby.exception_arg_error(), error.to_string())
|
|
98
|
+
}
|
|
94
99
|
}
|
|
95
100
|
}
|
|
96
101
|
|
|
97
102
|
pub fn flag(ruby: &Ruby, error: FlagError) -> Error {
|
|
98
103
|
match error {
|
|
99
|
-
FlagError::Nul | FlagError::TooLong => {
|
|
104
|
+
FlagError::Nul | FlagError::TooLong | FlagError::OverridesHeapLimit(_) => {
|
|
100
105
|
raised(ruby, ruby.exception_arg_error(), error.to_string())
|
|
101
106
|
}
|
|
102
107
|
FlagError::AlreadyInitialized => {
|
|
@@ -105,12 +110,22 @@ pub fn flag(ruby: &Ruby, error: FlagError) -> Error {
|
|
|
105
110
|
}
|
|
106
111
|
}
|
|
107
112
|
|
|
113
|
+
pub fn heap_limit(ruby: &Ruby, error: HeapLimitError) -> Error {
|
|
114
|
+
Error::new(ruby.exception_arg_error(), error.to_string())
|
|
115
|
+
}
|
|
116
|
+
|
|
108
117
|
// A JavaScript error object decoded into Ruby keeps its cause here, as in
|
|
109
118
|
// mini_racer, rather than in the cause Ruby gives a raised exception.
|
|
110
119
|
fn cause(_ruby: &Ruby, rb_self: RObject) -> Result<Value, Error> {
|
|
111
120
|
rb_self.ivar_get("@cause")
|
|
112
121
|
}
|
|
113
122
|
|
|
123
|
+
/// Whether a context that raised `exception` is spent: out of memory, or
|
|
124
|
+
/// disposed.
|
|
125
|
+
pub fn spends_context(ruby: &Ruby, exception: Exception) -> bool {
|
|
126
|
+
exception.is_kind_of(OUT_OF_MEMORY.get(ruby)) || exception.is_kind_of(DISPOSED.get(ruby))
|
|
127
|
+
}
|
|
128
|
+
|
|
114
129
|
pub fn script_error(ruby: &Ruby) -> ExceptionClass {
|
|
115
130
|
SCRIPT.get(ruby)
|
|
116
131
|
}
|
|
@@ -5,7 +5,11 @@ use std::sync::OnceLock;
|
|
|
5
5
|
use magnus::gc::Marker;
|
|
6
6
|
use magnus::typed_data::Obj;
|
|
7
7
|
use magnus::value::{Lazy, Opaque, ReprValue};
|
|
8
|
-
use magnus::{
|
|
8
|
+
use magnus::{
|
|
9
|
+
Class, DataTypeFunctions, Error, Module, Object, RModule, Ruby, TryConvert, TypedData, Value,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
use crate::rb_raw;
|
|
9
13
|
|
|
10
14
|
pub struct StaticRoot<T: ReprValue>(Lazy<T>);
|
|
11
15
|
|
|
@@ -173,6 +177,32 @@ impl Spill {
|
|
|
173
177
|
}
|
|
174
178
|
}
|
|
175
179
|
|
|
180
|
+
// A hidden instance variable, which Ruby code cannot read or change.
|
|
181
|
+
const SPAWNED: &str = "spawned_with";
|
|
182
|
+
|
|
183
|
+
/// Starts a Ruby thread that runs `run` with `value`, which the thread object
|
|
184
|
+
/// keeps alive until the thread ends. `run` must handle whatever its Ruby
|
|
185
|
+
/// calls raise.
|
|
186
|
+
pub fn spawn<T: ReprValue + TryConvert>(
|
|
187
|
+
ruby: &Ruby,
|
|
188
|
+
value: T,
|
|
189
|
+
run: impl FnOnce(&Ruby, T) + Send + 'static,
|
|
190
|
+
) -> Result<(), Error> {
|
|
191
|
+
// The thread reads `value` back from its own instance variable, which
|
|
192
|
+
// compaction updates if the value moves before the thread first runs; a
|
|
193
|
+
// copy held here would go stale. The thread waits for the GVL this one
|
|
194
|
+
// holds until the variable is set.
|
|
195
|
+
let thread = rb_raw::thread_create(
|
|
196
|
+
ruby,
|
|
197
|
+
Box::new(move |ruby: &Ruby| {
|
|
198
|
+
if let Ok(value) = ruby.thread_current().ivar_get::<_, T>(SPAWNED) {
|
|
199
|
+
run(ruby, value);
|
|
200
|
+
}
|
|
201
|
+
}),
|
|
202
|
+
)?;
|
|
203
|
+
thread.ivar_set(SPAWNED, value.as_value())
|
|
204
|
+
}
|
|
205
|
+
|
|
176
206
|
// Each holder's class is looked up once, by a Ruby call whose failure panics;
|
|
177
207
|
// near the end of a fiber's stack Ruby fails it with SystemStackError, so it is
|
|
178
208
|
// looked up here instead, at load, with room to spare.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
use magnus::value::ReprValue;
|
|
2
|
+
use magnus::{Error, Integer, Object, RModule, Ruby, Value};
|
|
3
|
+
use mini_racer_engine::process::platform;
|
|
4
|
+
|
|
5
|
+
use crate::{errors, export, rb_raw};
|
|
6
|
+
|
|
7
|
+
pub fn init(rs: RModule) -> Result<(), Error> {
|
|
8
|
+
rs.define_singleton_method("heap_limit", export!(function limit, 0))?;
|
|
9
|
+
rs.define_singleton_method("heap_limit=", export!(function set_limit, 1))?;
|
|
10
|
+
Ok(())
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
fn limit(ruby: &Ruby) -> Value {
|
|
14
|
+
platform::heap_limit()
|
|
15
|
+
.and_then(|limit| u64::try_from(limit).ok())
|
|
16
|
+
.map_or_else(
|
|
17
|
+
|| ruby.qnil().as_value(),
|
|
18
|
+
|limit| ruby.integer_from_u64(limit).as_value(),
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Checked as `Context.new` checks its integer options: anything but an
|
|
23
|
+
// Integer is a TypeError, and an Integer out of range, however large, an
|
|
24
|
+
// ArgumentError.
|
|
25
|
+
fn set_limit(ruby: &Ruby, value: Value) -> Result<Value, Error> {
|
|
26
|
+
let limit = if value.is_nil() {
|
|
27
|
+
None
|
|
28
|
+
} else {
|
|
29
|
+
let integer = Integer::from_value(value).ok_or_else(|| {
|
|
30
|
+
let name = rb_raw::type_name(ruby, value);
|
|
31
|
+
errors::raised(
|
|
32
|
+
ruby,
|
|
33
|
+
ruby.exception_type_error(),
|
|
34
|
+
format!("wrong argument type {name} (expected Integer)"),
|
|
35
|
+
)
|
|
36
|
+
})?;
|
|
37
|
+
let bytes = integer.to_i64().ok().and_then(|n| usize::try_from(n).ok());
|
|
38
|
+
Some(bytes.unwrap_or(usize::MAX))
|
|
39
|
+
};
|
|
40
|
+
platform::set_heap_limit(limit).map_err(|error| errors::heap_limit(ruby, error))?;
|
|
41
|
+
Ok(value)
|
|
42
|
+
}
|
|
@@ -193,7 +193,7 @@ impl<'r> RubyHost<'r> {
|
|
|
193
193
|
if rb_raw::stack_exhausted(ruby) {
|
|
194
194
|
return Err(errors::stack_exhausted(ruby));
|
|
195
195
|
}
|
|
196
|
-
let args = RArray::from_value(decode::
|
|
196
|
+
let args = RArray::from_value(decode::arguments(ruby, request)?).ok_or_else(bad)?;
|
|
197
197
|
let id = Integer::from_value(args.pop::<Value>()?).ok_or_else(bad)?;
|
|
198
198
|
let id = isize::try_from(id.to_i64()?).map_err(|_| bad())?;
|
|
199
199
|
let callables = self
|
|
@@ -10,9 +10,11 @@ mod fork;
|
|
|
10
10
|
)]
|
|
11
11
|
mod gc_root;
|
|
12
12
|
mod guard;
|
|
13
|
+
mod heap_limit;
|
|
13
14
|
mod host;
|
|
14
15
|
mod options;
|
|
15
16
|
mod platform;
|
|
17
|
+
mod pool;
|
|
16
18
|
#[allow(
|
|
17
19
|
unsafe_code,
|
|
18
20
|
reason = "the only module that calls rb-sys directly (invariant I6)"
|
|
@@ -21,6 +23,7 @@ mod rb_raw;
|
|
|
21
23
|
mod snapshot;
|
|
22
24
|
mod source_maps;
|
|
23
25
|
mod spin;
|
|
26
|
+
mod timeout;
|
|
24
27
|
|
|
25
28
|
use magnus::{Error, Module, Ruby};
|
|
26
29
|
|
|
@@ -49,7 +52,10 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
49
52
|
context::init(ruby, racer)?;
|
|
50
53
|
snapshot::init(ruby, racer)?;
|
|
51
54
|
source_maps::init(rs)?;
|
|
55
|
+
heap_limit::init(rs)?;
|
|
56
|
+
timeout::init(ruby, rs)?;
|
|
52
57
|
counters::init(rs)?;
|
|
58
|
+
pool::init(ruby, rs)?;
|
|
53
59
|
fork::init(ruby, rs)?;
|
|
54
60
|
Ok(())
|
|
55
61
|
}
|
|
@@ -65,7 +65,17 @@ fn within(
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
/// `
|
|
68
|
+
/// Milliseconds as `timeout:` takes them: an Integer up to 2**31 - 1, where 0
|
|
69
|
+
/// means no timeout. `MiniRacerRs.timeout` takes the same.
|
|
70
|
+
pub fn timeout(value: &OptionValue) -> Result<Option<Duration>, OptionError> {
|
|
71
|
+
let ms = within(value, "timeout", 0..=INT32_MAX)?;
|
|
72
|
+
Ok(u64::try_from(ms)
|
|
73
|
+
.ok()
|
|
74
|
+
.filter(|ms| *ms > 0)
|
|
75
|
+
.map(Duration::from_millis))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// `Context.new` keywords in the order given; `mini_racer` 0.22.2 checks them
|
|
69
79
|
/// last to first and raises on the first bad one.
|
|
70
80
|
pub fn configure(
|
|
71
81
|
options: impl DoubleEndedIterator<Item = (String, OptionValue)>,
|
|
@@ -74,13 +84,7 @@ pub fn configure(
|
|
|
74
84
|
let mut refused = None;
|
|
75
85
|
for (key, value) in options.rev() {
|
|
76
86
|
match key.as_str() {
|
|
77
|
-
"timeout" =>
|
|
78
|
-
let ms = within(&value, "timeout", 0..=INT32_MAX)?;
|
|
79
|
-
config.timeout = u64::try_from(ms)
|
|
80
|
-
.ok()
|
|
81
|
-
.filter(|ms| *ms > 0)
|
|
82
|
-
.map(Duration::from_millis);
|
|
83
|
-
}
|
|
87
|
+
"timeout" => config.timeout = timeout(&value)?,
|
|
84
88
|
"marshal_stack_depth" => {
|
|
85
89
|
fixnum(&value)?;
|
|
86
90
|
}
|
|
@@ -117,10 +121,88 @@ pub fn configure(
|
|
|
117
121
|
})
|
|
118
122
|
}
|
|
119
123
|
|
|
124
|
+
/// The most contexts one `MiniRacerRs::Pool` holds. A pool builds every
|
|
125
|
+
/// context before `new` returns, each an isolate and a runner thread, so a
|
|
126
|
+
/// size near what `timeout:` allows would exhaust the process instead.
|
|
127
|
+
pub const MAX_POOL_SIZE: i64 = 1024;
|
|
128
|
+
|
|
129
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
130
|
+
pub struct PoolLimits {
|
|
131
|
+
pub size: usize,
|
|
132
|
+
pub max_uses: Option<u64>,
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// `MiniRacerRs::Pool.new`'s `size:` and `max_uses:` (nil for no limit),
|
|
136
|
+
/// checked as `Context.new` checks its integers.
|
|
137
|
+
pub fn pool(size: &OptionValue, max_uses: &OptionValue) -> Result<PoolLimits, OptionError> {
|
|
138
|
+
let size = within(size, "size", 1..=MAX_POOL_SIZE)?;
|
|
139
|
+
let max_uses = match max_uses {
|
|
140
|
+
OptionValue::Nil => None,
|
|
141
|
+
other => Some(within(other, "max_uses", 1..)?),
|
|
142
|
+
};
|
|
143
|
+
Ok(PoolLimits {
|
|
144
|
+
size: usize::try_from(size).map_err(|_| OptionError::Range("size"))?,
|
|
145
|
+
max_uses: max_uses
|
|
146
|
+
.map(u64::try_from)
|
|
147
|
+
.transpose()
|
|
148
|
+
.map_err(|_| OptionError::Range("max_uses"))?,
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
|
|
120
152
|
#[cfg(test)]
|
|
121
153
|
mod tests {
|
|
122
154
|
use super::*;
|
|
123
155
|
|
|
156
|
+
#[test]
|
|
157
|
+
fn a_pool_takes_one_to_max_contexts_and_any_positive_use_limit() {
|
|
158
|
+
let limits = pool(&OptionValue::Fixnum(4), &OptionValue::Nil);
|
|
159
|
+
assert_eq!(
|
|
160
|
+
limits,
|
|
161
|
+
Ok(PoolLimits {
|
|
162
|
+
size: 4,
|
|
163
|
+
max_uses: None
|
|
164
|
+
})
|
|
165
|
+
);
|
|
166
|
+
let limits = pool(
|
|
167
|
+
&OptionValue::Fixnum(MAX_POOL_SIZE),
|
|
168
|
+
&OptionValue::Fixnum(i64::MAX),
|
|
169
|
+
);
|
|
170
|
+
assert_eq!(
|
|
171
|
+
limits.map(|limits| (limits.size, limits.max_uses)),
|
|
172
|
+
Ok((1024, Some(i64::MAX.cast_unsigned())))
|
|
173
|
+
);
|
|
174
|
+
for size in [0, -1, MAX_POOL_SIZE + 1] {
|
|
175
|
+
assert_eq!(
|
|
176
|
+
pool(&OptionValue::Fixnum(size), &OptionValue::Nil),
|
|
177
|
+
Err(OptionError::Range("size"))
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
assert_eq!(
|
|
181
|
+
pool(&OptionValue::Fixnum(1), &OptionValue::Fixnum(0))
|
|
182
|
+
.map_err(|error| error.to_string()),
|
|
183
|
+
Err("bad max_uses".to_owned())
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[test]
|
|
188
|
+
fn a_pool_names_a_value_of_the_wrong_type_as_context_new_does() {
|
|
189
|
+
assert_eq!(
|
|
190
|
+
pool(&OptionValue::Nil, &OptionValue::Nil).map_err(|error| error.to_string()),
|
|
191
|
+
Err("wrong argument type nil (expected Integer)".to_owned())
|
|
192
|
+
);
|
|
193
|
+
assert_eq!(
|
|
194
|
+
pool(&OptionValue::Fixnum(2), &OptionValue::False).map_err(|error| error.to_string()),
|
|
195
|
+
Err("wrong argument type false (expected Integer)".to_owned())
|
|
196
|
+
);
|
|
197
|
+
assert_eq!(
|
|
198
|
+
pool(
|
|
199
|
+
&OptionValue::Other("String".to_owned()),
|
|
200
|
+
&OptionValue::Other("Float".to_owned())
|
|
201
|
+
),
|
|
202
|
+
Err(OptionError::Type("String".to_owned()))
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
124
206
|
fn configure_pairs(pairs: &[(&str, OptionValue)]) -> Result<ContextConfig, OptionError> {
|
|
125
207
|
configure(
|
|
126
208
|
pairs
|