proguard 5.10.3

Basic proguard mapping file handling for Rust
Documentation
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Contains functionality for parsing ProGuard mapping files into a
//! structured representation ([`ParsedProguardMapping`]) that can be
//! used to create a [`ProguardMapper`](crate::ProguardMapper) or
//! [`ProguardCache`](crate::ProguardCache).

use std::collections::{HashMap, HashSet};
use std::hash::Hash;

use crate::{mapping::R8Header, ProguardMapping, ProguardRecord};

/// Newtype around &str for obfuscated class and method names.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub(crate) struct ObfuscatedName<'s>(&'s str);

impl<'s> ObfuscatedName<'s> {
    pub(crate) fn as_str(&self) -> &'s str {
        self.0
    }
}

impl std::ops::Deref for ObfuscatedName<'_> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

/// Newtype around &str for original class and method names.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub(crate) struct OriginalName<'s>(&'s str);

impl<'s> OriginalName<'s> {
    pub(crate) fn as_str(&self) -> &'s str {
        self.0
    }
}

impl std::ops::Deref for OriginalName<'_> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.0
    }
}

/// Information about a class in a ProGuard file.
#[derive(Clone, Debug, Default)]
pub(crate) struct ClassInfo<'s> {
    /// The source file in which the class is defined.
    pub(crate) source_file: Option<&'s str>,
    /// Whether this class was synthesized by the compiler.
    pub(crate) is_synthesized: bool,
}

/// The receiver of a method.
///
/// This enum is used to keep track of whether
/// a method's receiver is the class under which
/// it is encountered (`ThisClass`) or another
/// class (`OtherClass`).
///
/// # Example
/// Consider this mapping:
/// ```text
/// example.Main -> a:
///     1:1 run() 1:1 -> a
///     2:2 example.Other.run() 1:1 -> b
/// ```
/// The `receiver` of the first method would be
/// `ThisClass("example.Main")` (because it is defined
/// under `"example.Main"` and has no explicit receiver),
/// while the receiver of the second method would be
/// `OtherClass("example.Other")`.
#[derive(Clone, Copy, Debug)]
pub(crate) enum MethodReceiver<'s> {
    ThisClass(OriginalName<'s>),
    OtherClass(OriginalName<'s>),
}

impl<'s> MethodReceiver<'s> {
    pub(crate) fn name(&self) -> OriginalName<'s> {
        match self {
            Self::ThisClass(name) => *name,
            Self::OtherClass(name) => *name,
        }
    }
}

impl PartialEq for MethodReceiver<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.name() == other.name()
    }
}

impl Eq for MethodReceiver<'_> {}

impl std::hash::Hash for MethodReceiver<'_> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name().hash(state)
    }
}

/// A key that uniquely identifies a method.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub(crate) struct MethodKey<'s> {
    /// The method's receiver.
    pub(crate) receiver: MethodReceiver<'s>,
    /// The method's name.
    pub(crate) name: OriginalName<'s>,
    /// The method's argument string.
    pub(crate) arguments: &'s str,
}

/// Information about a method in a ProGuard file.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct MethodInfo {
    /// Whether this method was synthesized by the compiler.
    pub(crate) is_synthesized: bool,
    /// Whether this method is an outline.
    pub(crate) is_outline: bool,
}

/// Supported rewrite frame actions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum RewriteAction<'s> {
    RemoveInnerFrames(usize),
    /// Placeholder to retain unsupported action strings for future handling.
    Unknown(&'s str),
}

/// Supported rewrite frame conditions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum RewriteCondition<'s> {
    Throws(&'s str),
    /// Placeholder to retain unsupported condition strings for future handling.
    Unknown(&'s str),
}

/// A rewrite frame rule attached to a method mapping.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct RewriteRule<'s> {
    pub(crate) conditions: Vec<RewriteCondition<'s>>,
    pub(crate) actions: Vec<RewriteAction<'s>>,
}

/// A member record in a Proguard file.
#[derive(Clone, Debug)]
pub(crate) struct Member<'s> {
    /// The method the member refers to.
    pub(crate) method: MethodKey<'s>,
    /// The obfuscated/minified start line, `None` when no minified range prefix was present.
    pub(crate) startline: Option<usize>,
    /// The obfuscated/minified end line, `None` when no minified range prefix was present.
    pub(crate) endline: Option<usize>,
    /// The original start line, `None` when no line mapping was present.
    pub(crate) original_startline: Option<usize>,
    /// The original end line.
    pub(crate) original_endline: Option<usize>,
    /// Optional outline callsite positions map attached to this member.
    pub(crate) outline_callsite_positions: Option<HashMap<usize, usize>>,
    /// Optional rewrite rules attached to this member.
    pub(crate) rewrite_rules: Vec<RewriteRule<'s>>,
}

