cfsolve 0.1.3 → 0.1.4

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 (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +8 -3
  4. data/UPSTREAM.md +8 -2
  5. data/lib/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
  6. data/lib/cfsolve/profiles/chrome-157-macos.json +613 -0
  7. data/lib/cfsolve/version.rb +2 -2
  8. data/lib/cfsolve.rb +1 -1
  9. data/vendor/cfsolve/README.md +53 -5
  10. data/vendor/cfsolve/examples/turnstile_smoke.rs +58 -0
  11. data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
  12. data/vendor/cfsolve/profiles/chrome-157-macos.json +613 -0
  13. data/vendor/cfsolve/src/embedding_page/extra_params.rs +110 -7
  14. data/vendor/cfsolve/src/embedding_page/page.rs +41 -1
  15. data/vendor/cfsolve/src/session/initial.rs +7 -6
  16. data/vendor/cfsolve/src/session/probes.rs +1 -0
  17. data/vendor/cfsolve/src/stage2/analysis/analyze.rs +33 -4
  18. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +13 -2
  19. data/vendor/cfsolve/src/stage2/benchmark.rs +59 -18
  20. data/vendor/cfsolve/src/stage2/call_stack.rs +169 -36
  21. data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +1 -0
  22. data/vendor/cfsolve/src/stage2/probes/dates.rs +44 -0
  23. data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +326 -0
  24. data/vendor/cfsolve/src/stage2/probes/stack.rs +117 -4
  25. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +80 -2
  26. data/vendor/cfsolve/src/stage2/probes.rs +1 -0
  27. data/vendor/cfsolve/src/stage2/reference.rs +5 -0
  28. data/vendor/cfsolve/src/stage2/registry.rs +8 -2
  29. data/vendor/cfsolve/src/stage2/replay.rs +34 -3
  30. data/vendor/cfsolve/src/stage2/stylesheets.rs +35 -0
  31. data/vendor/cfsolve/src/stage2/value_labels.rs +185 -0
  32. data/vendor/cfsolve/src/stage2/vocabulary.rs +111 -6
  33. data/vendor/cfsolve/src/stage2.rs +1 -0
  34. data/vendor/cfsolve/src/vm/disassemble.rs +68 -4
  35. data/vendor/cfsolve/src/vm/encoded_names.rs +635 -0
  36. data/vendor/cfsolve/src/vm/extract.rs +246 -43
  37. data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +9 -0
  38. data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +7438 -0
  39. data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +1 -0
  40. data/vendor/cfsolve/src/vm/interpret/access.rs +8 -2
  41. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +39 -1
  42. data/vendor/cfsolve/src/vm/interpret/pattern.rs +33 -4
  43. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +14 -6
  44. data/vendor/cfsolve/src/vm/interpret/run.rs +4 -0
  45. data/vendor/cfsolve/src/vm/machine.rs +63 -5
  46. data/vendor/cfsolve/src/vm/output.rs +6 -1
  47. data/vendor/cfsolve/src/vm/registers.rs +35 -21
  48. data/vendor/cfsolve/src/vm/session.rs +6 -3
  49. data/vendor/cfsolve/src/vm.rs +1 -0
  50. metadata +13 -1
@@ -20,9 +20,10 @@ pub use super::audit_globals::{Globals, Signature};
20
20
  use super::probes::worker_navigator;
21
21
  use super::records::{ArrangedFields, Probe, ProbePlan};
22
22
  use super::reference::{
23
- ENVIRONMENT_AUDIT_DECODED, ENVIRONMENT_AUDIT_ENCODED, ENVIRONMENT_AUDIT_GROUPS,
24
- ENVIRONMENT_AUDIT_REDUCTION, ENVIRONMENT_AUDIT_VALUES, ENVIRONMENT_REFERRER, REFERENCE,
25
- STYLESHEETS_HASH, WORKER_NAVIGATOR_RESULT,
23
+ DOCUMENT_CUSTOM_ELEMENTS, DOCUMENT_ROOT_ATTRIBUTES, ENVIRONMENT_AUDIT_DECODED,
24
+ ENVIRONMENT_AUDIT_ENCODED, ENVIRONMENT_AUDIT_GROUPS, ENVIRONMENT_AUDIT_REDUCTION,
25
+ ENVIRONMENT_AUDIT_VALUES, ENVIRONMENT_REFERRER, REFERENCE, STYLESHEETS_HASH,
26
+ WORKER_NAVIGATOR_RESULT,
26
27
  };
27
28
  use super::registry::{AUDIT_PROBE, Kind, Operation, Treatment};
28
29
  pub use super::stylesheets::Stylesheet;
@@ -297,6 +298,8 @@ pub struct Session<'session> {
297
298
  pub controller: Option<&'session str>,
298
299
  /// Definitions in the current iframe, used to translate audited globals.
299
300
  pub globals: &'session Globals,
301
+ /// Current parent-page values, shared with the loader's page-signals message.
302
+ pub(crate) parent: Option<&'session crate::embedding_page::page::PageSignals>,
300
303
  }
