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
@@ -9,7 +9,7 @@ use ego_tree::{NodeId, NodeRef};
9
9
  use html5ever::{Attribute, QualName, ns};
10
10
  use scraper::node::Element;
11
11
  use scraper::{ElementRef, Html, Node, Selector};
12
- use serde::{Deserialize, Serialize};
12
+ use serde::{Deserialize, Serialize, Serializer};
13
13
  use url::Url;
14
14
 
15
15
  use crate::js::{JsObject, JsValue, json};
@@ -194,128 +194,107 @@ pub(crate) struct RenderedPage {
194
194
  parent: ParentInfo,
195
195
  }
196
196
 
197
- /// `wPr`: what `api.js` reports about the page around the widget. Fields serialize in the
198
- /// order `api.js` writes them, which the message keeps.
199
- #[derive(Clone, Debug, Serialize)]
197
+ /// `wPr`: what `api.js` reports about the page around the widget, sent as the array
198
+ /// [`Self::elements`].
199
+ #[derive(Clone, Debug)]
200
200
  pub(crate) struct PageSignals {
201
- #[serde(rename = "d.cT")]
202
201
  pub custom_element_tags: Vec<String>,
203
- #[serde(rename = "ht.atrs")]
204
202
  pub root_attributes: Vec<String>,
205
- #[serde(rename = "pg.ref")]
206
203
  pub referrer: String,
207
- #[serde(rename = "pi")]
208
204
  pub parent: ParentInfo,
209
- #[serde(rename = "w.iW")]
210
205
  pub inner_width: u32,
211
206
  }
212
207
 