fn parse_rewrite_rule<'s>(conditions: &[&'s str], actions: &[&'s str]) -> Option<RewriteRule<'s>> {
    if conditions.is_empty() || actions.is_empty() {
        return None;
    }

    let mut parsed_conditions = Vec::with_capacity(conditions.len());
    for condition in conditions {
        let condition = condition.trim();
        if condition.is_empty() {
            return None;
        }
        if let Some(rest) = condition.strip_prefix("throws(") {
            let descriptor = rest.strip_suffix(')')?;
            if descriptor.is_empty() {
                return None;
            }
            parsed_conditions.push(RewriteCondition::Throws(descriptor));
        } else {
            parsed_conditions.push(RewriteCondition::Unknown(condition));
        }
    }

    let mut parsed_actions = Vec::with_capacity(actions.len());
    for action in actions {
        let action = action.trim();
        if action.is_empty() {
            return None;
        }
        if let Some(rest) = action.strip_prefix("removeInnerFrames(") {
            let count_str = rest.strip_suffix(')')?;
            let count = count_str.parse().ok()?;
            parsed_actions.push(RewriteAction::RemoveInnerFrames(count));
        } else {
            parsed_actions.push(RewriteAction::Unknown(action));
        }
    }

    Some(RewriteRule {
        conditions: parsed_conditions,
        actions: parsed_actions,
    })
}

/// A collection of member records for a particular class
/// and obfuscated method.
#[derive(Clone, Debug, Default)]
pub(crate) struct Members<'s> {
    /// The complete list of members for the class and method.
    pub(crate) all: Vec<Member<'s>>,
    /// The complete list of members for the class and method,
    /// grouped by arguments string.
    pub(crate) by_params: HashMap<&'s str, Vec<Member<'s>>>,
}

/// A parsed representation of a [`ProguardMapping`].
#[derive(Clone, Debug, Default)]
pub(crate) struct ParsedProguardMapping<'s> {
    /// A mapping from obfuscated to original class names.
    pub(crate) class_names: HashMap<ObfuscatedName<'s>, OriginalName<'s>>,
    /// A mapping from original class names to class information.
    pub(crate) class_infos: HashMap<OriginalName<'s>, ClassInfo<'s>>,
    /// A mapping from method keys to method information.
    pub(crate) method_infos: HashMap<MethodKey<'s>, MethodInfo>,
    /// A mapping from obfuscated class and method names to members.
    pub(crate) members: HashMap<(ObfuscatedName<'s>, ObfuscatedName<'s>), Members<'s>>,
}

