1/* 2 * Copyright 2012, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32package org.jf.dexlib2.writer; 33 34import com.google.common.collect.ImmutableList; 35import com.google.common.collect.ImmutableSet; 36import com.google.common.collect.Iterables; 37import com.google.common.collect.Lists; 38import org.jf.dexlib2.Opcode; 39import org.jf.dexlib2.Opcodes; 40import org.jf.dexlib2.ReferenceType; 41import org.jf.dexlib2.builder.MethodImplementationBuilder; 42import org.jf.dexlib2.builder.instruction.BuilderInstruction10x; 43import org.jf.dexlib2.builder.instruction.BuilderInstruction21c; 44import org.jf.dexlib2.dexbacked.DexBackedDexFile; 45import org.jf.dexlib2.iface.*; 46import org.jf.dexlib2.iface.debug.DebugItem; 47import org.jf.dexlib2.iface.instruction.Instruction; 48import org.jf.dexlib2.iface.instruction.ReferenceInstruction; 49import org.jf.dexlib2.iface.instruction.formats.Instruction21c; 50import org.jf.dexlib2.iface.reference.Reference; 51import org.jf.dexlib2.iface.reference.StringReference; 52import org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x; 53import org.jf.dexlib2.writer.builder.DexBuilder; 54import org.jf.dexlib2.writer.io.MemoryDataStore; 55import org.junit.Assert; 56import org.junit.Test; 57 58import javax.annotation.Nonnull; 59import java.io.IOException; 60import java.util.List; 61 62public class JumboStringConversionTest { 63 @Test 64 public void testJumboStringConversion() throws IOException { 65 DexBuilder dexBuilder = DexBuilder.makeDexBuilder(15); 66 67 MethodImplementationBuilder methodBuilder = new MethodImplementationBuilder(1); 68 for (int i=0; i<66000; i++) { 69 methodBuilder.addInstruction(new BuilderInstruction21c(Opcode.CONST_STRING, 0, 70 dexBuilder.internStringReference(String.format("%08d", i)))); 71 } 72 methodBuilder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID)); 73 74 dexBuilder.internClassDef( 75 "Ltest;", 76 0, 77 "Ljava/lang/Object;", 78 null, 79 null, 80 ImmutableSet.<Annotation>of(), 81 null, 82 ImmutableList.of( 83 dexBuilder.internMethod( 84 "Ltest;", 85 "test", 86 null, 87 "V", 88 0, 89 ImmutableSet.<Annotation>of(), 90 methodBuilder.getMethodImplementation()))); 91 92 MemoryDataStore dexStore = new MemoryDataStore(); 93 dexBuilder.writeTo(dexStore); 94 95 DexBackedDexFile dexFile = new DexBackedDexFile(new Opcodes(15), dexStore.getData()); 96 97 ClassDef classDef = Iterables.getFirst(dexFile.getClasses(), null); 98 Assert.assertNotNull(classDef); 99 100 Method method = Iterables.getFirst(classDef.getMethods(), null); 101 Assert.assertNotNull(method); 102 103 MethodImplementation impl = method.getImplementation(); 104 Assert.assertNotNull(impl); 105 106 List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions()); 107 Assert.assertEquals(66001, instructions.size()); 108 109 for (int i=0; i<65536; i++) { 110 Assert.assertEquals(Opcode.CONST_STRING, instructions.get(i).getOpcode()); 111 Assert.assertEquals(String.format("%08d", i), 112 ((StringReference)((ReferenceInstruction)instructions.get(i)).getReference()).getString()); 113 } 114 for (int i=65536; i<66000; i++) { 115 Assert.assertEquals(Opcode.CONST_STRING_JUMBO, instructions.get(i).getOpcode()); 116 Assert.assertEquals(String.format("%08d", i), 117 ((StringReference)((ReferenceInstruction)instructions.get(i)).getReference()).getString()); 118 } 119 Assert.assertEquals(Opcode.RETURN_VOID, instructions.get(66000).getOpcode()); 120 } 121 122 123 @Test 124 public void testJumboStringConversion_NonMethodBuilder() throws IOException { 125 DexBuilder dexBuilder = DexBuilder.makeDexBuilder(15); 126 127 final List<Instruction> instructions = Lists.newArrayList(); 128 for (int i=0; i<66000; i++) { 129 final StringReference ref = dexBuilder.internStringReference(String.format("%08d", i)); 130 131 instructions.add(new Instruction21c() { 132 @Override public int getRegisterA() { 133 return 0; 134 } 135 136 @Nonnull @Override public Reference getReference() { 137 return ref; 138 } 139 140 @Override public int getReferenceType() { return ReferenceType.STRING; } 141 142 @Override public Opcode getOpcode() { 143 return Opcode.CONST_STRING; 144 } 145 146 @Override public int getCodeUnits() { 147 return getOpcode().format.size / 2; 148 } 149 }); 150 } 151 instructions.add(new ImmutableInstruction10x(Opcode.RETURN_VOID)); 152 153 MethodImplementation methodImpl = new MethodImplementation() { 154 @Override public int getRegisterCount() { 155 return 1; 156 } 157 158 @Nonnull @Override public Iterable<? extends Instruction> getInstructions() { 159 return instructions; 160 } 161 162 @Nonnull @Override public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() { 163 return ImmutableList.of(); 164 } 165 166 @Nonnull @Override public Iterable<? extends DebugItem> getDebugItems() { 167 return ImmutableList.of(); 168 } 169 }; 170 171 dexBuilder.internClassDef( 172 "Ltest;", 173 0, 174 "Ljava/lang/Object;", 175 null, 176 null, 177 ImmutableSet.<Annotation>of(), 178 null, 179 ImmutableList.of( 180 dexBuilder.internMethod( 181 "Ltest;", 182 "test", 183 null, 184 "V", 185 0, 186 ImmutableSet.<Annotation>of(), 187 methodImpl))); 188 189 MemoryDataStore dexStore = new MemoryDataStore(); 190 dexBuilder.writeTo(dexStore); 191 192 DexBackedDexFile dexFile = new DexBackedDexFile(new Opcodes(15), dexStore.getData()); 193 194 ClassDef classDef = Iterables.getFirst(dexFile.getClasses(), null); 195 Assert.assertNotNull(classDef); 196 197 Method method = Iterables.getFirst(classDef.getMethods(), null); 198 Assert.assertNotNull(method); 199 200 MethodImplementation impl = method.getImplementation(); 201 Assert.assertNotNull(impl); 202 203 List<? extends Instruction> actualInstructions = Lists.newArrayList(impl.getInstructions()); 204 Assert.assertEquals(66001, actualInstructions.size()); 205 206 for (int i=0; i<65536; i++) { 207 Assert.assertEquals(Opcode.CONST_STRING, actualInstructions.get(i).getOpcode()); 208 Assert.assertEquals(String.format("%08d", i), 209 ((StringReference)((ReferenceInstruction)actualInstructions.get(i)).getReference()).getString()); 210 } 211 for (int i=65536; i<66000; i++) { 212 Assert.assertEquals(Opcode.CONST_STRING_JUMBO, actualInstructions.get(i).getOpcode()); 213 Assert.assertEquals(String.format("%08d", i), 214 ((StringReference)((ReferenceInstruction)actualInstructions.get(i)).getReference()).getString()); 215 } 216 Assert.assertEquals(Opcode.RETURN_VOID, actualInstructions.get(66000).getOpcode()); 217 } 218} 219