213
208
  impl PageSignals {
214
- /// The positional `wPr` format used by newer loaders. Keep absent optional properties as
215
- /// null array entries, as JSON.stringify does for JavaScript's `undefined`.
216
- pub(crate) fn positional(&self) -> JsValue {
209
+ /// The array `api.js` sends. Its order is that of the named fields earlier builds sent,
210
+ /// noted beside each element. `pac` and `pad` are `undefined` when `api.js` leaves them out.
211
+ #[must_use]
212
+ pub(crate) fn elements(&self) -> Vec<JsValue> {
217
213
  let parent = &self.parent;
218
- json!([
219
- self.custom_element_tags,
220
- self.root_attributes,
221
- self.referrer,
222
- parent.form,
223
- parent.in_iframe,
224
- parent.location,
225
- parent.meta,
226
- parent.pac,
227
- parent.pad,
228
- parent.fingerprint,
229
- parent.scripts,
230
- parent.same_realm,
231
- parent.stylesheets,
232
- parent.title,
233
- parent.elements,
234
- parent.appearance,
235
- parent.error_handled,
236
- parent.host,
237
- parent.hidden_reason,
238
- parent.implicit,
239
- parent.mode,
240
- parent.mount,
241
- parent.reasons,
242
- parent.user_hidden,
243
- parent.viewport.height,
244
- parent.viewport.visibility_state,
245
- parent.viewport.width,
246
- parent.widget_position,
247
- parent.xpath,
248
- self.inner_width,
249
- ])
214
+ let viewport = &parent.viewport;
215
+ let optional = |value: Option<String>| value.map_or(JsValue::Undefined, JsValue::String);
216
+ vec![
217
+ json!(self.custom_element_tags), // d.cT
218
+ json!(self.root_attributes), // ht.atrs
219
+ json!(self.referrer), // pg.ref
220
+ json!(parent.form), // pi.ffp
221
+ json!(parent.in_iframe), // pi.ii
222
+ json!(parent.location), // pi.lH
223
+ json!(parent.meta), // pi.mL
224
+ optional(parent.pac.clone()), // pi.pac
225
+ optional(parent.pad.clone()), // pi.pad
226
+ json!(parent.fingerprint), // pi.pfp
227
+ json!(parent.scripts), // pi.sL
228
+ json!(parent.same_realm), // pi.sR
229
+ json!(parent.stylesheets), // pi.ssL
230
+ json!(parent.title), // pi.t
231
+ json!(parent.elements), // pi.tL
232
+ json!(parent.appearance), // pi.vDa
233
+ json!(parent.error_handled), // pi.vDeh
234
+ json!(parent.host), // pi.vDhp
235
+ json!(parent.hidden_reason), // pi.vDhr
236
+ json!(parent.implicit), // pi.vDie
237
+ json!(parent.mode), // pi.vDmd
238
+ json!(parent.mount), // pi.vDmt
239
+ json!(parent.reasons), // pi.vDrs
240
+ json!(parent.user_hidden), // pi.vDuh
241
+ json!(viewport.height), // pi.vDvp.height
242
+ json!(viewport.visibility_state), // pi.vDvp.visibilityState
243
+ json!(viewport.width), // pi.vDvp.width
244
+ json!(parent.widget_position), // pi.wp
245
+ json!(parent.xpath), // pi.xp
246
+ json!(self.inner_width), // w.iW
247
+ ]
250
248
  }
251
249
  }
252
250
 
253
- /// `pi`: the widget's place in the page. The parent info probe copies these properties by
254
- /// their `api.js` names.
255
- #[derive(Clone, Debug, Serialize)]
251
+ /// Writes [`PageSignals::elements`], with `undefined` as `null`.
252
+ impl Serialize for PageSignals {
253
+ fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
254
+ JsValue::Array(self.elements()).serialize(serializer)
255
+ }
256
+ }
257
+
258
+ /// `pi`: the widget's place in the page.
259
+ #[derive(Clone, Debug)]
256
260
  pub(crate) struct ParentInfo {
257
261
  /// The enclosing form's method and controls, or `nf` if there is no form.
258
- #[serde(rename = "ffp")]
259
262
  pub form: String,
260
- #[serde(rename = "ii")]
261
263
  pub in_iframe: bool,
262
- #[serde(rename = "lH")]
263
264
  pub location: String,
264
- #[serde(rename = "mL")]
265
265
  pub meta: usize,
266
- #[serde(skip_serializing_if = "Option::is_none")]
267
266
  pub pac: Option<String>,
268
- #[serde(skip_serializing_if = "Option::is_none")]
269
267
  pub pad: Option<String>,
270
- #[serde(rename = "pfp")]
271
268
  pub fingerprint: String,
272
- #[serde(rename = "sL")]
273
269
  pub scripts: usize,
274
- #[serde(rename = "sR")]
275
270
  pub same_realm: bool,
276
- #[serde(rename = "ssL")]
277
271
  pub stylesheets: usize,
278
272
  /// The title's UTF-16 length and djb2 hash.
279
- #[serde(rename = "t")]
280
273
  pub title: String,
281
- #[serde(rename = "tL")]
282
274
  pub elements: usize,
283
- #[serde(rename = "vDa")]
284
275
  pub appearance: String,
285
- #[serde(rename = "vDeh")]
286
276
  pub error_handled: bool,
287
- #[serde(rename = "vDhp")]
288
277
  pub host: &'static str,
289
278
  /// Why the widget is hidden; `null` while it shows.
290
- #[serde(rename = "vDhr")]
291
279
  pub hidden_reason: Option<String>,
292
280
  /// Whether the loader rendered the widget from the page's markup.
293
- #[serde(rename = "vDie")]
294
281
  pub implicit: bool,
295
282
  /// The challenge mode the iframe reported.
296
- #[serde(rename = "vDmd")]
297
283
  pub mode: Option<String>,
298
- #[serde(rename = "vDmt")]
299
284
  pub mount: &'static str,
300
285
  /// Why the widget was re-rendered; empty on a first render.
301
- #[serde(rename = "vDrs")]
302
286
  pub reasons: Vec<String>,
303
- #[serde(rename = "vDuh")]
304
287
  pub user_hidden: bool,
305
- #[serde(rename = "vDvp")]
306
288
  pub viewport: Viewport,
307
289
  /// The wrapper's top and right edges.
308
- #[serde(rename = "wp")]
309
290
  pub widget_position: String,
310
- #[serde(rename = "xp")]
311
291
  pub xpath: String,
312
292
  }
313
293
 
314
294
  /// `vDvp`: the page's viewport.