impl<'s> ParsedProguardMapping<'s> {
    pub(crate) fn parse(mapping: ProguardMapping<'s>, initialize_param_mapping: bool) -> Self {
        let mut slf = Self::default();
        let mut current_class_name = None;
        let mut current_class = ClassInfo::default();
        let mut unique_methods: HashSet<(&str, &str, &str)> = HashSet::new();

        let mut records = mapping.iter().filter_map(Result::ok).peekable();

        while let Some(record) = records.next() {
            match record {
                ProguardRecord::Field { .. } => {}
                ProguardRecord::Header { .. } => {}
                ProguardRecord::R8Header(_) => {
                    // R8 headers can be skipped; they are already
                    // handled in the branches for `Class` and `Method`.
                }
                ProguardRecord::Class {
                    original,
                    obfuscated,
                } => {
                    // Flush the previous class if there is one.
                    if let Some((obfuscated, original)) = current_class_name {
                        slf.class_names.insert(obfuscated, original);
                        slf.class_infos.insert(original, current_class);
                    }

                    current_class_name = Some((ObfuscatedName(obfuscated), OriginalName(original)));
                    current_class = ClassInfo::default();
                    unique_methods.clear();

                    // Consume R8 headers attached to this class.
                    while let Some(ProguardRecord::R8Header(r8_header)) = records.peek() {
                        match r8_header {
                            R8Header::RewriteFrame { .. } => {}
                            R8Header::SourceFile { file_name } => {
                                current_class.source_file = Some(file_name)
                            }
                            R8Header::Synthesized => current_class.is_synthesized = true,
                            R8Header::Outline => {}
                            R8Header::OutlineCallsite { .. } => {}
                            R8Header::Other => {}
                        }

                        records.next();
                    }
                }

                ProguardRecord::Method {
                    original,
                    obfuscated,
                    original_class,
                    line_mapping,
                    arguments,
                    ..
                } => {
                    let current_line = if initialize_param_mapping {
                        line_mapping
                    } else {
                        None
                    };
                    let (mut startline, mut endline) = match line_mapping.as_ref() {
                        Some(lm) => (lm.startline, lm.endline),
                        None => (None, None),
                    };
                    let (mut original_startline, mut original_endline) = match line_mapping {
                        None => (None, None),
                        Some(lm) => match lm.original_startline {
                            Some(os) => (Some(os), lm.original_endline),
                            None => (startline, endline),
                        },
                    };

                    // Normalize inverted ranges independently.
                    if let (Some(s), Some(e)) = (startline, endline) {
                        if s > e {
                            startline = Some(e);
                            endline = Some(s);
                        }
                    }
                    if let (Some(os), Some(oe)) = (original_startline, original_endline) {
                        if os > oe {
                            original_startline = Some(oe);
                            original_endline = Some(os);
                        }
                    }

                    let Some((current_class_obfuscated, current_class_original)) =
                        current_class_name
                    else {
                        // `current_class_name` is only `None` before the first class entry is encountered.
                        // If we hit this case, there's a member record before the first class record, which
                        // is an error. Properly handling this would be nice here, for now we return an empty `Self`.
                        return Self::default();
                    };

                    let members = slf
                        .members
                        .entry((current_class_obfuscated, ObfuscatedName(obfuscated)))
                        .or_default();

                    let mut rewrite_rules: Vec<RewriteRule<'s>> = Vec::new();
                    let method = MethodKey {
                        // Save the receiver name, keeping track of whether it's the current class
                        // (i.e. the one to which this member record belongs) or another class.
                        receiver: match original_class {
                            Some(original_class) => {
                                MethodReceiver::OtherClass(OriginalName(original_class))
                            }
                            None => MethodReceiver::ThisClass(current_class_original),
                        },
                        name: OriginalName(original),
                        arguments,
                    };

                    let method_info: &mut MethodInfo = slf.method_infos.entry(method).or_default();

                    // Collect any OutlineCallsite mapping attached to this member.
                    let mut outline_callsite_positions: Option<HashMap<usize, usize>> = None;

                    // Consume R8 headers attached to this method/member.
                    while let Some(ProguardRecord::R8Header(r8_header)) = records.peek() {
                        match r8_header {
                            R8Header::Synthesized => method_info.is_synthesized = true,
                            R8Header::Outline => {
                                method_info.is_outline = true;
                            }
                            R8Header::RewriteFrame {
                                conditions,
                                actions,
                            } => {
                                if let Some(rule) = parse_rewrite_rule(conditions, actions) {
                                    rewrite_rules.push(rule);
                                }
                            }
                            R8Header::OutlineCallsite {
                                positions,
                                outline: _,
                            } => {
                                // Attach outline callsite mapping to this specific member.
                                let map: HashMap<usize, usize> = positions
                                    .iter()
                                    .filter_map(|(k, v)| k.parse::<usize>().ok().map(|kk| (kk, *v)))
                                    .collect();
                                if !map.is_empty() {
                                    outline_callsite_positions = Some(map);
                                }
                            }
                            R8Header::SourceFile { .. } | R8Header::Other => {}
                        }

                        records.next();
                    }

                    let member = Member {
                        method,
                        startline,
                        endline,
                        original_startline,
                        original_endline,
                        outline_callsite_positions,
                        rewrite_rules,
                    };

                    members.all.push(member.clone());

                    if !initialize_param_mapping {
                        continue;
                    }
                    // If the next line has the same leading line range then this method
                    // has been inlined by the code minification process, as a result
                    // it can't show in method traces and can be safely ignored.
                    if let Some(ProguardRecord::Method {
                        line_mapping: Some(next_line),
                        ..
                    }) = records.peek()
                    {
                        if let Some(current_line_mapping) = current_line {
                            if (current_line_mapping.startline == next_line.startline)
                                && (current_line_mapping.endline == next_line.endline)
                            {
                                continue;
                            }
                        }
                    }

                    let key = (obfuscated, arguments, original);
                    if unique_methods.insert(key) {
                        members
                            .by_params
                            .entry(arguments)
                            .or_insert_with(|| Vec::with_capacity(1))
                            .push(member.clone());
                    }
                } // end ProguardRecord::Method
            }
        }

        // Flush the last class
        if let Some((obfuscated, original)) = current_class_name {
            slf.class_names.insert(obfuscated, original);
            slf.class_infos.insert(original, current_class);
        }

        slf
    }
}