From 78e92b05240eda9a1e15cdccb859200cd4d35566 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Sun, 2 Feb 2020 20:34:29 -0800 Subject: [PATCH] Add support for automatically rewriting array types in the rewriter This adds a new rewriteUnwrappedType to the TypeRewriter class, which gets called with the unwrapped type for array types, with the potentially modified return value automatically being re-array-ified. --- .../org/jf/dexlib2/rewriter/TypeRewriter.java | 23 +++++ .../rewriter/RewriteArrayTypeTest.java | 83 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 dexlib2/src/test/java/org/jf/dexlib2/rewriter/RewriteArrayTypeTest.java diff --git a/dexlib2/src/main/java/org/jf/dexlib2/rewriter/TypeRewriter.java b/dexlib2/src/main/java/org/jf/dexlib2/rewriter/TypeRewriter.java index 9731154d..2755294c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/rewriter/TypeRewriter.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/rewriter/TypeRewriter.java @@ -35,6 +35,29 @@ import javax.annotation.Nonnull; public class TypeRewriter implements Rewriter { @Nonnull @Override public String rewrite(@Nonnull String value) { + if (value.length() > 0 && value.charAt(0) == '[') { + int dimensions = value.lastIndexOf("[") + 1; + + String arraySpecifiers = value.substring(0, dimensions); + String unwrappedType = value.substring(dimensions); + String rewrittenType = rewriteUnwrappedType(unwrappedType); + return arraySpecifiers + rewrittenType; + } else { + return rewriteUnwrappedType(value); + } + } + + /** + * This is called by the default rewrite implementation with the unwrapped type. + * + *

For array types, the unwrapped type is the type with the array specifiers removed. And there is no difference + * for non-array types. + * + * @param value The unwrapped type + * @return The modified version of the unwrapped type. This will be re-array-ified if the original wrapped type was + * an array. + */ + @Nonnull public String rewriteUnwrappedType(@Nonnull String value) { return value; } } diff --git a/dexlib2/src/test/java/org/jf/dexlib2/rewriter/RewriteArrayTypeTest.java b/dexlib2/src/test/java/org/jf/dexlib2/rewriter/RewriteArrayTypeTest.java new file mode 100644 index 00000000..f3cd09ca --- /dev/null +++ b/dexlib2/src/test/java/org/jf/dexlib2/rewriter/RewriteArrayTypeTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020, Google Inc. + * All rights reserved. + * + * 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 Inc. 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 org.jf.dexlib2.rewriter; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import org.jf.dexlib2.AccessFlags; +import org.jf.dexlib2.AnnotationVisibility; +import org.jf.dexlib2.Opcodes; +import org.jf.dexlib2.iface.*; +import org.jf.dexlib2.immutable.*; +import org.junit.Assert; +import org.junit.Test; + +import javax.annotation.Nonnull; + +public class RewriteArrayTypeTest { + @Test + public void testRewriteArrayTypeTest() { + ClassDef class1 = new ImmutableClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, + Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation;", null)), + Lists.newArrayList( + new ImmutableField("Lcls1;", "field1", "I", AccessFlags.PUBLIC.getValue(), null, null, null) + ), + Lists.newArrayList( + new ImmutableMethod("Lcls1", "method1", + Lists.newArrayList(new ImmutableMethodParameter("[[[Lcls1;", null, null)), "V", + AccessFlags.PUBLIC.getValue(), null, null, null))); + + ImmutableDexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of(class1)); + + + DexRewriter rewriter = new DexRewriter(new RewriterModule() { + @Nonnull @Override public Rewriter getTypeRewriter(@Nonnull Rewriters rewriters) { + return new TypeRewriter() { + @Nonnull @Override public String rewriteUnwrappedType(@Nonnull String value) { + if (value.equals("Lcls1;")) { + return "Lcls2;"; + } + return value; + } + }; + } + }); + + DexFile rewrittenDexFile = rewriter.rewriteDexFile(dexFile); + + ClassDef rewrittenClassDef = Lists.newArrayList(rewrittenDexFile.getClasses()).get(0); + Method rewrittenMethodDef = Lists.newArrayList(rewrittenClassDef.getMethods()).get(0); + + Assert.assertEquals(rewrittenClassDef.getType(), "Lcls2;"); + Assert.assertEquals(rewrittenMethodDef.getParameterTypes().get(0), "[[[Lcls2;"); + } +}