lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import java.lang.reflect.Method;
import java.util.Enumeration;
 
import java.nio.file.Files;
import java.nio.file.Paths;
 
/**
 * DexFile tests (Dalvik-specific).
 */
public class Main {
    private static final String CLASS_PATH =
        System.getenv("DEX_LOCATION") + "/071-dexfile-map-clean-ex.jar";
 
    /**
     * Prep the environment then run the test.
     */
    public static void main(String[] args) throws Exception {
        // Load the dex file, this is a pre-requisite to mmap-ing it in.
        Class<?> AnotherClass = testDexFile();
        // Check that the memory maps are clean.
        testDexMemoryMaps();
 
        // Prevent garbage collector from collecting our DexFile
        // (and unmapping too early) by using it after we finish
        // our verification.
        AnotherClass.newInstance();
    }
 
    private static boolean checkSmapsEntry(String[] smapsLines, int offset) {
      String nameDescription = smapsLines[offset];
      String[] split = nameDescription.split(" ");
 
      String permissions = split[1];
      // Mapped as read-only + anonymous.
      if (!permissions.startsWith("r--p")) {
        return false;
      }
 
      boolean validated = false;
 
      // We have the right entry, now make sure it's valid.
      for (int i = offset; i < smapsLines.length; ++i) {
        String line = smapsLines[i];
 
        if (line.startsWith("Shared_Dirty") || line.startsWith("Private_Dirty")) {
          String lineTrimmed = line.trim();
          String[] lineSplit = lineTrimmed.split(" +");
 
          String sizeUsuallyInKb = lineSplit[lineSplit.length - 2];
 
          sizeUsuallyInKb = sizeUsuallyInKb.trim();
 
          if (!sizeUsuallyInKb.equals("0")) {
            System.out.println(
                "ERROR: Memory mapping for " + CLASS_PATH + " is unexpectedly dirty");
            System.out.println(line);
          } else {
            validated = true;
          }
        }
 
        // VmFlags marks the "end" of an smaps entry.
        if (line.startsWith("VmFlags")) {
          break;
        }
      }
 
      if (validated) {
        System.out.println("Secondary dexfile mmap is clean");
      } else {
        System.out.println("ERROR: Memory mapping is missing Shared_Dirty/Private_Dirty entries");
      }
 
      return true;
    }
 
    private static void testDexMemoryMaps() throws Exception {
        // Ensure that the secondary dex file is mapped clean (directly from JAR file).
        String smaps = new String(Files.readAllBytes(Paths.get("/proc/self/smaps")));
 
        String[] smapsLines = smaps.split("\n");
        boolean found = true;
        for (int i = 0; i < smapsLines.length; ++i) {
          if (smapsLines[i].contains(CLASS_PATH)) {
            if (checkSmapsEntry(smapsLines, i)) {
              return;
            } // else we found the wrong one, keep going.
          }
        }
 
        // Error case:
        System.out.println("Could not find " + CLASS_PATH + " RO-anonymous smaps entry");
        System.out.println(smaps);
    }
 
    private static Class<?> testDexFile() throws Exception {
        ClassLoader classLoader = Main.class.getClassLoader();
        Class<?> DexFile = classLoader.loadClass("dalvik.system.DexFile");
        Method DexFile_loadDex = DexFile.getMethod("loadDex",
                                                   String.class,
                                                   String.class,
                                                   Integer.TYPE);
        Method DexFile_entries = DexFile.getMethod("entries");
        Object dexFile = DexFile_loadDex.invoke(null, CLASS_PATH, null, 0);
        Enumeration<String> e = (Enumeration<String>) DexFile_entries.invoke(dexFile);
        while (e.hasMoreElements()) {
            String className = e.nextElement();
            System.out.println(className);
        }
 
        Method DexFile_loadClass = DexFile.getMethod("loadClass",
                                                     String.class,
                                                     ClassLoader.class);
        Class<?> AnotherClass = (Class<?>)DexFile_loadClass.invoke(dexFile,
            "Another", Main.class.getClassLoader());
        return AnotherClass;
    }
}