aboutsummaryrefslogtreecommitdiff
path: root/dexlib2/src/main/java/com/android/tools/smali/dexlib2/DexFileFactory.java
blob: c464c47403f3ba85de0036d2e7f33fd35934a1ec (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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * Copyright 2012, Google LLC
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google LLC nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.tools.smali.dexlib2;

import static java.util.Collections.unmodifiableList;

import com.android.tools.smali.dexlib2.iface.DexFile;
import com.android.tools.smali.dexlib2.iface.MultiDexContainer;
import com.android.tools.smali.dexlib2.iface.MultiDexContainer.DexEntry;

import com.android.tools.smali.dexlib2.dexbacked.DexBackedDexFile;
import com.android.tools.smali.dexlib2.dexbacked.DexBackedDexFile.NotADexFile;
import com.android.tools.smali.dexlib2.dexbacked.DexBackedOdexFile;
import com.android.tools.smali.dexlib2.dexbacked.OatFile;
import com.android.tools.smali.dexlib2.dexbacked.OatFile.NotAnOatFileException;
import com.android.tools.smali.dexlib2.dexbacked.OatFile.VdexProvider;
import com.android.tools.smali.dexlib2.dexbacked.ZipDexContainer;
import com.android.tools.smali.dexlib2.dexbacked.ZipDexContainer.NotAZipFileException;
import com.android.tools.smali.dexlib2.writer.pool.DexPool;
import com.android.tools.smali.util.ExceptionWithContext;
import com.android.tools.smali.util.InputStreamUtil;
import com.android.tools.smali.util.StringUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public final class DexFileFactory {

    @Nonnull
    public static DexBackedDexFile loadDexFile(@Nonnull String path, @Nullable Opcodes opcodes) throws IOException {
        return loadDexFile(new File(path), opcodes);
    }

    /**
     * Loads a dex/apk/odex/oat file.
     *
     * For oat files with multiple dex files, the first will be opened. For zip/apk files, the "classes.dex" entry
     * will be opened.
     *
     * @param file The file to open
     * @param opcodes The set of opcodes to use
     * @return A DexBackedDexFile for the given file
     *
     * @throws UnsupportedOatVersionException If file refers to an unsupported oat file
     * @throws DexFileNotFoundException If file does not exist, if file is a zip file but does not have a "classes.dex"
     * entry, or if file is an oat file that has no dex entries.
     * @throws UnsupportedFileTypeException If file is not a valid dex/zip/odex/oat file, or if the "classes.dex" entry
     * in a zip file is not a valid dex file
     */
    @Nonnull
    public static DexBackedDexFile loadDexFile(@Nonnull File file, @Nullable Opcodes opcodes) throws IOException {
        if (!file.exists()) {
            throw new DexFileNotFoundException("%s does not exist", file.getName());
        }

        try {
            ZipDexContainer container = new ZipDexContainer(file, opcodes);
            return new DexEntryFinder(file.getPath(), container).findEntry("classes.dex", true).getDexFile();
        } catch (NotAZipFileException ex) {
            // eat it and continue
        }

        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
            try {
                return DexBackedDexFile.fromInputStream(opcodes, inputStream);
            } catch (NotADexFile ex) {
                // just eat it
            }

            try {
                return DexBackedOdexFile.fromInputStream(opcodes, inputStream);
            } catch (DexBackedOdexFile.NotAnOdexFile ex) {
                // just eat it
            }

            // Note: DexBackedDexFile.fromInputStream and DexBackedOdexFile.fromInputStream will reset inputStream
            // back to the same position, if they fails

            OatFile oatFile = null;
            try {
                oatFile = OatFile.fromInputStream(inputStream, new FilenameVdexProvider(file));
            } catch (NotAnOatFileException ex) {
                // just eat it
            }

            if (oatFile != null) {
                if (oatFile.isSupportedVersion() == OatFile.UNSUPPORTED) {
                    throw new UnsupportedOatVersionException(oatFile);
                }

                List<DexBackedDexFile> oatDexFiles = oatFile.getDexFiles();

                if (oatDexFiles.size() == 0) {
                    throw new DexFileNotFoundException("Oat file %s contains no dex files", file.getName());
                }

                return oatDexFiles.get(0);
            }
        }

        throw new UnsupportedFileTypeException("%s is not an apk, dex, odex or oat file.", file.getPath());
    }

    /**
     * Loads a dex entry from a container format (zip/oat)
     *
     * This has two modes of operation, depending on the exactMatch parameter. When exactMatch is true, it will only
     * load an entry whose name exactly matches that provided by the dexEntry parameter.
     *
     * When exactMatch is false, then it will search for any entry that dexEntry is a path suffix of. "path suffix"
     * meaning all the path components in dexEntry must fully match the corresponding path components in the entry name,
     * but some path components at the beginning of entry name can be missing.
     *
     * For example, if an oat file contains a "/system/framework/framework.jar:classes2.dex" entry, then the following
     * will match (not an exhaustive list):
     *
     * "/system/framework/framework.jar:classes2.dex"
     * "system/framework/framework.jar:classes2.dex"
     * "framework/framework.jar:classes2.dex"
     * "framework.jar:classes2.dex"
     * "classes2.dex"
     *
     * Note that partial path components specifically don't match. So something like "work/framework.jar:classes2.dex"
     * would not match.
     *
     * If dexEntry contains an initial slash, it will be ignored for purposes of this suffix match -- but not when
     * performing an exact match.
     *
     * If multiple entries match the given dexEntry, a MultipleMatchingDexEntriesException will be thrown
     *
     * @param file The container file. This must be either a zip (apk) file or an oat file.
     * @param dexEntry The name of the entry to load. This can either be the exact entry name, if exactMatch is true,
     *                 or it can be a path suffix.
     * @param exactMatch If true, dexE
     * @param opcodes The set of opcodes to use
     * @return A DexBackedDexFile for the given entry
     *
     * @throws UnsupportedOatVersionException If file refers to an unsupported oat file
     * @throws DexFileNotFoundException If the file does not exist, or if no matching entry could be found
     * @throws UnsupportedFileTypeException If file is not a valid zip/oat file, or if the matching entry is not a
     * valid dex file
     * @throws MultipleMatchingDexEntriesException If multiple entries match the given dexEntry
     */
    public static DexEntry<? extends DexBackedDexFile> loadDexEntry(
            @Nonnull File file,
            @Nonnull String dexEntry,
            boolean exactMatch,
            @Nullable Opcodes opcodes) throws IOException {
        if (!file.exists()) {
            throw new DexFileNotFoundException("Container file %s does not exist", file.getName());
        }

        try {
            ZipDexContainer container = new ZipDexContainer(file, opcodes);
            return new DexEntryFinder(file.getPath(), container).findEntry(dexEntry, exactMatch);
        } catch (NotAZipFileException ex) {
            // eat it and continue
        }

        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
            OatFile oatFile = null;
            try {
                oatFile = OatFile.fromInputStream(inputStream, new FilenameVdexProvider(file));
            } catch (NotAnOatFileException ex) {
                // just eat it
            }

            if (oatFile != null) {
                if (oatFile.isSupportedVersion() == OatFile.UNSUPPORTED) {
                    throw new UnsupportedOatVersionException(oatFile);
                }

                List<? extends DexFile> oatDexFiles = oatFile.getDexFiles();

                if (oatDexFiles.size() == 0) {
                    throw new DexFileNotFoundException("Oat file %s contains no dex files", file.getName());
                }

                return new DexEntryFinder(file.getPath(), oatFile).findEntry(dexEntry, exactMatch);
            }
        }

        throw new UnsupportedFileTypeException("%s is not an apk or oat file.", file.getPath());
    }

    /**
     * Loads a file containing 1 or more dex files
     *
     * If the given file is a dex or odex file, it will return a MultiDexContainer containing that single entry.
     * Otherwise, for an oat or zip file, it will return an OatFile or ZipDexContainer respectively.
     *
     * @param file The file to open
     * @param opcodes The set of opcodes to use
     * @return A MultiDexContainer
     * @throws DexFileNotFoundException If the given file does not exist
     * @throws UnsupportedFileTypeException If the given file is not a valid dex/zip/odex/oat file
     */
    public static MultiDexContainer<? extends DexBackedDexFile> loadDexContainer(
            @Nonnull File file, @Nullable final Opcodes opcodes) throws IOException {
        if (!file.exists()) {
            throw new DexFileNotFoundException("%s does not exist", file.getName());
        }

        ZipDexContainer zipDexContainer = new ZipDexContainer(file, opcodes);
        if (zipDexContainer.isZipFile()) {
            return zipDexContainer;
        }

        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
            try {
                DexBackedDexFile dexFile = DexBackedDexFile.fromInputStream(opcodes, inputStream);
                return new SingletonMultiDexContainer(file.getPath(), dexFile);
            } catch (NotADexFile ex) {
                // just eat it
            }

            try {
                DexBackedOdexFile odexFile = DexBackedOdexFile.fromInputStream(opcodes, inputStream);
                return new SingletonMultiDexContainer(file.getPath(), odexFile);
            } catch (DexBackedOdexFile.NotAnOdexFile ex) {
                // just eat it
            }

            // Note: DexBackedDexFile.fromInputStream and DexBackedOdexFile.fromInputStream will reset inputStream
            // back to the same position, if they fails

            OatFile oatFile = null;
            try {
                oatFile = OatFile.fromInputStream(inputStream, new FilenameVdexProvider(file));
            } catch (NotAnOatFileException ex) {
                // just eat it
            }

            if (oatFile != null) {
                // TODO: we should support loading earlier oat files, just not deodexing them
                if (oatFile.isSupportedVersion() == OatFile.UNSUPPORTED) {
                    throw new UnsupportedOatVersionException(oatFile);
                }
                return oatFile;
            }
        }

        throw new UnsupportedFileTypeException("%s is not an apk, dex, odex or oat file.", file.getPath());
    }

    /**
     * Writes a DexFile out to disk
     *
     * @param path The path to write the dex file to
     * @param dexFile a DexFile to write
     */
    public static void writeDexFile(@Nonnull String path, @Nonnull DexFile dexFile) throws IOException {
        DexPool.writeTo(path, dexFile);
    }

    private DexFileFactory() {}

    public static class DexFileNotFoundException extends ExceptionWithContext {
        public DexFileNotFoundException(@Nullable String message, Object... formatArgs) {
            super(message, formatArgs);
        }

        public DexFileNotFoundException(Throwable cause, @Nullable String message, Object... formatArgs) {
            super(cause, message, formatArgs);
        }
    }

    public static class UnsupportedOatVersionException extends ExceptionWithContext {
        @Nonnull public final OatFile oatFile;

        public UnsupportedOatVersionException(@Nonnull OatFile oatFile) {
            super("Unsupported oat version: %d", oatFile.getOatVersion());
            this.oatFile = oatFile;
        }
    }

    public static class MultipleMatchingDexEntriesException extends ExceptionWithContext {
        public MultipleMatchingDexEntriesException(@Nonnull String message, Object... formatArgs) {
            super(String.format(message, formatArgs));
        }
    }

    public static class UnsupportedFileTypeException extends ExceptionWithContext {
        public UnsupportedFileTypeException(@Nonnull String message, Object... formatArgs) {
            super(String.format(message, formatArgs));
        }
    }

    /**
     * Matches two entries fully, ignoring any initial slash, if any
     */
    private static boolean fullEntryMatch(@Nonnull String entry, @Nonnull String targetEntry) {
        if (entry.equals(targetEntry)) {
            return true;
        }

        if (entry.charAt(0) == '/') {
            entry = entry.substring(1);
        }

        if (targetEntry.charAt(0) == '/') {
            targetEntry = targetEntry.substring(1);
        }

        return entry.equals(targetEntry);
    }

    /**
     * Performs a partial match against entry and targetEntry.
     *
     * This is considered a partial match if targetEntry is a suffix of entry, and if the suffix starts
     * on a path "part" (ignoring the initial separator, if any). '/' and ':' and '!' are considered separators for
     * this.
     *
     * So entry="/blah/blah/something.dex" and targetEntry="lah/something.dex" shouldn't match, but
     * both targetEntry="blah/something.dex" and "/blah/something.dex" should match.
     */
    private static boolean partialEntryMatch(String entry, String targetEntry) {
        if (entry.equals(targetEntry)) {
            return true;
        }

        if (!entry.endsWith(targetEntry)) {
            return false;
        }

        // Make sure the first matching part is a full entry. We don't want to match "/blah/blah/something.dex" with
        // "lah/something.dex", but both "/blah/something.dex" and "blah/something.dex" should match
        char precedingChar = entry.charAt(entry.length() - targetEntry.length() - 1);
        char firstTargetChar = targetEntry.charAt(0);
        // This is a device path, so we should always use the linux separator '/', rather than the current platform's
        // separator
        return firstTargetChar == ':' || firstTargetChar == '/' || firstTargetChar == '!' ||
                precedingChar == ':' || precedingChar == '/' || precedingChar == '!';
    }

    protected static class DexEntryFinder {
        private final String filename;
        private final MultiDexContainer<? extends DexBackedDexFile> dexContainer;

        public DexEntryFinder(@Nonnull String filename,
                              @Nonnull MultiDexContainer<? extends DexBackedDexFile> dexContainer) {
            this.filename = filename;
            this.dexContainer = dexContainer;
        }

        @Nonnull
        public MultiDexContainer.DexEntry<? extends DexBackedDexFile> findEntry(
                @Nonnull String targetEntry, boolean exactMatch) throws IOException {
            if (exactMatch) {
                try {
                    MultiDexContainer.DexEntry<? extends DexBackedDexFile> entry = dexContainer.getEntry(targetEntry);
                    if (entry == null) {
                        throw new DexFileNotFoundException("Could not find entry %s in %s.", targetEntry, filename);
                    }
                    return entry;
                } catch (NotADexFile ex) {
                    throw new UnsupportedFileTypeException("Entry %s in %s is not a dex file", targetEntry, filename);
                }
            }

            // find all full and partial matches
            List<String> fullMatches = new ArrayList<>();
            List<MultiDexContainer.DexEntry<? extends DexBackedDexFile>> fullEntries = new ArrayList<>();
            List<String> partialMatches = new ArrayList<>();
            List<MultiDexContainer.DexEntry<? extends DexBackedDexFile>> partialEntries = new ArrayList<>();
            for (String entry: dexContainer.getDexEntryNames()) {
                if (fullEntryMatch(entry, targetEntry)) {
                    // We want to grab all full matches, regardless of whether they're actually a dex file.
                    fullMatches.add(entry);
                    MultiDexContainer.DexEntry<? extends DexBackedDexFile> dexEntry = dexContainer.getEntry(entry);
                    assert dexEntry != null;
                    fullEntries.add(dexEntry);
                } else if (partialEntryMatch(entry, targetEntry)) {
                    partialMatches.add(entry);
                    MultiDexContainer.DexEntry<? extends DexBackedDexFile> dexEntry = dexContainer.getEntry(entry);
                    assert dexEntry != null;
                    partialEntries.add(dexEntry);
                }
            }

            // full matches always take priority
            if (fullEntries.size() == 1) {
                try {
                    MultiDexContainer.DexEntry<? extends DexBackedDexFile> dexEntry = fullEntries.get(0);
                    assert dexEntry != null;
                    return dexEntry;
                } catch (NotADexFile ex) {
                    throw new UnsupportedFileTypeException("Entry %s in %s is not a dex file",
                            fullMatches.get(0), filename);
                }
            }
            if (fullEntries.size() > 1) {
                // This should be quite rare. This would only happen if an oat file has two entries that differ
                // only by an initial path separator. e.g. "/blah/blah.dex" and "blah/blah.dex"
                throw new MultipleMatchingDexEntriesException(String.format(
                        "Multiple entries in %s match %s: %s", filename, targetEntry,
                        StringUtils.join(fullMatches, ", ")
                        ));
            }

            if (partialEntries.size() == 0) {
                throw new DexFileNotFoundException("Could not find a dex entry in %s matching %s",
                        filename, targetEntry);
            }
            if (partialEntries.size() > 1) {
                throw new MultipleMatchingDexEntriesException(String.format(
                        "Multiple dex entries in %s match %s: %s", filename, targetEntry,
                        StringUtils.join(partialMatches, ", ")
                        ));
            }
            return partialEntries.get(0);
        }
    }

    private static class SingletonMultiDexContainer implements MultiDexContainer<DexBackedDexFile> {
        private final String entryName;
        private final DexBackedDexFile dexFile;

        public SingletonMultiDexContainer(@Nonnull String entryName, @Nonnull DexBackedDexFile dexFile) {
            this.entryName = entryName;
            this.dexFile = dexFile;
        }

        @Nonnull @Override public List<String> getDexEntryNames() {
            return unmodifiableList(List.of(entryName));
        }

        @Nullable @Override public DexEntry<DexBackedDexFile> getEntry(@Nonnull String entryName) {
            if (entryName.equals(this.entryName)) {
                return new DexEntry<DexBackedDexFile>() {
                    @Nonnull
                    @Override
                    public String getEntryName() {
                        return entryName;
                    }

                    @Nonnull
                    @Override
                    public DexBackedDexFile getDexFile() {
                        return dexFile;
                    }

                    @Nonnull
                    @Override
                    public MultiDexContainer<? extends DexBackedDexFile> getContainer() {
                        return SingletonMultiDexContainer.this;
                    }
                };
            }
            return null;
        }
    }

    public static class FilenameVdexProvider implements VdexProvider {
        private final File vdexFile;

        @Nullable
        private byte[] buf = null;
        private boolean loadedVdex = false;

        public FilenameVdexProvider(File oatFile) {
            File oatParent = oatFile.getAbsoluteFile().getParentFile();
            String baseName = getNameWithoutExtension(oatFile.getAbsolutePath());
            vdexFile = new File(oatParent, baseName + ".vdex");
        }

        @Nullable @Override public byte[] getVdex() {
            if (!loadedVdex) {
                File candidateFile = vdexFile;

                if (!candidateFile.exists()) {
                    // On api 28, for framework files, the vdex file in the architecture-specific directory is just a
                    // symlink to a common vdex file in the framework directory. When loop-mounting a system image, that
                    // symlink won't resolve because it uses an absolute path. As a workaround, we'll just search upward
                    // one directory to see if it's there.
                    File parentDirectory = candidateFile.getParentFile().getParentFile();
                    if (parentDirectory != null) {
                        candidateFile = new File(parentDirectory, vdexFile.getName());
                    }
                }

                if (candidateFile.exists()) {
                    try {
                        buf = InputStreamUtil.toByteArray(new FileInputStream(candidateFile));
                    } catch (FileNotFoundException e) {
                        buf = null;
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
                loadedVdex = true;
            }

            return buf;
        }

        public static String getNameWithoutExtension(String file) {
            String fileName = new File(file).getName();
            int dotIndex = fileName.lastIndexOf('.');
            return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
          }
    }
}