-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathoptimized_inline_call_value_execution.rs
More file actions
337 lines (302 loc) · 11.7 KB
/
optimized_inline_call_value_execution.rs
File metadata and controls
337 lines (302 loc) · 11.7 KB
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
mod common;
use common::{init, FIXTURES};
use ghostscope_dwarf::{DirectValueResult, EvaluationResult};
use regex::Regex;
use std::path::Path;
use std::time::Duration;
// Keep this on the first executable line before consume_pair() is called.
const INLINE_BEFORE_CALL_TRACE_LINE: u32 = 20;
// Keep this on the first executable line after consume_pair() returns.
const INLINE_AFTER_CALL_TRACE_LINE: u32 = 22;
async fn spawn_inline_call_value_program(
binary_path: &Path,
) -> anyhow::Result<common::targets::TargetHandle> {
let bin_dir = binary_path
.parent()
.ok_or_else(|| anyhow::anyhow!("inline_call_value_program has no parent directory"))?;
let target = common::targets::TargetLauncher::binary(binary_path)
.current_dir(bin_dir)
.spawn()
.await?;
tokio::time::sleep(Duration::from_millis(500)).await;
Ok(target)
}
fn should_skip_for_ebpf_env(exit_code: i32, stderr: &str) -> bool {
exit_code != 0
&& (stderr.contains("BPF_PROG_LOAD")
|| stderr.contains("needs elevated privileges")
|| stderr.contains("cap_bpf"))
}
async fn run_ghostscope_with_script_for_target(
script_content: &str,
timeout_secs: u64,
target: &common::targets::TargetHandle,
) -> anyhow::Result<(i32, String, String)> {
common::runner::GhostscopeRunner::new()
.with_script(script_content)
.attach_to(target)
.timeout_secs(timeout_secs)
.enable_sysmon_shared_lib(false)
.run()
.await
}
fn assert_not_internal_call_register_aliases(
parameters: &[ghostscope_dwarf::VariableWithEvaluation],
address: u64,
) -> anyhow::Result<()> {
let original_x = parameters
.iter()
.find(|param| param.name == "original_x")
.ok_or_else(|| anyhow::anyhow!("missing original_x at 0x{:x}", address))?;
let original_y = parameters
.iter()
.find(|param| param.name == "original_y")
.ok_or_else(|| anyhow::anyhow!("missing original_y at 0x{:x}", address))?;
assert_ne!(
original_x.evaluation_result,
EvaluationResult::DirectValue(DirectValueResult::RegisterValue(5)),
"original_x aliased consume_pair's first argument register at 0x{address:x}: {parameters:?}"
);
assert_ne!(
original_y.evaluation_result,
EvaluationResult::DirectValue(DirectValueResult::RegisterValue(4)),
"original_y aliased consume_pair's second argument register at 0x{address:x}: {parameters:?}"
);
Ok(())
}
fn assert_parameters_are_live_in_registers(
parameters: &[ghostscope_dwarf::VariableWithEvaluation],
address: u64,
) -> anyhow::Result<()> {
for parameter_name in ["original_x", "original_y"] {
let parameter = parameters
.iter()
.find(|param| param.name == parameter_name)
.ok_or_else(|| anyhow::anyhow!("missing {parameter_name} at 0x{address:x}"))?;
assert!(
!matches!(parameter.evaluation_result, EvaluationResult::Optimized),
"{parameter_name} should still be live before consume_pair() at 0x{address:x}: {parameters:?}"
);
assert!(
matches!(
parameter.evaluation_result,
EvaluationResult::DirectValue(DirectValueResult::RegisterValue(_))
),
"{parameter_name} should resolve to a direct register value before consume_pair() at 0x{address:x}: {parameters:?}"
);
}
Ok(())
}
#[tokio::test]
async fn test_optimized_inline_parameters_are_live_before_internal_call() -> anyhow::Result<()> {
init();
let binary_path = FIXTURES.get_test_binary("inline_call_value_program")?;
let analyzer = ghostscope_dwarf::DwarfAnalyzer::from_exec_path(&binary_path).await?;
let addrs = analyzer.lookup_addresses_by_source_line(
"inline_call_value_program.c",
INLINE_BEFORE_CALL_TRACE_LINE,
);
anyhow::ensure!(
!addrs.is_empty(),
"No DWARF addresses found for inline_call_value_program.c:{INLINE_BEFORE_CALL_TRACE_LINE}"
);
for module_address in &addrs {
anyhow::ensure!(
analyzer.is_inline_at(module_address) == Some(true),
"Expected inline address at 0x{:x}",
module_address.address
);
}
let query_results = analyzer.query_source_line_best_effort(
"inline_call_value_program.c",
INLINE_BEFORE_CALL_TRACE_LINE,
)?;
anyhow::ensure!(
!query_results.is_empty(),
"No query results for inline_call_value_program.c:{INLINE_BEFORE_CALL_TRACE_LINE}"
);
for result in &query_results {
assert_parameters_are_live_in_registers(&result.parameters, result.address)?;
}
let target = spawn_inline_call_value_program(&binary_path).await?;
let pid_analyzer = ghostscope_dwarf::DwarfAnalyzer::from_pid(target.host_pid()).await?;
let pid_results = pid_analyzer.query_source_line_best_effort(
"inline_call_value_program.c",
INLINE_BEFORE_CALL_TRACE_LINE,
)?;
anyhow::ensure!(
!pid_results.is_empty(),
"No PID-backed query results for inline_call_value_program.c:{INLINE_BEFORE_CALL_TRACE_LINE}"
);
for result in &pid_results {
assert_parameters_are_live_in_registers(&result.parameters, result.address)?;
}
target.terminate().await?;
Ok(())
}
#[tokio::test]
async fn test_optimized_inline_parameters_have_exact_values_before_internal_call(
) -> anyhow::Result<()> {
init();
let binary_path = FIXTURES.get_test_binary("inline_call_value_program")?;
let analyzer = ghostscope_dwarf::DwarfAnalyzer::from_exec_path(&binary_path).await?;
let addrs = analyzer.lookup_addresses_by_source_line(
"inline_call_value_program.c",
INLINE_BEFORE_CALL_TRACE_LINE,
);
anyhow::ensure!(
!addrs.is_empty(),
"No DWARF addresses found for inline_call_value_program.c:{INLINE_BEFORE_CALL_TRACE_LINE}"
);
for module_address in &addrs {
anyhow::ensure!(
analyzer.is_inline_at(module_address) == Some(true),
"Expected inline address at 0x{:x}",
module_address.address
);
}
let target = spawn_inline_call_value_program(&binary_path).await?;
let script = format!(
"trace inline_call_value_program.c:{INLINE_BEFORE_CALL_TRACE_LINE} {{\n print \"PRECALL:{{}}:{{}}\", original_x, original_y;\n}}\n"
);
let (exit_code, stdout, stderr) =
run_ghostscope_with_script_for_target(&script, 4, &target).await?;
target.terminate().await?;
if should_skip_for_ebpf_env(exit_code, &stderr) {
return Ok(());
}
assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");
assert!(
!stdout.contains("ExprError"),
"Expected exact inline parameter values on the pre-call line. STDOUT: {stdout}\nSTDERR: {stderr}"
);
assert!(
!stdout.contains("<optimized_out>"),
"Pre-call inline parameters should not be optimized out. STDOUT: {stdout}\nSTDERR: {stderr}"
);
let re = Regex::new(r"PRECALL:([0-9-]+):([0-9-]+)")?;
let mut seen = 0;
for caps in re.captures_iter(&stdout) {
let original_x: i64 = caps[1].parse()?;
let original_y: i64 = caps[2].parse()?;
assert_eq!(
original_x,
(original_y - 11) * 7,
"Expected original_x/original_y to match wrapper(seed) on the pre-call line. STDOUT: {stdout}"
);
seen += 1;
}
assert!(
seen >= 2,
"Expected multiple pre-call inline events. STDOUT: {stdout}\nSTDERR: {stderr}"
);
Ok(())
}
#[tokio::test]
async fn test_optimized_inline_parameters_survive_internal_call_sites() -> anyhow::Result<()> {
init();
let binary_path = FIXTURES.get_test_binary("inline_call_value_program")?;
let analyzer = ghostscope_dwarf::DwarfAnalyzer::from_exec_path(&binary_path).await?;
let addrs = analyzer.lookup_addresses_by_source_line(
"inline_call_value_program.c",
INLINE_AFTER_CALL_TRACE_LINE,
);
anyhow::ensure!(
!addrs.is_empty(),
"No DWARF addresses found for inline_call_value_program.c:{INLINE_AFTER_CALL_TRACE_LINE}"
);
for module_address in &addrs {
anyhow::ensure!(
analyzer.is_inline_at(module_address) == Some(true),
"Expected inline address at 0x{:x}",
module_address.address
);
}
let query_results = analyzer.query_source_line_best_effort(
"inline_call_value_program.c",
INLINE_AFTER_CALL_TRACE_LINE,
)?;
anyhow::ensure!(
!query_results.is_empty(),
"No query results for inline_call_value_program.c:{INLINE_AFTER_CALL_TRACE_LINE}"
);
for result in &query_results {
assert_not_internal_call_register_aliases(&result.parameters, result.address)?;
}
let target = spawn_inline_call_value_program(&binary_path).await?;
let pid_analyzer = ghostscope_dwarf::DwarfAnalyzer::from_pid(target.host_pid()).await?;
let pid_results = pid_analyzer.query_source_line_best_effort(
"inline_call_value_program.c",
INLINE_AFTER_CALL_TRACE_LINE,
)?;
anyhow::ensure!(
!pid_results.is_empty(),
"No PID-backed query results for inline_call_value_program.c:{INLINE_AFTER_CALL_TRACE_LINE}"
);
for result in &pid_results {
assert_not_internal_call_register_aliases(&result.parameters, result.address)?;
}
target.terminate().await?;
Ok(())
}
#[tokio::test]
async fn test_entry_value_recovers_outer_parameter_inside_optimized_inline_after_internal_call(
) -> anyhow::Result<()> {
init();
let binary_path = FIXTURES.get_test_binary("inline_call_value_program")?;
let analyzer = ghostscope_dwarf::DwarfAnalyzer::from_exec_path(&binary_path).await?;
let addrs = analyzer.lookup_addresses_by_source_line(
"inline_call_value_program.c",
INLINE_AFTER_CALL_TRACE_LINE,
);
anyhow::ensure!(
!addrs.is_empty(),
"No DWARF addresses found for inline_call_value_program.c:{INLINE_AFTER_CALL_TRACE_LINE}"
);
for module_address in &addrs {
anyhow::ensure!(
analyzer.is_inline_at(module_address) == Some(true),
"Expected inline address at 0x{:x}",
module_address.address
);
}
let target = spawn_inline_call_value_program(&binary_path).await?;
let script = format!(
"trace inline_call_value_program.c:{INLINE_AFTER_CALL_TRACE_LINE} {{\n print \"POSTCALL:{{}}:{{}}\", seed, after_call;\n}}\n"
);
let (exit_code, stdout, stderr) =
run_ghostscope_with_script_for_target(&script, 4, &target).await?;
target.terminate().await?;
if should_skip_for_ebpf_env(exit_code, &stderr) {
return Ok(());
}
assert_eq!(exit_code, 0, "stderr={stderr} stdout={stdout}");
assert!(
!stdout.contains("ExprError"),
"Expected exact entry_value recovery inside the inline body. STDOUT: {stdout}\nSTDERR: {stderr}"
);
assert!(
!stdout.contains("<optimized_out>"),
"Inline post-call entry_value should not be optimized out. STDOUT: {stdout}\nSTDERR: {stderr}"
);
let re = Regex::new(r"POSTCALL:([0-9-]+):([0-9-]+)")?;
let mut seen = 0;
for caps in re.captures_iter(&stdout) {
let seed: i64 = caps[1].parse()?;
let after_call: i64 = caps[2].parse()?;
let original_x = seed * 7;
let original_y = seed + 11;
let combined = (original_x + original_y) * (original_x - original_y);
assert_eq!(
after_call,
combined + 7,
"Expected seed/after_call to match wrapper(seed) on the first post-call line. STDOUT: {stdout}"
);
seen += 1;
}
assert!(
seen >= 2,
"Expected multiple post-call entry_value events. STDOUT: {stdout}\nSTDERR: {stderr}"
);
Ok(())
}