Skip to content

Commit

Permalink
Merge pull request #233 from theseus-rs/correct-exception-byte-to-ins…
Browse files Browse the repository at this point in the history
…truction-conversion

fix: correct exception byte to instruction offset conversion error
  • Loading branch information
brianheineman authored Jan 23, 2025
2 parents 3ac160b + e9099e9 commit 53faf9c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ristretto_classfile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ anyhow = { workspace = true }
criterion = { workspace = true }
flate2 = { workspace = true }
indoc = { workspace = true }
reqwest = { workspace = true }
reqwest = { workspace = true, features = ["rustls-tls-native-roots"] }
tar = { workspace = true }
tokio = { workspace = true }
zip = { workspace = true }
Expand Down
13 changes: 7 additions & 6 deletions ristretto_classfile/src/attributes/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl Attribute {
.iter()
.filter(|(&k, _)| k <= exception.range_pc.end)
.max_by_key(|(&k, _)| k)
.map(|(_, &v)| v + 1)
.map(|(_, &v)| v)
.ok_or(InvalidInstructionOffset(u32::from(exception.range_pc.end)))?;
exception.handler_pc = *byte_to_instruction_map
.get(&exception.handler_pc)
Expand Down Expand Up @@ -730,7 +730,7 @@ impl Attribute {
.iter()
.filter(|(&k, _)| k <= exception.range_pc.end)
.max_by_key(|(&k, _)| k)
.map(|(_, &v)| v + 1)
.map(|(_, &v)| v)
.ok_or(InvalidInstructionOffset(u32::from(exception.range_pc.end)))?;
exception.handler_pc = *instruction_to_byte_map
.get(&exception.handler_pc)
Expand Down Expand Up @@ -1278,19 +1278,20 @@ mod test {
name_index: 1,
max_stack: 2,
max_locals: 3,
code: vec![Instruction::Iconst_1],
code: vec![Instruction::Iconst_1, Instruction::Return],
exception_table: vec![exception_table_entry],
attributes: vec![constant.clone(), line_number_table.clone()],
};
let expected_bytes = [
0, 1, 0, 0, 0, 41, 0, 2, 0, 3, 0, 0, 0, 1, 4, 0, 1, 0, 0, 0, 1, 0, 0, 0, 4, 0, 2, 0, 2,
0, 0, 0, 2, 0, 42, 0, 3, 0, 0, 0, 6, 0, 1, 0, 0, 0, 1,
0, 1, 0, 0, 0, 42, 0, 2, 0, 3, 0, 0, 0, 2, 4, 177, 0, 1, 0, 0, 0, 1, 0, 0, 0, 4, 0, 2,
0, 2, 0, 0, 0, 2, 0, 42, 0, 3, 0, 0, 0, 6, 0, 1, 0, 0, 0, 1,
];
let expected = indoc! {"
Code:
stack=2, locals=3
0: iconst_1
[ExceptionTableEntry { range_pc: 0..1, handler_pc: 0, catch_type: 4 }]
1: return
[ExceptionTableEntry { range_pc: 0..2, handler_pc: 0, catch_type: 4 }]
ConstantValue { name_index: 2, constant_value_index: 42 }
LineNumberTable:
line 1: 0
Expand Down
5 changes: 5 additions & 0 deletions ristretto_classfile/tests/class_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ pub fn test_expressions() -> Result<()> {
test_class(include_bytes!("../../classes/Expressions.class"))
}

#[test]
pub fn test_jdbc() -> Result<()> {
test_class(include_bytes!("../../classes/JDBC.class"))
}

#[test]
pub fn test_minimum() -> Result<()> {
test_class(include_bytes!("../../classes/Minimum.class"))
Expand Down
6 changes: 2 additions & 4 deletions ristretto_vm/src/native_methods/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,8 @@ mod tests {
extra_methods.join("\n"),
));
};
if !errors.is_empty() {
eprintln!("{}", errors.join("\n"));
assert!(errors.is_empty());
}
let errors = errors.join("\n");
assert_eq!("", errors);
Ok(())
}

Expand Down

0 comments on commit 53faf9c

Please sign in to comment.