|
|
|
@@ -1,9 +1,9 @@
|
|
|
|
|
use prometeu_hal::syscalls::Syscall;
|
|
|
|
|
use prometeu_bytecode::{decode_next, DecodeError};
|
|
|
|
|
use prometeu_bytecode::FunctionMeta;
|
|
|
|
|
use prometeu_bytecode::OpCode;
|
|
|
|
|
use prometeu_bytecode::OpCodeSpecExt;
|
|
|
|
|
use prometeu_bytecode::FunctionMeta;
|
|
|
|
|
use prometeu_bytecode::{compute_function_layouts, FunctionLayout};
|
|
|
|
|
use prometeu_bytecode::{DecodeError, decode_next};
|
|
|
|
|
use prometeu_bytecode::{FunctionLayout, compute_function_layouts};
|
|
|
|
|
use prometeu_hal::syscalls::Syscall;
|
|
|
|
|
use std::collections::{HashMap, HashSet, VecDeque};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
@@ -71,14 +71,23 @@ impl Verifier {
|
|
|
|
|
while pc < func_code.len() {
|
|
|
|
|
valid_pc.insert(pc);
|
|
|
|
|
let instr = decode_next(pc, func_code).map_err(|e| match e {
|
|
|
|
|
DecodeError::UnknownOpcode { pc: _, opcode } =>
|
|
|
|
|
VerifierError::UnknownOpcode { pc: func_start + pc, opcode },
|
|
|
|
|
DecodeError::TruncatedOpcode { pc: _ } =>
|
|
|
|
|
VerifierError::TruncatedOpcode { pc: func_start + pc },
|
|
|
|
|
DecodeError::TruncatedImmediate { pc: _, opcode, need, have } =>
|
|
|
|
|
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need, have },
|
|
|
|
|
DecodeError::ImmediateSizeMismatch { pc: _, opcode, expected, actual } =>
|
|
|
|
|
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need: expected, have: actual },
|
|
|
|
|
DecodeError::UnknownOpcode { pc: _, opcode } => {
|
|
|
|
|
VerifierError::UnknownOpcode { pc: func_start + pc, opcode }
|
|
|
|
|
}
|
|
|
|
|
DecodeError::TruncatedOpcode { pc: _ } => {
|
|
|
|
|
VerifierError::TruncatedOpcode { pc: func_start + pc }
|
|
|
|
|
}
|
|
|
|
|
DecodeError::TruncatedImmediate { pc: _, opcode, need, have } => {
|
|
|
|
|
VerifierError::TruncatedImmediate { pc: func_start + pc, opcode, need, have }
|
|
|
|
|
}
|
|
|
|
|
DecodeError::ImmediateSizeMismatch { pc: _, opcode, expected, actual } => {
|
|
|
|
|
VerifierError::TruncatedImmediate {
|
|
|
|
|
pc: func_start + pc,
|
|
|
|
|
opcode,
|
|
|
|
|
need: expected,
|
|
|
|
|
have: actual,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})?;
|
|
|
|
|
pc = instr.next_pc;
|
|
|
|
|
}
|
|
|
|
@@ -113,9 +122,7 @@ impl Verifier {
|
|
|
|
|
})?;
|
|
|
|
|
(callee.param_slots, callee.return_slots)
|
|
|
|
|
}
|
|
|
|
|
OpCode::Ret => {
|
|
|
|
|
(func.return_slots, 0)
|
|
|
|
|
}
|
|
|
|
|
OpCode::Ret => (func.return_slots, 0),
|
|
|
|
|
OpCode::Syscall => {
|
|
|
|
|
let id = instr.imm_u32().unwrap();
|
|
|
|
|
let syscall = Syscall::from_u32(id).ok_or_else(|| {
|
|
|
|
@@ -127,7 +134,10 @@ impl Verifier {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if in_height < pops {
|
|
|
|
|
return Err(VerifierError::StackUnderflow { pc: func_start + pc, opcode: instr.opcode });
|
|
|
|
|
return Err(VerifierError::StackUnderflow {
|
|
|
|
|
pc: func_start + pc,
|
|
|
|
|
opcode: instr.opcode,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let out_height = in_height - pops + pushes;
|
|
|
|
@@ -135,7 +145,11 @@ impl Verifier {
|
|
|
|
|
|
|
|
|
|
if instr.opcode == OpCode::Ret {
|
|
|
|
|
if in_height != func.return_slots {
|
|
|
|
|
return Err(VerifierError::BadRetStackHeight { pc: func_start + pc, height: in_height, expected: func.return_slots });
|
|
|
|
|
return Err(VerifierError::BadRetStackHeight {
|
|
|
|
|
pc: func_start + pc,
|
|
|
|
|
height: in_height,
|
|
|
|
|
expected: func.return_slots,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -143,7 +157,8 @@ impl Verifier {
|
|
|
|
|
if spec.is_branch {
|
|
|
|
|
// Canonical contract: branch immediate is RELATIVE to function start.
|
|
|
|
|
let target_rel = instr.imm_u32().unwrap() as usize;
|
|
|
|
|
let func_end_abs = layouts.get(func_idx).map(|l| l.end).unwrap_or_else(|| code.len());
|
|
|
|
|
let func_end_abs =
|
|
|
|
|
layouts.get(func_idx).map(|l| l.end).unwrap_or_else(|| code.len());
|
|
|
|
|
let func_len = func_end_abs - func_start;
|
|
|
|
|
|
|
|
|
|
if target_rel > func_len {
|
|
|
|
@@ -168,23 +183,38 @@ impl Verifier {
|
|
|
|
|
target_abs_expected,
|
|
|
|
|
is_boundary_target_rel
|
|
|
|
|
);
|
|
|
|
|
return Err(VerifierError::InvalidJumpTarget { pc: pc_abs, target: target_abs_expected });
|
|
|
|
|
return Err(VerifierError::InvalidJumpTarget {
|
|
|
|
|
pc: pc_abs,
|
|
|
|
|
target: target_abs_expected,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if target_rel == func_len {
|
|
|
|
|
// salto para o fim da função
|
|
|
|
|
if out_height != func.return_slots {
|
|
|
|
|
return Err(VerifierError::BadRetStackHeight { pc: func_start + pc, height: out_height, expected: func.return_slots });
|
|
|
|
|
return Err(VerifierError::BadRetStackHeight {
|
|
|
|
|
pc: func_start + pc,
|
|
|
|
|
height: out_height,
|
|
|
|
|
expected: func.return_slots,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// caminho termina aqui
|
|
|
|
|
} else {
|
|
|
|
|
if !valid_pc.contains(&target_rel) {
|
|
|
|
|
return Err(VerifierError::JumpToMidInstruction { pc: func_start + pc, target: func_start + target_rel });
|
|
|
|
|
return Err(VerifierError::JumpToMidInstruction {
|
|
|
|
|
pc: func_start + pc,
|
|
|
|
|
target: func_start + target_rel,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(&existing_height) = stack_height_in.get(&target_rel) {
|
|
|
|
|
if existing_height != out_height {
|
|
|
|
|
return Err(VerifierError::StackMismatchJoin { pc: func_start + pc, target: func_start + target_rel, height_in: out_height, height_target: existing_height });
|
|
|
|
|
return Err(VerifierError::StackMismatchJoin {
|
|
|
|
|
pc: func_start + pc,
|
|
|
|
|
target: func_start + target_rel,
|
|
|
|
|
height_in: out_height,
|
|
|
|
|
height_target: existing_height,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
stack_height_in.insert(target_rel, out_height);
|
|
|
|
@@ -199,7 +229,12 @@ impl Verifier {
|
|
|
|
|
if next_pc < func_len {
|
|
|
|
|
if let Some(&existing_height) = stack_height_in.get(&next_pc) {
|
|
|
|
|
if existing_height != out_height {
|
|
|
|
|
return Err(VerifierError::StackMismatchJoin { pc: func_start + pc, target: func_start + next_pc, height_in: out_height, height_target: existing_height });
|
|
|
|
|
return Err(VerifierError::StackMismatchJoin {
|
|
|
|
|
pc: func_start + pc,
|
|
|
|
|
target: func_start + next_pc,
|
|
|
|
|
height_in: out_height,
|
|
|
|
|
height_target: existing_height,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
stack_height_in.insert(next_pc, out_height);
|
|
|
|
@@ -221,18 +256,15 @@ mod tests {
|
|
|
|
|
fn test_verifier_underflow() {
|
|
|
|
|
// OpCode::Add (2 bytes)
|
|
|
|
|
let code = vec![OpCode::Add as u8, 0x00];
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 2,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}];
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 2, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
assert_eq!(res, Err(VerifierError::StackUnderflow { pc: 0, opcode: OpCode::Add }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_verifier_dup_underflow() {
|
|
|
|
|
let code = vec![(OpCode::Dup as u16).to_le_bytes()[0], (OpCode::Dup as u16).to_le_bytes()[1]];
|
|
|
|
|
let code =
|
|
|
|
|
vec![(OpCode::Dup as u16).to_le_bytes()[0], (OpCode::Dup as u16).to_le_bytes()[1]];
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 2, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
assert_eq!(res, Err(VerifierError::StackUnderflow { pc: 0, opcode: OpCode::Dup }));
|
|
|
|
@@ -243,11 +275,7 @@ mod tests {
|
|
|
|
|
// Jmp (2 bytes) + 100u32 (4 bytes)
|
|
|
|
|
let mut code = vec![OpCode::Jmp as u8, 0x00];
|
|
|
|
|
code.extend_from_slice(&100u32.to_le_bytes());
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 6,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}];
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 6, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
assert_eq!(res, Err(VerifierError::InvalidJumpTarget { pc: 0, target: 100 }));
|
|
|
|
|
}
|
|
|
|
@@ -261,12 +289,8 @@ mod tests {
|
|
|
|
|
code.push(OpCode::Jmp as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&1u32.to_le_bytes());
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 12,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 12, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
assert_eq!(res, Err(VerifierError::JumpToMidInstruction { pc: 6, target: 1 }));
|
|
|
|
|
}
|
|
|
|
@@ -295,11 +319,7 @@ mod tests {
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_verifier_truncation_opcode() {
|
|
|
|
|
let code = vec![OpCode::PushI32 as u8]; // Truncated u16 opcode
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 1,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}];
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 1, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
assert_eq!(res, Err(VerifierError::TruncatedOpcode { pc: 0 }));
|
|
|
|
|
}
|
|
|
|
@@ -308,13 +328,17 @@ mod tests {
|
|
|
|
|
fn test_verifier_truncation_immediate() {
|
|
|
|
|
let mut code = vec![OpCode::PushI32 as u8, 0x00];
|
|
|
|
|
code.push(0x42); // Only 1 byte of 4-byte immediate
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 3,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}];
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 3, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
assert_eq!(res, Err(VerifierError::TruncatedImmediate { pc: 0, opcode: OpCode::PushI32, need: 4, have: 1 }));
|
|
|
|
|
assert_eq!(
|
|
|
|
|
res,
|
|
|
|
|
Err(VerifierError::TruncatedImmediate {
|
|
|
|
|
pc: 0,
|
|
|
|
|
opcode: OpCode::PushI32,
|
|
|
|
|
need: 4,
|
|
|
|
|
have: 1
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
@@ -326,26 +350,41 @@ mod tests {
|
|
|
|
|
// 15: PushI32 1
|
|
|
|
|
// 21: Jmp 27
|
|
|
|
|
// 27: Nop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut code = Vec::new();
|
|
|
|
|
code.push(OpCode::PushBool as u8); code.push(0x00); code.push(1); // 0: PushBool (3 bytes)
|
|
|
|
|
code.push(OpCode::JmpIfTrue as u8); code.push(0x00); code.extend_from_slice(&15u32.to_le_bytes()); // 3: JmpIfTrue (6 bytes)
|
|
|
|
|
code.push(OpCode::Jmp as u8); code.push(0x00); code.extend_from_slice(&27u32.to_le_bytes()); // 9: Jmp (6 bytes)
|
|
|
|
|
code.push(OpCode::PushI32 as u8); code.push(0x00); code.extend_from_slice(&1u32.to_le_bytes()); // 15: PushI32 (6 bytes)
|
|
|
|
|
code.push(OpCode::Jmp as u8); code.push(0x00); code.extend_from_slice(&27u32.to_le_bytes()); // 21: Jmp (6 bytes)
|
|
|
|
|
code.push(OpCode::Nop as u8); code.push(0x00); // 27: Nop (2 bytes)
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 29,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}];
|
|
|
|
|
code.push(OpCode::PushBool as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.push(1); // 0: PushBool (3 bytes)
|
|
|
|
|
code.push(OpCode::JmpIfTrue as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&15u32.to_le_bytes()); // 3: JmpIfTrue (6 bytes)
|
|
|
|
|
code.push(OpCode::Jmp as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&27u32.to_le_bytes()); // 9: Jmp (6 bytes)
|
|
|
|
|
code.push(OpCode::PushI32 as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&1u32.to_le_bytes()); // 15: PushI32 (6 bytes)
|
|
|
|
|
code.push(OpCode::Jmp as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&27u32.to_le_bytes()); // 21: Jmp (6 bytes)
|
|
|
|
|
code.push(OpCode::Nop as u8);
|
|
|
|
|
code.push(0x00); // 27: Nop (2 bytes)
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 29, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
// Path 0->3->9->27: height 1-1+0 = 0.
|
|
|
|
|
// Path 0->3->15->21->27: height 1-1+1 = 1.
|
|
|
|
|
// Mismatch at 27: 0 vs 1.
|
|
|
|
|
|
|
|
|
|
assert_eq!(res, Err(VerifierError::StackMismatchJoin { pc: 21, target: 27, height_in: 1, height_target: 0 }));
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
res,
|
|
|
|
|
Err(VerifierError::StackMismatchJoin {
|
|
|
|
|
pc: 21,
|
|
|
|
|
target: 27,
|
|
|
|
|
height_in: 1,
|
|
|
|
|
height_target: 0
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
@@ -356,7 +395,7 @@ mod tests {
|
|
|
|
|
code.extend_from_slice(&1u32.to_le_bytes());
|
|
|
|
|
code.push(OpCode::Ret as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 8,
|
|
|
|
@@ -374,11 +413,17 @@ mod tests {
|
|
|
|
|
// Add
|
|
|
|
|
// Ret
|
|
|
|
|
let mut code = Vec::new();
|
|
|
|
|
code.push(OpCode::PushI32 as u8); code.push(0x00); code.extend_from_slice(&1u32.to_le_bytes());
|
|
|
|
|
code.push(OpCode::PushI32 as u8); code.push(0x00); code.extend_from_slice(&2u32.to_le_bytes());
|
|
|
|
|
code.push(OpCode::Add as u8); code.push(0x00);
|
|
|
|
|
code.push(OpCode::Ret as u8); code.push(0x00);
|
|
|
|
|
|
|
|
|
|
code.push(OpCode::PushI32 as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&1u32.to_le_bytes());
|
|
|
|
|
code.push(OpCode::PushI32 as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&2u32.to_le_bytes());
|
|
|
|
|
code.push(OpCode::Add as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.push(OpCode::Ret as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 16,
|
|
|
|
@@ -392,14 +437,11 @@ mod tests {
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_verifier_invalid_syscall_id() {
|
|
|
|
|
let mut code = Vec::new();
|
|
|
|
|
code.push(OpCode::Syscall as u8); code.push(0x00);
|
|
|
|
|
code.push(OpCode::Syscall as u8);
|
|
|
|
|
code.push(0x00);
|
|
|
|
|
code.extend_from_slice(&0xDEADBEEFu32.to_le_bytes()); // Unknown ID
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta {
|
|
|
|
|
code_offset: 0,
|
|
|
|
|
code_len: 6,
|
|
|
|
|
..Default::default()
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
let functions = vec![FunctionMeta { code_offset: 0, code_len: 6, ..Default::default() }];
|
|
|
|
|
let res = Verifier::verify(&code, &functions);
|
|
|
|
|
assert_eq!(res, Err(VerifierError::InvalidSyscallId { pc: 0, id: 0xDEADBEEF }));
|
|
|
|
|
}
|
|
|
|
|