cfsolve 0.1.4 → 0.1.5

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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +26 -6
  4. data/UPSTREAM.md +23 -8
  5. data/lib/cfsolve/profiles/safari-26-ios/answers.json +805 -797
  6. data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
  7. data/lib/cfsolve/version.rb +2 -2
  8. data/lib/cfsolve.rb +22 -11
  9. data/vendor/cfsolve/README.md +5 -53
  10. data/vendor/cfsolve/examples/listing_dump.rs +24 -0
  11. data/vendor/cfsolve/examples/probe_similarity.rs +37 -0
  12. data/vendor/cfsolve/examples/rebase_reference.rs +96 -0
  13. data/vendor/cfsolve/examples/reread_capture.rs +31 -0
  14. data/vendor/cfsolve/examples/turnstile_smoke.rs +24 -3
  15. data/vendor/cfsolve/profiles/safari-26-ios/answers.json +805 -797
  16. data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
  17. data/vendor/cfsolve/src/capture/sessions.rs +88 -4
  18. data/vendor/cfsolve/src/capture.rs +1 -0
  19. data/vendor/cfsolve/src/deobfuscate/ast.rs +16 -0
  20. data/vendor/cfsolve/src/deobfuscate/control_flow.rs +1 -1
  21. data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +33 -0
  22. data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +24 -4
  23. data/vendor/cfsolve/src/embedding_page/extra_params.rs +10 -113
  24. data/vendor/cfsolve/src/embedding_page/page.rs +70 -99
  25. data/vendor/cfsolve/src/session/initial.rs +6 -7
  26. data/vendor/cfsolve/src/session/probes.rs +0 -1
  27. data/vendor/cfsolve/src/stage2/analysis/analyze.rs +4 -33
  28. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +9 -17
  29. data/vendor/cfsolve/src/stage2/benchmark.rs +42 -62
  30. data/vendor/cfsolve/src/stage2/call_stack.rs +133 -227
  31. data/vendor/cfsolve/src/stage2/challenge_page.rs +10 -3
  32. data/vendor/cfsolve/src/stage2/probes/dates.rs +0 -44
  33. data/vendor/cfsolve/src/stage2/probes/parent_info.rs +345 -61
  34. data/vendor/cfsolve/src/stage2/probes/performance.rs +42 -5
  35. data/vendor/cfsolve/src/stage2/probes/stack.rs +45 -117
  36. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +7 -76
  37. data/vendor/cfsolve/src/stage2/probes.rs +0 -1
  38. data/vendor/cfsolve/src/stage2/reference.json +1 -1
  39. data/vendor/cfsolve/src/stage2/reference.rs +70 -115
  40. data/vendor/cfsolve/src/stage2/registry.rs +6 -11
  41. data/vendor/cfsolve/src/stage2/replay.rs +3 -34
  42. data/vendor/cfsolve/src/stage2/stylesheets.rs +0 -35
  43. data/vendor/cfsolve/src/stage2/trace.rs +1 -1
  44. data/vendor/cfsolve/src/stage2/vocabulary.rs +6 -111
  45. data/vendor/cfsolve/src/stage2.rs +0 -1
  46. data/vendor/cfsolve/src/vm/dataflow.rs +15 -6
  47. data/vendor/cfsolve/src/vm/disassemble.rs +5 -68
  48. data/vendor/cfsolve/src/vm/encoded_strings.rs +1553 -0
  49. data/vendor/cfsolve/src/vm/extract.rs +193 -343
  50. data/vendor/cfsolve/src/vm/interpret/access.rs +2 -10
  51. data/vendor/cfsolve/src/vm/interpret/builtin.rs +0 -13
  52. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +28 -43
  53. data/vendor/cfsolve/src/vm/interpret/mix32.rs +216 -86
  54. data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +42 -21
  55. data/vendor/cfsolve/src/vm/interpret/pattern.rs +272 -100
  56. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +259 -36
  57. data/vendor/cfsolve/src/vm/interpret/run.rs +0 -4
  58. data/vendor/cfsolve/src/vm/machine.rs +14 -70
  59. data/vendor/cfsolve/src/vm/output.rs +3 -5
  60. data/vendor/cfsolve/src/vm/registers.rs +26 -46
  61. data/vendor/cfsolve/src/vm/session.rs +11 -9
  62. data/vendor/cfsolve/src/vm.rs +1 -1
  63. metadata +6 -12
  64. data/lib/cfsolve/profiles/chrome-157-macos/answers.json +0 -4777
  65. data/lib/cfsolve/profiles/chrome-157-macos.json +0 -613
  66. data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +0 -4777
  67. data/vendor/cfsolve/profiles/chrome-157-macos.json +0 -613
  68. data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +0 -1
  69. data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +0 -326
  70. data/vendor/cfsolve/src/stage2/value_labels.rs +0 -185
  71. data/vendor/cfsolve/src/vm/encoded_names.rs +0 -635
  72. data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +0 -9
  73. data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +0 -7438
  74. data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +0 -1
