From e9099e981f018a0bdada529254f738e6a81c761d Mon Sep 17 00:00:00 2001 From: brianheineman Date: Thu, 23 Jan 2025 13:05:14 -0700 Subject: [PATCH] fix: correct exception byte to instruction offset conversion error --- ristretto_classfile/Cargo.toml | 2 +- ristretto_classfile/src/attributes/attribute.rs | 13 +++++++------ ristretto_classfile/tests/class_tests.rs | 5 +++++ ristretto_vm/src/native_methods/registry.rs | 6 ++---- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ristretto_classfile/Cargo.toml b/ristretto_classfile/Cargo.toml index 9a0d1830..48a17067 100644 --- a/ristretto_classfile/Cargo.toml +++ b/ristretto_classfile/Cargo.toml @@ -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 } diff --git a/ristretto_classfile/src/attributes/attribute.rs b/ristretto_classfile/src/attributes/attribute.rs index e479b171..f63550da 100644 --- a/ristretto_classfile/src/attributes/attribute.rs +++ b/ristretto_classfile/src/attributes/attribute.rs @@ -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) @@ -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) @@ -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 diff --git a/ristretto_classfile/tests/class_tests.rs b/ristretto_classfile/tests/class_tests.rs index 37d40fc8..f66bc13b 100644 --- a/ristretto_classfile/tests/class_tests.rs +++ b/ristretto_classfile/tests/class_tests.rs @@ -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")) diff --git a/ristretto_vm/src/native_methods/registry.rs b/ristretto_vm/src/native_methods/registry.rs index 19d516b3..95441828 100644 --- a/ristretto_vm/src/native_methods/registry.rs +++ b/ristretto_vm/src/native_methods/registry.rs @@ -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(()) }