315
- #[derive(Clone, Debug, Default, Serialize)]
295
+ #[derive(Clone, Debug, Default)]
316
296
  pub(crate) struct Viewport {
317
297
  pub height: u32,
318
- #[serde(rename = "visibilityState")]
319
298
  pub visibility_state: String,
320
299
  pub width: u32,
321
300
  }
@@ -1027,26 +1006,15 @@ mod tests {
1027
1006
  ..Layout::default()
1028
1007
  };
1029
1008
  let signals = embedding_page::tests::page().signals(&layout, Some("managed"));
1030
- // Recorded from a real browser rendering exactly this page.
1031
- let expected = json!({
1032
- "d.cT": [],
1033
- "ht.atrs": [],
1034
- "pg.ref": "",
1035
- "pi": {
1036
- "ffp": "nf", "ii": false, "lH": "http://127.0.0.1:8000/index.html", "mL": 1,
1037
- "pac": "n", "pad": "n|n|n|0|0|0|0|",
1038
- "pfp": "htm>hea>met_ch>tit>-tscr_sr_as_de>-tbod>h1>p>-tdiv_cl_da_da_da>-t",
1039
- "sL": 1, "sR": true, "ssL": 0, "t": "19|3058264090", "tL": 12,
1040
- "vDa": "always", "vDeh": false, "vDhp": "wrapper", "vDhr": null,
1041
- "vDie": true, "vDmd": "managed", "vDmt": "wrapper", "vDrs": [],
1042
- "vDuh": false,
1043
- "vDvp": {"height": 813, "visibilityState": "hidden", "width": 1280},
1044
- "wp": "113.875|1272", "xp": "/div[1]/div[1]"
1045
- },
1046
- "w.iW": 1280
1047
- });
1048
- // Field order is part of the message, so compare the serialized text.
1049
- assert_eq!(json!(signals).stringify(), expected.stringify());
1009
+ // Recorded from a real browser rendering exactly this page. Order is part of the message,
1010
+ // so compare the serialized text.
1011
+ let expected = concat!(
1012
+ r#"[[],[],"","nf",false,"http://127.0.0.1:8000/index.html",1,"n","n|n|n|0|0|0|0|","#,
1013
+ r#""htm>hea>met_ch>tit>-tscr_sr_as_de>-tbod>h1>p>-tdiv_cl_da_da_da>-t",1,true,0,"#,
1014
+ r#""19|3058264090",12,"always",false,"wrapper",null,true,"managed","wrapper",[],"#,
1015
+ r#"false,813,"hidden",1280,"113.875|1272","/div[1]/div[1]",1280]"#,
1016
+ );
1017
+ assert_eq!(json!(signals).stringify().as_deref(), Some(expected));
1050
1018
  }
1051
1019
 
1052
1020
  /// Checks `ffp`, `pac`, and `pad` for forms and nearby controls. Without a surrounding
@@ -1084,8 +1052,8 @@ mod tests {
1084
1052
  cases.push((nested(depth), if depth <= 3 { newsletter } else { none }));
1085
1053
  }
1086
1054
  for (html, expected) in cases {
1087
- let pi = &signals(&html)["pi"];
1088
- assert_eq!([&pi["ffp"], &pi["pac"], &pi["pad"]], expected, "{html}");
1055
+ let signals = signals(&html);
1056
+ assert_eq!([&signals[3], &signals[7], &signals[8]], expected, "{html}");
1089
1057
  }
1090
1058
  }
1091
1059
 
@@ -1101,9 +1069,12 @@ mod tests {
1101
1069
  json!(page.unwrap().signals(&Layout::default(), None))
1102
1070
  };
1103
1071
  let challenge = rendered("data");
1104
- assert!(challenge["pi"].get("pac").is_none());
1105
- assert!(challenge["pi"].get("pad").is_none());
1106
- assert_eq!(rendered("")["pi"]["pac"], "n");
1072
+ assert_eq!(
1073
+ [&challenge[7], &challenge[8]],
1074
+ [&JsValue::Null; 2],
1075
+ "`undefined` in place"
1076
+ );
1077
+ assert_eq!(rendered("")[7], "n");
1107
1078
  }
1108
1079
 
1109
1080
  #[test]
