1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Bounded, context-sensitive expression dependencies across direct helper calls.
use super::{backward_slice, SliceBlock, SliceNode, MAX_DEPTH, MAX_INPUTS, MAX_NODES};
use crate::{
callgraph::{dependency_call, DependencyCall},
fold::CallingConv,
function_summary::{dependency_summary, DependencySummary},
ir::{SsaCfg, VarId},
provenance::OperationOrigin,
};
use pcode_ir::PcodeOp;
use serde::Serialize;
use std::{
collections::{HashMap, HashSet, VecDeque},
rc::Rc,
};
pub trait Function {
fn ssa(&self) -> &SsaCfg;
fn operation(&self, origin: OperationOrigin) -> Option<&PcodeOp>;
}
#[derive(Clone, Copy, Debug, Serialize)]
pub struct Limits {
pub max_nodes: usize,
pub max_depth: usize,
pub max_call_depth: usize,
pub max_functions: usize,
pub max_work: usize,
}
impl Limits {
pub fn bounded(self) -> Self {
Self {
max_nodes: self.max_nodes.min(MAX_NODES),
max_depth: self.max_depth.min(MAX_DEPTH),
max_call_depth: self.max_call_depth.min(8),
max_functions: self.max_functions.min(32),
max_work: self.max_work.min(1_000_000),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct Reference {
pub context_id: usize,
pub var_id: u32,
}
#[derive(Debug, Serialize)]
pub struct Link {
pub kind: &'static str,
pub target: Reference,
pub stop_reason: Option<&'static str>,
}
#[derive(Debug, Serialize)]
pub struct Node {
#[serde(flatten)]
pub node: SliceNode,
pub context_id: usize,
pub function_address: u64,
pub links: Vec<Link>,
pub call: Option<DependencyCall>,
}
#[derive(Debug, Serialize)]
pub struct Context {
pub id: usize,
pub function_address: u64,
pub caller_context: Option<usize>,
pub call_origin: Option<OperationOrigin>,
pub call_depth: usize,
}
#[derive(Debug, Serialize)]
pub struct Block {
#[serde(flatten)]
pub block: SliceBlock,
pub context_id: usize,
}
#[derive(Debug, Default, Serialize)]
pub struct Metrics {
pub functions_visited: usize,
pub contexts_created: usize,
pub traversal_work: usize,
}
#[derive(Debug, Serialize)]
pub struct Slice {
pub root: u32,
pub root_context: usize,
pub nodes: Vec<Node>,
pub blocks: Vec<Block>,
pub contexts: Vec<Context>,
pub complete: bool,
pub truncated: bool,
pub limits: Limits,
pub metrics: Metrics,
pub stops: Vec<String>,
}
struct Frame<F> {
function: Rc<F>,
caller_arguments: HashMap<usize, Reference>,
}
fn charge(result: &mut Slice, amount: usize) -> bool {
if amount
> result
.limits
.max_work
.saturating_sub(result.metrics.traversal_work)
{
if !result.stops.iter().any(|s| s == "traversal_work_limit") {
result.stops.push("traversal_work_limit".into());
}
result.truncated = true;
return false;
}
result.metrics.traversal_work += amount;
true
}
/// `load` must return snapshots for the same binary/build/options. The caller
/// owns caching and decoding budgets; this layer bounds traversal separately.
pub fn backward<F: Function>(
address: u64,
root: VarId,
function: Rc<F>,
cc: CallingConv,
imports: &HashMap<u64, String>,
limits: Limits,
mut load: impl FnMut(u64) -> Result<Rc<F>, String>,
) -> Result<Slice, String> {
let limits = limits.bounded();
if limits.max_nodes == 0 || limits.max_functions == 0 {
return Err("node and function limits must be positive".into());
}
let mut result = Slice {
root: root.0,
root_context: 0,
nodes: vec![],
blocks: vec![],
contexts: vec![Context {
id: 0,
function_address: address,
caller_context: None,
call_origin: None,
call_depth: 0,
}],
complete: false,
truncated: false,
limits,
metrics: Metrics::default(),
stops: vec![],
};
let mut frames = vec![Frame {
function,
caller_arguments: HashMap::new(),
}];
let mut summaries: HashMap<u64, DependencySummary> = HashMap::new();
let mut functions = HashSet::from([address]);
let mut visited = HashSet::new();
let mut jobs = VecDeque::from([(
Reference {
context_id: 0,
var_id: root.0,
},
0,
)]);
let mut inputs_used = 0;
let mut block_seen = HashSet::new();
let attempt =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result<(), String> {
while let Some((reference, depth)) = jobs.pop_front() {
if visited.contains(&reference) {
continue;
}
if depth > limits.max_depth || result.nodes.len() >= limits.max_nodes {
result.truncated = true;
result.stops.push(
if depth > limits.max_depth {
"depth_limit"
} else {
"node_limit"
}
.into(),
);
continue;
}
let context_id = reference.context_id;
let address = result.contexts[context_id].function_address;
let function = Rc::clone(&frames[context_id].function);
let ssa = function.ssa();
// Account for scans needed by summaries and the local slice before
// running them. Traversal work includes variables, statements,
// nodes, edges, and function admissions; decode/SSA work is separate.
let scan_work = ssa
.vars
.len()
.saturating_add(ssa.blocks.iter().map(|b| 1 + b.stmts.len()).sum::<usize>());
if !charge(&mut result, scan_work) {
break;
}
summaries
.entry(address)
.or_insert_with(|| crate::budget::traversal(|| dependency_summary(ssa, cc)));
let local = backward_slice(
ssa,
VarId(reference.var_id),
limits.max_nodes - result.nodes.len(),
limits.max_depth - depth,
)?;
result.truncated |= local.truncated;
for block in local.blocks {
if block_seen.insert((context_id, block.id)) {
if result.blocks.len() < MAX_NODES {
result.blocks.push(Block { block, context_id });
} else {
result.truncated = true;
result.stops.push("block_limit".into());
}
}
}
for mut node in local.nodes {
let reference = Reference {
context_id,
var_id: node.var_id,
};
if !visited.insert(reference) {
continue;
}
if !charge(&mut result, 1 + node.inputs.len()) {
break;
}
node.depth += depth;
let available = MAX_INPUTS.saturating_sub(inputs_used);
if node.inputs.len() > available {
node.inputs.truncate(available);
result.truncated = true;
result.stops.push("input_limit".into());
}
inputs_used += node.inputs.len();
let slot = summaries[&address]
.parameters
.get(&VarId(node.var_id))
.copied();
let mut output = Node {
node,
context_id,
function_address: address,
links: vec![],
call: None,
};
if let Some(slot) = slot.filter(|_| context_id != 0) {
if let Some(&actual) = frames[context_id].caller_arguments.get(&slot) {
output.node.boundary = None;
// A bound stack parameter is supplied by the caller;
// its address is not another data dependency to follow.
output.node.inputs.clear();
output.node.inputs_total = 0;
output.links.push(Link {
kind: "argument_binding",
target: actual,
stop_reason: None,
});
jobs.push_back((actual, output.node.depth + 1));
} else {
output.node.boundary = Some("missing_call_argument");
}
} else if output.node.boundary == Some("unmodeled_call") {
match crate::budget::traversal(|| {
dependency_call(ssa, VarId(output.node.var_id), cc, imports, |origin| {
function.operation(origin)
})
}) {
Err(reason) => output.node.boundary = Some(reason),
Ok(call) => {
let target = call.target_address;
let mut reason = if call.import.is_some() {
Some("external_call")
} else if target.is_none() {
Some("unknown_call")
} else {
None
};
if reason.is_none() && result.nodes.len() + 1 >= limits.max_nodes {
reason = Some("node_limit");
}
let next_depth = result.contexts[context_id].call_depth + 1;
if reason.is_none() && next_depth > limits.max_call_depth {
reason = Some("call_depth_limit");
}
let mut ancestor = Some(context_id);
while let Some(id) = ancestor {
if target == Some(result.contexts[id].function_address)
&& reason.is_none()
{
reason = Some("recursion_limit");
}
ancestor = result.contexts[id].caller_context;
}
if let Some(target) = target.filter(|_| reason.is_none()) {
if !functions.contains(&target)
&& functions.len() >= limits.max_functions
{
reason = Some("function_limit");
} else if !charge(&mut result, 1) {
reason = Some("traversal_work_limit");
} else {
functions.insert(target);
match load(target) {
Err(error) => {
result.stops.push(error);
reason = Some("function_unavailable");
}
Ok(callee) => {
functions.insert(target);
let callee_ssa = callee.ssa();
let cost = callee_ssa.vars.len().saturating_add(
callee_ssa
.blocks
.iter()
.map(|b| 1 + b.stmts.len())
.sum::<usize>(),
);
if !charge(&mut result, cost) {
reason = Some("traversal_work_limit");
} else {
let summary = summaries
.entry(target)
.or_insert_with(|| {
crate::budget::traversal(|| {
dependency_summary(callee_ssa, cc)
})
});
if summary.unsupported_side_effects {
reason = Some("unsupported_side_effects");
} else if summary.returns.is_empty() {
reason = Some("unsupported_return");
} else if result.contexts.len()
>= limits.max_nodes
{
reason = Some("context_limit");
} else {
let id = result.contexts.len();
result.contexts.push(Context {
id,
function_address: target,
caller_context: Some(context_id),
call_origin: Some(call.origin),
call_depth: next_depth,
});
frames.push(Frame {
function: callee,
caller_arguments: call
.arguments
.iter()
.map(|(&slot, value)| {
(
slot,
Reference {
context_id,
var_id: value.0,
},
)
})
.collect(),
});
for &ret in &summary.returns {
let to = Reference {
context_id: id,
var_id: ret.0,
};
output.links.push(Link {
kind: "call_return",
target: to,
stop_reason: None,
});
jobs.push_back((
to,
output.node.depth + 1,
));
}
}
}
}
}
}
}
if reason.is_some_and(|r| {
matches!(
r,
"call_depth_limit"
| "recursion_limit"
| "function_limit"
| "traversal_work_limit"
| "context_limit"
| "node_limit"
)
}) {
result.truncated = true;
}
output.node.boundary = reason;
output.call = Some(call);
}
}
}
if !charge(&mut result, output.links.len()) {
output.node.boundary = Some("traversal_work_limit");
}
let available = MAX_INPUTS.saturating_sub(inputs_used);
if output.links.len() > available {
output.links.truncate(available);
output.node.boundary = Some("input_limit");
result.truncated = true;
}
inputs_used += output.links.len();
result.nodes.push(output);
if inputs_used >= MAX_INPUTS {
result.truncated = true;
result.stops.push("input_limit".into());
break;
}
}
}
Ok(())
}));
match attempt {
Ok(result) => result?,
Err(_) if crate::budget::stopped().is_some() => {
result.stops.push("execution_limit".into());
result.truncated = true;
}
Err(_) => return Err("dependency traversal panicked".into()),
}
let included: HashSet<_> = result
.nodes
.iter()
.map(|n| Reference {
context_id: n.context_id,
var_id: n.node.var_id,
})
.collect();
for node in &mut result.nodes {
for input in &mut node.node.inputs {
if input.stop_reason.is_none()
&& !included.contains(&Reference {
context_id: node.context_id,
var_id: input.var_id,
})
{
input.stop_reason = Some("traversal_limit");
}
}
for link in &mut node.links {
if !included.contains(&link.target) {
link.stop_reason = Some("traversal_limit");
}
}
}
result.metrics.functions_visited = functions.len();
result.metrics.contexts_created = result.contexts.len();
result.complete = !result.truncated
&& result.stops.is_empty()
&& result.nodes.iter().all(|n| {
n.node.boundary.is_none()
&& n.node.inputs.iter().all(|i| i.stop_reason.is_none())
&& n.links.iter().all(|l| l.stop_reason.is_none())
});
Ok(result)
}