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
|
@@ -1,10 +1,31 @@
|
|
|
1
|
-
use std::
|
|
1
|
+
use std::num::NonZeroUsize;
|
|
2
|
+
use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
|
|
2
3
|
use std::sync::{Mutex, MutexGuard, PoisonError};
|
|
3
4
|
|
|
4
|
-
use crate::FlagError;
|
|
5
|
+
use crate::{FlagError, HeapLimitError};
|
|
5
6
|
|
|
6
7
|
const MAX_FLAG_BYTES: usize = 256;
|
|
7
8
|
|
|
9
|
+
/// The least heap limit `set_heap_limit` takes.
|
|
10
|
+
///
|
|
11
|
+
/// V8 15.2 gives the young generation at least 6 MiB of a limit, and from
|
|
12
|
+
/// 16 MiB down a `JSON.parse` whose result is about twice the limit aborts
|
|
13
|
+
/// the process, past the room a hit lends.
|
|
14
|
+
pub const MIN_HEAP_LIMIT: usize = 32 << 20;
|
|
15
|
+
/// The most: the largest heap V8 sizes by default on a 64-bit host.
|
|
16
|
+
pub const MAX_HEAP_LIMIT: usize = 4 << 30;
|
|
17
|
+
|
|
18
|
+
// The flags that size V8 15.2's heap in place of the limits an isolate's
|
|
19
|
+
// `CreateParams` set: the three sizes `Heap::ConfigureHeap` reads when not 0,
|
|
20
|
+
// and a boolean that implies a semi-space larger than a small limit gives.
|
|
21
|
+
const SIZING: [&str; 4] = [
|
|
22
|
+
"max_old_space_size",
|
|
23
|
+
"max_heap_size",
|
|
24
|
+
"max_semi_space_size",
|
|
25
|
+
"predictable_gc_schedule",
|
|
26
|
+
];
|
|
27
|
+
const BOOLEAN: usize = 3;
|
|
28
|
+
|
|
8
29
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
9
30
|
pub enum Mode {
|
|
10
31
|
MultiThreaded,
|
|
@@ -15,14 +36,22 @@ struct Flags {
|
|
|
15
36
|
list: Vec<String>,
|
|
16
37
|
frozen: bool,
|
|
17
38
|
mode: Mode,
|
|
39
|
+
// Which of `SIZING` V8 may hold set once it has read `list`.
|
|
40
|
+
sizing: [bool; SIZING.len()],
|
|
18
41
|
}
|
|
19
42
|
|
|
20
43
|
static FLAGS: Mutex<Flags> = Mutex::new(Flags {
|
|
21
44
|
list: Vec::new(),
|
|
22
45
|
frozen: false,
|
|
23
46
|
mode: Mode::MultiThreaded,
|
|
47
|
+
sizing: [false; SIZING.len()],
|
|
24
48
|
});
|
|
25
49
|
|
|
50
|
+
// The heap limit for contexts made from now on, 0 for none. Written under the
|
|
51
|
+
// flags lock, so a flag and a limit never both pass their checks, and read
|
|
52
|
+
// without it, so a context made in a forked child never waits for it.
|
|
53
|
+
static HEAP_LIMIT: AtomicUsize = AtomicUsize::new(0);
|
|
54
|
+
|
|
26
55
|
fn flags() -> MutexGuard<'static, Flags> {
|
|
27
56
|
FLAGS.lock().unwrap_or_else(PoisonError::into_inner)
|
|
28
57
|
}
|
|
@@ -69,6 +98,13 @@ pub fn push_flag(name: &[u8], value: Option<&[u8]>) -> Result<(), FlagError> {
|
|
|
69
98
|
if flags.frozen {
|
|
70
99
|
return Err(FlagError::AlreadyInitialized);
|
|
71
100
|
}
|
|
101
|
+
let sizing = read_sizing(&normalized.flag, flags.sizing);
|
|
102
|
+
if HEAP_LIMIT.load(Ordering::Relaxed) != 0
|
|
103
|
+
&& let Some(flag) = first(sizing)
|
|
104
|
+
{
|
|
105
|
+
return Err(FlagError::OverridesHeapLimit(flag));
|
|
106
|
+
}
|
|
107
|
+
flags.sizing = sizing;
|
|
72
108
|
if let Some(mode) = normalized.mode {
|
|
73
109
|
flags.mode = mode;
|
|
74
110
|
}
|
|
@@ -77,6 +113,156 @@ pub fn push_flag(name: &[u8], value: Option<&[u8]>) -> Result<(), FlagError> {
|
|
|
77
113
|
Ok(())
|
|
78
114
|
}
|
|
79
115
|
|
|
116
|
+
/// Sets the heap limit, in bytes, of the contexts made from now on, or with
|
|
117
|
+
/// `None` clears it.
|
|
118
|
+
pub fn set_heap_limit(limit: Option<usize>) -> Result<(), HeapLimitError> {
|
|
119
|
+
if limit.is_some_and(|limit| !(MIN_HEAP_LIMIT..=MAX_HEAP_LIMIT).contains(&limit)) {
|
|
120
|
+
return Err(HeapLimitError::Range);
|
|
121
|
+
}
|
|
122
|
+
let flags = flags();
|
|
123
|
+
if limit.is_some()
|
|
124
|
+
&& let Some(flag) = first(flags.sizing)
|
|
125
|
+
{
|
|
126
|
+
return Err(HeapLimitError::Overridden(flag));
|
|
127
|
+
}
|
|
128
|
+
HEAP_LIMIT.store(limit.unwrap_or(0), Ordering::Relaxed);
|
|
129
|
+
drop(flags);
|
|
130
|
+
Ok(())
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
#[must_use]
|
|
134
|
+
pub fn heap_limit() -> Option<usize> {
|
|
135
|
+
NonZeroUsize::new(HEAP_LIMIT.load(Ordering::Relaxed)).map(NonZeroUsize::get)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn first(sizing: [bool; SIZING.len()]) -> Option<&'static str> {
|
|
139
|
+
SIZING
|
|
140
|
+
.iter()
|
|
141
|
+
.zip(sizing)
|
|
142
|
+
.find_map(|(flag, set)| set.then_some(*flag))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Which of `SIZING` V8 may hold set after `SetFlagsFromString` reads `flag`,
|
|
146
|
+
// given `before`. It errs toward set: V8 splits at white space and reads each
|
|
147
|
+
// argument that starts with a dash as a flag, a `no` before its name
|
|
148
|
+
// included, with a value after `=` or in the next argument, which it reads
|
|
149
|
+
// even when it starts with a dash. It sets a size before it finds a negation
|
|
150
|
+
// or characters after the number wrong, goes on past a negative size, and
|
|
151
|
+
// stops at an unknown flag or other errors only after setting what came
|
|
152
|
+
// before. So every argument up to a `--` V8 cannot take as a flag's value
|
|
153
|
+
// counts, a following argument both as a value and as a flag, and a size or a
|
|
154
|
+
// boolean is cleared only by a flag that is that one argument, which V8 must
|
|
155
|
+
// read whole.
|
|
156
|
+
fn read_sizing(flag: &str, before: [bool; SIZING.len()]) -> [bool; SIZING.len()] {
|
|
157
|
+
// C's isspace, which V8 splits at.
|
|
158
|
+
let space = |c: char| matches!(c, ' ' | '\t' | '\n' | '\x0b' | '\x0c' | '\r');
|
|
159
|
+
let args: Vec<&str> = flag.split(space).filter(|arg| !arg.is_empty()).collect();
|
|
160
|
+
let mut sizing = before;
|
|
161
|
+
if let [arg] = args.as_slice()
|
|
162
|
+
&& let Some((index, read)) = parse(arg)
|
|
163
|
+
&& read.clears()
|
|
164
|
+
&& let Some(set) = sizing.get_mut(index)
|
|
165
|
+
{
|
|
166
|
+
*set = false;
|
|
167
|
+
return sizing;
|
|
168
|
+
}
|
|
169
|
+
let mut previous: Option<&str> = None;
|
|
170
|
+
for (at, arg) in args.iter().enumerate() {
|
|
171
|
+
let takes_value = previous.is_some_and(|flag| flag.starts_with('-') && !flag.contains('='));
|
|
172
|
+
if *arg == "--" && !takes_value {
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
previous = Some(arg);
|
|
176
|
+
let Some((index, read)) = parse(arg) else {
|
|
177
|
+
continue;
|
|
178
|
+
};
|
|
179
|
+
let next = args.get(at + 1).copied();
|
|
180
|
+
if read.sets(next)
|
|
181
|
+
&& let Some(set) = sizing.get_mut(index)
|
|
182
|
+
{
|
|
183
|
+
*set = true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
sizing
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// One argument naming a flag of `SIZING`, as V8 splits it.
|
|
190
|
+
struct Read<'a> {
|
|
191
|
+
index: usize,
|
|
192
|
+
negated: bool,
|
|
193
|
+
value: Option<&'a str>,
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
impl Read<'_> {
|
|
197
|
+
// Whether V8 may set it: a size to a number that is not 0, the value
|
|
198
|
+
// after `=` or else the next argument, or the boolean to true.
|
|
199
|
+
fn sets(&self, next: Option<&str>) -> bool {
|
|
200
|
+
if self.index == BOOLEAN {
|
|
201
|
+
return !self.negated;
|
|
202
|
+
}
|
|
203
|
+
self.value
|
|
204
|
+
.or(next)
|
|
205
|
+
.and_then(megabytes)
|
|
206
|
+
.is_some_and(|(size, _)| size != 0)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Whether V8 surely clears it, read as the only argument: a size given
|
|
210
|
+
// all digits that make 0, or the boolean negated without a value.
|
|
211
|
+
fn clears(&self) -> bool {
|
|
212
|
+
if self.index == BOOLEAN {
|
|
213
|
+
self.negated && self.value.is_none()
|
|
214
|
+
} else {
|
|
215
|
+
!self.negated && self.value.and_then(megabytes) == Some((0, true))
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
fn parse(arg: &str) -> Option<(usize, Read<'_>)> {
|
|
221
|
+
let arg = arg.strip_prefix('-')?;
|
|
222
|
+
let arg = arg.strip_prefix('-').unwrap_or(arg);
|
|
223
|
+
let (negated, arg) = arg.strip_prefix("no").map_or((false, arg), |arg| {
|
|
224
|
+
(true, arg.strip_prefix(['-', '_']).unwrap_or(arg))
|
|
225
|
+
});
|
|
226
|
+
let (name, value) = match arg.split_once('=') {
|
|
227
|
+
Some((name, value)) => (name, Some(value)),
|
|
228
|
+
None => (arg, None),
|
|
229
|
+
};
|
|
230
|
+
let index = SIZING.iter().position(|size| same_flag(size, name))?;
|
|
231
|
+
Some((
|
|
232
|
+
index,
|
|
233
|
+
Read {
|
|
234
|
+
index,
|
|
235
|
+
negated,
|
|
236
|
+
value,
|
|
237
|
+
},
|
|
238
|
+
))
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// V8 takes `-` and `_` in a flag's name as one character.
|
|
242
|
+
fn same_flag(a: &str, b: &str) -> bool {
|
|
243
|
+
let unify = |c: u8| if c == b'_' { b'-' } else { c };
|
|
244
|
+
a.len() == b.len() && a.bytes().zip(b.bytes()).all(|(a, b)| unify(a) == unify(b))
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// `strtoll` in base 10, as V8 reads a size: `None` where V8 leaves the flag
|
|
248
|
+
// alone, a negative number or one past `i64::MAX`, and where no digit comes
|
|
249
|
+
// first, which only some C libraries refuse; with the number, whether it is
|
|
250
|
+
// all of `text`.
|
|
251
|
+
fn megabytes(text: &str) -> Option<(u64, bool)> {
|
|
252
|
+
let (negative, digits) = match text.as_bytes().first() {
|
|
253
|
+
Some(b'-') => (true, text.get(1..)?),
|
|
254
|
+
Some(b'+') => (false, text.get(1..)?),
|
|
255
|
+
_ => (false, text),
|
|
256
|
+
};
|
|
257
|
+
let count = digits.bytes().take_while(u8::is_ascii_digit).count();
|
|
258
|
+
let number = digits.get(..count).filter(|number| !number.is_empty())?;
|
|
259
|
+
let size = u64::try_from(number.parse::<i64>().ok()?).ok()?;
|
|
260
|
+
if negative && size != 0 {
|
|
261
|
+
return None;
|
|
262
|
+
}
|
|
263
|
+
Some((size, count == digits.len()))
|
|
264
|
+
}
|
|
265
|
+
|
|
80
266
|
// The mode once frozen, readable without the flags lock, which runner threads
|
|
81
267
|
// must not take: a fork that does not wait for them could leave it held.
|
|
82
268
|
static FROZEN_MODE: AtomicU8 = AtomicU8::new(0);
|
|
@@ -166,6 +352,132 @@ mod tests {
|
|
|
166
352
|
assert_eq!(normalize(b"use_strict", None).map(|n| n.mode), Ok(None));
|
|
167
353
|
}
|
|
168
354
|
|
|
355
|
+
const OLD: [bool; 4] = [true, false, false, false];
|
|
356
|
+
const HEAP: [bool; 4] = [false, true, false, false];
|
|
357
|
+
const SEMI: [bool; 4] = [false, false, true, false];
|
|
358
|
+
const PREDICTABLE: [bool; 4] = [false, false, false, true];
|
|
359
|
+
const NONE: [bool; 4] = [false; 4];
|
|
360
|
+
|
|
361
|
+
fn sizing_after(flags: &[(&[u8], Option<&[u8]>)]) -> [bool; 4] {
|
|
362
|
+
flags.iter().fold(NONE, |sizing, (name, value)| {
|
|
363
|
+
read_sizing(&flag(name, *value).expect("flag"), sizing)
|
|
364
|
+
})
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
fn or(a: [bool; 4], b: [bool; 4]) -> [bool; 4] {
|
|
368
|
+
[a[0] || b[0], a[1] || b[1], a[2] || b[2], a[3] || b[3]]
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
#[test]
|
|
372
|
+
fn reads_heap_sizing_in_every_spelling_v8_takes() {
|
|
373
|
+
for (name, value, sizing) in [
|
|
374
|
+
(&b"max_old_space_size"[..], Some(&b"128"[..]), OLD),
|
|
375
|
+
(b"--max-old-space-size", Some(b"128"), OLD),
|
|
376
|
+
(b"max-heap-size", Some(b"256"), HEAP),
|
|
377
|
+
(b"max_semi_space_size", Some(b"16"), SEMI),
|
|
378
|
+
(b"max-semi_space-size", Some(b"+16"), SEMI),
|
|
379
|
+
(b"predictable_gc_schedule", None, PREDICTABLE),
|
|
380
|
+
(b"--predictable-gc-schedule", None, PREDICTABLE),
|
|
381
|
+
// V8 sets a value before it finds the negation, the letters after
|
|
382
|
+
// the number or a boolean's value wrong.
|
|
383
|
+
(b"no-max-old-space-size", Some(b"128"), OLD),
|
|
384
|
+
(b"nomax_heap_size", Some(b"64"), HEAP),
|
|
385
|
+
(b"no_max_semi_space_size", Some(b"8"), SEMI),
|
|
386
|
+
(b"max_old_space_size", Some(b"12abc"), OLD),
|
|
387
|
+
(b"predictable_gc_schedule", Some(b"false"), PREDICTABLE),
|
|
388
|
+
// A value holds more arguments, or the next argument is the value.
|
|
389
|
+
(b"stack_size", Some(b"512 --max-heap-size=64"), HEAP),
|
|
390
|
+
(b"max_old_space_size 96", None, OLD),
|
|
391
|
+
(b"max_old_space_size", Some(b"0"), NONE),
|
|
392
|
+
(b"max_old_space_size", Some(b"-5"), NONE),
|
|
393
|
+
(b"max_old_space_size", Some(b"99999999999999999999"), NONE),
|
|
394
|
+
(b"max_old_space_size", None, NONE),
|
|
395
|
+
(b"nopredictable_gc_schedule", None, NONE),
|
|
396
|
+
(b"max_young_generation_size", Some(b"64"), NONE),
|
|
397
|
+
(b"initial_heap_size", Some(b"64"), NONE),
|
|
398
|
+
(b"optimize_for_size", None, NONE),
|
|
399
|
+
(b"MAX_OLD_SPACE_SIZE", Some(b"64"), NONE),
|
|
400
|
+
(b"stack_size=1 -- --max-heap-size=64", None, NONE),
|
|
401
|
+
// A flag of another kind might take the `--` as its value.
|
|
402
|
+
(b"logfile -- --max-heap-size=64", None, HEAP),
|
|
403
|
+
] {
|
|
404
|
+
assert_eq!(
|
|
405
|
+
sizing_after(&[(name, value)]),
|
|
406
|
+
sizing,
|
|
407
|
+
"{}",
|
|
408
|
+
String::from_utf8_lossy(name)
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// V8 goes on past a negative size or an empty one, and only stops at an
|
|
414
|
+
// unknown flag after setting what came before it.
|
|
415
|
+
#[test]
|
|
416
|
+
fn every_argument_v8_might_read_counts() {
|
|
417
|
+
for (name, value, sizing) in [
|
|
418
|
+
(
|
|
419
|
+
&b"max_heap_size"[..],
|
|
420
|
+
&b"-1 --max_old_space_size=256"[..],
|
|
421
|
+
OLD,
|
|
422
|
+
),
|
|
423
|
+
(b"max_old_space_size", b" --max_heap_size=256", HEAP),
|
|
424
|
+
(
|
|
425
|
+
b"max_old_space_size",
|
|
426
|
+
b"256 --bogus --max_old_space_size=0",
|
|
427
|
+
OLD,
|
|
428
|
+
),
|
|
429
|
+
(
|
|
430
|
+
b"max_heap_size",
|
|
431
|
+
b"1x --max_old_space_size=8",
|
|
432
|
+
or(HEAP, OLD),
|
|
433
|
+
),
|
|
434
|
+
(
|
|
435
|
+
b"stack_size",
|
|
436
|
+
b"--max_semi_space_size --max_heap_size=64",
|
|
437
|
+
HEAP,
|
|
438
|
+
),
|
|
439
|
+
(b"stack_size", b"1 --max_semi_space_size 4", SEMI),
|
|
440
|
+
] {
|
|
441
|
+
assert_eq!(
|
|
442
|
+
sizing_after(&[(name, Some(value))]),
|
|
443
|
+
sizing,
|
|
444
|
+
"{}",
|
|
445
|
+
String::from_utf8_lossy(value)
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// Each flag is a call of its own to V8, so a later one that is a single
|
|
451
|
+
// argument V8 reads whole clears what an earlier one set.
|
|
452
|
+
#[test]
|
|
453
|
+
fn only_a_flag_v8_surely_reads_whole_clears_a_size() {
|
|
454
|
+
let cleared = sizing_after(&[
|
|
455
|
+
(b"max_old_space_size", Some(b"128")),
|
|
456
|
+
(b"max-old-space-size", Some(b"0")),
|
|
457
|
+
(b"predictable_gc_schedule", None),
|
|
458
|
+
(b"no-predictable-gc-schedule", None),
|
|
459
|
+
]);
|
|
460
|
+
assert_eq!(cleared, NONE);
|
|
461
|
+
for clear in [&b"0 --use_strict"[..], b"", b"0x", b"-0 --"] {
|
|
462
|
+
assert_eq!(
|
|
463
|
+
sizing_after(&[
|
|
464
|
+
(b"max_heap_size", Some(b"64")),
|
|
465
|
+
(b"max_heap_size", Some(clear)),
|
|
466
|
+
]),
|
|
467
|
+
HEAP,
|
|
468
|
+
"{}",
|
|
469
|
+
String::from_utf8_lossy(clear)
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
assert_eq!(
|
|
473
|
+
sizing_after(&[
|
|
474
|
+
(b"max_heap_size", Some(b"64")),
|
|
475
|
+
(b"no-max-heap-size", Some(b"0")),
|
|
476
|
+
]),
|
|
477
|
+
HEAP
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
169
481
|
// Flags freeze at the first initialization in a process, so this runs in
|
|
170
482
|
// a process of its own even where other tests share one.
|
|
171
483
|
#[test]
|
|
@@ -191,4 +503,50 @@ mod tests {
|
|
|
191
503
|
Err(FlagError::AlreadyInitialized)
|
|
192
504
|
);
|
|
193
505
|
}
|
|
506
|
+
|
|
507
|
+
// The limit and the flags are process state, so this runs alone too.
|
|
508
|
+
#[test]
|
|
509
|
+
fn a_heap_limit_and_a_sizing_flag_refuse_each_other() {
|
|
510
|
+
const ALONE: &str = "MINI_RACER_RS_TEST_ALONE";
|
|
511
|
+
if std::env::var_os(ALONE).is_none() {
|
|
512
|
+
let status = std::process::Command::new(std::env::current_exe().expect("test binary"))
|
|
513
|
+
.args([
|
|
514
|
+
"process::platform::tests::a_heap_limit_and_a_sizing_flag_refuse_each_other",
|
|
515
|
+
"--exact",
|
|
516
|
+
])
|
|
517
|
+
.env(ALONE, "1")
|
|
518
|
+
.status()
|
|
519
|
+
.expect("run the test binary");
|
|
520
|
+
assert!(status.success(), "{status}");
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
assert_eq!(heap_limit(), None);
|
|
524
|
+
assert_eq!(
|
|
525
|
+
set_heap_limit(Some(MIN_HEAP_LIMIT - 1)),
|
|
526
|
+
Err(HeapLimitError::Range)
|
|
527
|
+
);
|
|
528
|
+
assert_eq!(
|
|
529
|
+
set_heap_limit(Some(MAX_HEAP_LIMIT + 1)),
|
|
530
|
+
Err(HeapLimitError::Range)
|
|
531
|
+
);
|
|
532
|
+
assert_eq!(set_heap_limit(Some(64 << 20)), Ok(()));
|
|
533
|
+
assert_eq!(heap_limit(), Some(64 << 20));
|
|
534
|
+
assert_eq!(
|
|
535
|
+
push_flag(b"no-max-heap-size", Some(b"64")),
|
|
536
|
+
Err(FlagError::OverridesHeapLimit("max_heap_size"))
|
|
537
|
+
);
|
|
538
|
+
assert_eq!(push_flag(b"max_old_space_size", Some(b"0")), Ok(()));
|
|
539
|
+
assert_eq!(push_flag(b"initial_heap_size", Some(b"64")), Ok(()));
|
|
540
|
+
assert_eq!(set_heap_limit(None), Ok(()));
|
|
541
|
+
assert_eq!(heap_limit(), None);
|
|
542
|
+
assert_eq!(push_flag(b"max_semi_space_size", Some(b"4")), Ok(()));
|
|
543
|
+
assert_eq!(
|
|
544
|
+
set_heap_limit(Some(64 << 20)),
|
|
545
|
+
Err(HeapLimitError::Overridden("max_semi_space_size"))
|
|
546
|
+
);
|
|
547
|
+
assert_eq!(heap_limit(), None);
|
|
548
|
+
assert_eq!(push_flag(b"max_semi_space_size", Some(b"0")), Ok(()));
|
|
549
|
+
assert_eq!(set_heap_limit(Some(MAX_HEAP_LIMIT)), Ok(()));
|
|
550
|
+
assert_eq!(set_heap_limit(Some(MIN_HEAP_LIMIT)), Ok(()));
|
|
551
|
+
}
|
|
194
552
|
}
|
|
@@ -6,7 +6,7 @@ use std::time::{Duration, Instant};
|
|
|
6
6
|
use mini_racer_sync::{DispatchId, Interrupt, lock, wait, wait_timeout};
|
|
7
7
|
|
|
8
8
|
use crate::EngineError;
|
|
9
|
-
use crate::control::ControlBlock;
|
|
9
|
+
use crate::control::{ControlBlock, Timer};
|
|
10
10
|
|
|
11
11
|
struct Entry {
|
|
12
12
|
at: Instant,
|
|
@@ -87,13 +87,30 @@ pub fn schedule(
|
|
|
87
87
|
after: Duration,
|
|
88
88
|
block: &Arc<ControlBlock>,
|
|
89
89
|
dispatch: DispatchId,
|
|
90
|
+
) -> Result<(), EngineError> {
|
|
91
|
+
arm(after, block, |timer, at| timer.due = Some((at, dispatch)))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/// Arms the timeout a nested script runs under on its own.
|
|
95
|
+
pub fn schedule_nested(
|
|
96
|
+
after: Duration,
|
|
97
|
+
block: &Arc<ControlBlock>,
|
|
98
|
+
id: DispatchId,
|
|
99
|
+
) -> Result<(), EngineError> {
|
|
100
|
+
arm(after, block, |timer, at| timer.nested.push((at, id)))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
fn arm(
|
|
104
|
+
after: Duration,
|
|
105
|
+
block: &Arc<ControlBlock>,
|
|
106
|
+
set: impl FnOnce(&mut Timer, Instant),
|
|
90
107
|
) -> Result<(), EngineError> {
|
|
91
108
|
let watchdog = current()?;
|
|
92
109
|
let at = Instant::now()
|
|
93
110
|
.checked_add(after)
|
|
94
111
|
.ok_or_else(|| EngineError::Internal("timeout out of range".to_owned()))?;
|
|
95
112
|
let mut timer = lock(&block.timer);
|
|
96
|
-
timer
|
|
113
|
+
set(&mut timer, at);
|
|
97
114
|
let wake = timer.queued.is_none_or(|queued| at < queued);
|
|
98
115
|
if wake {
|
|
99
116
|
timer.queued = Some(at);
|
|
@@ -106,6 +123,20 @@ pub fn schedule(
|
|
|
106
123
|
Ok(())
|
|
107
124
|
}
|
|
108
125
|
|
|
126
|
+
/// Whether the outermost dispatch's own timeout, or a nested script's, fires
|
|
127
|
+
/// within `after` from now.
|
|
128
|
+
pub fn ends_within(block: &ControlBlock, after: Duration) -> bool {
|
|
129
|
+
let Some(deadline) = Instant::now().checked_add(after) else {
|
|
130
|
+
return true;
|
|
131
|
+
};
|
|
132
|
+
let timer = lock(&block.timer);
|
|
133
|
+
timer
|
|
134
|
+
.due
|
|
135
|
+
.iter()
|
|
136
|
+
.chain(&timer.nested)
|
|
137
|
+
.any(|(at, _)| *at <= deadline)
|
|
138
|
+
}
|
|
139
|
+
|
|
109
140
|
/// Bounds work `dispatch` does after its timeout fired with the timeout again.
|
|
110
141
|
/// A deadline that has not fired yet already bounds it.
|
|
111
142
|
pub fn rearm_if_fired(
|
|
@@ -130,6 +161,11 @@ pub fn clear(block: &ControlBlock, dispatch: DispatchId) {
|
|
|
130
161
|
}
|
|
131
162
|
}
|
|
132
163
|
|
|
164
|
+
/// Forgets a nested script's own timeout once the script is over.
|
|
165
|
+
pub fn clear_nested(block: &ControlBlock, id: DispatchId) {
|
|
166
|
+
lock(&block.timer).nested.retain(|(_, due)| *due != id);
|
|
167
|
+
}
|
|
168
|
+
|
|
133
169
|
fn run(watchdog: &Watchdog) {
|
|
134
170
|
loop {
|
|
135
171
|
let mut timers = lock(&watchdog.timers);
|
|
@@ -167,19 +203,32 @@ fn run(watchdog: &Watchdog) {
|
|
|
167
203
|
|
|
168
204
|
fn fire(watchdog: &Watchdog, block: &Arc<ControlBlock>, now: Instant) {
|
|
169
205
|
let mut timer = lock(&block.timer);
|
|
170
|
-
|
|
171
|
-
let
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
206
|
+
let mut fired = Vec::new();
|
|
207
|
+
if let Some((at, id)) = timer.due
|
|
208
|
+
&& at <= now
|
|
209
|
+
{
|
|
210
|
+
timer.due = None;
|
|
211
|
+
fired.push(id);
|
|
176
212
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
213
|
+
timer.nested.retain(|&(at, id)| {
|
|
214
|
+
let due = at <= now;
|
|
215
|
+
if due {
|
|
216
|
+
fired.push(id);
|
|
181
217
|
}
|
|
182
|
-
|
|
183
|
-
|
|
218
|
+
!due
|
|
219
|
+
});
|
|
220
|
+
let next = timer
|
|
221
|
+
.due
|
|
222
|
+
.iter()
|
|
223
|
+
.chain(&timer.nested)
|
|
224
|
+
.map(|(at, _)| *at)
|
|
225
|
+
.min();
|
|
226
|
+
timer.queued = next;
|
|
227
|
+
drop(timer);
|
|
228
|
+
for id in fired {
|
|
229
|
+
block.term.request(Interrupt::Timeout(id));
|
|
230
|
+
}
|
|
231
|
+
if let Some(at) = next {
|
|
232
|
+
watchdog.push(at, Arc::downgrade(block));
|
|
184
233
|
}
|
|
185
234
|
}
|