Skip to content

Commit 2d036e9

Browse files
sn-o-wsolohsu
authored andcommitted
1 parent e7a4720 commit 2d036e9

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

Diff for: dexmaker/src/main/java/external/com/android/dx/AppDataDirGuesser.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,31 @@ private static String processClassLoaderString43OrLater(String input) {
136136

137137
File[] guessPath(String input) {
138138
List<File> results = new ArrayList<>();
139+
String apkPathRoot = "/data/app/";
139140
for (String potential : splitPathList(input)) {
140-
if (!potential.startsWith("/data/app/")) {
141+
if (!potential.startsWith(apkPathRoot)) {
141142
continue;
142143
}
143-
int start = "/data/app/".length();
144144
int end = potential.lastIndexOf(".apk");
145145
if (end != potential.length() - 4) {
146146
continue;
147147
}
148-
int dash = potential.indexOf("-");
149-
if (dash != -1) {
150-
end = dash;
148+
int endSlash = potential.lastIndexOf("/", end);
149+
if (endSlash == apkPathRoot.length() - 1) {
150+
// Apks cannot be directly under /data/app
151+
continue;
152+
}
153+
int startSlash = potential.lastIndexOf("/", endSlash - 1);
154+
if (startSlash == -1) {
155+
continue;
156+
}
157+
// Look for the first dash after the package name
158+
int dash = potential.indexOf("-", startSlash);
159+
if (dash == -1) {
160+
continue;
151161
}
152-
String packageName = potential.substring(start, end);
162+
end = dash;
163+
String packageName = potential.substring(startSlash + 1, end);
153164
File dataDir = getWriteableDirectory("/data/data/" + packageName);
154165

155166
if (dataDir == null) {

Diff for: dexmaker/src/main/java/external/com/android/dx/Code.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -484,15 +484,27 @@ private void splitCurrentLabel(Label alternateSuccessor, List<Label> catchLabels
484484
* must be a primitive, String, Class, TypeId, or null.
485485
*/
486486
public <T> void loadConstant(Local<T> target, T value) {
487+
loadConstantInternal(target, value);
488+
}
489+
490+
/**
491+
* Copies a class type in {@code target}. The benefit to using this method vs {@link Code#loadConstant(Local, Object)}
492+
* is that the {@code value} can itself be a generated type - {@link TypeId} allows for deferred referencing of class types.
493+
*/
494+
public void loadDeferredClassConstant(Local<Class> target, TypeId value) {
495+
loadConstantInternal(target, value);
496+
}
497+
498+
private void loadConstantInternal(Local target, Object value) {
487499
Rop rop = value == null
488-
? Rops.CONST_OBJECT_NOTHROW
489-
: Rops.opConst(target.type.ropType);
500+
? Rops.CONST_OBJECT_NOTHROW
501+
: Rops.opConst(target.type.ropType);
490502
if (rop.getBranchingness() == BRANCH_NONE) {
491503
addInstruction(new PlainCstInsn(rop, sourcePosition, target.spec(),
492-
RegisterSpecList.EMPTY, Constants.getConstant(value)));
504+
RegisterSpecList.EMPTY, Constants.getConstant(value)));
493505
} else {
494506
addInstruction(new ThrowingCstInsn(rop, sourcePosition,
495-
RegisterSpecList.EMPTY, catches, Constants.getConstant(value)));
507+
RegisterSpecList.EMPTY, catches, Constants.getConstant(value)));
496508
moveResult(target, true);
497509
}
498510
}

Diff for: dexmaker/src/main/java/external/com/android/dx/DexMaker.java

+24-12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import external.com.android.dx.rop.cst.CstType;
3333
import external.com.android.dx.rop.type.StdTypeList;
3434

35+
import java.io.BufferedOutputStream;
3536
import java.io.File;
3637
import java.io.FileOutputStream;
3738
import java.io.IOException;
@@ -46,8 +47,7 @@
4647
import java.util.jar.JarOutputStream;
4748

4849
import static external.com.android.dx.rop.code.AccessFlags.ACC_CONSTRUCTOR;
49-
import static java.lang.reflect.Modifier.PRIVATE;
50-
import static java.lang.reflect.Modifier.STATIC;
50+
import static java.lang.reflect.Modifier.*;
5151

5252
/**
5353
* Generates a <strong>D</strong>alvik <strong>EX</strong>ecutable (dex)
@@ -265,8 +265,8 @@ public Code declare(MethodId<?, ?> method, int flags) {
265265
throw new IllegalStateException("already declared: " + method);
266266
}
267267

268-
int supportedFlags = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED
269-
| Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED
268+
int supportedFlags = Modifier.ABSTRACT | Modifier.NATIVE | Modifier.PUBLIC | Modifier.PRIVATE
269+
| Modifier.PROTECTED | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED | Modifier.TRANSIENT
270270
| AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE;
271271
if ((flags & ~supportedFlags) != 0) {
272272
throw new IllegalArgumentException("Unexpected flag: "
@@ -558,14 +558,22 @@ public ClassLoader generateAndLoad(ClassLoader parent, File dexCache, String dex
558558

559559
result.createNewFile();
560560

561-
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result));
562-
JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME);
563-
byte[] dex = generate();
564-
entry.setSize(dex.length);
565-
jarOut.putNextEntry(entry);
566-
jarOut.write(dex);
567-
jarOut.closeEntry();
568-
jarOut.close();
561+
JarOutputStream jarOut =
562+
new JarOutputStream(new BufferedOutputStream(new FileOutputStream(result)));
563+
try {
564+
JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME);
565+
byte[] dex = generate();
566+
entry.setSize(dex.length);
567+
jarOut.putNextEntry(entry);
568+
try {
569+
jarOut.write(dex);
570+
} finally {
571+
jarOut.closeEntry();
572+
}
573+
} finally {
574+
jarOut.close();
575+
}
576+
569577
return generateClassLoader(result, dexCache, parent);
570578
}
571579

@@ -702,6 +710,10 @@ boolean isDirect() {
702710
}
703711

704712
EncodedMethod toEncodedMethod(DexOptions dexOptions) {
713+
if ((flags & ABSTRACT) != 0 || (flags & NATIVE) != 0) {
714+
return new EncodedMethod(method.constant, flags, null, StdTypeList.EMPTY);
715+
}
716+
705717
RopMethod ropMethod = new RopMethod(code.toBasicBlocks(), 0);
706718
LocalVariableInfo locals = null;
707719
DalvCode dalvCode = RopTranslator.translate(

0 commit comments

Comments
 (0)