1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std::borrow::Cow;
use std::error::Error;
use std::ffi::CStr;
use std::fmt;
use std::mem;
use std::os::raw::c_char;
use std::ptr;
use thiserror::Error;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum UsymLiteErrorKind {
MisalignedBuffer,
BadHeader,
BadMagic,
BadVersion,
BadLineCount,
BufferSmallerThanAdvertised,
MissingStringTable,
UnterminatedStringTable,
BadLines,
BadId,
BadName,
BadOperatingSystem,
BadArchitecture,
BadEncoding,
}
impl fmt::Display for UsymLiteErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UsymLiteErrorKind::MisalignedBuffer => write!(f, "misaligned pointer to buffer"),
UsymLiteErrorKind::BadHeader => write!(f, "missing or undersized header"),
UsymLiteErrorKind::BadMagic => write!(f, "missing or wrong usymlite magic bytes"),
UsymLiteErrorKind::BadVersion => write!(f, "missing or wrong version number"),
UsymLiteErrorKind::BadLineCount => write!(f, "unreadable record count"),
UsymLiteErrorKind::BufferSmallerThanAdvertised => {
write!(f, "buffer does not contain all data header claims it has")
}
UsymLiteErrorKind::MissingStringTable => write!(f, "string table is missing"),
UsymLiteErrorKind::UnterminatedStringTable => {
write!(f, "string table does not end with a NULL byte")
}
UsymLiteErrorKind::BadLines => {
write!(f, "could not construct list of source records")
}
UsymLiteErrorKind::BadId => write!(f, "assembly ID is missing or unreadable"),
UsymLiteErrorKind::BadName => write!(f, "assembly name is missing or unreadable"),
UsymLiteErrorKind::BadOperatingSystem => {
write!(f, "operating system is missing or unreadable")
}
UsymLiteErrorKind::BadArchitecture => {
write!(f, "architecture is missing or unreadable")
}
UsymLiteErrorKind::BadEncoding => {
write!(f, "part of the file is not encoded in valid UTF-8")
}
}
}
}
#[derive(Debug, Error)]
#[error("{kind}")]
pub struct UsymLiteError {
kind: UsymLiteErrorKind,
#[source]
source: Option<Box<dyn Error + Send + Sync + 'static>>,
}
impl UsymLiteError {
fn new<E>(kind: UsymLiteErrorKind, source: E) -> Self
where
E: Into<Box<dyn Error + Send + Sync>>,
{
let source = Some(source.into());
Self { kind, source }
}
pub fn kind(&self) -> UsymLiteErrorKind {
self.kind
}
}
impl From<UsymLiteErrorKind> for UsymLiteError {
fn from(kind: UsymLiteErrorKind) -> Self {
Self { kind, source: None }
}
}
#[derive(Debug, Clone)]
#[repr(C)]
struct UsymLiteHeader {
magic: u32,
version: u32,
line_count: u32,
id: u32,
os: u32,
arch: u32,
}
#[derive(Debug, Clone)]
#[repr(C)]
pub struct UsymLiteLine {
address: u64,
filename: u32,
line: u32,
}
pub struct UsymLiteSymbols<'a> {
header: &'a UsymLiteHeader,
lines: &'a [UsymLiteLine],
string_table: &'a [u8],
}
impl<'a> UsymLiteSymbols<'a> {
const MAGIC: &'static [u8] = b"sym-";
pub fn parse(buf: &'a [u8]) -> Result<UsymLiteSymbols<'a>, UsymLiteError> {
if buf.as_ptr().align_offset(8) != 0 {
return Err(UsymLiteError::from(UsymLiteErrorKind::MisalignedBuffer));
}
if buf.len() < mem::size_of::<UsymLiteHeader>() {
return Err(UsymLiteError::from(UsymLiteErrorKind::BadHeader));
}
if buf.get(..Self::MAGIC.len()) != Some(Self::MAGIC) {
return Err(UsymLiteError::from(UsymLiteErrorKind::BadMagic));
}
let header = unsafe { &*(buf.as_ptr() as *const UsymLiteHeader) };
if header.version != 2 {
return Err(UsymLiteError::from(UsymLiteErrorKind::BadVersion));
}
let line_count: usize = header
.line_count
.try_into()
.map_err(|e| UsymLiteError::new(UsymLiteErrorKind::BadLineCount, e))?;
let stringtable_offset =
mem::size_of::<UsymLiteHeader>() + line_count * mem::size_of::<UsymLiteLine>();
if buf.len() < stringtable_offset {
return Err(UsymLiteError::from(
UsymLiteErrorKind::BufferSmallerThanAdvertised,
));
}
let lines_ptr = unsafe { buf.as_ptr().add(mem::size_of::<UsymLiteHeader>()) };
let lines = unsafe {
let lines_ptr: *const UsymLiteLine = lines_ptr.cast();
let lines_ptr = ptr::slice_from_raw_parts(lines_ptr, line_count);
lines_ptr
.as_ref()
.ok_or_else(|| UsymLiteError::from(UsymLiteErrorKind::BadLines))
}?;
let stringtable = buf
.get(stringtable_offset..)
.ok_or_else(|| UsymLiteError::from(UsymLiteErrorKind::MissingStringTable))?;
if stringtable.last() != Some(&0u8) {
return Err(UsymLiteError::from(
UsymLiteErrorKind::UnterminatedStringTable,
));
}
Ok(Self {
header,
lines,
string_table: stringtable,
})
}
fn get_string(&self, offset: u32) -> Option<&'a CStr> {
let offset: usize = offset.try_into().unwrap();
if offset >= self.string_table.len() {
return None;
}
let table_ptr = self.string_table.as_ptr();
let string_ptr = unsafe { table_ptr.add(offset) as *const c_char };
let string = unsafe { CStr::from_ptr(string_ptr) };
Some(string)
}
pub fn id(&self) -> Result<Cow<'a, str>, UsymLiteError> {
self.get_string(self.header.id)
.map(|s| s.to_string_lossy())
.ok_or_else(|| UsymLiteError::from(UsymLiteErrorKind::BadId))
}
pub fn os(&self) -> Result<Cow<'a, str>, UsymLiteError> {
self.get_string(self.header.os)
.map(|s| s.to_string_lossy())
.ok_or_else(|| UsymLiteError::from(UsymLiteErrorKind::BadOperatingSystem))
}
pub fn arch(&self) -> Result<Cow<'a, str>, UsymLiteError> {
self.get_string(self.header.arch)
.map(|s| s.to_string_lossy())
.ok_or_else(|| UsymLiteError::from(UsymLiteErrorKind::BadArchitecture))
}
pub fn get_record(&self, index: usize) -> Option<&UsymLiteLine> {
self.lines.get(index)
}
}
#[cfg(test)]
mod tests {
use std::{fs::File, io};
use symbolic_common::ByteView;
use symbolic_testutils::fixture;
use super::*;
fn empty_usymlite() -> Result<ByteView<'static>, io::Error> {
let file = File::open(fixture("il2cpp/empty.usymlite"))?;
ByteView::map_file_ref(&file)
}
#[test]
fn test_parse_header() {
let data = empty_usymlite().unwrap();
let info = UsymLiteSymbols::parse(&data).unwrap();
assert_eq!(
info.header.magic,
u32::from_le_bytes([b's', b'y', b'm', b'-'])
);
assert_eq!(info.header.version, 2);
assert_eq!(info.header.line_count, 0);
assert_eq!(info.id().unwrap(), "153d10d10db033d6aacda4e1948da97b");
assert_eq!(info.os().unwrap(), "mac");
assert_eq!(info.arch().unwrap(), "arm64");
}
}