Skip to main content

lvm_common/
error.rs

1use crate::utils::cursor::CursorError;
2use std::fmt::{Display, Formatter};
3
4// TODO: looks like a trash bin, needs refactoring
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum SignatureErr {
7    UnexpectedEnd,
8    MissingParamsOpenParen,
9    MissingParamsCloseParen,
10    TrailingCharacters,
11    InvalidIdentifier,
12    MissingSuper,
13    InvalidBound,
14    Type(TypeDescriptorErr),
15    InvalidSuperClassType,
16}
17
18impl Display for SignatureErr {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        match self {
21            SignatureErr::UnexpectedEnd => write!(f, "Unexpected end of signature"),
22            SignatureErr::MissingParamsOpenParen => {
23                write!(f, "Missing opening parenthesis for method parameters")
24            }
25            SignatureErr::MissingParamsCloseParen => {
26                write!(f, "Missing closing parenthesis for method parameters")
27            }
28            SignatureErr::TrailingCharacters => write!(f, "Trailing characters after signature"),
29            SignatureErr::InvalidIdentifier => write!(f, "Invalid identifier in signature"),
30            SignatureErr::MissingSuper => write!(f, "Missing 'super' in class signature"),
31            SignatureErr::InvalidBound => write!(f, "Invalid bound in type variable"),
32            SignatureErr::Type(err) => write!(f, "Type descriptor error: {}", err),
33            SignatureErr::InvalidSuperClassType => {
34                write!(f, "Invalid superclass type in class signature")
35            }
36        }
37    }
38}
39
40impl From<TypeDescriptorErr> for SignatureErr {
41    fn from(value: TypeDescriptorErr) -> Self {
42        SignatureErr::Type(value)
43    }
44}
45
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub enum TypeDescriptorErr {
48    UnexpectedEnd,
49    InvalidType(char),
50    InvalidObjectRef,
51}
52
53impl Display for TypeDescriptorErr {
54    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55        match self {
56            TypeDescriptorErr::UnexpectedEnd => write!(f, "Unexpected end of type descriptor"),
57            TypeDescriptorErr::InvalidType(c) => write!(f, "Invalid type character: {}", c),
58            TypeDescriptorErr::InvalidObjectRef => write!(f, "Invalid object reference type"),
59        }
60    }
61}
62
63#[derive(Debug, Clone, PartialEq, Eq)]
64pub enum MethodDescriptorErr {
65    ShouldStartWithParentheses(String),
66    MissingClosingParenthesis(String),
67    TrailingCharacters,
68    Type(String, TypeDescriptorErr),
69}
70
71impl Display for MethodDescriptorErr {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        match self {
74            MethodDescriptorErr::ShouldStartWithParentheses(desc) => {
75                write!(f, "Method descriptor should start with '(': {}", desc)
76            }
77            MethodDescriptorErr::MissingClosingParenthesis(desc) => {
78                write!(
79                    f,
80                    "Missing closing parenthesis in method descriptor: {}",
81                    desc
82                )
83            }
84            MethodDescriptorErr::TrailingCharacters => {
85                write!(f, "Trailing characters after method descriptor")
86            }
87            MethodDescriptorErr::Type(desc, err) => {
88                write!(f, "Type descriptor error in '{}': {}", desc, err)
89            }
90        }
91    }
92}
93
94#[derive(Debug, Clone, PartialEq, Eq)]
95pub enum InstructionErr {
96    UnsupportedOpCode(u8),
97    UnknownArrayType(u8),
98    Cursor(CursorError),
99    UnexpectedEof,
100}
101
102impl Display for InstructionErr {
103    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
104        match self {
105            InstructionErr::UnsupportedOpCode(opcode) => {
106                write!(f, "Unsupported opcode: {:#X}", opcode)
107            }
108            InstructionErr::UnknownArrayType(atype) => {
109                write!(f, "Unknown array type: {}", atype)
110            }
111            InstructionErr::Cursor(err) => write!(f, "Cursor error: {}", err),
112            InstructionErr::UnexpectedEof => write!(f, "Unexpected end of instruction stream"),
113        }
114    }
115}
116
117impl From<CursorError> for InstructionErr {
118    fn from(value: CursorError) -> Self {
119        InstructionErr::Cursor(value)
120    }
121}
122
123#[derive(Debug)]
124pub enum LinkageError {
125    Instruction(InstructionErr),
126    UnsupportedOpCode(u8),
127    DuplicatedCodeAttr,
128    //TODO: confused 4.7.13. The LocalVariableTable Attribute
129    //DuplicatedLocalVariableTableAttr,
130    DuplicatedSignatureAttr,
131    DuplicatedStackMapTable,
132    DuplicatedExceptionAttribute,
133    DuplicatedRuntimeVisibleAnnotationsAttr,
134    DuplicatedRuntimeInvisibleAnnotationsAttr,
135    CodeAttrIsAmbiguousForNative,
136    RuntimeConstantPool(RuntimePoolError),
137    Cursor(CursorError),
138    ClassFile(ClassFormatErr),
139    DuplicatedClassInMethod,
140    MethodClassIsNotSet,
141}
142
143impl From<InstructionErr> for LinkageError {
144    fn from(value: InstructionErr) -> Self {
145        LinkageError::Instruction(value)
146    }
147}
148
149impl From<CursorError> for LinkageError {
150    fn from(value: CursorError) -> Self {
151        LinkageError::Cursor(value)
152    }
153}
154
155impl From<RuntimePoolError> for LinkageError {
156    fn from(value: RuntimePoolError) -> Self {
157        LinkageError::RuntimeConstantPool(value)
158    }
159}
160
161impl From<ClassFormatErr> for LinkageError {
162    fn from(value: ClassFormatErr) -> Self {
163        LinkageError::ClassFile(value)
164    }
165}
166
167#[derive(Debug)]
168pub enum RuntimePoolError {
169    MethodDescriptor(MethodDescriptorErr),
170    TypeDescriptor(TypeDescriptorErr),
171    TryingToAccessUnresolved(u16, String),
172}
173
174#[derive(Debug, Clone, PartialEq, Eq)]
175pub enum ClassFormatErr {
176    Cursor(CursorError),
177    WrongMagic(u32),
178    TrailingBytes,
179    UnknownTag(u8),
180    /// First u16 is index, second is expected type, third is actual type
181    TypeError(u16, String, String),
182    ConstantNotFound(u16),
183    UnknownStackFrameType(u8),
184    UnknownAttribute(String),
185    AttributeIsNotShared(String),
186    InvalidMethodHandleKind(u8),
187    Signature(SignatureErr),
188    MethodDescriptor(MethodDescriptorErr),
189    Instruction(InstructionErr),
190    TypeDescriptor(TypeDescriptorErr),
191    Format(std::fmt::Error),
192}
193
194impl Display for ClassFormatErr {
195    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
196        match self {
197            ClassFormatErr::Cursor(err) => write!(f, "Cursor error: {}", err),
198            ClassFormatErr::WrongMagic(magic) => write!(f, "Wrong magic number: {:#X}", magic),
199            ClassFormatErr::TrailingBytes => write!(f, "Trailing bytes after class file"),
200            ClassFormatErr::UnknownTag(tag) => write!(f, "Unknown constant pool tag: {}", tag),
201            ClassFormatErr::TypeError(index, expected, actual) => write!(
202                f,
203                "Type error at index {}: expected {}, got {}",
204                index, expected, actual
205            ),
206            ClassFormatErr::ConstantNotFound(index) => {
207                write!(f, "Constant not found at index {}", index)
208            }
209            ClassFormatErr::UnknownStackFrameType(frame_type) => {
210                write!(f, "Unknown stack frame type: {}", frame_type)
211            }
212            ClassFormatErr::UnknownAttribute(name) => write!(f, "Unknown attribute: {}", name),
213            ClassFormatErr::AttributeIsNotShared(name) => {
214                write!(f, "Attribute is not shared: {}", name)
215            }
216            ClassFormatErr::InvalidMethodHandleKind(kind) => {
217                write!(f, "Invalid method handle kind: {}", kind)
218            }
219            ClassFormatErr::Signature(err) => write!(f, "Signature error: {}", err),
220            ClassFormatErr::MethodDescriptor(err) => {
221                write!(f, "Method descriptor error: {}", err)
222            }
223            ClassFormatErr::Instruction(err) => write!(f, "Instruction error: {}", err),
224            ClassFormatErr::TypeDescriptor(err) => {
225                write!(f, "Type descriptor error: {}", err)
226            }
227            ClassFormatErr::Format(err) => write!(f, "Format error: {}", err),
228        }
229    }
230}
231
232impl From<CursorError> for ClassFormatErr {
233    fn from(value: CursorError) -> Self {
234        ClassFormatErr::Cursor(value)
235    }
236}
237
238impl From<SignatureErr> for ClassFormatErr {
239    fn from(value: SignatureErr) -> Self {
240        ClassFormatErr::Signature(value)
241    }
242}
243
244impl From<MethodDescriptorErr> for ClassFormatErr {
245    fn from(value: MethodDescriptorErr) -> Self {
246        ClassFormatErr::MethodDescriptor(value)
247    }
248}
249
250impl From<InstructionErr> for ClassFormatErr {
251    fn from(value: InstructionErr) -> Self {
252        ClassFormatErr::Instruction(value)
253    }
254}
255
256impl From<TypeDescriptorErr> for ClassFormatErr {
257    fn from(value: TypeDescriptorErr) -> Self {
258        ClassFormatErr::TypeDescriptor(value)
259    }
260}
261
262impl From<std::fmt::Error> for ClassFormatErr {
263    fn from(value: std::fmt::Error) -> Self {
264        ClassFormatErr::Format(value)
265    }
266}