@@ -1183,7 +1154,7 @@ mod tests {
1183
1154
  let html = format!(
1184
1155
  r#"<html><body><div{attributes}></div><div class="cf-turnstile" data-sitekey="k"></div></body></html>"#
1185
1156
  );
1186
- let shape = signals(&html)["pi"]["pfp"].as_str().unwrap().to_owned();
1157
+ let shape = signals(&html)[9].as_str().unwrap().to_owned();
1187
1158
  assert_eq!(shape.encode_utf16().count(), FINGERPRINT_LIMIT);
1188
1159
  }
1189
1160
  }
@@ -338,17 +338,16 @@ pub(crate) fn merge(
338
338
  embedding_page.script.build
339
339
  ));
340
340
  }
341
- let page_signals = embedding_page.page.signals(
342
- &embedding_page.layout,
343
- source::mode(&program.request.options),
344
- );
345
- let message = json!(extra_params::collect(
341
+ let message = extra_params::collect(
346
342
  &embedding_page.page,
347
- &page_signals,
343
+ &embedding_page.layout,
348
344
  &page.timeline,
349
345
  &embedding_page.script,
350
- ));
346
+ source::mode(&program.request.options),
347
+ );
351
348
  sink.json("extra-params.json", &message)?;
349
+ let page_signals = message.signals.clone();
350
+ let message = json!(message);
352
351
  let mut unset: Vec<String> = Vec::new();
353
352
  let built = FirstPayload::build(
354
353
  sink,
@@ -115,7 +115,6 @@ pub(crate) async fn prepare(session: &WidgetSession<'_>, solve: &Solve) -> Resul
115
115
  trace: Trace::recover(&session.fetched.source),
116
116
  controller: plan.controller_trace.as_deref(),
117
117
  globals: &globals,
118
- parent: observed.page_signals.as_ref(),
119
118
  };
120
119
  let records = records(
121
120
  plan,
@@ -146,39 +146,10 @@ pub fn analyze(source: &Source, program: &[u8]) -> Result<Analysis> {
146
146
  .collect();
147
147
  let mut vocabulary = Vocabulary::infer(&session_features);
148
148
  for (position, raw) in discovery.probes.iter().enumerate() {
149
- match vocabulary.partner(position).and_then(Kind::from_reference) {
150
- Some(Kind::FontEnumeration) => {
151
- if let Some(labels) = crate::stage2::value_labels::fonts(&index, raw.function.pc) {
152
- vocabulary.pin(position, labels);
153
- }
154
- }
155
- Some(Kind::Environment) => {
156
- if let Some(labels) = crate::stage2::value_labels::permissions(&raw.features) {
157
- vocabulary.pin(position, labels);
158
- }
159
- }
160
- Some(Kind::Stylesheets) => {
161
- if let Some(field) = crate::stage2::stylesheets::text_field(&index, raw.function.pc)
162
- {
163
- vocabulary.pin(
164
- position,
165
- [(field, crate::stage2::reference::STYLESHEETS_TEXT)],
166
- );
167
- }
168
- }
169
- Some(Kind::DateParsingOffsets) => {
170
- if let Some(names) =
171
- crate::stage2::probes::dates::names(&raw.features, &raw.writes.fields)
172
- {
173
- vocabulary.pin(position, names);
174
- }
175
- }
176
- Some(Kind::EnvironmentAudit) => {
177
- if let Some(names) = audit::names(&index, &raw.writes.fields) {
178
- vocabulary.pin(position, names);
179
- }
180
- }
181
- _ => {}
149
+ if vocabulary.partner(position) == Some(AUDIT_PROBE)
150
+ && let Some(names) = audit::names(&index, &raw.writes.fields)
151
+ {
152
+ vocabulary.pin(position, names);
182
153
  }
183
154
  }
184
155
  let mut probes = Vec::new();
@@ -266,23 +266,12 @@ fn collection_strings(
266
266
  outputs.insert(name.clone());
267
267
  }
268
268
  effect.visit_terms(&mut |term| match term {
269
- Term::Const(Constant::String(text))
270
- if text.len() <= FEATURE_LENGTH
271
- && !index
272
- .listing()
273
- .decoded_inputs
274
- .get(&pc)
275
- .is_some_and(|inputs| inputs.contains(text)) =>
276
- {
269
+ Term::Const(Constant::String(text)) if text.len() <= FEATURE_LENGTH => {
277
270
  if !at_pc.contains(text) {
278
271
  at_pc.push(text.clone());
279
272
  }
280
273
  }
281
- Term::Closure { pc, .. }
282
- if !index.listing().string_decoders.contains(pc) =>
283
- {
284
- pending.push(*pc)
285
- }
274
+ Term::Closure { pc, .. } => pending.push(*pc),
286
275
  // Exception results are recovered separately from collection paths.
287
276
  _ => {}
288
277
  });
@@ -295,10 +284,13 @@ fn collection_strings(
295
284
  }
296
285
  },
297
286
  );
298
- (
299
- without_inline_descriptors(strings.into_values().flatten()),
300
- outputs,
301
- )
287
+ // Text the program decodes with its own decoders stands for the decoded text that follows.
288
+ let encoded = &index.listing().encoded;
289
+ let strings = strings
290
+ .into_values()
291
+ .flatten()
292
+ .filter(|text| !encoded.contains(text));
293
+ (without_inline_descriptors(strings), outputs)
302
294
  }
303
295
 
304
296
  /// Drops string descriptors the program decodes in place, keeping the decoded text that follows
@@ -7,7 +7,9 @@ use std::time::Duration;
7
7
  use anyhow::{Context, Result};
8
8
  use base64::Engine as _;
9
9
  use base64::engine::general_purpose::STANDARD;
10
- use oxc::ast::ast::{CallExpression, Expression, MemberExpression};
10
+ use oxc::ast::ast::{
11
+ CallExpression, Expression, MemberExpression, StringLiteral, VariableDeclarator,
12
+ };
11
13
  use oxc::ast_visit::{Visit, walk};
12
14
  use oxc::str::Ident;
13
15
  use serde::{Deserialize, Serialize};
@@ -166,8 +168,27 @@ pub(crate) fn recognize(probe: &ProbeView<'_>) -> Option<Fields> {
166
168
  });
167
169
  }
