Skip to content

Commit

Permalink
Add stronger typing for opcodes with classinfo
Browse files Browse the repository at this point in the history
This is from section 4.4.1 which allows classinfo
to reference binary names or array type descriptors,
except for the new opcode which is only allowed
binary names.
  • Loading branch information
staktrace committed Aug 28, 2024
1 parent aee1368 commit ca0940a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
33 changes: 20 additions & 13 deletions src/bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::borrow::Cow;
use std::convert::TryFrom;

use crate::constant_pool::{
get_cp_loadable, read_cp_classinfo, read_cp_invokedynamic, read_cp_memberref,
get_cp_loadable, read_cp_invokedynamic, read_cp_memberref, read_cp_object_array_type,
};
use crate::constant_pool::{
ConstantPoolEntry, ConstantPoolEntryTypes, InvokeDynamic, Loadable, MemberRef,
ConstantPoolEntry, ConstantPoolEntryTypes, InvokeDynamic, Loadable, MemberRef, ObjectArrayType,
};
use crate::{read_u1, read_u2, read_u4, CafeRc, ParseError};

Expand Down Expand Up @@ -43,7 +42,7 @@ pub enum Opcode<'a> {
Aastore,
AconstNull,
Aload(u16), // both wide and narrow
Anewarray(Cow<'a, str>),
Anewarray(ObjectArrayType<'a>),
Areturn,
Arraylength,
Astore(u16), // both wide and narrow
Expand All @@ -54,7 +53,7 @@ pub enum Opcode<'a> {
Breakpoint,
Caload,
Castore,
Checkcast(Cow<'a, str>),
Checkcast(ObjectArrayType<'a>),
D2f,
D2i,
D2l,
Expand Down Expand Up @@ -141,7 +140,7 @@ pub enum Opcode<'a> {
Impdep2,
Imul,
Ineg,
Instanceof(Cow<'a, str>),
Instanceof(ObjectArrayType<'a>),
Invokedynamic(InvokeDynamic<'a>),
Invokeinterface(MemberRef<'a>, u8),
Invokespecial(MemberRef<'a>),
Expand Down Expand Up @@ -186,8 +185,8 @@ pub enum Opcode<'a> {
Lxor,
Monitorenter,
Monitorexit,
Multianewarray(Cow<'a, str>, u8),
New(Cow<'a, str>),
Multianewarray(ObjectArrayType<'a>, u8),
New(ObjectArrayType<'a>),
Newarray(PrimitiveArrayType),
Nop,
Pop,
Expand Down Expand Up @@ -614,7 +613,15 @@ fn read_opcodes<'a>(
}
Opcode::Invokedynamic(invokedynamic)
}
0xbb => Opcode::New(read_cp_classinfo(code, &mut ix, pool)?),
0xbb => {
let object_array_type = match read_cp_object_array_type(code, &mut ix, pool)? {
ObjectArrayType::ArrayType(_) => {
fail!("Array types not allowed for new opcode at index {}", ix - 2)
}
ObjectArrayType::BinaryName(name) => ObjectArrayType::BinaryName(name),
};
Opcode::New(object_array_type)
}
0xbc => {
let primitive_type = match read_u1(code, &mut ix)? {
4 => PrimitiveArrayType::Boolean,
Expand All @@ -632,11 +639,11 @@ fn read_opcodes<'a>(
};
Opcode::Newarray(primitive_type)
}
0xbd => Opcode::Anewarray(read_cp_classinfo(code, &mut ix, pool)?),
0xbd => Opcode::Anewarray(read_cp_object_array_type(code, &mut ix, pool)?),
0xbe => Opcode::Arraylength,
0xbf => Opcode::Athrow,
0xc0 => Opcode::Checkcast(read_cp_classinfo(code, &mut ix, pool)?),
0xc1 => Opcode::Instanceof(read_cp_classinfo(code, &mut ix, pool)?),
0xc0 => Opcode::Checkcast(read_cp_object_array_type(code, &mut ix, pool)?),
0xc1 => Opcode::Instanceof(read_cp_object_array_type(code, &mut ix, pool)?),
0xc2 => Opcode::Monitorenter,
0xc3 => Opcode::Monitorexit,
0xc4 => {
Expand All @@ -662,7 +669,7 @@ fn read_opcodes<'a>(
}
}
0xc5 => Opcode::Multianewarray(
read_cp_classinfo(code, &mut ix, pool)?,
read_cp_object_array_type(code, &mut ix, pool)?,
read_u1(code, &mut ix)?,
),
0xc6 => Opcode::Ifnull((read_u2(code, &mut ix)? as i16).into()),
Expand Down
29 changes: 28 additions & 1 deletion src/constant_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::ops::DerefMut;
#[cfg(feature = "threadsafe")]
use std::sync::Mutex;

use crate::descriptors::{is_array_descriptor, is_field_descriptor, is_method_descriptor};
use crate::descriptors::FieldDescriptor;
use crate::descriptors::{
is_array_descriptor, is_field_descriptor, is_method_descriptor, parse_array_descriptor,
};
use crate::names::{
is_binary_name, is_module_name, is_unqualified_method_name, is_unqualified_name,
};
Expand Down Expand Up @@ -1071,6 +1074,30 @@ pub(crate) fn read_cp_bootstrap_argument<'a>(
}
}

#[derive(Clone, Debug)]
pub enum ObjectArrayType<'a> {
ArrayType(FieldDescriptor<'a>),
BinaryName(Cow<'a, str>),
}

pub(crate) fn read_cp_object_array_type<'a>(
bytes: &'a [u8],
ix: &mut usize,
pool: &[CafeRc<ConstantPoolEntry<'a>>],
) -> Result<ObjectArrayType<'a>, ParseError> {
let cp_ref = read_cp_ref_any(bytes, ix, pool)?;
match cp_ref.deref() {
ConstantPoolEntry::ClassInfo(x) => {
let name = peel!(x).utf8();
match parse_array_descriptor(&name)? {
Some(desc) => Ok(ObjectArrayType::ArrayType(desc)),
None => Ok(ObjectArrayType::BinaryName(name)),
}
}
_ => fail!("Unexpected constant pool reference type"),
}
}

#[derive(Debug)]
pub enum ConstantPoolItem<'a> {
LiteralConstant(LiteralConstant<'a>),
Expand Down
14 changes: 14 additions & 0 deletions src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ pub(crate) fn parse_method_descriptor<'a>(
})
}

pub(crate) fn parse_array_descriptor<'a>(
data: &Cow<'a, str>,
) -> Result<Option<FieldDescriptor<'a>>, ParseError> {
let desc = parse_field_descriptor(data, 0)?;
if data.len() != desc.byte_len() {
fail!("Not a field descriptor")
}
if data.as_bytes()[0] == b'[' {
Ok(Some(desc))
} else {
Ok(None)
}
}

pub(crate) fn is_field_descriptor(name: &str) -> bool {
match parse_field_descriptor(&Cow::Borrowed(name), 0) {
Ok(desc) => name.len() == desc.byte_len(),
Expand Down

0 comments on commit ca0940a

Please sign in to comment.