aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/android/tools/r8/naming/ProguardMapReader.java
blob: 415b8a7193c057ce18349a6c5d0b5df4b9296f58 (plain)
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
// Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.naming;

import com.android.tools.r8.logging.Log;
import com.android.tools.r8.naming.MemberNaming.FieldSignature;
import com.android.tools.r8.naming.MemberNaming.MethodSignature;
import com.android.tools.r8.naming.MemberNaming.Range;
import com.android.tools.r8.naming.MemberNaming.Signature;
import com.android.tools.r8.naming.MemberNaming.SingleLineRange;
import com.google.common.collect.ImmutableMap;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Parses a Proguard mapping file and produces mappings from obfuscated class names to the original
 * name and from obfuscated member signatures to the original members the obfuscated member
 * was formed of.
 * <p>
 * The expected format is as follows
 * <p>
 * original-type-name ARROW obfuscated-type-name COLON starts a class mapping
 * description and maps original to obfuscated.
 * <p>
 * followed by one or more of
 * <p>
 * signature ARROW name
 * <p>
 * which maps the member with the given signature to the new name. This mapping is not
 * bidirectional as member names are overloaded by signature. To make it bidirectional, we extend
 * the name with the signature of the original member.
 * <p>
 * Due to inlining, we might have the above prefixed with a range (two numbers separated by :).
 * <p>
 * range COLON signature ARROW name
 * <p>
 * This has the same meaning as the above but also encodes the line number range of the member. This
 * may be followed by multiple inline mappings of the form
 * <p>
 * range COLON signature COLON range ARROW name
 * <p>
 * to identify that signature was inlined from the second range to the new line numbers in the first
 * range. This is then followed by information on the call trace to where the member was inlined.
 * These entries have the form
 * <p>
 * range COLON signature COLON number ARROW name
 * <p>
 * and are currently only stored to be able to reproduce them later.
 */
public class ProguardMapReader implements AutoCloseable {

  private final BufferedReader reader;

  @Override
  public void close() throws IOException {
    if (reader != null) {
      reader.close();
    }
  }

  private ProguardMapReader(BufferedReader reader) {
    this.reader = reader;
  }