168
170
  }
169
- let ([samples, compilation, total], [warmup, _, _, rounds]) =
170
- worker_fields(&strings, probe.pool)?;
171
+ // Programs that decode strings in place also hold the source as a descriptor. Some builds
172
+ // hide the worker's own strings behind a decoder in its source, which deobfuscation inlines.
173
+ let sources: Vec<String> = strings
174
+ .iter()
175
+ .filter(|text| text.contains("WebAssembly"))
176
+ .filter_map(|source| crate::deobfuscate::deobfuscate(source.clone()).ok())
177
+ .map(|deobfuscated| deobfuscated.text)
178
+ .collect();
179
+ let parsed = sources
180
+ .iter()
181
+ .filter(|text| text.contains("WebAssembly.instantiate"))
182
+ .find_map(|source| ParsedScript::parse("benchmark worker", source).ok())?;
183
+ let mut worker = Worker::new(parsed.script());
184
+ worker.visit_program(parsed.script().program);
185
+ // Only the kernels the recorded speeds measure are supported. Field names are recovered
186
+ // separately and may change without changing the binary.
187
+ if functions_digest(&worker.module?)? != KERNELS {
188
+ return None;
189
+ }
190
+ let [samples, compilation, total]: [String; 3] = worker.outputs.try_into().ok()?;
191
+ let [warmup, _, _, rounds]: [String; 4] = worker.plan_fields.try_into().ok()?;
171
192
  let number = |name: &str| {
172
193
  strings.iter().find_map(|text| {
173
194
  text.strip_prefix(&format!("{name}:"))?
@@ -235,29 +256,6 @@ pub(crate) fn recognize(probe: &ProbeView<'_>) -> Option<Fields> {
235
256
  })
236
257
  }
237
258
 
238
- /// Ignore unrelated WebAssembly mentions and only accept the complete worker whose
239
- /// kernels match the captured measurements. Sources may be packed descriptors.
240
- fn worker_fields(strings: &[String], pool: &str) -> Option<([String; 3], [String; 4])> {
241
- strings
242
- .iter()
243
- .filter(|text| text.contains("WebAssembly"))
244
- .find_map(|source| {
245
- let source = crate::deobfuscate::deobfuscate(source.clone())
246
- .or_else(|_| crate::deobfuscate::deobfuscate(listing::decode_string(source, pool)?))
247
- .ok()?;
248
- let parsed = ParsedScript::parse("benchmark worker", &source.text).ok()?;
249
- let mut worker = Worker::new(parsed.script());
250
- worker.visit_program(parsed.script().program);
251
- if functions_digest(&worker.module?)? != KERNELS {
252
- return None;
253
- }
254
- Some((
255
- worker.outputs.try_into().ok()?,
256
- worker.plan_fields.try_into().ok()?,
257
- ))
258
- })
259
- }
260
-
261
259
  /// Digest of the kernel functions measured by the profiles; see [`functions_digest`].
262
260
  const KERNELS: &str = "259924f166f0a6ab5c9709a91c80f12f9cfd24cc1827c3a76bf93ac5cbb60a3a";
263
261
 
@@ -318,17 +316,29 @@ impl<'a> Worker<'a> {
318
316
  plan_fields: Vec::new(),
319
317
  }
320
318
  }