@@ -1,43 +1,84 @@
1
- //! Recognizes register bookkeeping that is redundant in the interpreter's unmasked model.
2
- //! Recognition checks the whole body; calls with ordinary JavaScript objects still interpret it.
1
+ //! Recognizes the register file's accessors and register bookkeeping that is redundant in the
2
+ //! interpreter's unmasked model. Recognition checks the whole body; calls with ordinary
3
+ //! JavaScript objects still interpret it.
4
+
5
+ use std::ptr;
3
6
 
4
7
  use oxc::ast::ast::{
5
- AssignmentOperator, AssignmentTarget, BindingPattern, Expression, Function, MemberExpression,
6
- Statement, UnaryOperator,
8
+ Argument, ArrowFunctionExpression, AssignmentOperator, AssignmentTarget, BindingPattern,
9
+ Expression, Function, FunctionBody, IdentifierReference, IfStatement, MemberExpression,
10
+ ReturnStatement, Statement, UnaryOperator,
7
11
  };
8
- use oxc::semantic::SymbolId;
12
+ use oxc::ast_visit::Visit;
13
+ use oxc::semantic::{ScopeFlags, SymbolId};
9
14
  use oxc::syntax::operator::BinaryOperator;
10
15
 
11
16
  use super::mix32;
12
17
  use crate::js::ast::{self, Script};
13
- use crate::vm::registers::{Access, Member};
14
- use crate::vm::{HashSet, extract};
18
+ use crate::vm::HashSet;
19
+ use crate::vm::extract::{self, Accessors};
20
+ use crate::vm::registers::Member;
15
21
 
16
22
  #[derive(Clone, Copy)]
17
23
  pub(crate) enum Helper {
18
24
  Mask,
19
- Pc,
20
- Key,
25
+ /// Stores the program counter, returning the value if `returns` and nothing otherwise.
26
+ Pc {
27
+ returns: bool,
28
+ },
29
+ /// Stores the dispatch key, returning the value if `returns` and nothing otherwise.
30
+ Key {
31
+ returns: bool,
32
+ },
21
33
  Number,
34
+ /// `read(registers, index)`: the register's value.
35
+ Read,
36
+ /// `write(registers, index, value)`: stores the value and returns nothing.
37
+ Write,
22
38
  }
23
39
 
40
+ /// Recognizes a register helper. With `accessors`, also recognizes the register file's reader and
41
+ /// writer, and the wrappers that differ from them only in ways the interpreter's model does not
42
+ /// represent:
43
+ ///
44
+ /// - `save(registers, index)` returns a string record's stored form instead of decrypting it:
45
+ /// `if (<tag> === registers.<tags>[index]) return registers.<values>[index]; else return
46
+ /// read(registers, index);`. The model stores records decrypted, so both branches read the
47
+ /// same value.
48
+ /// - `restore(registers, index, value)` restores closures through a separate function:
49
+ /// `if (<closure test>) <restore>(registers, index, value); else write(registers, index,
50
+ /// value);`. The model stores closures as values.
24
51
  pub(super) fn recognize(
25
52
  script: Script<'_>,
53
+ accessors: Option<Accessors<'_>>,
26
54
  function: &Function<'_>,
27
- access: Access<'_>,
28
55
  ) -> Option<Helper> {
29
56
  if function.r#async || function.generator {
30
57
  return None;
31
58
  }
59
+ let body = function.body.as_deref()?;
60
+ if !body.directives.is_empty() {
61
+ return None;
62
+ }
63
+ if let Some(accessors) = accessors {
64
+ if ptr::eq(function, accessors.read) {
65
+ return Some(Helper::Read);
66
+ }
67
+ if ptr::eq(function, accessors.write) && returns_nothing(body) {
68
+ return Some(Helper::Write);
69
+ }
70
+ }
32
71
  let parameters: Vec<SymbolId> = extract::parameters(&function.params)?
33
72
  .into_iter()
34
73
  .map(|parameter| parameter.symbol_id())