  public static ClassNameMapper mapperFromInputStream(InputStream in) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF8"));
    try (ProguardMapReader proguardReader = new ProguardMapReader(reader)) {
      return proguardReader.parse();
    }
  }

  public static ClassNameMapper mapperFromFile(Path path) throws IOException {
    return mapperFromInputStream(Files.newInputStream(path));
  }

  public static ClassNameMapper mapperFromString(String contents) throws IOException {
    return mapperFromInputStream(
        new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8)));
  }

  // Internal parser state
  private int lineNo = 0;
  private int lineOffset = 0;
  private String line;

  private char peek() {
    return peek(0);
  }

  private char peek(int distance) {
    return lineOffset + distance < line.length()
        ? line.charAt(lineOffset + distance)
        : '\n';
  }

  private char next() {
    try {
      return line.charAt(lineOffset++);
    } catch (ArrayIndexOutOfBoundsException e) {
      throw new ParseException("Unexpected end of line");
    }
  }

  private boolean nextLine() throws IOException {
    if (line.length() != lineOffset) {
      throw new ParseException("Expected end of line");
    }
    return skipLine();
  }

  private boolean skipLine() throws IOException {
    lineNo++;
    lineOffset = 0;
    line = reader.readLine();
    return hasLine();
  }

  private boolean hasLine() {
    return line != null;
  }

  // Helpers for common pattern
  private void skipWhitespace() {
    while (Character.isWhitespace(peek())) {
      next();
    }
  }

  private char expect(char c) {
    if (next() != c) {
      throw new ParseException("Expected '" + c + "'");
    }
    return c;
  }

  public ClassNameMapper parse() throws IOException {
    // Read the first line.
    line = reader.readLine();
    Map<String, ClassNaming> classNames = parseClassMappings();
    return new ClassNameMapper(classNames);
  }

  // Parsing of entries

  private Map<String, ClassNaming> parseClassMappings() throws IOException {
    ImmutableMap.Builder<String, ClassNaming> builder = ImmutableMap.builder();
    while (hasLine()) {
      String before = parseType(false);
      skipWhitespace();
      // Workaround for proguard map files that contain entries for package-info.java files.
      if (!acceptArrow()) {
        // If this was a package-info line, we parsed the "package" string.
        if (!before.endsWith("package") || !acceptString("-info")) {
          throw new ParseException("Expected arrow after class name " + before);
        }
        skipLine();
        continue;
      }
      skipWhitespace();
      String after = parseType(false);
      expect(':');
      ClassNaming currentClass = new ClassNaming(after, before);
      builder.put(after, currentClass);
      if (nextLine()) {
        parseMemberMappings(currentClass);
      }
    }
    return builder.build();
  }

  private void parseMemberMappings(ClassNaming currentClass) throws IOException {
    MemberNaming current = null;
    Range previousInlineRange = null;
    Signature previousSignature = null;
    String previousRenamedName = null;
    List<Consumer<MemberNaming>> collectedInfos = new ArrayList<>(10);

    while (Character.isWhitespace(peek())) {
      skipWhitespace();
      Range inlinedLineRange = maybeParseRange();
      if (inlinedLineRange != null) {
        expect(':');
      }
      Signature signature = parseSignature();
      Range originalLineRange;
      if (peek() == ':') {
        // This is an inlining definition
        next();
        originalLineRange = maybeParseRange();
        if (originalLineRange == null) {
          if (!skipLine()) {
            break;
          }
          continue;
        }
      } else {
        originalLineRange = null;
      }
      skipWhitespace();
      skipArrow();
      skipWhitespace();
      String renamedName = parseMethodName();
      // If there is no line number information at the front or if it changes, we have a new
      // segment. Likewise, if the range information on the right hand side has two values, we have
      // a new segment.
      if (inlinedLineRange == null
          || previousInlineRange == null
          || originalLineRange == null
          || !previousInlineRange.equals(inlinedLineRange)
          || !originalLineRange.isSingle()) {
        // We are at a range boundary. Either we parsed something new, or an inline frame is over.
        // We detect this by checking whether the previous signature matches the one of current.
        if (current == null || !previousSignature.equals(current.signature)) {
          if (collectedInfos.size() == 1) {
            current = new MemberNaming(previousSignature, previousRenamedName, previousInlineRange);
            currentClass.addMemberEntry(current);
          } else {
            if (Log.ENABLED && !collectedInfos.isEmpty()) {
              Log.warn(getClass(),
                  "More than one member entry that forms a new group at %s %s -> %s",
                  previousInlineRange, previousSignature, previousRenamedName);
            }
          }
        } else {
          MemberNaming finalCurrent = current;
          collectedInfos.forEach(info -> info.accept(finalCurrent));
        }
        collectedInfos.clear();
      }
      // Defer the creation of the info until we have the correct member.
      collectedInfos.add((m) -> m.addInliningRange(inlinedLineRange, signature, originalLineRange));
      // We have parsed the whole line, move on.
      previousInlineRange = inlinedLineRange;
      previousSignature = signature;
      previousRenamedName = renamedName;
      if (!nextLine()) {
        break;
      }
    }
    // Process the last round if lines have been read.
    if (current == null || !previousSignature.equals(current.signature)) {
      if (collectedInfos.size() == 1) {
        current = new MemberNaming(previousSignature, previousRenamedName, previousInlineRange);
        currentClass.addMemberEntry(current);
      }
    } else {
      MemberNaming finalCurrent = current;
      collectedInfos.forEach(info -> info.accept(finalCurrent));
    }
    collectedInfos.clear();
  }

  // Parsing of components

  private void skipIdentifier(boolean allowInit) {
    boolean isInit = false;
    if (allowInit && peek() == '<') {
      // swallow the leading < character
      next();
      isInit = true;
    }
    if (!Character.isJavaIdentifierStart(peek())) {
      throw new ParseException("Identifier expected");
    }
    next();
    while (Character.isJavaIdentifierPart(peek())) {
      next();
    }
    if (isInit) {
      expect('>');
    }
    if (Character.isJavaIdentifierPart(peek())) {
      throw new ParseException("End of identifier expected");
    }
  }

  // Cache for canonicalizing strings.
  // This saves 10% of heap space for large programs.
  final HashMap<String, String> cache = new HashMap<>();

  private String substring(int start) {
    String result = line.substring(start, lineOffset);
    if (cache.containsKey(result)) {
      return cache.get(result);
    }
    cache.put(result, result);
    return result;
  }

  private String parseMethodName() {
    int startPosition = lineOffset;
    skipIdentifier(true);
    while (peek() == '.') {
      next();
      skipIdentifier(true);
    }
    return substring(startPosition);
  }

  private String parseType(boolean allowArray) {
    int startPosition = lineOffset;
    skipIdentifier(false);
    while (peek() == '.') {
      next();
      skipIdentifier(false);
    }
    if (allowArray) {
      while (peek() == '[') {
        next();
        expect(']');
      }
    }
    return substring(startPosition);
  }

  private Signature parseSignature() {
    String type = parseType(true);
    expect(' ');
    String name = parseMethodName();
    Signature signature;
    if (peek() == '(') {
      next();
      String[] arguments;
      if (peek() == ')') {
        arguments = new String[0];
      } else {
        List<String> items = new LinkedList<>();
        items.add(parseType(true));
        while (peek() != ')') {
          expect(',');
          items.add(parseType(true));
        }
        arguments = items.toArray(new String[items.size()]);
      }
      expect(')');
      signature = new MethodSignature(name, type, arguments);
    } else {
      signature = new FieldSignature(name, type);
    }
    return signature;
  }

  private void skipArrow() {
    expect('-');
    expect('>');
  }

  private boolean acceptArrow() {
    if (peek() == '-' && peek(1) == '>') {
      next();
      next();
      return true;
    }
    return false;
  }

  private boolean acceptString(String s) {
    for (int i = 0; i < s.length(); i++) {
      if (peek(i) != s.charAt(i)) {
        return false;
      }
    }
    for (int i = 0; i < s.length(); i++) {
      next();
    }
    return true;
  }

  private Range maybeParseRange() {
    if (!Character.isDigit(peek())) {
      return null;
    }
    int from = parseNumber();
    if (peek() != ':') {
      return new SingleLineRange(from);
    }
    expect(':');
    int to = parseNumber();
    return new Range(from, to);
  }

  private int parseNumber() {
    int result = 0;
    if (!Character.isDigit(peek())) {
      throw new ParseException("Number expected");
    }
    do {
      result *= 10;
      result += Character.getNumericValue(next());
    } while (Character.isDigit(peek()));
    return result;
  }

  private class ParseException extends RuntimeException {

    private final int lineNo;
    private final int lineOffset;
    private final String msg;

    ParseException(String msg) {
      lineNo = ProguardMapReader.this.lineNo;
      lineOffset = ProguardMapReader.this.lineOffset;
      this.msg = msg;
    }

    @Override
    public String toString() {
      return "Parse error [" + lineNo + ":" + lineOffset + "] " + msg;
    }
  }
}