Skip to content

Commit 369faca

Browse files
committed
issue/94: include last pc in stack trace
1 parent 1249265 commit 369faca

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

crates/lean_vm/src/diagnostics/stack_trace.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) fn pretty_stack_trace(
1010
source_code: &str,
1111
instructions: &[SourceLineNumber], // SourceLineNumber = usize
1212
function_locations: &BTreeMap<usize, String>,
13+
last_pc: usize,
1314
) -> String {
1415
let source_lines: Vec<&str> = source_code.lines().collect();
1516
let mut result = String::new();
@@ -97,7 +98,11 @@ pub(crate) fn pretty_stack_trace(
9798
if !call_stack.is_empty() {
9899
result.push_str("\nCall stack:\n");
99100
for (i, (line, func)) in call_stack.iter().enumerate() {
100-
result.push_str(&format!(" {}. {} (line {})\n", i + 1, func, line));
101+
if i + 1 == call_stack.len() {
102+
result.push_str(&format!(" {}. {} (line {}, pc {})\n", i + 1, func, line, last_pc));
103+
} else {
104+
result.push_str(&format!(" {}. {} (line {})\n", i + 1, func, line));
105+
}
101106
}
102107
}
103108

crates/lean_vm/src/execution/runner.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ pub fn execute_bytecode(
7070
profiling,
7171
(poseidons_16_precomputed, poseidons_24_precomputed),
7272
)
73-
.unwrap_or_else(|err| {
73+
.unwrap_or_else(|(last_pc, err)| {
7474
let lines_history = &instruction_history.lines;
7575
let latest_instructions = &lines_history[lines_history.len().saturating_sub(STACK_TRACE_INSTRUCTIONS)..];
7676
println!(
7777
"\n{}",
7878
crate::diagnostics::pretty_stack_trace(
7979
&bytecode.program,
8080
latest_instructions,
81-
&bytecode.function_locations
81+
&bytecode.function_locations,
82+
last_pc
8283
)
8384
);
8485
if !std_out.is_empty() {
@@ -146,15 +147,15 @@ fn execute_bytecode_helper(
146147
no_vec_runtime_memory: usize,
147148
profiling: bool,
148149
(poseidons_16_precomputed, poseidons_24_precomputed): (&Poseidon16History, &Poseidon24History),
149-
) -> Result<ExecutionResult, RunnerError> {
150+
) -> Result<ExecutionResult, (CodeAddress, RunnerError)> {
150151
// set public memory
151152
let mut memory = Memory::new(build_public_memory(public_input));
152153

153154
let public_memory_size = (NONRESERVED_PROGRAM_INPUT_START + public_input.len()).next_power_of_two();
154155
let mut fp = public_memory_size;
155156

156157
for (i, value) in private_input.iter().enumerate() {
157-
memory.set(fp + i, *value)?;
158+
memory.set(fp + i, *value).expect("to set private input in memory");
158159
}
159160

160161
let mut mem_profile = MemoryProfile {
@@ -199,7 +200,7 @@ fn execute_bytecode_helper(
199200

200201
while pc != ENDING_PC {
201202
if pc >= bytecode.instructions.len() {
202-
return Err(RunnerError::PCOutOfBounds);
203+
return Err((pc, RunnerError::PCOutOfBounds));
203204
}
204205

205206
pcs.push(pc);
@@ -225,7 +226,7 @@ fn execute_bytecode_helper(
225226
profiling,
226227
memory_profile: &mut mem_profile,
227228
};
228-
hint.execute_hint(&mut hint_ctx)?;
229+
hint.execute_hint(&mut hint_ctx).map_err(|e| (pc, e))?;
229230
}
230231

231232
let instruction = &bytecode.instructions[pc];
@@ -244,7 +245,7 @@ fn execute_bytecode_helper(
244245
n_poseidon16_precomputed_used: &mut n_poseidon16_precomputed_used,
245246
n_poseidon24_precomputed_used: &mut n_poseidon24_precomputed_used,
246247
};
247-
instruction.execute_instruction(&mut instruction_ctx)?;
248+
instruction.execute_instruction(&mut instruction_ctx).map_err(|e| (pc, e))?;
248249
}
249250

250251
assert_eq!(

0 commit comments

Comments
 (0)