35
74
  .collect();
36
- let body = function.body.as_deref()?;
37
- if !body.directives.is_empty() {
38
- return None;
75
+ let recognizer = Recognizer { script };
76
+ if let Some(accessors) = accessors
77
+ && let [Statement::IfStatement(branch)] = &body.statements[..]
78
+ && let Some(helper) = recognizer.wrapper(accessors, branch, &parameters)
79
+ {
80
+ return Some(helper);
39
81
  }
40
- let recognizer = Recognizer { script, access };
41
82
  let body = &body.statements;
42
83
  if let [value] = parameters[..]
43
84
  && let [
@@ -66,11 +107,18 @@ pub(super) fn recognize(
66
107
  if object == value {
67
108
  return None;
68
109
  }
69
- if let [
70
- Statement::IfStatement(branch),
71
- Statement::ReturnStatement(returned),
72
- ] = &body[..]
73
- && recognizer.returns(returned.argument.as_ref(), value)
110
+ // A setter returns the value it stores, or nothing.
111
+ let setter = match &body[..] {
112
+ [
113
+ Statement::IfStatement(branch),
114
+ Statement::ReturnStatement(returned),
115
+ ] => recognizer
116
+ .returns(returned.argument.as_ref(), value)
117
+ .then_some((branch, true)),
118
+ [Statement::IfStatement(branch)] => Some((branch, false)),
119
+ _ => None,
120
+ };
121
+ if let Some((branch, returns)) = setter
74
122
  && recognizer.guard(&branch.test, &HashSet::from_iter([value]))
75
123
  && let Some(other) = &branch.alternate
76
124
  && let Some(first) = recognizer.setter(&branch.consequent, object, value)
@@ -78,8 +126,8 @@ pub(super) fn recognize(
78
126
  && first == second
79
127
  {
80
128
  return Some(match first {
81
- Member::Pc => Helper::Pc,
82
- _ => Helper::Key,
129
+ Member::Pc => Helper::Pc { returns },
130
+ _ => Helper::Key { returns },
83
131
  });
84
132
  }
85
133
  let mut locals = HashSet::from_iter([value]);
@@ -99,10 +147,69 @@ fn constant(member: Member) -> bool {
99
147
  /// Resolves the helper's references in the script declaring it.
100
148
  struct Recognizer<'s> {
101
149
  script: Script<'s>,
102
- access: Access<'s>,
103
150
  }
104
151
 
105
152
  impl Recognizer<'_> {
153
+ /// Recognizes the reader and writer wrappers that [`recognize`] describes.
154
+ fn wrapper(
155
+ &self,
156
+ accessors: Accessors<'_>,
157
+ branch: &IfStatement<'_>,
158
+ parameters: &[SymbolId],
159
+ ) -> Option<Helper> {
160
+ let script = self.script;
161
+ let alternate = branch.alternate.as_ref()?;
162
+ // The callee `statement` passes the parameters to unchanged.
163
+ let forwarded = |statement: &Statement<'_>, returned: bool| {
164
+ let (callee, arguments) = call_statement(only(statement), returned)?;
165
+ let unchanged = arguments.len() == parameters.len()
166
+ && arguments
167
+ .iter()
168
+ .zip(parameters)
169
+ .all(|(argument, parameter)| {
170
+ (argument.as_expression())
171
+ .is_some_and(|argument| script.refers_to(argument, *parameter))
172
+ });
173
+ unchanged.then(|| script.symbol(callee))
174
+ };
175
+ let symbol = |function: &Function<'_>| Some(function.id.as_ref().map(|id| id.symbol_id()));
176
+ match *parameters {
177
+ [registers, index] => {
178
+ let slot = |expression: &Expression<'_>, member| {
179
+ let Some(MemberExpression::ComputedMemberExpression(element)) =
180
+ expression.without_parentheses().as_member_expression()
181
+ else {
182
+ return false;
183
+ };
184
+ script.refers_to(&element.expression, index)
185
+ && element
186
+ .object
187
+ .as_member_expression()
188
+ .is_some_and(|slots| self.member(slots, registers) == Some(member))
189
+ };
190
+ let Expression::BinaryExpression(test) = branch.test.without_parentheses() else {
191
+ return None;
192
+ };
193
+ let tagged = test.operator == BinaryOperator::StrictEquality
194
+ && ((ast::number(&test.left).is_some() && slot(&test.right, Member::Tags))
195
+ || (slot(&test.left, Member::Tags) && ast::number(&test.right).is_some()));
196
+ let Statement::ReturnStatement(stored) = only(&branch.consequent) else {
197
+ return None;
198
+ };
199
+ let stored = stored
200
+ .argument
201
+ .as_ref()
202
+ .is_some_and(|stored| slot(stored, Member::Values));
203
+ let reads = forwarded(alternate, true) == symbol(accessors.read);
204
+ (tagged && stored && reads).then_some(Helper::Read)
205
+ }
206
+ [_, _, _] => (forwarded(&branch.consequent, false).is_some()
207
+ && forwarded(alternate, false) == symbol(accessors.write))
208
+ .then_some(Helper::Write),
209
+ _ => None,
210
+ }
211
+ }
212
+
106
213
  fn returns(&self, returned: Option<&Expression<'_>>, value: SymbolId) -> bool {
107
214
  returned.is_some_and(|returned| mix32::is_symbol(self.script, returned, value))
108
215
  }
@@ -111,7 +218,7 @@ impl Recognizer<'_> {
111
218
  if !mix32::is_symbol(self.script, member.object(), object) {
112
219
  return None;
113
220
  }
114
- self.access.member(ast::member_str(member)?)
221
+ Member::of(ast::member_str(member)?)
115
222
  }
116
223
 
117
224
  fn target_member(&self, target: &AssignmentTarget<'_>, object: SymbolId) -> Option<Member> {
@@ -270,6 +377,46 @@ impl Recognizer<'_> {
270
377
  }
271
378
  }
