cfsolve 0.1.3 → 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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -0
  3. data/README.md +30 -5
  4. data/UPSTREAM.md +24 -3
  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 +21 -10
  9. data/vendor/cfsolve/examples/listing_dump.rs +24 -0
  10. data/vendor/cfsolve/examples/probe_similarity.rs +37 -0
  11. data/vendor/cfsolve/examples/rebase_reference.rs +96 -0
  12. data/vendor/cfsolve/examples/reread_capture.rs +31 -0
  13. data/vendor/cfsolve/examples/turnstile_smoke.rs +79 -0
  14. data/vendor/cfsolve/profiles/safari-26-ios/answers.json +805 -797
  15. data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +909 -899
  16. data/vendor/cfsolve/src/capture/sessions.rs +88 -4
  17. data/vendor/cfsolve/src/capture.rs +1 -0
  18. data/vendor/cfsolve/src/deobfuscate/ast.rs +16 -0
  19. data/vendor/cfsolve/src/deobfuscate/control_flow.rs +1 -1
  20. data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +33 -0
  21. data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +24 -4
  22. data/vendor/cfsolve/src/embedding_page/extra_params.rs +3 -3
  23. data/vendor/cfsolve/src/embedding_page/page.rs +76 -65
  24. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +7 -4
  25. data/vendor/cfsolve/src/stage2/benchmark.rs +29 -8
  26. data/vendor/cfsolve/src/stage2/call_stack.rs +134 -95
  27. data/vendor/cfsolve/src/stage2/challenge_page.rs +10 -3
  28. data/vendor/cfsolve/src/stage2/probes/parent_info.rs +345 -61
  29. data/vendor/cfsolve/src/stage2/probes/performance.rs +42 -5
  30. data/vendor/cfsolve/src/stage2/probes/stack.rs +41 -0
  31. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +11 -2
  32. data/vendor/cfsolve/src/stage2/reference.json +1 -1
  33. data/vendor/cfsolve/src/stage2/reference.rs +70 -110
  34. data/vendor/cfsolve/src/stage2/registry.rs +4 -3
  35. data/vendor/cfsolve/src/stage2/trace.rs +1 -1
  36. data/vendor/cfsolve/src/vm/dataflow.rs +15 -6
  37. data/vendor/cfsolve/src/vm/disassemble.rs +2 -1
  38. data/vendor/cfsolve/src/vm/encoded_strings.rs +1553 -0
  39. data/vendor/cfsolve/src/vm/extract.rs +205 -152
  40. data/vendor/cfsolve/src/vm/interpret/access.rs +0 -2
  41. data/vendor/cfsolve/src/vm/interpret/builtin.rs +0 -13
  42. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +30 -7
  43. data/vendor/cfsolve/src/vm/interpret/mix32.rs +216 -86
  44. data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +42 -21
  45. data/vendor/cfsolve/src/vm/interpret/pattern.rs +268 -67
  46. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +261 -30
  47. data/vendor/cfsolve/src/vm/machine.rs +14 -12
  48. data/vendor/cfsolve/src/vm/output.rs +4 -1
  49. data/vendor/cfsolve/src/vm/registers.rs +17 -23
  50. data/vendor/cfsolve/src/vm/session.rs +9 -4
  51. data/vendor/cfsolve/src/vm.rs +1 -0
  52. metadata +7 -1
@@ -1,39 +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};
18
+ use crate::vm::HashSet;
19
+ use crate::vm::extract::{self, Accessors};
13
20
  use crate::vm::registers::Member;
14
- use crate::vm::{HashSet, extract};
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
 
24
- pub(super) fn recognize(script: Script<'_>, function: &Function<'_>) -> Option<Helper> {
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.
51
+ pub(super) fn recognize(
52
+ script: Script<'_>,
53
+ accessors: Option<Accessors<'_>>,
54
+ function: &Function<'_>,
55
+ ) -> Option<Helper> {
25
56
  if function.r#async || function.generator {
26
57
  return None;
27
58
  }
28
- let parameters: Vec<SymbolId> = extract::parameters(&function.params)?
29
- .into_iter()
30
- .map(|parameter| parameter.symbol_id())
31
- .collect();
32
59
  let body = function.body.as_deref()?;
33
60
  if !body.directives.is_empty() {
34
61
  return None;
35
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
+ }
71
+ let parameters: Vec<SymbolId> = extract::parameters(&function.params)?
72
+ .into_iter()
73
+ .map(|parameter| parameter.symbol_id())
74
+ .collect();
36
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);
81
+ }
37
82
  let body = &body.statements;
