mini_racer_rs 1.2.0 → 1.4.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 +27 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +55 -9
- data/crates/mini_racer_engine/src/context.rs +89 -20
- data/crates/mini_racer_engine/src/control.rs +118 -4
- data/crates/mini_racer_engine/src/counters.rs +208 -0
- data/crates/mini_racer_engine/src/error.rs +40 -0
- data/crates/mini_racer_engine/src/lib.rs +5 -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 +362 -52
- data/crates/mini_racer_engine/src/sys.rs +73 -2
- data/crates/mini_racer_sourcemap/src/map.rs +3 -2
- data/crates/mini_racer_sync/src/termination.rs +182 -44
- data/ext/mini_racer_rs/src/context.rs +45 -23
- data/ext/mini_racer_rs/src/counters.rs +40 -0
- 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/lib.rs +8 -0
- data/ext/mini_racer_rs/src/options.rs +89 -7
- 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/timeout.rs +132 -0
- 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 +8 -2
|
@@ -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
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
mod context;
|
|
2
|
+
mod counters;
|
|
2
3
|
mod decode;
|
|
3
4
|
mod encode;
|
|
4
5
|
mod errors;
|
|
@@ -9,9 +10,11 @@ mod fork;
|
|
|
9
10
|
)]
|
|
10
11
|
mod gc_root;
|
|
11
12
|
mod guard;
|
|
13
|
+
mod heap_limit;
|
|
12
14
|
mod host;
|
|
13
15
|
mod options;
|
|
14
16
|
mod platform;
|
|
17
|
+
mod pool;
|
|
15
18
|
#[allow(
|
|
16
19
|
unsafe_code,
|
|
17
20
|
reason = "the only module that calls rb-sys directly (invariant I6)"
|
|
@@ -20,6 +23,7 @@ mod rb_raw;
|
|
|
20
23
|
mod snapshot;
|
|
21
24
|
mod source_maps;
|
|
22
25
|
mod spin;
|
|
26
|
+
mod timeout;
|
|
23
27
|
|
|
24
28
|
use magnus::{Error, Module, Ruby};
|
|
25
29
|
|
|
@@ -48,6 +52,10 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
|
48
52
|
context::init(ruby, racer)?;
|
|
49
53
|
snapshot::init(ruby, racer)?;
|
|
50
54
|
source_maps::init(rs)?;
|
|
55
|
+
heap_limit::init(rs)?;
|
|
56
|
+
timeout::init(ruby, rs)?;
|
|
57
|
+
counters::init(rs)?;
|
|
58
|
+
pool::init(ruby, rs)?;
|
|
51
59
|
fork::init(ruby, rs)?;
|
|
52
60
|
Ok(())
|
|
53
61
|
}
|
|
@@ -65,6 +65,16 @@ fn within(
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
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
|
+
|
|
68
78
|
/// `Context.new` keywords in the order given; `mini_racer` 0.22.1 checks them
|
|
69
79
|
/// last to first and raises on the first bad one.
|
|
70
80
|
pub fn configure(
|
|
@@ -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
|