|
| 1 | +use alloc::collections::BTreeMap; |
| 2 | + |
| 3 | +use java_constants::MethodAccessFlags; |
| 4 | + |
| 5 | +use crate::{AttributeInfo, ClassFileError, ClassInfo, ConstantPoolReference, constant_pool::ConstantPoolItem}; |
| 6 | + |
| 7 | +enum MemberKind { |
| 8 | + Field, |
| 9 | + Method, |
| 10 | +} |
| 11 | + |
| 12 | +pub(crate) fn validate_class(class: &ClassInfo) -> Result<(), ClassFileError> { |
| 13 | + if !is_internal_class_name(&class.this_class) |
| 14 | + || class.super_class.as_ref().is_some_and(|name| !is_internal_class_name(name)) |
| 15 | + || class.interfaces.iter().any(|name| !is_internal_class_name(name)) |
| 16 | + || !validate_constant_pool(&class.constant_pool) |
| 17 | + { |
| 18 | + return Err(ClassFileError::InvalidFormat); |
| 19 | + } |
| 20 | + |
| 21 | + for field in &class.fields { |
| 22 | + if !is_field_descriptor(&field.descriptor) { |
| 23 | + return Err(ClassFileError::InvalidFormat); |
| 24 | + } |
| 25 | + |
| 26 | + let constant_values = field |
| 27 | + .attributes |
| 28 | + .iter() |
| 29 | + .filter_map(|attribute| match attribute { |
| 30 | + AttributeInfo::ConstantValue(value) => Some(value), |
| 31 | + _ => None, |
| 32 | + }) |
| 33 | + .collect::<alloc::vec::Vec<_>>(); |
| 34 | + if constant_values.len() > 1 |
| 35 | + || constant_values.first().is_some_and(|value| { |
| 36 | + !matches!( |
| 37 | + (field.descriptor.as_str(), *value), |
| 38 | + ("Z" | "B" | "C" | "S" | "I", ConstantPoolReference::Integer(_)) |
| 39 | + | ("J", ConstantPoolReference::Long(_)) |
| 40 | + | ("F", ConstantPoolReference::Float(_)) |
| 41 | + | ("D", ConstantPoolReference::Double(_)) |
| 42 | + | ("Ljava/lang/String;", ConstantPoolReference::String(_)) |
| 43 | + ) |
| 44 | + }) |
| 45 | + { |
| 46 | + return Err(ClassFileError::InvalidFormat); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + for method in &class.methods { |
| 51 | + if !is_method_descriptor(&method.descriptor) { |
| 52 | + return Err(ClassFileError::InvalidFormat); |
| 53 | + } |
| 54 | + |
| 55 | + let code_attributes = method |
| 56 | + .attributes |
| 57 | + .iter() |
| 58 | + .filter(|attribute| matches!(attribute, AttributeInfo::Code(_))) |
| 59 | + .count(); |
| 60 | + if method.access_flags.intersects(MethodAccessFlags::ABSTRACT | MethodAccessFlags::NATIVE) { |
| 61 | + if code_attributes != 0 { |
| 62 | + return Err(ClassFileError::InvalidFormat); |
| 63 | + } |
| 64 | + } else if code_attributes != 1 { |
| 65 | + return Err(ClassFileError::InvalidFormat); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | + |
| 72 | +fn validate_constant_pool(constant_pool: &BTreeMap<u16, ConstantPoolItem>) -> bool { |
| 73 | + constant_pool.values().all(|item| match item { |
| 74 | + ConstantPoolItem::Class { name_index } => constant_pool |
| 75 | + .get(name_index) |
| 76 | + .and_then(ConstantPoolItem::utf8) |
| 77 | + .is_some_and(|name| is_class_constant_name(&name)), |
| 78 | + ConstantPoolItem::String { string_index } => constant_pool.get(string_index).and_then(ConstantPoolItem::utf8).is_some(), |
| 79 | + ConstantPoolItem::Fieldref { |
| 80 | + class_index, |
| 81 | + name_and_type_index, |
| 82 | + } => validate_member_reference(constant_pool, *class_index, *name_and_type_index, MemberKind::Field), |
| 83 | + ConstantPoolItem::Methodref { |
| 84 | + class_index, |
| 85 | + name_and_type_index, |
| 86 | + } |
| 87 | + | ConstantPoolItem::InterfaceMethodref { |
| 88 | + class_index, |
| 89 | + name_and_type_index, |
| 90 | + } => validate_member_reference(constant_pool, *class_index, *name_and_type_index, MemberKind::Method), |
| 91 | + ConstantPoolItem::NameAndType { |
| 92 | + name_index, |
| 93 | + descriptor_index, |
| 94 | + } => { |
| 95 | + let name = constant_pool.get(name_index).and_then(ConstantPoolItem::utf8); |
| 96 | + let descriptor = constant_pool.get(descriptor_index).and_then(ConstantPoolItem::utf8); |
| 97 | + name.is_some_and(|name| !name.is_empty()) |
| 98 | + && descriptor.is_some_and(|descriptor| is_field_descriptor(&descriptor) || is_method_descriptor(&descriptor)) |
| 99 | + } |
| 100 | + _ => true, |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +fn validate_member_reference(constant_pool: &BTreeMap<u16, ConstantPoolItem>, class_index: u16, name_and_type_index: u16, kind: MemberKind) -> bool { |
| 105 | + let class_name = constant_pool |
| 106 | + .get(&class_index) |
| 107 | + .and_then(ConstantPoolItem::class_name_index) |
| 108 | + .and_then(|index| constant_pool.get(&index)) |
| 109 | + .and_then(ConstantPoolItem::utf8); |
| 110 | + let name_and_type = constant_pool.get(&name_and_type_index).and_then(ConstantPoolItem::name_and_type); |
| 111 | + let Some((name_index, descriptor_index)) = name_and_type else { |
| 112 | + return false; |
| 113 | + }; |
| 114 | + let name = constant_pool.get(&name_index).and_then(ConstantPoolItem::utf8); |
| 115 | + let descriptor = constant_pool.get(&descriptor_index).and_then(ConstantPoolItem::utf8); |
| 116 | + |
| 117 | + class_name.is_some_and(|name| is_class_constant_name(&name)) |
| 118 | + && name.is_some_and(|name| !name.is_empty()) |
| 119 | + && descriptor.is_some_and(|descriptor| match kind { |
| 120 | + MemberKind::Field => is_field_descriptor(&descriptor), |
| 121 | + MemberKind::Method => is_method_descriptor(&descriptor), |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +fn is_internal_class_name(name: &str) -> bool { |
| 126 | + !name.is_empty() && !name.starts_with('[') && !name.contains(['.', ';', '[']) |
| 127 | +} |
| 128 | + |
| 129 | +fn is_class_constant_name(name: &str) -> bool { |
| 130 | + is_internal_class_name(name) || array_dimensions(name).is_some() |
| 131 | +} |
| 132 | + |
| 133 | +fn is_field_descriptor(descriptor: &str) -> bool { |
| 134 | + let mut cursor = 0; |
| 135 | + parse_field_type(descriptor.as_bytes(), &mut cursor) && cursor == descriptor.len() |
| 136 | +} |
| 137 | + |
| 138 | +fn is_method_descriptor(descriptor: &str) -> bool { |
| 139 | + let bytes = descriptor.as_bytes(); |
| 140 | + if bytes.first() != Some(&b'(') { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + let mut cursor = 1; |
| 145 | + while bytes.get(cursor).is_some_and(|byte| *byte != b')') { |
| 146 | + if !parse_field_type(bytes, &mut cursor) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + } |
| 150 | + if bytes.get(cursor) != Some(&b')') { |
| 151 | + return false; |
| 152 | + } |
| 153 | + cursor += 1; |
| 154 | + |
| 155 | + if bytes.get(cursor) == Some(&b'V') { |
| 156 | + cursor += 1; |
| 157 | + } else if !parse_field_type(bytes, &mut cursor) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + cursor == bytes.len() |
| 162 | +} |
| 163 | + |
| 164 | +fn array_dimensions(descriptor: &str) -> Option<usize> { |
| 165 | + let bytes = descriptor.as_bytes(); |
| 166 | + let dimensions = bytes.iter().take_while(|byte| **byte == b'[').count(); |
| 167 | + if dimensions == 0 || dimensions > u8::MAX as usize { |
| 168 | + return None; |
| 169 | + } |
| 170 | + |
| 171 | + let mut cursor = 0; |
| 172 | + if parse_field_type(bytes, &mut cursor) && cursor == bytes.len() { |
| 173 | + Some(dimensions) |
| 174 | + } else { |
| 175 | + None |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +fn parse_field_type(bytes: &[u8], cursor: &mut usize) -> bool { |
| 180 | + let mut dimensions = 0; |
| 181 | + while bytes.get(*cursor) == Some(&b'[') { |
| 182 | + dimensions += 1; |
| 183 | + if dimensions > u8::MAX as usize { |
| 184 | + return false; |
| 185 | + } |
| 186 | + *cursor += 1; |
| 187 | + } |
| 188 | + |
| 189 | + match bytes.get(*cursor) { |
| 190 | + Some(b'B' | b'C' | b'D' | b'F' | b'I' | b'J' | b'S' | b'Z') => { |
| 191 | + *cursor += 1; |
| 192 | + true |
| 193 | + } |
| 194 | + Some(b'L') => { |
| 195 | + let name_start = *cursor + 1; |
| 196 | + let Some(relative_end) = bytes[name_start..].iter().position(|byte| *byte == b';') else { |
| 197 | + return false; |
| 198 | + }; |
| 199 | + let name_end = name_start + relative_end; |
| 200 | + if name_end == name_start || bytes[name_start..name_end].iter().any(|byte| matches!(byte, b'.' | b'[' | b';')) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + *cursor = name_end + 1; |
| 204 | + true |
| 205 | + } |
| 206 | + _ => false, |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +#[cfg(test)] |
| 211 | +mod tests { |
| 212 | + use super::{array_dimensions, is_field_descriptor, is_method_descriptor}; |
| 213 | + |
| 214 | + #[test] |
| 215 | + fn validates_field_and_method_descriptors() { |
| 216 | + assert!(is_field_descriptor("Ljava/lang/String;")); |
| 217 | + assert!(is_field_descriptor("[[I")); |
| 218 | + assert!(!is_field_descriptor("V")); |
| 219 | + assert!(!is_field_descriptor("[V")); |
| 220 | + assert!(!is_field_descriptor("Igarbage")); |
| 221 | + |
| 222 | + assert!(is_method_descriptor("([Ljava/lang/String;I)V")); |
| 223 | + assert!(!is_method_descriptor("(V)V")); |
| 224 | + assert!(!is_method_descriptor("(I")); |
| 225 | + assert!(!is_method_descriptor("()")); |
| 226 | + } |
| 227 | + |
| 228 | + #[test] |
| 229 | + fn counts_valid_array_dimensions() { |
| 230 | + assert_eq!(array_dimensions("[[Ljava/lang/String;"), Some(2)); |
| 231 | + assert_eq!(array_dimensions("java/lang/String"), None); |
| 232 | + assert_eq!(array_dimensions("[V"), None); |
| 233 | + } |
| 234 | +} |
0 commit comments