38
83
  if let [value] = parameters[..]
39
84
  && let [
@@ -62,11 +107,18 @@ pub(super) fn recognize(script: Script<'_>, function: &Function<'_>) -> Option<H
62
107
  if object == value {
63
108
  return None;
64
109
  }
65
- if let [
66
- Statement::IfStatement(branch),
67
- Statement::ReturnStatement(returned),
68
- ] = &body[..]
69
- && 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
70
122
  && recognizer.guard(&branch.test, &HashSet::from_iter([value]))
71
123
  && let Some(other) = &branch.alternate
72
124
  && let Some(first) = recognizer.setter(&branch.consequent, object, value)
@@ -74,8 +126,8 @@ pub(super) fn recognize(script: Script<'_>, function: &Function<'_>) -> Option<H
74
126
  && first == second
75
127
  {
76
128
  return Some(match first {
77
- Member::Pc => Helper::Pc,
78
- _ => Helper::Key,
129
+ Member::Pc => Helper::Pc { returns },
130
+ _ => Helper::Key { returns },
79
131
  });
80
132
  }
81
133
  let mut locals = HashSet::from_iter([value]);
@@ -98,6 +150,66 @@ struct Recognizer<'s> {
98
150
  }
99
151
 
100
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
+
101
213
  fn returns(&self, returned: Option<&Expression<'_>>, value: SymbolId) -> bool {
102
214
  returned.is_some_and(|returned| mix32::is_symbol(self.script, returned, value))
103
215
  }
@@ -265,6 +377,46 @@ impl Recognizer<'_> {
265
377
  }
266
378
  }
267
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
+
268
420
  #[cfg(test)]