272
379
 
380
+ /// The statement a block of one statement holds, or else `statement`.
381
+ fn only<'b, 'a>(statement: &'b Statement<'a>) -> &'b Statement<'a> {
382
+ match statement {
383
+ Statement::BlockStatement(block) if let [statement] = &block.body[..] => only(statement),
384
+ statement => statement,
385
+ }
386
+ }
387
+
388
+ /// Matches `<callee>(<arguments>);`, or `return <callee>(<arguments>);` if `returned`.
389
+ fn call_statement<'b, 'a>(
390
+ statement: &'b Statement<'a>,
391
+ returned: bool,
392
+ ) -> Option<(&'b IdentifierReference<'a>, &'b [Argument<'a>])> {
393
+ let expression = match statement {
394
+ Statement::ExpressionStatement(statement) if !returned => &statement.expression,
395
+ Statement::ReturnStatement(statement) if returned => statement.argument.as_ref()?,
396
+ _ => return None,
397
+ };
398
+ ast::direct_call(expression.without_parentheses())
399
+ }
400
+
401
+ /// Whether every return in `body`, outside nested functions, returns nothing.
402
+ fn returns_nothing(body: &FunctionBody<'_>) -> bool {
403
+ struct Returns(bool);
404
+
405
+ impl<'a> Visit<'a> for Returns {
406
+ fn visit_return_statement(&mut self, statement: &ReturnStatement<'a>) {
407
+ self.0 |= statement.argument.is_some();
408
+ }
409
+
410
+ fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
411
+
412
+ fn visit_arrow_function_expression(&mut self, _: &ArrowFunctionExpression<'a>) {}
413
+ }
414
+
415
+ let mut returns = Returns(false);
416
+ returns.visit_function_body(body);
417
+ !returns.0
418
+ }
419
+
273
420
  #[cfg(test)]
274
421
  pub(super) mod tests {
275
422
  use super::*;
@@ -357,35 +504,111 @@ pub(super) mod tests {
357
504
  #[test]
358
505
  fn native_bookkeeping_matches_interpretation() {
359
506
  for source in [
360
- "function f(a,b) { var x,y; x = a.J ^ (b & 65535); y = x * 403; if (a.i === 1) { a.h ^= y; } if (1 === a.l) { a.j ^= y; } a.state = x; a.J = y; }",
361
- "function f(a,b) { if (typeof b === 'number' && (b | 0) === b && (b !== 0 || 1/b > 0)) { a.i=1; a.h=b^a.state; } else { a.i=0; a.v=b; } return b; }",
362
- "function f(a,b) { if ('number' === typeof b && b === (0 | b) && (b !== 0 || 1/b > 0)) { a.l=1; a.j=a.state^b; } else { a.l=0; a.u=b; } return b; }",
507
+ "function f(a,b) { var x,y; x = a.L ^ (b & 65535); y = x * 403; if (a.j === 1) { a.i ^= y; } if (1 === a.m) { a.l ^= y; } a.h = x; a.L = y; }",
508
+ "function f(a,b) { if (typeof b === 'number' && (b | 0) === b && (b !== 0 || 1/b > 0)) { a.j=1; a.i=b^a.h; } else { a.j=0; a.A=b; } return b; }",
509
+ "function f(a,b) { if ('number' === typeof b && b === (0 | b) && (b !== 0 || 1/b > 0)) { a.m=1; a.l=a.h^b; } else { a.m=0; a.v=b; } return b; }",
363
510
  "function f(b) { if (typeof b !== 'number') throw new TypeError; return b; }",
364
511
  ] {
365
512
  assert_native_matches_interpreted(source, |script, function| {
366
- recognize(script, function, Access::default()).is_some()
513
+ recognize(script, None, function).is_some()
367
514
  });
368
515
  }
369
516
  }
370
517
 
518
+ /// The run function names the reader and writer. Wrappers are accessors only if they forward
519
+ /// to them unchanged.
520
+ #[test]
521
+ fn recognizes_accessors_and_their_wrappers() {
522
+ let source = "function read(r,i) { return r.H[i]; }\
523
+ function write(r,i,v) { if (i === r.D) pc(r,v); else r.H[i] = v; }\
524
+ function save(r,i) { if (-4 === r.J[i]) { return r.H[i]; } else { return read(r,i); } }\
525
+ function restore(r,i,v) { if (typeof v === 'object' && has(v)) { closure(r,i,v); } else { write(r,i,v); } }\
526
+ function swapped(r,i) { if (-4 === r.J[i]) { return r.H[i]; } else { return read(i,r); } }\
527
+ function other(r,i) { if (-4 === r.J[i]) { return r.J[i]; } else { return read(r,i); } }\
528
+ function sideways(r,i,v) { if (v) { closure(r,i,v); } else { write(r,v,i); } }\
529
+ function returning(r,i,v) { write(r,i,v); return v; }";
530
+ let parsed = ParsedScript::parse("t", source).unwrap();
531
+ let script = parsed.script();
532
+ let session = Session::empty(script);
533
+ let accessors = Accessors {
534
+ read: session.declared("read"),
535
+ write: session.declared("write"),
536
+ };
537
+ let recognized =
538
+ |name: &str| match recognize(script, Some(accessors), session.declared(name)) {
539
+ Some(Helper::Read) => "read",
540
+ Some(Helper::Write) => "write",
541
+ Some(_) => "other",
542
+ None => "none",
543
+ };
544
+ for (name, expected) in [
545
+ ("read", "read"),
546
+ ("write", "write"),
547
+ ("save", "read"),
548
+ ("restore", "write"),
549
+ ("swapped", "none"),
550
+ ("other", "none"),
551
+ ("sideways", "none"),
552
+ ("returning", "none"),
553
+ ] {
554
+ assert_eq!(recognized(name), expected, "{name}");
555
+ }
556
+ let returns = Accessors {
557
+ write: session.declared("returning"),
558
+ ..accessors
559
+ };
560
+ assert!(
561
+ recognize(script, Some(returns), session.declared("returning")).is_none(),
562
+ "a writer that returns a value is not the native writer"
563
+ );
564
+ }
565
+
566
+ /// Writing through the native writer returns nothing, and reading returns what it stored.
567
+ #[test]
568
+ fn native_accessors_access_registers() {
569
+ let source = "function read(r,i) { return r.H[i]; }\
570
+ function write(r,i,v) { if (typeof v === 'number') r.H[i] = v; }";
571
+ let parsed = ParsedScript::parse("t", source).unwrap();
572
+ let script = parsed.script();
573
+ let mut session = Session::empty(script);
574
+ session.accessors = Some(Accessors {
575
+ read: session.declared("read"),
576
+ write: session.declared("write"),
577
+ });
578
+ session.intrinsics = intrinsics(&session);
579
+ let mut run = Run::method(&session);
580
+ let function = |name| Callable::Script {
581
+ name: None,
582
+ function: session.declared(name),
583
+ scope: None,
584
+ };
585
+ let symbolic = Value::symbolic(Term::Reg(7));
586
+ let arguments = vec![Value::Registers, Value::Number(5.0), symbolic.clone()];
587
+ let written = run.invoke(&function("write"), Value::Undefined, arguments);
588
+ assert_eq!(
589
+ written.ok().map(|value| value.expr()),
590
+ Some(Value::Undefined.expr())
591
+ );
592
+ let arguments = vec![Value::Registers, Value::Number(5.0)];
593
+ let read = run.invoke(&function("read"), Value::Undefined, arguments);
594
+ assert_eq!(read.ok().map(|value| value.expr()), Some(symbolic.expr()));
595
+ }
596
+
371
597
  #[test]
372
598
  fn observable_work_is_never_skipped() {
373
599
  for source in [
374
- "function f(a,b) { a.state = sideEffect(b); }",
375
- "function f(a,b) { a.h = b; }",
376
- "function f(a,b) { a.state = b; return b; }",
377
- "function f(a,b) { if (b) a.state = 0; }",
600
+ "function f(a,b) { a.h = sideEffect(b); }",
601
+ "function f(a,b) { a.i = b; }",
602
+ "function f(a,b) { a.h = b; return b; }",
603
+ "function f(a,b) { if (b) a.h = 0; }",
378
604
  "function f(a,b) { a.other = b; }",
379
605
  "function f(a,b) { b.x = 1; }",
380
- "function f(a,b) { a.h ||= b; }",
606
+ "function f(a,b) { a.i ||= b; }",
381
607
  ] {
382
608
  let parsed = ParsedScript::parse("t", source).expect("test sources parse");
383
609
  let script = parsed.script();
384
610
  for function in extract::functions(&script.program.body).values() {
385
- assert!(
386
- recognize(script, function, Access::default()).is_none(),
387
- "{source}"
388
- );
611
+ assert!(recognize(script, None, function).is_none(), "{source}");
389
612
  }
390
613
  }
391
614
  }
@@ -380,10 +380,6 @@ impl<'ast, 'session> Run<'ast, 'session> {
380
380
  self.session.script
381
381
  }
382
382
 
383
- pub(in crate::vm) fn register_access(&self) -> crate::vm::registers::Access<'ast> {
384
- self.session.register_access
385
- }
386
-
387
383
  /// The path this run took, or why the handler rejected its operands.
388
384
  pub(super) fn finish(mut self, outcome: Evaluation<'ast, Value<'ast>>) -> Result<Path, String> {
389
385
  self.session.spare.replace(mem::take(&mut self.spare));
@@ -8,14 +8,14 @@ use oxc::semantic::SymbolId;
8
8
 
9
9
  use super::HashMap;
10
10
  use super::dispatch::{Decoded, Dispatch};
11
- use super::extract::{self, RunMethod};
11
+ use super::extract::{self, RunFunction};
12
12
  use super::interpret::{self, Decrypted, Invocation, MAXIMUM_PATHS, NativeDispatch, Way};
13
13
  use super::output::{Cause, Failure, Instruction, Position};
14
14
  use super::session::Session;
15
15
  use super::value::Value;
16
16
  use crate::js::ast::Script;
17
17
 
18
- /// The VM class, enclosing function body, `run` method layout, opcode decoder, and statements
18
+ /// The VM class, enclosing function body, run function layout, opcode decoder, and statements
19
19
  /// executed before its scope is entered, in the script that defines them.
20
20
  pub struct Definition<'a> {
21
21
  pub(super) script: Script<'a>,
@@ -24,29 +24,28 @@ pub struct Definition<'a> {
24
24
  pub(super) prelude: Vec<&'a Statement<'a>>,
25
25
  /// Functions declared beside the class: handlers and their helpers.
26
26
  pub(super) functions: HashMap<SymbolId, &'a Function<'a>>,
27
- pub(super) run: RunMethod<'a>,
27
+ pub(super) run: RunFunction<'a>,
28
+ /// Where the program runner starts a program.
29
+ pub(super) entry: Position,
28
30
  dispatch: Dispatch<'a>,
29
31
  }
30
32
 
31
33
  impl<'a> Definition<'a> {
32
- /// Finds the VM in `script`, including a class method or a free-function dispatch loop.
34
+ /// Finds the VM in `script` from its program runner. Fails unless the run function's
35
+ /// dispatch loop has the expected shape.
33
36
  pub(crate) fn from_script(script: Script<'a>) -> Result<Self> {
34
- let extract::MachineScope {
35
- body,
36
- class,
37
- run,
38
- machine,
39
- } = extract::find(script).context("no VM class in the script")?;
40
- let functions = extract::functions(&body.statements);
41
- let run = extract::run_method(script, &functions, run, machine)?;
37
+ let found = extract::find(script).context("finding the VM")?;
38
+ let functions = extract::functions(&found.body.statements);
39
+ let run = extract::run_function(script, &functions, found.run)?;
42
40
  let dispatch = Dispatch::from_loop(script, &run.dispatch)?;
43
41
  Ok(Self {
44
42
  script,
45
- body,
46
- class,
47
- prelude: extract::prelude(script, body),
43
+ body: found.body,
44
+ class: found.class,
45
+ prelude: extract::prelude(script, found.body),
48
46
  functions,
49
47
  run,
48
+ entry: found.entry,
50
49
  dispatch,
51
50
  })
52
51
  }
@@ -180,58 +179,3 @@ impl<'definition> Machine<'definition> {
180
179
  Ok((opcode, ways.taken))
181
180
  }
182
181
  }
183
-
184
- #[cfg(test)]
185
- mod tests {
186
- use super::*;
187
- use crate::script::Source;
188
- use crate::vm::{Constant, Effect, Term};
189
- use base64::Engine;
190
- use base64::engine::general_purpose::STANDARD;
191
-
192
- #[test]
193
- fn disassembles_the_free_function_vm_capture() {
194
- check_free_function_vm(include_str!("fixtures/free_functions/machine.js"));
195
- }
196
-
197
- #[test]
198
- fn accepts_reversed_tag_comparison_and_dead_return_in_save_helper() {
199
- let source = include_str!("fixtures/free_functions/machine.js")
200
- .replace("T1.J[T2] === -4", "-4 === T1.J[T2]")
201
- .replace(
202
- "return Ch(T1, T2);\n\t\t\t\t}\n\t\t\t}",
203
- "return Ch(T1, T2);\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}",
204
- );
205
- assert!(source.contains("-4 === T1.J[T2]"));
206
- assert!(source.contains("return Ch(T1, T2);\n\t\t\t\t}\n\t\t\t\treturn;"));
207
- check_free_function_vm(&source);
208
- }
209
-
210
- fn check_free_function_vm(text: &str) {
211
- let source = Source::try_new(text).unwrap();
212
- let program = STANDARD
213
- .decode(include_str!("fixtures/free_functions/program.base64").trim())
214
- .unwrap();
215
- let machine = source.machine().unwrap();
216
- assert_eq!(machine.entry(), Position::new(0, 231));
217
- let listing = machine.disassemble(&program).unwrap();
218
- assert!(listing.failures.is_empty(), "{:?}", listing.failures);
219
- assert_eq!(listing.instructions.len(), 1704);
220
- assert_eq!(listing.functions.len(), 19);
221
- assert_eq!(
222
- listing.instructions[&0].paths[0].effects,
223
- vec![Effect::Set(
224
- 0,
225
- Term::Const(Constant::String("window".into()))
226
- )]
227
- );
228
- assert_eq!(
229
- listing
230
- .hidden
231
- .iter()
232
- .map(|branch| branch.string.as_str())
233
- .collect::<Vec<_>>(),
234
- ["function", "window"]
235
- );
236
- }
237
- }
@@ -233,11 +233,9 @@ pub struct Listing {
233
233
  pub functions: Vec<Position>,
234
234
  pub failures: Vec<Failure>,
235
235
  pub hidden: Vec<Hidden>,
236
- /// Encoded string loads whose decoded values now appear at their call sites. Feature
237
- /// extraction omits these inputs so ciphertext does not distort reference matching.
238
- pub decoded_inputs: BTreeMap<u32, BTreeSet<String>>,
239
- /// String decoder functions and their factories, whose scaffolding is not a probe feature.
240
- pub string_decoders: BTreeSet<u32>,
236
+ /// String constants the program passes to its own decoders, whose calls the listing shows
237
+ /// as loads of the decoded text.
238
+ pub encoded: BTreeSet<String>,
241
239
  }
242
240
 
243
241
  impl Term {
@@ -1,58 +1,19 @@
1
- //! The VM register file, whose `read` and `write` methods access 256 registers.
1
+ //! The VM register file, whose reader and writer functions access 256 registers.
2
2
  //!
3
3
  //! Each value has a type tag and is masked by a rolling `state`. A digest updated on every write
4
4
  //! determines the mask. Separate fields hold the program counter and dispatch key. Handlers access
5
- //! registers through methods, direct tag and value writes, or the counter and key fields.
5
+ //! registers through the reader and writer, direct tag and value writes, or the counter and key
6
+ //! fields.
6
7
  //!
7
8
  //! The interpreter keeps the mask and digest at zero. Stored values are therefore unmasked, and
8
9
  //! changing the mask has no effect. Member names match the current build.
9
10
 
10
11
  use crate::js;
11
- use oxc::ast::ast::Function;
12
12
 
13
- /// Register representations shipped with the method-based and free-function VMs.
14
- /// The same short field names have different meanings, so interpretation is per session.
15
- #[derive(Clone, Copy, Default)]
16
- pub(crate) enum Access<'a> {
17
- #[default]
18
- Methods,
19
- Functions {
20
- read: &'a Function<'a>,
21
- write: &'a Function<'a>,
22
- },
23
- }
24
-
25
- impl Access<'_> {
26
- pub(crate) fn member(self, name: &str) -> Option<Member> {
27
- use Member::*;
28
- Some(match (self, name) {
29
- (Self::Methods, "read" | "save") => Read,
30
- (Self::Methods, "write" | "restore") => Write,
31
- (Self::Methods, "state" | "digest" | "I" | "J")
32
- | (Self::Functions { .. }, "h" | "O" | "L" | "K") => Mask,
33
- (Self::Methods, "i" | "l") | (Self::Functions { .. }, "j" | "m") => Integer,
34
- (Self::Methods, "h") | (Self::Functions { .. }, "i") => Pc,
35
- (Self::Methods, "j") | (Self::Functions { .. }, "l") => Key,
36
- (Self::Methods, "v") | (Self::Functions { .. }, "A") => OtherPc,
37
- (Self::Methods, "u") | (Self::Functions { .. }, "v") => OtherKey,
38
- (Self::Methods, "C") | (Self::Functions { .. }, "D") => PcIndex,
39
- (Self::Methods, "D") | (Self::Functions { .. }, "F") => KeyIndex,
40
- (Self::Methods, "values") | (Self::Functions { .. }, "H") => Values,
41
- (Self::Methods, "H") | (Self::Functions { .. }, "J") => Tags,
42
- _ => return None,
43
- })
44
- }
45
- }
46
-
47
- /// Meaning of a field or method on the register file.
13
+ /// Meaning of a field on the register file.
48
14
  #[derive(Clone, Copy, Debug, PartialEq, Eq)]
49
15
  pub(crate) enum Member {
50
- /// Read methods: `read(index)` and `save(index)`. They differ only for closures.
51
- Read,
52
- /// Write methods: `write(index, value)` and `restore(index, value)`. They differ only for
53
- /// closures.
54
- Write,
55
- /// Rolling mask or the digest used to compute it.
16
+ /// Rolling mask, the digest used to compute it, or the digest's halves.
56
17
  Mask,
57
18
  /// Flag indicating whether the program counter or dispatch key holds a masked integer.
58
19
  Integer,
@@ -68,6 +29,25 @@ pub(crate) enum Member {
68
29
  Tags,
69
30
  }
70
31
 
32
+ impl Member {
33
+ #[must_use]
34
+ pub(crate) fn of(name: &str) -> Option<Self> {
35
+ Some(match name {
36
+ "h" | "O" | "K" | "L" => Self::Mask,
37
+ "j" | "m" => Self::Integer,
38
+ "i" => Self::Pc,
39
+ "l" => Self::Key,
40
+ "A" => Self::OtherPc,
41
+ "v" => Self::OtherKey,
42
+ "D" => Self::PcIndex,
43
+ "F" => Self::KeyIndex,
44
+ "H" => Self::Values,
45
+ "J" => Self::Tags,
46
+ _ => return None,
47
+ })
48
+ }
49
+ }
50
+
71
51
  /// Arrays storing register values and tags at matching indices.
72
52
  #[derive(Clone, Copy, Debug, PartialEq, Eq)]
73
53
  pub(crate) enum Slots {
@@ -75,14 +55,14 @@ pub(crate) enum Slots {
75
55
  Tags,
76
56
  }
77
57
 
78
- /// Decoding rule used by `read` for a value with `tag`, assuming a zero mask.
58
+ /// Decoding rule used by the reader for a value with `tag`, assuming a zero mask.
79
59
  #[derive(Clone, Copy, Debug, PartialEq, Eq)]
80
60
  pub(crate) enum Stored {
81
61
  /// Tag 1 stores an integer XORed with the tag's upper bits.
82
62
  Integer(i32),
83
63
  /// Tag 2 stores a boolean as a comparison with the tag's upper bits.
84
64
  Boolean(i32),
85
- /// Tag -4 stores an encrypted string record, decrypted by `read`.
65
+ /// Tag -4 stores an encrypted string record, decrypted by the reader.
86
66
  Record,
87
67
  /// All other tags store the value directly.
88
68
  Plain,