mini_racer_rs 1.4.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.
@@ -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(usize),
67
+ Value {
68
+ index: usize,
69
+ depth: u32,
70
+ },
65
71
  // A pinned key of a hash.
66
- Key(usize),
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 { array: usize, index: isize },
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; how deep V8 then reads is V8's own limit.
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 root = self.pin(value);
233
- self.work.push(Item::Value(root));
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(index) => self.value(self.pinned(index))?,
243
- Item::Key(index) => {
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 { array, index } => {
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
- self.array(value);
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
- fn hash(&mut self, value: Value) -> Result<(), EncodeError> {
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 keys_fit && pairs.is_empty() && immediate(item_kind) {
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(item));
406
- self.work.push(Item::Key(key));
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
  }
@@ -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::value(ruby, request)?).ok_or_else(bad)?;
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
@@ -75,7 +75,7 @@ pub fn timeout(value: &OptionValue) -> Result<Option<Duration>, OptionError> {
75
75
  .map(Duration::from_millis))
76
76
  }
77
77
 
78
- /// `Context.new` keywords in the order given; `mini_racer` 0.22.1 checks them
78
+ /// `Context.new` keywords in the order given; `mini_racer` 0.22.2 checks them
79
79
  /// last to first and raises on the first bad one.
80
80
  pub fn configure(
81
81
  options: impl DoubleEndedIterator<Item = (String, OptionValue)>,
@@ -26,10 +26,8 @@ use crate::{context, errors, export, rb_raw};
26
26
  pub struct Snapshot {
27
27
  blob: OwnedRoot<RString>,
28
28
  // What contexts made from this snapshot start from, reused while one of
29
- // them lives and `blob` holds the same bytes: `dump` hands `blob` out, and
30
- // a context of mini_racer's starts from whatever it holds then. Held
31
- // weakly, so once its contexts are gone a snapshot holds only `blob`, as
32
- // mini_racer's does.
29
+ // them lives and `blob` holds the same bytes. Held weakly, so once its
30
+ // contexts are gone a snapshot holds only `blob`, as mini_racer's does.
33
31
  shared: RefCell<Option<WeakSnapshotBlob>>,
34
32
  }
35
33
 
@@ -96,8 +94,7 @@ impl Snapshot {
96
94
  self.shared.borrow_mut().take();
97
95
  }
98
96
 
99
- // mini_racer allocates every snapshot with an empty blob it then hands
100
- // out, so `dump` on one never built is that same String.
97
+ // mini_racer allocates every snapshot with an empty blob.
101
98
  fn blob(&self, ruby: &Ruby) -> RString {
102
99
  self.blob.get(ruby).unwrap_or_else(|| {
103
100
  let empty = ruby.str_from_slice(b"");
@@ -116,8 +113,10 @@ impl Snapshot {
116
113
  Ok(())
117
114
  }
118
115
 
116
+ // A copy, as mini_racer's is, so changing it changes nothing a context
117
+ // starts from.
119
118
  fn dump(ruby: &Ruby, rb_self: &Self) -> RString {
120
- rb_self.blob(ruby)
119
+ rb_raw::str_dup(ruby, rb_self.blob(ruby))
121
120
  }
122
121
 
123
122
  fn size(ruby: &Ruby, rb_self: &Self) -> usize {
@@ -133,9 +132,6 @@ impl Snapshot {
133
132
  Ok(rb_self)
134
133
  }
135
134
 
136
- // mini_racer keeps whatever it is given and aborts when V8 reads a bad
137
- // blob; refused here, a stale blob read back from disk can be rescued
138
- // and built again.
139
135
  fn load(ruby: &Ruby, class: RClass, blob: Value) -> Result<Obj<Self>, Error> {
140
136
  let blob = rb_raw::expect_string(ruby, blob)?;
141
137
  let copy = rb_raw::str_dup(ruby, blob);
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniRacer
4
- VERSION = "0.22.1"
4
+ VERSION = "0.22.2"
5
5
  LIBV8_NODE_VERSION = "~> 24.12.0.1"
6
6
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniRacerRs
4
- VERSION = "1.4.0"
4
+ VERSION = "1.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_racer_rs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Aragon
@@ -23,7 +23,7 @@ dependencies:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
25
  version: 0.9.130
26
- description: mini_racer_rs provides the same MiniRacer Ruby API as mini_racer 0.22.1
26
+ description: mini_racer_rs provides the same MiniRacer Ruby API as mini_racer 0.22.2
27
27
  using Rust and V8 15.2. Releases provide precompiled native gems for supported Linux
28
28
  and macOS platforms, plus a source gem that builds with Rust and libclang. The project
29
29
  is not affiliated with the mini_racer maintainers.
@@ -171,5 +171,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  requirements: []
172
172
  rubygems_version: 3.6.9
173
173
  specification_version: 4
174
- summary: A Rust drop-in replacement for mini_racer 0.22.1
174
+ summary: A Rust drop-in replacement for mini_racer 0.22.2
175
175
  test_files: []