269
421
  pub(super) mod tests {
270
422
  use super::*;
@@ -352,32 +504,111 @@ pub(super) mod tests {
352
504
  #[test]
353
505
  fn native_bookkeeping_matches_interpretation() {
354
506
  for source in [
355
- "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; }",
356
- "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; }",
357
- "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; }",
358
510
  "function f(b) { if (typeof b !== 'number') throw new TypeError; return b; }",
359
511
  ] {
360
512
  assert_native_matches_interpreted(source, |script, function| {
361
- recognize(script, function).is_some()
513
+ recognize(script, None, function).is_some()
362
514
  });
363
515
  }
364
516
  }
365
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
+
366
597
  #[test]
367
598
  fn observable_work_is_never_skipped() {
368
599
  for source in [
369
- "function f(a,b) { a.state = sideEffect(b); }",
370
- "function f(a,b) { a.h = b; }",
371
- "function f(a,b) { a.state = b; return b; }",
372
- "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; }",
373
604
  "function f(a,b) { a.other = b; }",
374
605
  "function f(a,b) { b.x = 1; }",
375
- "function f(a,b) { a.h ||= b; }",
606
+ "function f(a,b) { a.i ||= b; }",
376
607
  ] {
377
608
  let parsed = ParsedScript::parse("t", source).expect("test sources parse");
378
609
  let script = parsed.script();
379
610
  for function in extract::functions(&script.program.body).values() {
380
- assert!(recognize(script, function).is_none(), "{source}");
611
+ assert!(recognize(script, None, function).is_none(), "{source}");
381
612
  }
382
613
  }
383
614
  }
@@ -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,26 +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`. Fails unless a class has a `run` method whose dispatch loop has
33
- /// the expected shape.
34
+ /// Finds the VM in `script` from its program runner. Fails unless the run function's
35
+ /// dispatch loop has the expected shape.
34
36
  pub(crate) fn from_script(script: Script<'a>) -> Result<Self> {
35
- let (body, class) = extract::find(script).context("no VM class in the script")?;
36
- let run = extract::method(class, "run").context("the VM class has no run method")?;
37
- let functions = extract::functions(&body.statements);
38
- let run = extract::run_method(script, &functions, run)?;
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)?;
39
40
  let dispatch = Dispatch::from_loop(script, &run.dispatch)?;
40
41
  Ok(Self {
41
42
  script,
42
- body,
43
- class,
44
- prelude: extract::prelude(script, body),
43
+ body: found.body,
44
+ class: found.class,
45
+ prelude: extract::prelude(script, found.body),
45
46
  functions,
46
47
  run,
48
+ entry: found.entry,
47
49
  dispatch,
48
50
  })
49
51
  }
@@ -1,7 +1,7 @@
1
1
  //! Disassembly values, effects, and exits for each feasible handler path. Byte-dependent values
2
2
  //! are concrete; register-dependent values remain expressions.
3
3
 
4
- use std::collections::BTreeMap;
4
+ use std::collections::{BTreeMap, BTreeSet};
5
5
  use std::error::Error;
6
6
  use std::fmt::{self, Display, Formatter};
7
7
  use std::iter;
@@ -233,6 +233,9 @@ pub struct Listing {
233
233
  pub functions: Vec<Position>,
234
234
  pub failures: Vec<Failure>,
235
235
  pub hidden: Vec<Hidden>,
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>,
236
239
  }
237
240
 
238
241
  impl Term {
@@ -1,23 +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
12
 
12
- /// Meaning of a field or method on the register file.
13
+ /// Meaning of a field on the register file.
13
14
  #[derive(Clone, Copy, Debug, PartialEq, Eq)]
14
15
  pub(crate) enum Member {
15
- /// Read methods: `read(index)` and `save(index)`. They differ only for closures.
16
- Read,
17
- /// Write methods: `write(index, value)` and `restore(index, value)`. They differ only for
18
- /// closures.
19
- Write,
20
- /// Rolling mask or the digest used to compute it.
16
+ /// Rolling mask, the digest used to compute it, or the digest's halves.
21
17
  Mask,
22
18
  /// Flag indicating whether the program counter or dispatch key holds a masked integer.
23
19
  Integer,
@@ -37,18 +33,16 @@ impl Member {
37
33
  #[must_use]
38
34
  pub(crate) fn of(name: &str) -> Option<Self> {
39
35
  Some(match name {
40
- "read" | "save" => Self::Read,
41
- "write" | "restore" => Self::Write,
42
- "state" | "digest" | "I" | "J" => Self::Mask,
43
- "i" | "l" => Self::Integer,
44
- "h" => Self::Pc,
45
- "j" => Self::Key,
46
- "v" => Self::OtherPc,
47
- "u" => Self::OtherKey,
48
- "C" => Self::PcIndex,
49
- "D" => Self::KeyIndex,
50
- "values" => Self::Values,
51
- "H" => Self::Tags,
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,
52
46
  _ => return None,
53
47
  })
54
48
  }
@@ -61,14 +55,14 @@ pub(crate) enum Slots {
61
55
  Tags,
62
56
  }
63
57
 
64
- /// 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.
65
59
  #[derive(Clone, Copy, Debug, PartialEq, Eq)]
66
60
  pub(crate) enum Stored {
67
61
  /// Tag 1 stores an integer XORed with the tag's upper bits.
68
62
  Integer(i32),
69
63
  /// Tag 2 stores a boolean as a comparison with the tag's upper bits.
70
64
  Boolean(i32),
71
- /// Tag -4 stores an encrypted string record, decrypted by `read`.
65
+ /// Tag -4 stores an encrypted string record, decrypted by the reader.
72
66
  Record,
73
67
  /// All other tags store the value directly.
74
68
  Plain,
@@ -17,7 +17,7 @@ use oxc::ast::ast::{
17
17
  use oxc::semantic::SymbolId;
18
18
  use oxc::syntax::operator::BinaryOperator;
19
19
 
20
- use super::extract::{self, RunMethod};
20
+ use super::extract::{self, Accessors, RunFunction};
21
21
  use super::interpret::{self, Branching, Frozen, Intrinsics, Plans, Run, Spare, StringDecoder};
22
22
  use super::machine::Definition;
23
23
  use super::output::{Constant, Position, Term};
@@ -52,6 +52,8 @@ pub(crate) struct Session<'ast> {
52
52
  pub initial: Vec<(u8, Value<'ast>)>,
53
53
  /// Function used to decrypt string records stored in registers.
54
54
  pub record_decoder: Option<&'ast Function<'ast>>,
55
+ /// The register file's reader and writer, which the run function calls.
56
+ pub accessors: Option<Accessors<'ast>>,
55
57
  /// Script functions run natively.
56
58
  pub intrinsics: Intrinsics,
57
59
  /// The record decoder, run natively, if it has the recognized shape.
@@ -90,17 +92,19 @@ impl<'ast> Session<'ast> {
90
92
  ref prelude,
91
93
  ref functions,
92
94
  ref run,
95
+ entry,
93
96
  ..
94
97
  } = definition;
95
98
  let constructor = extract::constructor(class)?;
96
99
  let handlers = extract::handlers(script, functions, &run.dispatch)?;
97
- let record_decoder = extract::record_decoder(script, body)
100
+ let record_decoder = extract::record_decoder(script, run.accessors.read)
98
101
  .and_then(|symbol| functions.get(&symbol).copied());
99
102
  let mut session = Self {
100
103
  functions: functions.clone(),
101
104
  handlers,
102
- entry: extract::entry(body)?,
105
+ entry,
103
106
  record_decoder,
107
+ accessors: Some(run.accessors),
104
108
  ..Self::unevaluated(script, constructor)
105
109
  };
106
110
  (session.bindings, session.unavailable) =
@@ -137,6 +141,7 @@ impl<'ast> Session<'ast> {
137
141
  symbol_fields: Vec::new(),
138
142
  initial: Vec::new(),
139
143
  record_decoder: None,
144
+ accessors: None,
140
145
  intrinsics: Intrinsics::default(),
141
146
  string_decoder: None,
142
147
  fields: HashMap::default(),
@@ -434,7 +439,7 @@ fn concrete<'a>(mut fields: HashMap<String, Value<'a>>) -> HashMap<String, Value
434
439
  /// zero for the mask during analysis.
435
440
  fn recover_state<'a>(
436
441
  session: &mut Session<'a>,
437
- names: &RunMethod<'_>,
442
+ names: &RunFunction<'_>,
438
443
  constructed: &Constructed<'a>,
439
444
  ) -> Result<()> {
440
445
  let fields = &constructed.fields;
@@ -14,6 +14,7 @@ mod convert;
14
14
  pub(crate) mod dataflow;
15
15
  mod disassemble;
16
16
  mod dispatch;
17
+ mod encoded_strings;
17
18
  mod extract;
18
19
  mod interpret;
19
20
  mod machine;
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfsolve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Morgan
@@ -593,11 +593,16 @@ files:
593
593
  - vendor/cfsolve/examples/compare_session.rs
594
594
  - vendor/cfsolve/examples/disassemble.rs
595
595
  - vendor/cfsolve/examples/fill_profile.rs
596
+ - vendor/cfsolve/examples/listing_dump.rs
596
597
  - vendor/cfsolve/examples/probe_plan.rs
598
+ - vendor/cfsolve/examples/probe_similarity.rs
599
+ - vendor/cfsolve/examples/rebase_reference.rs
600
+ - vendor/cfsolve/examples/reread_capture.rs
597
601
  - vendor/cfsolve/examples/solver_profile.rs
598
602
  - vendor/cfsolve/examples/solver_smoke.rs
599
603
  - vendor/cfsolve/examples/stage2_snapshot.rs
600
604
  - vendor/cfsolve/examples/tls_echo.rs
605
+ - vendor/cfsolve/examples/turnstile_smoke.rs
601
606
  - vendor/cfsolve/examples/vm_snapshot.rs
602
607
  - vendor/cfsolve/examples/vocabulary.rs
603
608
  - vendor/cfsolve/examples/write_order_check.rs
@@ -784,6 +789,7 @@ files:
784
789
  - vendor/cfsolve/src/vm/dataflow.rs
785
790
  - vendor/cfsolve/src/vm/disassemble.rs
786
791
  - vendor/cfsolve/src/vm/dispatch.rs
792
+ - vendor/cfsolve/src/vm/encoded_strings.rs
787
793
  - vendor/cfsolve/src/vm/extract.rs
788
794
  - vendor/cfsolve/src/vm/interpret.rs
789
795
  - vendor/cfsolve/src/vm/interpret/access.rs