소스 검색

feat: catch error from instruction byte parser

Claire xyz 1 개월 전
부모
커밋
ed9eb6db2d
3개의 변경된 파일24개의 추가작업 그리고 8개의 파일을 삭제
  1. 11 0
      Cargo.lock
  2. 2 0
      Cargo.toml
  3. 11 8
      src/byteparser.rs

+ 11 - 0
Cargo.lock

@@ -827,6 +827,16 @@ dependencies = [
  "wasm-bindgen",
 ]
 
+[[package]]
+name = "sbpf-common"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39a8ff075e738e3850c291334f6f7e45379ad442366f3f4b1f9f9bf0674fa61d"
+dependencies = [
+ "num-derive",
+ "num-traits",
+]
+
 [[package]]
 name = "sbpf-linker"
 version = "0.1.3"
@@ -840,6 +850,7 @@ dependencies = [
  "llvm-sys 211.0.0",
  "object",
  "sbpf-assembler",
+ "sbpf-common",
  "thiserror 2.0.17",
  "tracing",
  "tracing-appender",

+ 2 - 0
Cargo.toml

@@ -11,7 +11,9 @@ crate-type = ["cdylib", "lib"]
 name = "sbpf_linker"
 
 [dependencies]
+# TODO: update versions for sbpf-assembler and sbpf-common
 sbpf-assembler = "0.1.2"
+sbpf-common = "0.1.2"
 clap = { version = "4.5.13", features = ["derive"] }
 object = "0.37.3"
 anyhow = "1.0"

+ 11 - 8
src/byteparser.rs

@@ -2,8 +2,8 @@ use sbpf_assembler::ast::AST;
 use sbpf_assembler::astnode::{ASTNode, ROData};
 use sbpf_assembler::instruction::Instruction;
 use sbpf_assembler::lexer::{ImmediateValue, Token};
-use sbpf_assembler::opcode::Opcode;
 use sbpf_assembler::parser::ParseResult;
+use sbpf_common::opcode::Opcode;
 
 use object::{File, Object, ObjectSection, ObjectSymbol};
 use object::RelocationTarget::Symbol;
@@ -54,13 +54,18 @@ pub fn parse_bytecode(bytes: &[u8]) -> Result<ParseResult, String> {
                     _ => 8,
                 };
                 let node = &section.data().unwrap()[offset..offset + node_len];
-                ast.nodes.push(ASTNode::Instruction {
-                    instruction: Instruction::from_bytes(node).unwrap(),
-                    offset: offset as u64,
-                });
+                let instruction = Instruction::from_bytes(node);
+                if let Err(error) = instruction {
+                    return Err(error.to_string());
+                } else {
+                    ast.nodes.push(ASTNode::Instruction {
+                        instruction: instruction.unwrap(),
+                        offset: offset as u64,
+                    });
+                }
                 offset += node_len;
             }
-            
+
             if let Some(ro_section) = obj.section_by_name(".rodata") {
                 // handle relocations
                 for rel in section.relocations() {
@@ -69,10 +74,8 @@ pub fn parse_bytecode(bytes: &[u8]) -> Result<ParseResult, String> {
                         Symbol(sym) => Some(obj.symbol_by_index(sym).unwrap()), 
                         _ => None
                     };
-                    println!("Symbol: {:?}", symbol);
                 
                     if symbol.unwrap().section_index() == Some(ro_section.index()) {
-                        println!("Relocation found");
                         // addend is not explicit in the relocation entry, but implicitly encoded
                         // as the immediate value of the instruction
                         let addend = //