301
304
 
302
305
  impl Session<'_> {
@@ -311,6 +314,9 @@ impl Session<'_> {
311
314
  for value in fields.values_mut() {
312
315
  refresh_transplant(value, self.iframe_url, &self.modified);
313
316
  }
317
+ if let Some(parent) = self.parent {
318
+ refresh_parent_page(fields, &parent.root_attributes, &parent.custom_element_tags);
319
+ }
314
320
  if let Some(referrer) = fields.get_mut(ENVIRONMENT_REFERRER) {
315
321
  *referrer = json!(self.referrer);
316
322
  }
@@ -355,6 +361,18 @@ impl Session<'_> {
355
361
  }
356
362
  }
357
363
 
364
+ /// The profile's capture page is unrelated to the page currently embedding the widget.
365
+ fn refresh_parent_page(fields: &mut JsObject, attributes: &[String], elements: &[String]) {
366
+ for (field, values) in [
367
+ (DOCUMENT_ROOT_ATTRIBUTES, attributes),
368
+ (DOCUMENT_CUSTOM_ELEMENTS, elements),
369
+ ] {
370
+ if let Some(value) = fields.get_mut(field) {
371
+ *value = json!(values);
372
+ }
373
+ }
374
+ }
375
+
358
376
  /// Finds the option naming the audit's Server-Timing metric: the first option name after
359
377
  /// `decodedBodySize` in the probe's strings, which some builds precede with `_cf_chl_opt`.
360
378
  fn timing_option<'f>(features: &'f [String], options: &Options) -> Option<&'f str> {
