Skip to content

Commit

Permalink
[dexdiff] bugfix.
Browse files Browse the repository at this point in the history
    If old dex has no changes, while small dex rules require us to generate
small dex for it, the original code will crash.
  • Loading branch information
tys282000 committed Sep 22, 2016
1 parent ddd547f commit a4c0c2c
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
public class DexPatchApplier {
private final Dex oldDex;
private final Dex patchedDex;

/** May be null if we need to generate small patch. **/
private final DexPatchFile patchFile;

private final SmallPatchedDexItemFile extraInfoFile;
private final IndexMap oldToFullPatchedIndexMap;
private final IndexMap patchedToSmallPatchedIndexMap;
Expand Down Expand Up @@ -172,11 +175,11 @@ public DexPatchApplier(
this.extraInfoFile = extraAddedDexElementsFile;

if ((patchFileIn == null) && (extraAddedDexElementsFile == null
|| extraAddedDexElementsFile.isAffectedOldDex(this.oldDexSignStr))) {
|| !extraAddedDexElementsFile.isAffectedOldDex(this.oldDexSignStr))) {
throw new UnsupportedOperationException(
"patchFileIn is null, and extraAddedDexElementFile"
+ "(smallPatchInfo) is null or does not mention "
+ "oldDexIn. Consider copy oldDexIn instead."
+ "oldDexIn."
);
}
}
Expand All @@ -189,15 +192,17 @@ public void executeAndSaveTo(OutputStream out) throws IOException {
throw new IOException("failed to compute old dex's signature.");
}

byte[] oldDexSignInPatchFile = this.patchFile.getOldDexSignature();
if (CompareUtils.uArrCompare(oldDexSign, oldDexSignInPatchFile) != 0) {
throw new IOException(
String.format(
"old dex signature mismatch! expected: %s, actual: %s",
Arrays.toString(oldDexSign),
Arrays.toString(oldDexSignInPatchFile)
)
);
if (this.patchFile != null) {
byte[] oldDexSignInPatchFile = this.patchFile.getOldDexSignature();
if (CompareUtils.uArrCompare(oldDexSign, oldDexSignInPatchFile) != 0) {
throw new IOException(
String.format(
"old dex signature mismatch! expected: %s, actual: %s",
Arrays.toString(oldDexSign),
Arrays.toString(oldDexSignInPatchFile)
)
);
}
}

String oldDexSignStr = Hex.toHexString(oldDexSign);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(Annotation item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedAnnotationSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedAnnotationSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ protected int getItemSize(AnnotationSetRefList item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedAnnotationSetRefListSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedAnnotationSetRefListSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(AnnotationSet item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedAnnotationSetSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedAnnotationSetSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(AnnotationsDirectory item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedAnnotationsDirectorySectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedAnnotationsDirectorySectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(ClassData item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedClassDataSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedClassDataSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ protected int getItemSize(ClassDef item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedClassDefSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedClassDefSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(Code item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedCodeSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedCodeSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(DebugInfoItem item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedDebugInfoSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedDebugInfoSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(FieldId item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedFieldIdSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedFieldIdSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(MethodId item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedMethodIdSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedMethodIdSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(ProtoId item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedProtoIdSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedProtoIdSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(EncodedValue item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedEncodedArraySectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedEncodedArraySectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ protected int getItemSize(StringData item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedStringDataSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedStringDataSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(Integer item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedTypeIdSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedTypeIdSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected int getItemSize(TypeList item) {

@Override
protected int getFullPatchSectionBase() {
return this.patchFile.getPatchedTypeListSectionOffset();
if (this.patchFile != null) {
return this.patchFile.getPatchedTypeListSectionOffset();
} else {
return getTocSection(this.oldDex).off;
}
}

@Override
Expand Down

0 comments on commit a4c0c2c

Please sign in to comment.