full refactor on linker and verifier to become more JVM-like

This commit is contained in:
2026-03-24 13:40:30 +00:00
parent e486c1980e
commit c6411fd53c
17 changed files with 701 additions and 237 deletions
+37 -14
View File
@@ -2,6 +2,7 @@ use prometeu_abi::syscalls::Syscall;
use crate::bytecode::decoder::{decode_at, DecodeError};
use prometeu_bytecode::opcode::OpCode;
use prometeu_bytecode::FunctionMeta;
use prometeu_bytecode::layout;
use std::collections::{HashMap, HashSet, VecDeque};
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -33,7 +34,8 @@ impl Verifier {
fn verify_function(code: &[u8], func: &FunctionMeta, func_idx: usize, all_functions: &[FunctionMeta]) -> Result<u16, VerifierError> {
let func_start = func.code_offset as usize;
let func_end = func_start + func.code_len as usize;
// Use o cálculo canônico compartilhado com o compiler/linker
let func_end = layout::function_end_from_next(all_functions, func_idx, code.len());
if func_start > code.len() || func_end > code.len() || func_start > func_end {
return Err(VerifierError::FunctionOutOfBounds {
@@ -46,6 +48,14 @@ impl Verifier {
let func_code = &code[func_start..func_end];
// Funções vazias (sem qualquer byte de código) são consideradas válidas no verificador.
// Elas não consomem nem produzem valores na pilha e não possuem fluxo interno.
// Observação: se uma função vazia for chamada em tempo de execução e retorno/efeitos
// forem esperados, caberá ao gerador de código/linker impedir tal situação.
if func_code.is_empty() {
return Ok(0);
}
// First pass: find all valid instruction boundaries
let mut valid_pc = HashSet::new();
let mut pc = 0;
@@ -120,28 +130,41 @@ impl Verifier {
// Propagate to successors
if spec.is_branch {
let target = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
if target >= func.code_len as usize {
return Err(VerifierError::InvalidJumpTarget { pc: func_start + pc, target: func_start + target });
}
if !valid_pc.contains(&target) {
return Err(VerifierError::JumpToMidInstruction { pc: func_start + pc, target: func_start + target });
// Canonical contract: branch immediate is RELATIVE to function start.
let target_rel = u32::from_le_bytes(instr.imm[0..4].try_into().unwrap()) as usize;
let func_end_abs = layout::function_end_from_next(all_functions, func_idx, code.len());
let func_len = func_end_abs - func_start;
if target_rel > func_len {
return Err(VerifierError::InvalidJumpTarget { pc: func_start + pc, target: func_start + target_rel });
}
if let Some(&existing_height) = stack_height_in.get(&target) {
if existing_height != out_height {
return Err(VerifierError::StackMismatchJoin { pc: func_start + pc, target: func_start + target, height_in: out_height, height_target: existing_height });
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 });
}
// caminho termina aqui
} else {
stack_height_in.insert(target, out_height);
worklist.push_back(target);
if !valid_pc.contains(&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 });
}
} else {
stack_height_in.insert(target_rel, out_height);
worklist.push_back(target_rel);
}
}
}
if !spec.is_terminator {
let next_pc = instr.next_pc;
if next_pc < func.code_len as usize {
let func_len = layout::function_len_from_next(all_functions, func_idx, code.len());
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 });