@@ -537,6 +555,19 @@ mod tests {
537
555
  use crate::challenge::installed::Installed;
538
556
  use crate::js;
539
557
 
558
+ #[test]
559
+ fn document_structure_uses_the_current_parent_page() {
560
+ let mut fields = JsObject::from_iter([
561
+ (DOCUMENT_ROOT_ATTRIBUTES.into(), json!([])),
562
+ (DOCUMENT_CUSTOM_ELEMENTS.into(), json!(["old-element"])),
563
+ ("other".into(), json!(true)),
564
+ ]);
565
+ refresh_parent_page(&mut fields, &["lang".into()], &[]);
566
+ assert_eq!(fields[DOCUMENT_ROOT_ATTRIBUTES], json!(["lang"]));
567
+ assert_eq!(fields[DOCUMENT_CUSTOM_ELEMENTS], json!([]));
568
+ assert_eq!(fields["other"], json!(true));
569
+ }
570
+
540
571
  #[test]
541
572
  fn audit_globals_keep_their_groups_under_current_names() {
542
573
  let mut groups: JsObject = js::from_value(json!({
@@ -16,6 +16,41 @@ use super::reference::{STYLESHEETS_HASH, STYLESHEETS_TEXT};
16
16
  use super::registry::Kind;
17
17
  use super::vocabulary;
18
18
  use crate::js::{JsObject, json};
19
+ use crate::listing::Index;
20
+ use crate::vm::Term;
21
+
22
+ /// The excluded sheet's serialized rules, followed by its numeric index. API loads can move
23
+ /// around the stored field, so recognize `rules.join("|") + index` directly.
24
+ pub(crate) fn text_field(index: &Index<'_>, entry: u32) -> Option<String> {
25
+ let code = index.reachable(entry);
26
+ let fields = index
27
+ .stores()
28
+ .iter()
29
+ .filter(|store| code.contains(&store.pc))
30
+ .filter_map(|store| {
31
+ let (at, Term::Binary(oxc::syntax::operator::BinaryOperator::Addition, left, _)) =
32
+ index.dereference(store.pc, &store.value)?
33
+ else {
34
+ return None;
35
+ };
36
+ let (
37
+ at,
38
+ Term::Call {
39
+ callee, arguments, ..
40
+ },
41
+ ) = index.dereference(at, &left)?
42
+ else {
43
+ return None;
44
+ };
45
+ let method = index.method(at, &callee);
46
+ (method.as_deref() == Some("join")
47
+ && arguments.len() == 1
48
+ && index.text(at, &arguments[0]).as_deref() == Some("|"))
49
+ .then(|| store.key.clone())
50
+ })
51
+ .collect::<std::collections::BTreeSet<_>>();
52
+ crate::candidates::exactly_one(fields, "excluded stylesheet text").ok()
53
+ }
19
54
 
20
55
  static NAME: LazyLock<Regex> =
21
56
  LazyLock::new(|| Regex::new(r"_[0-9a-f]{24}").expect("the session name pattern is valid"));
@@ -0,0 +1,185 @@
1
+ //! Session labels replacing stable values in newer fingerprint programs.
2
+
3
+ use std::collections::{BTreeMap, BTreeSet};
4
+
5
+ use super::vocabulary;
6
+ use crate::listing::Index;
7
+ use crate::vm::{Constant, Effect, Term};
8
+
9
+ /// Parallel literal arrays hold font names and the labels returned for detected fonts.
10
+ /// Recover both arrays, requiring contiguous indices, equal lengths and unique labels.
11
+ pub(crate) fn fonts(index: &Index<'_>, entry: u32) -> Option<Vec<(String, String)>> {
12
+ let code = index.reachable(entry);
13
+ let mut arrays: BTreeMap<u32, BTreeMap<usize, String>> = BTreeMap::new();
14
+ for pc in code {
15
+ for effect in index.listing().instructions[&pc].effects() {
16
+ let Effect::SetProperty { object, key, value } = effect else {
17
+ continue;
18
+ };
19
+ let Some((created, Term::Array(_))) = index.dereference(pc, object) else {
20
+ continue;
21
+ };
22
+ let Some((_, Term::Const(Constant::Number(slot)))) = index.dereference(pc, key) else {
23
+ continue;
24
+ };
25
+ if slot < 0.0 || slot.fract() != 0.0 {
26
+ continue;
27
+ }
28
+ let Some(value) = index.text(pc, value) else {
29
+ continue;
30
+ };
31
+ arrays
32
+ .entry(created)
33
+ .or_default()
34
+ .insert(slot as usize, value);
35
+ }
36
+ }
37
+ let arrays: Vec<_> = arrays
38
+ .into_values()
39
+ .filter_map(|values| {
40
+ values
41
+ .keys()
42
+ .copied()
43
+ .eq(0..values.len())
44
+ .then(|| values.into_values().collect::<Vec<_>>())
45
+ })
46
+ .collect();
47
+ let pairs = arrays.windows(2).filter_map(|pair| {
48
+ let [names, labels] = pair else {
49
+ return None;
50
+ };
51
+ let named = ["Arial", "Georgia", "Times New Roman"]
52
+ .iter()
53
+ .all(|name| names.iter().any(|value| value == name));
54
+ (named
55
+ && names.len() == labels.len()
56
+ && labels.iter().all(|label| vocabulary::is_hex_name(label))
57
+ && labels.iter().collect::<BTreeSet<_>>().len() == labels.len())
58
+ .then(|| labels.iter().cloned().zip(names.iter().cloned()).collect())
59
+ });
60
+ crate::candidates::exactly_one(pairs, "font labels").ok()
61
+ }
62
+
63
+ /// The permission-label object precedes the unchanged array of permissions to query.
64
+ pub(crate) fn permissions(features: &[String]) -> Option<Vec<(String, String)>> {
65
+ let candidates = features.windows(8).filter_map(|window| {
66
+ let names = ["geolocation", "notifications", "camera", "microphone"];
67
+ names
68
+ .iter()
69
+ .enumerate()
70
+ .all(|(i, name)| window[2 * i] == *name && vocabulary::is_hex_name(&window[2 * i + 1]))
71
+ .then_some(())
72
+ .filter(|()| {
73
+ window
74
+ .iter()
75
+ .skip(1)
76
+ .step_by(2)
77
+ .collect::<BTreeSet<_>>()
78
+ .len()
79
+ == names.len()
80
+ })
81
+ .map(|()| {
82
+ names
83
+ .into_iter()
84
+ .enumerate()
85
+ .map(|(i, name)| (window[2 * i + 1].clone(), name.to_owned()))
86
+ .collect()
87
+ })
88
+ });
89
+ crate::candidates::exactly_one(candidates, "permission labels").ok()
90
+ }
91
+
92
+ #[cfg(test)]
93
+ mod tests {
94
+ use super::*;
95
+ use crate::vm::{Exit, Instruction, Listing, Path, Position};
96
+
97
+ #[test]
98
+ fn font_labels_require_parallel_complete_arrays() {
99
+ let text = |value: &str| Term::Const(Constant::String(value.into()));
100
+ let mut listing = Listing {
101
+ functions: vec![Position::new(0, 0)],
102
+ ..Listing::default()
103
+ };
104
+ let names = ["Arial", "Georgia", "Times New Roman"];
105
+ let labels = [
106
+ "_111111111111111111111111",
107
+ "_222222222222222222222222",
108
+ "_333333333333333333333333",
109
+ ];
110
+ let mut effects = Vec::new();
111
+ for (register, values) in [(1, names), (2, labels)] {
112
+ effects.push(Effect::Set(register, Term::Array(vec![])));
113
+ for (slot, value) in values.into_iter().enumerate() {
114
+ effects.push(Effect::SetProperty {
115
+ object: Box::new(Term::Reg(register)),
116
+ key: Box::new(Term::Const(Constant::Number(slot as f64))),
117
+ value: Box::new(text(value)),
118
+ });
119
+ }
120
+ }
121
+ for (pc, effect) in effects.into_iter().enumerate() {
122
+ let pc = pc as u32;
123
+ listing.instructions.insert(
124
+ pc,
125
+ Instruction {
126
+ pc,
127
+ key: 0,
128
+ opcode: 0,
129
+ paths: vec![Path {
130
+ conditions: vec![],
131
+ effects: vec![effect],
132
+ exit: if pc == 7 {
133
+ Exit::Return
134
+ } else {
135
+ Exit::Next { pc: pc + 1, key: 0 }
136
+ },
137
+ }],
138
+ },
139
+ );
140
+ }
141
+ assert_eq!(
142
+ fonts(&Index::new(&listing), 0),
143
+ Some(
144
+ labels
145
+ .into_iter()
146
+ .zip(names)
147
+ .map(|(label, name)| (label.to_owned(), name.to_owned()))
148
+ .collect()
149
+ )
150
+ );
151
+ if let Effect::SetProperty { value, .. } =
152
+ &mut listing.instructions.get_mut(&6).unwrap().paths[0].effects[0]
153
+ {
154
+ **value = text(labels[0]);
155
+ }
156
+ assert!(
157
+ fonts(&Index::new(&listing), 0).is_none(),
158
+ "duplicate labels are ambiguous"
159
+ );
160
+ }
161
+
162
+ #[test]
163
+ fn permission_labels_must_be_unique() {
164
+ let features = [
165
+ "geolocation",
166
+ "_111111111111111111111111",
167
+ "notifications",
168
+ "_222222222222222222222222",
169
+ "camera",
170
+ "_333333333333333333333333",
171
+ "microphone",
172
+ "_444444444444444444444444",
173
+ ]
174
+ .map(str::to_owned);
175
+ assert_eq!(
176
+ permissions(&features).unwrap()[0],
177
+ (features[1].clone(), "geolocation".to_owned())
178
+ );
179
+ let mut duplicate = features.clone();
180
+ duplicate[3] = duplicate[1].clone();
181
+ assert!(permissions(&duplicate).is_none());
182
+ let ambiguous: Vec<_> = features.iter().chain(features.iter()).cloned().collect();
183
+ assert!(permissions(&ambiguous).is_none());
184
+ }
185
+ }
@@ -85,6 +85,8 @@ impl Vocabulary {
85
85
 
86
86
  fn between<S: AsRef<[String]>>(session: &[S], reference: &[Vec<String>]) -> Self {
87
87
  let (partners, candidates) = pair_probes(session, reference);
88
+ let cleaned_session = without_wrapper_names(session.iter().map(AsRef::as_ref));
89
+ let cleaned_reference = without_wrapper_names(reference.iter().map(Vec::as_slice));
88
90
  let mut all = Votes::new();
89
91
  let mut probes = Vec::with_capacity(session.len());
90
92
  for (index, strings) in session.iter().enumerate() {
@@ -95,7 +97,25 @@ impl Vocabulary {
95
97
  *all.entry(pair).or_default() += 1;
96
98
  }
97
99
  }
98
- probes.push(Names::one_to_one(&votes));
100
+ let mut names = Names::one_to_one(&votes);
101
+ if let Some(partner) = partners[index] {
102
+ let mut supplemental = Votes::new();
103
+ for pair in aligned_names(&cleaned_session[index], &cleaned_reference[partner - 1])
104
+ {
105
+ *supplemental.entry(pair).or_default() += 1;
106
+ }
107
+ let consistent = Names::one_to_one(&supplemental);
108
+ for (&(session, reference), &count) in &supplemental {
109
+ if consistent.to_reference.get(session).map(String::as_str) == Some(reference)
110
+ && !names.to_reference.contains_key(session)
111
+ && !names.to_session.contains_key(reference)
112
+ {
113
+ names.pair(session.to_owned(), reference.to_owned());
114
+ *all.entry((session, reference)).or_default() += count;
115
+ }
116
+ }
117
+ }
118
+ probes.push(names);
99
119
  }
100
120
  Self {
101
121
  global: Names::one_to_one(&all),
@@ -108,17 +128,17 @@ impl Vocabulary {
108
128
  /// Replaces name pairs for probe `index` with mappings recovered from its code. Removes any
109
129
  /// existing pair involving either name. Used when code analysis identifies a field more
110
130
  /// reliably than string alignment.
111
- pub(crate) fn pin<'a>(
131
+ pub(crate) fn pin<S: Into<String>>(
112
132
  &mut self,
113
133
  index: usize,
114
- pairs: impl IntoIterator<Item = (String, &'a str)>,
134
+ pairs: impl IntoIterator<Item = (String, S)>,
115
135
  ) {
116
136
  let Some(names) = self.probes.get_mut(index) else {
117
137
  return;
118
138
  };
119
139
  for (session, reference) in pairs {
120
140
  // Identical names are kept too, so the program-wide map cannot rename them.
121
- names.pair(session, reference.to_owned());
141
+ names.pair(session, reference.into());
122
142
  }
123
143
  }
124
144
 
@@ -177,6 +197,31 @@ impl Vocabulary {
177
197
  }
178
198
  }
179
199
 
200
+ /// Wrapper names and encrypted diagnostics move independently of probe fields. A second,
201
+ /// conservative alignment omits them and only fills pairs the original alignment left open.
202
+ fn without_wrapper_names<'a>(probes: impl Iterator<Item = &'a [String]>) -> Vec<Vec<String>> {
203
+ let probes: Vec<_> = probes.collect();
204
+ let mut counts = HashMap::new();
205
+ for probe in &probes {
206
+ for text in probe.iter().collect::<HashSet<_>>() {
207
+ *counts.entry(text.as_str()).or_insert(0) += 1;
208
+ }
209
+ }
210
+ probes
211
+ .iter()
212
+ .map(|probe| {
213
+ probe
214
+ .iter()
215
+ .filter(|text| {
216
+ !is_log_message(text)
217
+ && !(is_name(text) && 4 * counts[text.as_str()] > 3 * probes.len())
218
+ })
219
+ .cloned()
220
+ .collect()
221
+ })
222
+ .collect()
223
+ }
224
+
180
225
  /// Renames strings and object keys using `rename`, including randomized names inside longer
181
226
  /// strings, such as a list of names or a style rule's selector.
182
227
  fn rewrite<'a>(value: &mut JsValue, rename: &impl Fn(&str) -> Option<&'a String>) {
@@ -184,6 +229,16 @@ fn rewrite<'a>(value: &mut JsValue, rename: &impl Fn(&str) -> Option<&'a String>
184
229
  JsValue::String(text) => {
185
230
  if let Some(name) = rename(text) {
186
231
  *text = name.clone();
232
+ } else if matches!(text.trim_start().as_bytes().first(), Some(b'[' | b'{'))
233
+ && let Ok(mut value) = serde_json::from_str::<JsValue>(text)
234
+ {
235
+ // Some probes serialize structured results before adding them to the record.
236
+ // Canonical labels inside those results need the same translation as arrays.
237
+ let original = value.clone();
238
+ rewrite(&mut value, rename);
239
+ if value != original {
240
+ *text = serde_json::to_string(&value).expect("JSON values serialize");
241
+ }
187
242
  } else if let Some(renamed) = rename_embedded(text, rename) {
188
243
  *text = renamed;
189
244
  }
@@ -358,11 +413,11 @@ fn aligned_names<'a>(session: &'a [String], reference: &'a [String]) -> Vec<(&'a
358
413
  let (mut left_names, left_texts): (Vec<_>, Vec<_>) = session[i..run.session]
359
414
  .iter()
360
415
  .copied()
361
- .partition(|text| is_name(text));
416
+ .partition(|text| is_name(text) && !shared.contains(text));
362
417
  let (mut right_names, right_texts): (Vec<_>, Vec<_>) = reference[j..run.reference]
363
418
  .iter()
364
419
  .copied()
365
- .partition(|text| is_name(text));
420
+ .partition(|text| is_name(text) && !shared.contains(text));
366
421
  // Repeats of a name in a row count once, even across non-name text.
367
422
  left_names.dedup();
368
423
  right_names.dedup();
@@ -596,6 +651,11 @@ mod tests {
596
651
  "Math sqrt log1p acosh Qrst1 toString",
597
652
  vec![("Abcd1", "Qrst1")],
598
653
  ),
654
+ (
655
+ "a:b Abcd1 toString c:d toString",
656
+ "a:b Qrst1 c:d toString",
657
+ vec![("Abcd1", "Qrst1")],
658
+ ),
599
659
  ];
600
660
  for (session, reference, expected) in cases {
601
661
  assert_eq!(
@@ -605,6 +665,23 @@ mod tests {
605
665
  }
606
666
  }
607
667
 
668
+ #[test]
669
+ fn shared_wrapper_names_do_not_hide_unmapped_outputs() {
670
+ let session = [
671
+ strings("a:b Wrap1 Abcd1 c:d"),
672
+ strings("e:f Wrap1 c:d"),
673
+ strings("g:h Wrap1 c:d"),
674
+ ];
675
+ let reference = [
676
+ strings("a:b Qrst1 c:d"),
677
+ strings("e:f c:d"),
678
+ strings("g:h c:d"),
679
+ ];
680
+ let vocabulary = Vocabulary::between(&session, &reference);
681
+ assert_eq!(vocabulary.to_session(0, "Qrst1"), "Abcd1");
682
+ assert_eq!(vocabulary.to_reference(0, "Wrap1"), "Wrap1");
683
+ }
684
+
608
685
  /// A probe that shares only three of seven stable strings with its reference still pairs,
609
686
  /// as the devtools probe must when a program never names `document`.
610
687
  #[test]
@@ -637,6 +714,34 @@ mod tests {
637
714
  assert_eq!(vocabulary.global.to_session.len(), 1);
638
715
  }
639
716
 
717
+ #[test]
718
+ fn translates_serialized_results_without_converting_their_schema() {
719
+ let mut vocabulary = Vocabulary::infer(&[strings("geolocation")]);
720
+ vocabulary.pin(0, [("_111111111111111111111111".into(), "geolocation")]);
721
+ let mut fields = JsObject::from_iter([
722
+ ("pairs".into(), json!(r#"[["geolocation","denied"]]"#)),
723
+ (
724
+ "objects".into(),
725
+ json!(r#"[{"name":"geolocation","state":"denied"}]"#),
726
+ ),
727
+ ("unrelated".into(), json!(" [ 1, 2 ]")),
728
+ ("invalid".into(), json!("[not JSON")),
729
+ ]);
730
+ vocabulary.rewrite_to_session(0, &mut fields);
731
+ assert_eq!(
732
+ fields["pairs"],
733
+ json!(r#"[["_111111111111111111111111","denied"]]"#)
734
+ );
735
+ assert_eq!(
736
+ fields["objects"],
737
+ json!(r#"[{"name":"_111111111111111111111111","state":"denied"}]"#)
738
+ );
739
+ assert_eq!(fields["unrelated"], json!(" [ 1, 2 ]"));
740
+ assert_eq!(fields["invalid"], json!("[not JSON"));
741
+ vocabulary.rewrite_to_reference(0, &mut fields);
742
+ assert_eq!(fields["pairs"], json!(r#"[["geolocation","denied"]]"#));
743
+ }
744
+
640
745
  #[test]
641
746
  fn pinned_names_replace_aligned_pairs_in_their_probe() {
642
747
  let session = [strings("a:b Abcd1 c:d"), strings("e:f Wxyz2 g:h")];
@@ -25,5 +25,6 @@ pub mod replay;
25
25
  mod stylesheets;
26
26
  mod syntax;
27
27
  pub mod trace;
28
+ mod value_labels;
28
29
  pub mod vocabulary;
29
30
  mod worker_timing;
@@ -21,7 +21,7 @@ use super::interpret::Way;
21
21
  use super::machine::{Loaded, Machine};
22
22
  use super::output::{Constant, Effect, Exit, Hidden, Instruction, Listing, Path, Position, Term};
23
23
  use super::value::Value;
24
- use super::{HashMap, convert, dataflow, packed_names};
24
+ use super::{HashMap, convert, dataflow, encoded_names, packed_names};
25
25
  use crate::{candidates, profiling};
26
26
 
27
27
  impl Machine<'_> {
@@ -70,6 +70,10 @@ impl Machine<'_> {
70
70
  listing.failures.sort_unstable_by_key(|failure| failure.pc);
71
71
  fuse_method_calls(&mut listing);
72
72
  packed_names::fold(&mut listing);
73
+ encoded_names::fold(&mut listing);
74
+ // Decoding captured keys makes method loads stable even after their key register is
75
+ // reused before the call.
76
+ fuse_method_calls(&mut listing);
73
77
  profiling::report("VM");
74
78
  Ok(listing)
75
79
  }
@@ -453,7 +457,7 @@ fn incoming_loads(
453
457
  /// Tracks registers holding `object[key]` while the object and key registers remain unchanged.
454
458
  /// Saves facts at each instruction's entry. Uses a vector because these sets are small.
455
459
  #[derive(Clone, Default)]
456
- struct Loads(Vec<(u8, Load)>);
460
+ struct Loads(Vec<(u8, Load)>, BTreeMap<u8, Term>);
457
461
 
458
462
  impl Loads {
459
463
  fn get(&self, register: u8) -> Option<&Load> {
@@ -466,9 +470,12 @@ impl Loads {
466
470
  impl dataflow::Facts for Loads {
467
471
  fn meet(&mut self, other: &Self) -> bool {
468
472
  let before = self.0.len();
473
+ let constants = self.1.len();
469
474
  self.0
470
475
  .retain(|(register, load)| other.get(*register) == Some(load));
471
- self.0.len() < before
476
+ self.1
477
+ .retain(|register, value| other.1.get(register) == Some(value));
478
+ self.0.len() < before || self.1.len() < constants
472
479
  }
473
480
  }
474
481
 
@@ -483,6 +490,11 @@ fn update_loads(loads: &mut Loads, effect: &Effect) {
483
490
  let Effect::Set(register, value) = effect else {
484
491
  return;
485
492
  };
493
+ let constant = match value {
494
+ Term::Const(Constant::String(text)) if text.len() <= 128 => Some(value.clone()),
495
+ Term::Reg(source) => loads.1.get(source).cloned(),
496
+ _ => None,
497
+ };
486
498
  loads.0.retain(|(loaded, load)| {
487
499
  loaded != register && load.object != *register && load.key != Term::Reg(*register)
488
500
  });
@@ -495,9 +507,22 @@ fn update_loads(loads: &mut Loads, effect: &Effect) {
495
507
  _ => false,
496
508
  }
497
509
  {
498
- let key = (**key).clone();
510
+ let key = if let Term::Reg(register) = **key {
511
+ loads
512
+ .1
513
+ .get(&register)
514
+ .cloned()
515
+ .unwrap_or_else(|| (**key).clone())
516
+ } else {
517
+ (**key).clone()
518
+ };
499
519
  loads.0.push((*register, Load { object, key }));
500
520
  }
521
+ if let Some(value) = constant {
522
+ loads.1.insert(*register, value);
523
+ } else {
524
+ loads.1.remove(register);
525
+ }
501
526
  }
502
527
 
503
528
  /// Rewrites a call of a register still holding `object[key]` with `object` as receiver. Calls
@@ -515,3 +540,42 @@ fn fuse_call(term: &mut Term, loads: &Loads) {
515
540
  *this = None;
516
541
  }
517
542
  }
543
+
544
+ #[cfg(test)]
545
+ mod tests {
546
+ use super::*;
547
+
548
+ #[test]
549
+ fn method_keys_survive_register_reuse_but_receivers_do_not() {
550
+ let mut loads = Loads::default();
551
+ let key = Term::Const(Constant::String("mark".into()));
552
+ update_loads(&mut loads, &Effect::Set(1, key.clone()));
553
+ update_loads(
554
+ &mut loads,
555
+ &Effect::Set(2, Term::Get(Box::new(Term::Reg(3)), Box::new(Term::Reg(1)))),
556
+ );
557
+ update_loads(
558
+ &mut loads,
559
+ &Effect::Set(1, Term::Const(Constant::String("prefix".into()))),
560
+ );
561
+ let call = Term::Call {
562
+ callee: Box::new(Term::Reg(2)),
563
+ this: Some(Box::new(Term::Reg(3))),
564
+ arguments: vec![],
565
+ };
566
+ let mut fused = call.clone();
567
+ fuse_call(&mut fused, &loads);
568
+ assert_eq!(
569
+ fused,
570
+ Term::Call {
571
+ callee: Box::new(Term::Get(Box::new(Term::Reg(3)), Box::new(key))),
572
+ this: None,
573
+ arguments: vec![]
574
+ }
575
+ );
576
+ update_loads(&mut loads, &Effect::Set(3, Term::Const(Constant::Null)));
577
+ let mut after_replacement = call.clone();
578
+ fuse_call(&mut after_replacement, &loads);
579
+ assert_eq!(after_replacement, call);
580
+ }
581
+ }