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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +8 -3
- data/UPSTREAM.md +8 -2
- data/lib/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
- data/lib/cfsolve/profiles/chrome-157-macos.json +613 -0
- data/lib/cfsolve/version.rb +2 -2
- data/lib/cfsolve.rb +1 -1
- data/vendor/cfsolve/README.md +53 -5
- data/vendor/cfsolve/examples/turnstile_smoke.rs +58 -0
- data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
- data/vendor/cfsolve/profiles/chrome-157-macos.json +613 -0
- data/vendor/cfsolve/src/embedding_page/extra_params.rs +110 -7
- data/vendor/cfsolve/src/embedding_page/page.rs +41 -1
- data/vendor/cfsolve/src/session/initial.rs +7 -6
- data/vendor/cfsolve/src/session/probes.rs +1 -0
- data/vendor/cfsolve/src/stage2/analysis/analyze.rs +33 -4
- data/vendor/cfsolve/src/stage2/analysis/discovery.rs +13 -2
- data/vendor/cfsolve/src/stage2/benchmark.rs +59 -18
- data/vendor/cfsolve/src/stage2/call_stack.rs +169 -36
- data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +1 -0
- data/vendor/cfsolve/src/stage2/probes/dates.rs +44 -0
- data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +326 -0
- data/vendor/cfsolve/src/stage2/probes/stack.rs +117 -4
- data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +80 -2
- data/vendor/cfsolve/src/stage2/probes.rs +1 -0
- data/vendor/cfsolve/src/stage2/reference.rs +5 -0
- data/vendor/cfsolve/src/stage2/registry.rs +8 -2
- data/vendor/cfsolve/src/stage2/replay.rs +34 -3
- data/vendor/cfsolve/src/stage2/stylesheets.rs +35 -0
- data/vendor/cfsolve/src/stage2/value_labels.rs +185 -0
- data/vendor/cfsolve/src/stage2/vocabulary.rs +111 -6
- data/vendor/cfsolve/src/stage2.rs +1 -0
- data/vendor/cfsolve/src/vm/disassemble.rs +68 -4
- data/vendor/cfsolve/src/vm/encoded_names.rs +635 -0
- data/vendor/cfsolve/src/vm/extract.rs +246 -43
- data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +9 -0
- data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +7438 -0
- data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +1 -0
- data/vendor/cfsolve/src/vm/interpret/access.rs +8 -2
- data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +39 -1
- data/vendor/cfsolve/src/vm/interpret/pattern.rs +33 -4
- data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +14 -6
- data/vendor/cfsolve/src/vm/interpret/run.rs +4 -0
- data/vendor/cfsolve/src/vm/machine.rs +63 -5
- data/vendor/cfsolve/src/vm/output.rs +6 -1
- data/vendor/cfsolve/src/vm/registers.rs +35 -21
- data/vendor/cfsolve/src/vm/session.rs +6 -3
- data/vendor/cfsolve/src/vm.rs +1 -0
- metadata +13 -1
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
//! Folds calls to the program's per-probe string decoders. These cache a base64 string,
|
|
2
|
+
//! rotate a 16-bit key by one byte before each XOR, and escape UTF-16 units with 255.
|
|
3
|
+
//! Decoder cells belong to their creating function and its closures, not the whole program.
|
|
4
|
+
|
|
5
|
+
use std::collections::{BTreeSet, HashMap, HashSet};
|
|
6
|
+
|
|
7
|
+
use base64::Engine;
|
|
8
|
+
use base64::engine::general_purpose::STANDARD;
|
|
9
|
+
use oxc::syntax::operator::BinaryOperator;
|
|
10
|
+
|
|
11
|
+
use super::{Constant, Effect, Exit, Listing, Term};
|
|
12
|
+
use crate::listing::Index;
|
|
13
|
+
|
|
14
|
+
pub(super) fn fold(listing: &mut Listing) {
|
|
15
|
+
let index = Index::new(listing);
|
|
16
|
+
let decoders: HashMap<u32, u16> = index
|
|
17
|
+
.entries()
|
|
18
|
+
.iter()
|
|
19
|
+
.filter_map(|&entry| decoder(&index, entry).map(|seed| (entry, seed)))
|
|
20
|
+
.collect();
|
|
21
|
+
if decoders.is_empty() {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
let factories: HashMap<u32, u16> = index
|
|
25
|
+
.entries()
|
|
26
|
+
.iter()
|
|
27
|
+
.filter_map(|&entry| {
|
|
28
|
+
let mut seeds = BTreeSet::new();
|
|
29
|
+
for (&pc, instruction) in listing.instructions.range(index.function_at(entry)) {
|
|
30
|
+
for path in &instruction.paths {
|
|
31
|
+
if matches!(path.exit, Exit::Return) {
|
|
32
|
+
let Some(Effect::Set(_, value)) = path.effects.first() else {
|
|
33
|
+
return None;
|
|
34
|
+
};
|
|
35
|
+
let (_, Term::Closure { pc, .. }) = index.dereference(pc, value)? else {
|
|
36
|
+
return None;
|
|
37
|
+
};
|
|
38
|
+
seeds.insert(*decoders.get(&pc)?);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
(seeds.len() == 1).then(|| (entry, *seeds.first().unwrap()))
|
|
43
|
+
})
|
|
44
|
+
.collect();
|
|
45
|
+
let cells = captured_values(&index, |pc, value| {
|
|
46
|
+
factory_result(&index, pc, value, &factories)
|
|
47
|
+
});
|
|
48
|
+
let mut replacements = Vec::new();
|
|
49
|
+
let mut decoded_inputs = Vec::new();
|
|
50
|
+
for (&pc, instruction) in &listing.instructions {
|
|
51
|
+
let Some(entry) = index.entry_at_or_before(pc) else {
|
|
52
|
+
continue;
|
|
53
|
+
};
|
|
54
|
+
for (path_index, path) in instruction.paths.iter().enumerate() {
|
|
55
|
+
for (effect_index, effect) in path.effects.iter().enumerate() {
|
|
56
|
+
let (Effect::Let(
|
|
57
|
+
_,
|
|
58
|
+
Term::Call {
|
|
59
|
+
callee,
|
|
60
|
+
this: None,
|
|
61
|
+
arguments,
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
| Effect::Set(
|
|
65
|
+
_,
|
|
66
|
+
Term::Call {
|
|
67
|
+
callee,
|
|
68
|
+
this: None,
|
|
69
|
+
arguments,
|
|
70
|
+
},
|
|
71
|
+
)) = effect
|
|
72
|
+
else {
|
|
73
|
+
continue;
|
|
74
|
+
};
|
|
75
|
+
let [argument] = &arguments[..] else {
|
|
76
|
+
continue;
|
|
77
|
+
};
|
|
78
|
+
let seed = match index.dereference(pc, callee) {
|
|
79
|
+
Some((_, Term::Cell(cell))) => cells.get(&(entry, cell)).copied(),
|
|
80
|
+
_ => factory_result(&index, pc, callee, &factories),
|
|
81
|
+
};
|
|
82
|
+
let Some(seed) = seed else {
|
|
83
|
+
continue;
|
|
84
|
+
};
|
|
85
|
+
if let Some((at, Term::Const(Constant::String(encoded)))) =
|
|
86
|
+
index.dereference(pc, argument)
|
|
87
|
+
&& let Some(decoded) = decode(&encoded, seed)
|
|
88
|
+
{
|
|
89
|
+
replacements.push((pc, path_index, effect_index, decoded));
|
|
90
|
+
decoded_inputs.push((at, encoded));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
let decoded_at: HashSet<u32> = replacements.iter().map(|(pc, ..)| *pc).collect();
|
|
96
|
+
let string_decoders = decoders.keys().chain(factories.keys()).copied().collect();
|
|
97
|
+
for (pc, input) in decoded_inputs {
|
|
98
|
+
listing.decoded_inputs.entry(pc).or_default().insert(input);
|
|
99
|
+
}
|
|
100
|
+
listing.string_decoders = string_decoders;
|
|
101
|
+
for (pc, path, effect, decoded) in replacements {
|
|
102
|
+
let effects = &mut listing.instructions.get_mut(&pc).unwrap().paths[path].effects;
|
|
103
|
+
let temporary = match &effects[effect] {
|
|
104
|
+
Effect::Let(temporary, _) => Some(*temporary),
|
|
105
|
+
_ => None,
|
|
106
|
+
};
|
|
107
|
+
let value = Term::Const(Constant::String(decoded));
|
|
108
|
+
match &mut effects[effect] {
|
|
109
|
+
Effect::Let(_, term) | Effect::Set(_, term) => *term = value.clone(),
|
|
110
|
+
_ => unreachable!(),
|
|
111
|
+
}
|
|
112
|
+
if let Some(temporary) = temporary {
|
|
113
|
+
for following in &mut effects[effect + 1..] {
|
|
114
|
+
if let Effect::Set(_, term @ Term::Temp(_)) = following
|
|
115
|
+
&& *term == Term::Temp(temporary)
|
|
116
|
+
{
|
|
117
|
+
*term = value.clone();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
fold_cells(listing, &decoded_at);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fn factory_result(
|
|
126
|
+
index: &Index<'_>,
|
|
127
|
+
pc: u32,
|
|
128
|
+
value: &Term,
|
|
129
|
+
factories: &HashMap<u32, u16>,
|
|
130
|
+
) -> Option<u16> {
|
|
131
|
+
let (at, Term::Call { callee, .. }) = index.dereference(pc, value)? else {
|
|
132
|
+
return None;
|
|
133
|
+
};
|
|
134
|
+
let (defined, Term::Closure { pc: factory, .. }) = index.dereference(at, &callee)? else {
|
|
135
|
+
return None;
|
|
136
|
+
};
|
|
137
|
+
let entry = index.entry_at_or_before(pc);
|
|
138
|
+
if entry != index.entry_at_or_before(at) || entry != index.entry_at_or_before(defined) {
|
|
139
|
+
return None;
|
|
140
|
+
}
|
|
141
|
+
factories.get(&factory).copied()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// Descendants capturing this cell, stopping at a new lexical binding of the same cell number.
|
|
145
|
+
fn scopes(index: &Index<'_>, parent: u32, cell: u32) -> HashSet<u32> {
|
|
146
|
+
let mut pending = vec![parent];
|
|
147
|
+
let mut seen = HashSet::new();
|
|
148
|
+
while let Some(entry) = pending.pop() {
|
|
149
|
+
if seen.contains(&entry)
|
|
150
|
+
|| (entry != parent
|
|
151
|
+
&& index.effects_in(index.function_at(entry)).any(
|
|
152
|
+
|(_, effect)| matches!(effect, Effect::NewCell(shadow) if *shadow == cell),
|
|
153
|
+
))
|
|
154
|
+
{
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
seen.insert(entry);
|
|
158
|
+
pending.extend(index.closures_in(index.function_at(entry)));
|
|
159
|
+
}
|
|
160
|
+
seen
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/// Values assigned once in a lexical scope and captured by its descendants. Any other
|
|
164
|
+
/// write invalidates the binding, even if that write cannot itself be resolved.
|
|
165
|
+
fn captured_values<T: Clone + PartialEq>(
|
|
166
|
+
index: &Index<'_>,
|
|
167
|
+
resolve: impl Fn(u32, &Term) -> Option<T>,
|
|
168
|
+
) -> HashMap<(u32, u32), T> {
|
|
169
|
+
let mut values: HashMap<(u32, u32), Option<T>> = HashMap::new();
|
|
170
|
+
for (pc, effect) in index.effects_in(..) {
|
|
171
|
+
let Effect::SetCell(cell, assigned) = effect else {
|
|
172
|
+
continue;
|
|
173
|
+
};
|
|
174
|
+
let Some(value) = resolve(pc, assigned) else {
|
|
175
|
+
continue;
|
|
176
|
+
};
|
|
177
|
+
let Some(parent) = index.entry_at_or_before(pc) else {
|
|
178
|
+
continue;
|
|
179
|
+
};
|
|
180
|
+
let scopes = scopes(index, parent, *cell);
|
|
181
|
+
if scopes.iter().any(|&entry| {
|
|
182
|
+
index
|
|
183
|
+
.effects_in(index.function_at(entry))
|
|
184
|
+
.any(|(other, effect)| {
|
|
185
|
+
matches!(effect, Effect::SetCell(written, term)
|
|
186
|
+
if written == cell && (other != pc || term != assigned))
|
|
187
|
+
})
|
|
188
|
+
}) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
for entry in scopes {
|
|
192
|
+
values
|
|
193
|
+
.entry((entry, *cell))
|
|
194
|
+
.and_modify(|known| {
|
|
195
|
+
if known.as_ref() != Some(&value) {
|
|
196
|
+
*known = None;
|
|
197
|
+
}
|
|
198
|
+
})
|
|
199
|
+
.or_insert_with(|| Some(value.clone()));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
values
|
|
203
|
+
.into_iter()
|
|
204
|
+
.filter_map(|(cell, value)| value.map(|value| (cell, value)))
|
|
205
|
+
.collect()
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// Decoder initialization commonly stores property names in cells for nested collectors. Only
|
|
209
|
+
/// propagate decoded strings whose captured binding has no other assignment in its scope.
|
|
210
|
+
fn fold_cells(listing: &mut Listing, decoded_at: &HashSet<u32>) {
|
|
211
|
+
let index = Index::new(listing);
|
|
212
|
+
let constants = captured_values(&index, |pc, value| {
|
|
213
|
+
let (at, Term::Const(Constant::String(text))) = index.dereference(pc, value)? else {
|
|
214
|
+
return None;
|
|
215
|
+
};
|
|
216
|
+
(decoded_at.contains(&at) && index.entry_at_or_before(at) == index.entry_at_or_before(pc))
|
|
217
|
+
.then_some(text)
|
|
218
|
+
});
|
|
219
|
+
let entries = index.entries().clone();
|
|
220
|
+
for (&pc, instruction) in &mut listing.instructions {
|
|
221
|
+
let Some(&entry) = entries.range(..=pc).next_back() else {
|
|
222
|
+
continue;
|
|
223
|
+
};
|
|
224
|
+
for path in &mut instruction.paths {
|
|
225
|
+
for effect in &mut path.effects {
|
|
226
|
+
if let Effect::Set(_, term @ Term::Cell(_)) = effect
|
|
227
|
+
&& let Term::Cell(cell) = term
|
|
228
|
+
&& let Some(text) = constants.get(&(entry, *cell))
|
|
229
|
+
{
|
|
230
|
+
*term = Term::Const(Constant::String(text.clone()));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/// Recognizes the decoder's byte rotation and its base64 / UTF-16 operations. The seed must
|
|
238
|
+
/// be a literal assignment before the first rotation; later iterations cannot invent a key.
|
|
239
|
+
fn decoder(index: &Index<'_>, entry: u32) -> Option<u16> {
|
|
240
|
+
let mut methods = HashSet::new();
|
|
241
|
+
let mut seeds = BTreeSet::new();
|
|
242
|
+
let mut escape = false;
|
|
243
|
+
for (pc, effect) in index.effects_in(index.function_at(entry)) {
|
|
244
|
+
effect.visit_terms(&mut |term| {
|
|
245
|
+
if let Term::Call { callee, .. } = term
|
|
246
|
+
&& let Some(method) = index.method(pc, callee)
|
|
247
|
+
{
|
|
248
|
+
methods.insert(method);
|
|
249
|
+
}
|
|
250
|
+
if let Term::Const(Constant::String(text)) = term {
|
|
251
|
+
methods.insert(text.clone());
|
|
252
|
+
}
|
|
253
|
+
if matches!(term, Term::Const(Constant::Number(255.0))) {
|
|
254
|
+
escape = true;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
if let Effect::Set(register, term) = effect
|
|
258
|
+
&& let Some(expanded) = expand(index, pc, term, *register, 14)
|
|
259
|
+
&& rotation(&expanded, *register)
|
|
260
|
+
&& let Some((_, Term::Const(Constant::Number(seed)))) = index.assignment(pc, *register)
|
|
261
|
+
&& (0.0..=65535.0).contains(&seed)
|
|
262
|
+
&& seed.fract() == 0.0
|
|
263
|
+
{
|
|
264
|
+
seeds.insert(seed as u16);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
(escape
|
|
268
|
+
&& ["atob", "push", "fromCharCode"]
|
|
269
|
+
.iter()
|
|
270
|
+
.all(|method| methods.contains(*method))
|
|
271
|
+
&& seeds.len() == 1)
|
|
272
|
+
.then(|| *seeds.first().unwrap())
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
fn expand(index: &Index<'_>, pc: u32, term: &Term, stop: u8, fuel: usize) -> Option<Term> {
|
|
276
|
+
if fuel == 0 {
|
|
277
|
+
return None;
|
|
278
|
+
}
|
|
279
|
+
match term {
|
|
280
|
+
Term::Reg(register) if *register != stop => {
|
|
281
|
+
let (at, value) = index.assignment(pc, *register)?;
|
|
282
|
+
expand(index, at, &value, stop, fuel - 1)
|
|
283
|
+
}
|
|
284
|
+
Term::Binary(operator, left, right) => {
|
|
285
|
+
let left = expand(index, pc, left, stop, fuel - 1)?;
|
|
286
|
+
let right = expand(index, pc, right, stop, fuel - 1)?;
|
|
287
|
+
if *operator == BinaryOperator::BitwiseXOR {
|
|
288
|
+
if left == Term::Const(Constant::Number(0.0)) {
|
|
289
|
+
return Some(right);
|
|
290
|
+
}
|
|
291
|
+
if right == Term::Const(Constant::Number(0.0)) {
|
|
292
|
+
return Some(left);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
Some(Term::Binary(*operator, Box::new(left), Box::new(right)))
|
|
296
|
+
}
|
|
297
|
+
Term::Reg(_) | Term::Const(Constant::Number(_)) => Some(term.clone()),
|
|
298
|
+
_ => None,
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
fn rotation(term: &Term, register: u8) -> bool {
|
|
303
|
+
let Term::Binary(BinaryOperator::BitwiseAnd, left, right) = term else {
|
|
304
|
+
return false;
|
|
305
|
+
};
|
|
306
|
+
let rotated = if **left == Term::Const(Constant::Number(65535.0)) {
|
|
307
|
+
right
|
|
308
|
+
} else if **right == Term::Const(Constant::Number(65535.0)) {
|
|
309
|
+
left
|
|
310
|
+
} else {
|
|
311
|
+
return false;
|
|
312
|
+
};
|
|
313
|
+
let Term::Binary(BinaryOperator::BitwiseOR, left, right) = &**rotated else {
|
|
314
|
+
return false;
|
|
315
|
+
};
|
|
316
|
+
let shift = |term: &Term, operator| {
|
|
317
|
+
matches!(term, Term::Binary(op, value, count)
|
|
318
|
+
if *op == operator && **value == Term::Reg(register)
|
|
319
|
+
&& **count == Term::Const(Constant::Number(8.0)))
|
|
320
|
+
};
|
|
321
|
+
(shift(left, BinaryOperator::ShiftLeft) && shift(right, BinaryOperator::ShiftRightZeroFill))
|
|
322
|
+
|| (shift(right, BinaryOperator::ShiftLeft)
|
|
323
|
+
&& shift(left, BinaryOperator::ShiftRightZeroFill))
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
fn decode(encoded: &str, mut key: u16) -> Option<String> {
|
|
327
|
+
let bytes = STANDARD.decode(encoded).ok()?;
|
|
328
|
+
let mut bytes = bytes.into_iter().map(|byte| {
|
|
329
|
+
key = key.rotate_left(8);
|
|
330
|
+
byte ^ (key >> 8) as u8
|
|
331
|
+
});
|
|
332
|
+
let mut units = Vec::new();
|
|
333
|
+
while let Some(byte) = bytes.next() {
|
|
334
|
+
units.push(if byte == 255 {
|
|
335
|
+
u16::from(bytes.next()?) | (u16::from(bytes.next()?) << 8)
|
|
336
|
+
} else {
|
|
337
|
+
u16::from(byte)
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
String::from_utf16(&units).ok()
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
#[cfg(test)]
|
|
344
|
+
mod tests {
|
|
345
|
+
use super::*;
|
|
346
|
+
use crate::vm::{Instruction, Path, Position};
|
|
347
|
+
|
|
348
|
+
#[test]
|
|
349
|
+
fn decodes_captured_names_and_utf16_escapes() {
|
|
350
|
+
assert_eq!(
|
|
351
|
+
decode("Ca8AsCyyC7g/sgazGw==", 56687).as_deref(),
|
|
352
|
+
Some("fromCodePoint")
|
|
353
|
+
);
|
|
354
|
+
assert_eq!(decode("Argcrg66Cg==", 56687).as_deref(), Some("message"));
|
|
355
|
+
// Literal 255, a surrogate pair, then ASCII. Every escaped byte advances the key.
|
|
356
|
+
let clear = [255, 255, 0, 255, 0x3d, 0xd8, 255, 0, 0xde, b'!'];
|
|
357
|
+
let cipher: Vec<u8> = clear
|
|
358
|
+
.iter()
|
|
359
|
+
.enumerate()
|
|
360
|
+
.map(|(i, byte)| byte ^ [0x34, 0x12][i % 2])
|
|
361
|
+
.collect();
|
|
362
|
+
assert_eq!(
|
|
363
|
+
decode(&STANDARD.encode(cipher), 0x1234).as_deref(),
|
|
364
|
+
Some("ÿ😀!")
|
|
365
|
+
);
|
|
366
|
+
assert!(decode("not base64!", 0).is_none());
|
|
367
|
+
assert!(decode(&STANDARD.encode([255, 1]), 0).is_none());
|
|
368
|
+
assert!(decode(&STANDARD.encode([255, 0, 0xd8]), 0).is_none());
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
fn number(value: f64) -> Term {
|
|
372
|
+
Term::Const(Constant::Number(value))
|
|
373
|
+
}
|
|
374
|
+
fn string(value: &str) -> Term {
|
|
375
|
+
Term::Const(Constant::String(value.to_owned()))
|
|
376
|
+
}
|
|
377
|
+
fn binary(op: BinaryOperator, left: Term, right: Term) -> Term {
|
|
378
|
+
Term::Binary(op, Box::new(left), Box::new(right))
|
|
379
|
+
}
|
|
380
|
+
fn rotate(mask: f64) -> Term {
|
|
381
|
+
binary(
|
|
382
|
+
BinaryOperator::BitwiseAnd,
|
|
383
|
+
number(mask),
|
|
384
|
+
binary(
|
|
385
|
+
BinaryOperator::BitwiseOR,
|
|
386
|
+
binary(BinaryOperator::ShiftLeft, Term::Reg(7), number(8.0)),
|
|
387
|
+
binary(
|
|
388
|
+
BinaryOperator::ShiftRightZeroFill,
|
|
389
|
+
Term::Reg(7),
|
|
390
|
+
number(8.0),
|
|
391
|
+
),
|
|
392
|
+
),
|
|
393
|
+
)
|
|
394
|
+
}
|
|
395
|
+
fn closure(pc: u32) -> Term {
|
|
396
|
+
Term::Closure {
|
|
397
|
+
pc,
|
|
398
|
+
key: 0,
|
|
399
|
+
parameters: vec![],
|
|
400
|
+
arity: 1,
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
fn call(callee: Term, argument: Term) -> Term {
|
|
404
|
+
Term::Call {
|
|
405
|
+
callee: Box::new(callee),
|
|
406
|
+
this: None,
|
|
407
|
+
arguments: vec![argument],
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
fn insert(listing: &mut Listing, pc: u32, effects: Vec<Effect>, exit: Exit) {
|
|
411
|
+
listing.instructions.insert(
|
|
412
|
+
pc,
|
|
413
|
+
Instruction {
|
|
414
|
+
pc,
|
|
415
|
+
key: 0,
|
|
416
|
+
opcode: 0,
|
|
417
|
+
paths: vec![Path {
|
|
418
|
+
effects,
|
|
419
|
+
conditions: vec![],
|
|
420
|
+
exit,
|
|
421
|
+
}],
|
|
422
|
+
},
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
#[test]
|
|
427
|
+
fn folds_decoder_cells_only_inside_their_closure_scope() {
|
|
428
|
+
let mut listing = Listing {
|
|
429
|
+
functions: [0, 100, 200, 300, 400, 500]
|
|
430
|
+
.map(|pc| Position::new(pc, 0))
|
|
431
|
+
.into(),
|
|
432
|
+
..Listing::default()
|
|
433
|
+
};
|
|
434
|
+
let next = |pc| Exit::Next { pc, key: 0 };
|
|
435
|
+
// Decoder, factory, creator, child, unrelated function, shadowing child.
|
|
436
|
+
insert(
|
|
437
|
+
&mut listing,
|
|
438
|
+
0,
|
|
439
|
+
vec![Effect::Set(7, number(56687.0))],
|
|
440
|
+
next(1),
|
|
441
|
+
);
|
|
442
|
+
insert(
|
|
443
|
+
&mut listing,
|
|
444
|
+
1,
|
|
445
|
+
vec![
|
|
446
|
+
Effect::Set(7, rotate(65535.0)),
|
|
447
|
+
Effect::Set(8, string("atob")),
|
|
448
|
+
Effect::Set(9, string("push")),
|
|
449
|
+
Effect::Set(10, string("fromCharCode")),
|
|
450
|
+
Effect::Set(11, number(255.0)),
|
|
451
|
+
],
|
|
452
|
+
Exit::Return,
|
|
453
|
+
);
|
|
454
|
+
insert(
|
|
455
|
+
&mut listing,
|
|
456
|
+
100,
|
|
457
|
+
vec![Effect::Set(1, closure(0))],
|
|
458
|
+
next(101),
|
|
459
|
+
);
|
|
460
|
+
insert(
|
|
461
|
+
&mut listing,
|
|
462
|
+
101,
|
|
463
|
+
vec![Effect::Set(63, Term::Reg(1))],
|
|
464
|
+
Exit::Return,
|
|
465
|
+
);
|
|
466
|
+
insert(
|
|
467
|
+
&mut listing,
|
|
468
|
+
200,
|
|
469
|
+
vec![Effect::Set(1, closure(100))],
|
|
470
|
+
next(201),
|
|
471
|
+
);
|
|
472
|
+
insert(
|
|
473
|
+
&mut listing,
|
|
474
|
+
201,
|
|
475
|
+
vec![
|
|
476
|
+
Effect::Let(0, call(Term::Reg(1), Term::Global("globalThis".into()))),
|
|
477
|
+
Effect::Set(2, Term::Temp(0)),
|
|
478
|
+
],
|
|
479
|
+
next(202),
|
|
480
|
+
);
|
|
481
|
+
insert(
|
|
482
|
+
&mut listing,
|
|
483
|
+
202,
|
|
484
|
+
vec![
|
|
485
|
+
Effect::SetCell(17, Term::Reg(2)),
|
|
486
|
+
Effect::Set(3, closure(300)),
|
|
487
|
+
Effect::Set(4, closure(500)),
|
|
488
|
+
Effect::Set(5, string("Ca8AsCyyC7g/sgazGw==")),
|
|
489
|
+
Effect::Set(8, string("Argcrg66Cg==")),
|
|
490
|
+
],
|
|
491
|
+
next(203),
|
|
492
|
+
);
|
|
493
|
+
insert(
|
|
494
|
+
&mut listing,
|
|
495
|
+
203,
|
|
496
|
+
vec![
|
|
497
|
+
Effect::Let(0, call(Term::Reg(2), Term::Reg(5))),
|
|
498
|
+
Effect::Set(6, Term::Temp(0)),
|
|
499
|
+
Effect::Let(1, call(Term::Reg(2), Term::Reg(8))),
|
|
500
|
+
Effect::Set(9, Term::Temp(1)),
|
|
501
|
+
],
|
|
502
|
+
Exit::Return,
|
|
503
|
+
);
|
|
504
|
+
for entry in [300, 400, 500] {
|
|
505
|
+
let mut effects = vec![Effect::Set(1, string("Ca8AsCyyC7g/sgazGw=="))];
|
|
506
|
+
if entry == 500 {
|
|
507
|
+
effects.push(Effect::NewCell(17));
|
|
508
|
+
}
|
|
509
|
+
insert(&mut listing, entry, effects, next(entry + 1));
|
|
510
|
+
insert(
|
|
511
|
+
&mut listing,
|
|
512
|
+
entry + 1,
|
|
513
|
+
vec![
|
|
514
|
+
Effect::Let(0, call(Term::Cell(17), Term::Reg(1))),
|
|
515
|
+
Effect::Set(2, Term::Temp(0)),
|
|
516
|
+
],
|
|
517
|
+
Exit::Return,
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
let mut not_a_factory = listing.clone();
|
|
521
|
+
not_a_factory.instructions.get_mut(&101).unwrap().paths[0].effects = vec![
|
|
522
|
+
Effect::Set(63, Term::Const(Constant::Null)),
|
|
523
|
+
Effect::Set(10, closure(0)),
|
|
524
|
+
];
|
|
525
|
+
fold(&mut not_a_factory);
|
|
526
|
+
assert!(
|
|
527
|
+
not_a_factory.decoded_inputs.is_empty(),
|
|
528
|
+
"merely storing a decoder does not return it"
|
|
529
|
+
);
|
|
530
|
+
fold(&mut listing);
|
|
531
|
+
assert_eq!(
|
|
532
|
+
listing.instructions[&203].paths[0].effects[3],
|
|
533
|
+
Effect::Set(9, string("message"))
|
|
534
|
+
);
|
|
535
|
+
assert_eq!(
|
|
536
|
+
listing.decoded_inputs[&202],
|
|
537
|
+
BTreeSet::from(["Ca8AsCyyC7g/sgazGw==".to_owned(), "Argcrg66Cg==".to_owned(),])
|
|
538
|
+
);
|
|
539
|
+
assert_eq!(
|
|
540
|
+
listing.instructions[&203].paths[0].effects[1],
|
|
541
|
+
Effect::Set(6, string("fromCodePoint"))
|
|
542
|
+
);
|
|
543
|
+
assert_eq!(
|
|
544
|
+
listing.decoded_inputs.get(&300),
|
|
545
|
+
Some(&BTreeSet::from(["Ca8AsCyyC7g/sgazGw==".to_owned()]))
|
|
546
|
+
);
|
|
547
|
+
assert_eq!(
|
|
548
|
+
listing.instructions[&301].paths[0].effects[1],
|
|
549
|
+
Effect::Set(2, string("fromCodePoint"))
|
|
550
|
+
);
|
|
551
|
+
for pc in [401, 501] {
|
|
552
|
+
assert_eq!(
|
|
553
|
+
listing.instructions[&pc].paths[0].effects[1],
|
|
554
|
+
Effect::Set(2, Term::Temp(0))
|
|
555
|
+
);
|
|
556
|
+
}
|
|
557
|
+
assert!(rotation(&rotate(65535.0), 7));
|
|
558
|
+
assert!(!rotation(&rotate(255.0), 7));
|
|
559
|
+
assert!(!rotation(&rotate(65535.0), 8));
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
#[test]
|
|
563
|
+
fn unknown_reassignments_invalidate_captured_values() {
|
|
564
|
+
for (write_pc, write) in [(0, Term::Reg(9)), (10, Term::Reg(9))] {
|
|
565
|
+
let mut listing = Listing {
|
|
566
|
+
functions: [0, 10].map(|pc| Position::new(pc, 0)).into(),
|
|
567
|
+
..Listing::default()
|
|
568
|
+
};
|
|
569
|
+
insert(
|
|
570
|
+
&mut listing,
|
|
571
|
+
0,
|
|
572
|
+
vec![
|
|
573
|
+
Effect::SetCell(7, number(42.0)),
|
|
574
|
+
Effect::Set(1, closure(10)),
|
|
575
|
+
],
|
|
576
|
+
Exit::Return,
|
|
577
|
+
);
|
|
578
|
+
insert(&mut listing, 10, vec![], Exit::Return);
|
|
579
|
+
listing.instructions.get_mut(&write_pc).unwrap().paths[0]
|
|
580
|
+
.effects
|
|
581
|
+
.push(Effect::SetCell(7, write));
|
|
582
|
+
let index = Index::new(&listing);
|
|
583
|
+
let values = captured_values(&index, |_, term| match term {
|
|
584
|
+
Term::Const(Constant::Number(value)) => Some(*value),
|
|
585
|
+
_ => None,
|
|
586
|
+
});
|
|
587
|
+
assert!(
|
|
588
|
+
values.is_empty(),
|
|
589
|
+
"an unknown write at {write_pc} must invalidate the binding"
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
#[test]
|
|
595
|
+
fn propagates_decoded_cells_but_keeps_mutable_bindings_unknown() {
|
|
596
|
+
for mutable in [false, true] {
|
|
597
|
+
let mut listing = Listing {
|
|
598
|
+
functions: [0, 10].map(|pc| Position::new(pc, 0)).into(),
|
|
599
|
+
..Listing::default()
|
|
600
|
+
};
|
|
601
|
+
insert(
|
|
602
|
+
&mut listing,
|
|
603
|
+
0,
|
|
604
|
+
vec![Effect::Set(1, string("length"))],
|
|
605
|
+
Exit::Next { pc: 1, key: 0 },
|
|
606
|
+
);
|
|
607
|
+
insert(
|
|
608
|
+
&mut listing,
|
|
609
|
+
1,
|
|
610
|
+
vec![
|
|
611
|
+
Effect::SetCell(7, Term::Reg(1)),
|
|
612
|
+
Effect::Set(2, closure(10)),
|
|
613
|
+
],
|
|
614
|
+
Exit::Return,
|
|
615
|
+
);
|
|
616
|
+
let mut effects = vec![Effect::Set(3, Term::Cell(7))];
|
|
617
|
+
if mutable {
|
|
618
|
+
effects.push(Effect::SetCell(7, string("changed")));
|
|
619
|
+
}
|
|
620
|
+
insert(&mut listing, 10, effects, Exit::Return);
|
|
621
|
+
fold_cells(&mut listing, &HashSet::from([0]));
|
|
622
|
+
assert_eq!(
|
|
623
|
+
listing.instructions[&10].paths[0].effects[0],
|
|
624
|
+
Effect::Set(
|
|
625
|
+
3,
|
|
626
|
+
if mutable {
|
|
627
|
+
Term::Cell(7)
|
|
628
|
+
} else {
|
|
629
|
+
string("length")
|
|
630
|
+
}
|
|
631
|
+
)
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|