321
- }
322
319
 
323
- impl<'a> Visit<'a> for Worker<'a> {
324
- fn visit_expression(&mut self, expression: &Expression<'a>) {
325
- if let Some(value) = ast::static_str(expression)
326
- && let Ok(bytes) = STANDARD.decode(value)
320
+ /// Takes `text` as the module if it is a module's base64.
321
+ fn module_text(&mut self, text: &str) {
322
+ if let Ok(bytes) = STANDARD.decode(text)
327
323
  && bytes.starts_with(b"\0asm")
328
324
  {
329
325
  self.module = Some(bytes);
330
326
  }
331
- walk::walk_expression(self, expression);
327
+ }
328
+ }
329
+
330
+ impl<'a> Visit<'a> for Worker<'a> {
331
+ fn visit_variable_declarator(&mut self, declaration: &VariableDeclarator<'a>) {
332
+ if let Some(value) = declaration.init.as_ref().and_then(ast::static_str) {
333
+ self.module_text(value);
334
+ }
335
+ walk::walk_variable_declarator(self, declaration);
336
+ }
337
+ /// Deobfuscation inlines a module used once, as in `instantiate(decode("AGFzbQ..."))`.
338
+ fn visit_string_literal(&mut self, literal: &StringLiteral<'a>) {
339
+ if let Some(text) = literal.value.as_str() {
340
+ self.module_text(text);
341
+ }
332
342
  }
333
343
  fn visit_member_expression(&mut self, member: &MemberExpression<'a>) {
334
344
  if let MemberExpression::StaticMemberExpression(field) = member
@@ -371,36 +381,6 @@ mod tests {
371
381
  use super::*;
372
382
  use crate::js;
373
383
 
374
- #[test]
375
- fn recovers_the_encoded_workers_inlined_module_and_plan() {
376
- let (outputs, plan_fields) = worker_fields(
377
- &[
378
- "WebAssembly".to_owned(),
379
- "const irrelevant = WebAssembly;".to_owned(),
380
- include_str!("fixtures/encoded_benchmark_worker.js").to_owned(),
381
- ],
382
- "",
383
- )
384
- .unwrap();
385
- assert_eq!(
386
- outputs,
387
- [
388
- "_e712758a2abf0550c8c03caf",
389
- "_330c1c3a2512b4edd9f7a483",
390
- "_6e07b0330abb107d89bcb031"
391
- ]
392
- );
393
- assert_eq!(
394
- plan_fields,
395
- [
396
- "_0edcdf5edd841b2aaa59320c",
397
- "_5789c64e096112b22c52e80f",
398
- "_7f8289304361962296ebf3f6",
399
- "_447c0ac98523cf3d6a2324d9"
400
- ]
401
- );
402
- }
403
-
404
384
  fn sample() -> Fields {
405
385
  js::from_value(json!({
406
386
  "samples": "samples", "compilation": "compile", "total": "total",