wasm: self-review edits

This commit is contained in:
Brian Carroll 2022-05-25 22:53:44 +01:00
parent 4300ef8b47
commit 11c1c92cc1
No known key found for this signature in database
GPG key ID: 9CF4E3BF9C4722C7
3 changed files with 17 additions and 22 deletions

View file

@ -2,7 +2,7 @@ use bumpalo::collections::vec::Vec;
use bumpalo::Bump; use bumpalo::Bump;
use super::opcodes::OpCode; use super::opcodes::OpCode;
use super::parse::{Parse, SkipBytes}; use super::parse::{Parse, SkipBytes, ParseError};
use super::serialize::{SerialBuffer, Serialize}; use super::serialize::{SerialBuffer, Serialize};
use super::CodeBuilder; use super::CodeBuilder;
@ -70,7 +70,7 @@ pub fn parse_preloads_call_graph<'a>(
imported_fn_signatures: &[u32], imported_fn_signatures: &[u32],
defined_fn_signatures: &[u32], defined_fn_signatures: &[u32],
indirect_callees: &[u32], indirect_callees: &[u32],
) -> PreloadsCallGraph<'a> { ) -> Result<PreloadsCallGraph<'a>, ParseError> {
let mut call_graph = PreloadsCallGraph::new( let mut call_graph = PreloadsCallGraph::new(
arena, arena,
imported_fn_signatures.len(), imported_fn_signatures.len(),
@ -92,13 +92,13 @@ pub fn parse_preloads_call_graph<'a>(
call_graph.code_offsets.push(cursor as u32); call_graph.code_offsets.push(cursor as u32);
call_graph.calls_offsets.push(call_graph.calls.len() as u32); call_graph.calls_offsets.push(call_graph.calls.len() as u32);
let func_size = u32::parse((), code_section_body, &mut cursor).unwrap(); let func_size = u32::parse((), code_section_body, &mut cursor)?;
let func_end = cursor + func_size as usize; let func_end = cursor + func_size as usize;
// Skip over local variable declarations // Skip over local variable declarations
let local_groups_count = u32::parse((), code_section_body, &mut cursor).unwrap(); let local_groups_count = u32::parse((), code_section_body, &mut cursor)?;
for _ in 0..local_groups_count { for _ in 0..local_groups_count {
u32::parse((), code_section_body, &mut cursor).unwrap(); u32::parse((), code_section_body, &mut cursor)?;
cursor += 1; // ValueType cursor += 1; // ValueType
} }
@ -107,25 +107,20 @@ pub fn parse_preloads_call_graph<'a>(
let opcode_byte: u8 = code_section_body[cursor]; let opcode_byte: u8 = code_section_body[cursor];
if opcode_byte == OpCode::CALL as u8 { if opcode_byte == OpCode::CALL as u8 {
cursor += 1; cursor += 1;
let call_index = u32::parse((), code_section_body, &mut cursor).unwrap(); let call_index = u32::parse((), code_section_body, &mut cursor)?;
call_graph.calls.push(call_index as u32); call_graph.calls.push(call_index as u32);
} else if opcode_byte == OpCode::CALLINDIRECT as u8 { } else if opcode_byte == OpCode::CALLINDIRECT as u8 {
cursor += 1; cursor += 1;
// Insert all indirect callees with a matching type signature // Insert all indirect callees with a matching type signature
let sig = u32::parse((), code_section_body, &mut cursor).unwrap(); let sig = u32::parse((), code_section_body, &mut cursor)?;
call_graph.calls.extend( call_graph.calls.extend(
indirect_callees indirect_callees
.iter() .iter()
.filter(|f| signatures[**f as usize] == sig), .filter(|f| signatures[**f as usize] == sig),
); );
u32::skip_bytes(code_section_body, &mut cursor).unwrap(); // table_idx u32::skip_bytes(code_section_body, &mut cursor)?; // table_idx
} else { } else {
OpCode::skip_bytes(code_section_body, &mut cursor).unwrap_or_else(|e| { OpCode::skip_bytes(code_section_body, &mut cursor)?;
panic!(
"Error parsing host object file, at offset 0x{:x} within Code section: {}",
cursor, e.message
)
});
} }
} }
} }
@ -134,7 +129,7 @@ pub fn parse_preloads_call_graph<'a>(
call_graph.code_offsets.push(cursor as u32); call_graph.code_offsets.push(cursor as u32);
call_graph.calls_offsets.push(call_graph.calls.len() as u32); call_graph.calls_offsets.push(call_graph.calls.len() as u32);
call_graph Ok(call_graph)
} }
/// Trace the dependencies of a list of functions /// Trace the dependencies of a list of functions

View file

@ -13,12 +13,6 @@ pub struct ParseError {
pub message: String, pub message: String,
} }
/// Skip over serialized bytes for a type
/// This may, or may not, require looking at the byte values
pub trait SkipBytes: Sized {
fn skip_bytes(bytes: &[u8], cursor: &mut usize) -> Result<(), ParseError>;
}
/// Decode an unsigned 32-bit integer from the provided buffer in LEB-128 format /// Decode an unsigned 32-bit integer from the provided buffer in LEB-128 format
/// Return the integer itself and the offset after it ends /// Return the integer itself and the offset after it ends
fn decode_u32(bytes: &[u8]) -> Result<(u32, usize), ()> { fn decode_u32(bytes: &[u8]) -> Result<(u32, usize), ()> {
@ -64,6 +58,12 @@ impl<'a> Parse<&'a Bump> for &'a str {
} }
} }
/// Skip over serialized bytes for a type
/// This may, or may not, require looking at the byte values
pub trait SkipBytes: Sized {
fn skip_bytes(bytes: &[u8], cursor: &mut usize) -> Result<(), ParseError>;
}
impl SkipBytes for u32 { impl SkipBytes for u32 {
fn skip_bytes(bytes: &[u8], cursor: &mut usize) -> Result<(), ParseError> { fn skip_bytes(bytes: &[u8], cursor: &mut usize) -> Result<(), ParseError> {
const MAX_LEN: usize = 5; const MAX_LEN: usize = 5;

View file

@ -1154,7 +1154,7 @@ impl<'a> CodeSection<'a> {
import_signatures, import_signatures,
function_signatures, function_signatures,
indirect_callees, indirect_callees,
); )?;
Ok(CodeSection { Ok(CodeSection {
preloaded_count, preloaded_count,