From 792188b9feb5553bff83af8feda36910c038870a Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Sun, 15 Sep 2013 18:36:45 -0500 Subject: [PATCH] [smali] add untracked 2b6 files --- .../jf/dexlib2/builder/BuilderDebugItem.java | 54 + .../builder/BuilderExceptionHandler.java | 89 ++ .../dexlib2/builder/BuilderInstruction.java | 70 ++ .../builder/BuilderOffsetInstruction.java | 67 ++ .../dexlib2/builder/BuilderSwitchPayload.java | 55 + .../jf/dexlib2/builder/BuilderTryBlock.java | 82 ++ .../java/org/jf/dexlib2/builder/Label.java | 62 ++ .../builder/MethodImplementationBuilder.java | 154 +++ .../jf/dexlib2/builder/MethodLocation.java | 213 ++++ .../builder/MutableMethodImplementation.java | 943 ++++++++++++++++++ .../dexlib2/builder/SwitchLabelElement.java | 44 + .../builder/debug/BuilderEndLocal.java | 57 ++ .../builder/debug/BuilderEpilogueBegin.java | 47 + .../builder/debug/BuilderLineNumber.java | 53 + .../builder/debug/BuilderPrologueEnd.java | 47 + .../builder/debug/BuilderRestartLocal.java | 57 ++ .../builder/debug/BuilderSetSourceFile.java | 62 ++ .../builder/debug/BuilderStartLocal.java | 81 ++ .../instruction/BuilderArrayPayload.java | 62 ++ .../instruction/BuilderInstruction10t.java | 51 + .../instruction/BuilderInstruction10x.java | 49 + .../instruction/BuilderInstruction11n.java | 61 ++ .../instruction/BuilderInstruction11x.java | 56 ++ .../instruction/BuilderInstruction12x.java | 60 ++ .../instruction/BuilderInstruction20bc.java | 61 ++ .../instruction/BuilderInstruction20t.java | 51 + .../instruction/BuilderInstruction21c.java | 61 ++ .../instruction/BuilderInstruction21ih.java | 62 ++ .../instruction/BuilderInstruction21lh.java | 61 ++ .../instruction/BuilderInstruction21s.java | 61 ++ .../instruction/BuilderInstruction21t.java | 58 ++ .../instruction/BuilderInstruction22b.java | 65 ++ .../instruction/BuilderInstruction22c.java | 65 ++ .../instruction/BuilderInstruction22s.java | 65 ++ .../instruction/BuilderInstruction22t.java | 62 ++ .../instruction/BuilderInstruction22x.java | 60 ++ .../instruction/BuilderInstruction23x.java | 64 ++ .../instruction/BuilderInstruction30t.java | 52 + .../instruction/BuilderInstruction31c.java | 61 ++ .../instruction/BuilderInstruction31i.java | 61 ++ .../instruction/BuilderInstruction31t.java | 59 ++ .../instruction/BuilderInstruction32x.java | 60 ++ .../instruction/BuilderInstruction35c.java | 81 ++ .../instruction/BuilderInstruction3rc.java | 67 ++ .../instruction/BuilderInstruction51l.java | 60 ++ .../BuilderPackedSwitchPayload.java | 74 ++ .../BuilderSparseSwitchPayload.java | 71 ++ .../instruction/BuilderSwitchElement.java | 29 + .../writer/io/DeferredOutputStream.java | 8 + .../io/DeferredOutputStreamFactory.java | 7 + .../jf/dexlib2/writer/io/DexDataStore.java | 12 + .../jf/dexlib2/writer/io/FileDataStore.java | 28 + .../writer/io/FileDeferredOutputStream.java | 104 ++ .../jf/dexlib2/writer/io/MemoryDataStore.java | 105 ++ .../writer/io/MemoryDeferredOutputStream.java | 88 ++ .../org/jf/dexlib2/builder/FixGotoTest.java | 122 +++ .../jf/dexlib2/builder/FixOffsetsTest.java | 140 +++ .../dexlib2/builder/PayloadAlignmentTest.java | 251 +++++ .../jf/util/RandomAccessFileInputStream.java | 81 ++ .../org/jf/util/ClassFileNameHandlerTest.java | 49 + 60 files changed, 5042 insertions(+) create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderDebugItem.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderExceptionHandler.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderInstruction.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderOffsetInstruction.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderSwitchPayload.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderTryBlock.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/Label.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodImplementationBuilder.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodLocation.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MutableMethodImplementation.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/SwitchLabelElement.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/debug/BuilderEndLocal.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/debug/BuilderEpilogueBegin.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/debug/BuilderLineNumber.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/debug/BuilderPrologueEnd.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/debug/BuilderRestartLocal.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/debug/BuilderSetSourceFile.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/debug/BuilderStartLocal.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderArrayPayload.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction10t.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction10x.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction11n.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction11x.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction12x.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction20bc.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction20t.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction21c.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction21ih.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction21lh.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction21s.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction21t.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction22b.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction22c.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction22s.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction22t.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction22x.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction23x.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction30t.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction31c.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction31i.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction31t.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction32x.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction35c.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction3rc.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderInstruction51l.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderPackedSwitchPayload.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderSparseSwitchPayload.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/instruction/BuilderSwitchElement.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/io/DeferredOutputStream.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/io/DeferredOutputStreamFactory.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/io/DexDataStore.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/io/FileDataStore.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/io/FileDeferredOutputStream.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/io/MemoryDataStore.java create mode 100644 brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/writer/io/MemoryDeferredOutputStream.java create mode 100644 brut.apktool.smali/dexlib2/src/test/java/org/jf/dexlib2/builder/FixGotoTest.java create mode 100644 brut.apktool.smali/dexlib2/src/test/java/org/jf/dexlib2/builder/FixOffsetsTest.java create mode 100644 brut.apktool.smali/dexlib2/src/test/java/org/jf/dexlib2/builder/PayloadAlignmentTest.java create mode 100644 brut.apktool.smali/util/src/main/java/org/jf/util/RandomAccessFileInputStream.java create mode 100644 brut.apktool.smali/util/src/test/java/org/jf/util/ClassFileNameHandlerTest.java diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderDebugItem.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderDebugItem.java new file mode 100644 index 00000000..bea9ca32 --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderDebugItem.java @@ -0,0 +1,54 @@ +/* + * Copyright 2013, 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.builder; + +import org.jf.dexlib2.iface.debug.DebugItem; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public abstract class BuilderDebugItem implements DebugItem { + @Nullable MethodLocation location; + + public BuilderDebugItem(@Nonnull MethodLocation location) { + this.location = location; + } + + @Override public int getCodeAddress() { + if (location == null) { + throw new IllegalStateException("Cannot get the address of a BuilderDebugItem that isn't associated with " + + "a method."); + } + return location.getCodeAddress(); + } + +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderExceptionHandler.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderExceptionHandler.java new file mode 100644 index 00000000..3574bd48 --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderExceptionHandler.java @@ -0,0 +1,89 @@ +/* + * Copyright 2013, 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.builder; + +import org.jf.dexlib2.base.BaseExceptionHandler; +import org.jf.dexlib2.iface.ExceptionHandler; +import org.jf.dexlib2.iface.reference.TypeReference; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +class BuilderExceptionHandler { + static ExceptionHandler newExceptionHandler(@Nullable final TypeReference exceptionType, + @Nonnull final Label handler) { + if (exceptionType == null) { + return newExceptionHandler(handler); + } + return new BaseExceptionHandler() { + @Nullable @Override public String getExceptionType() { + return exceptionType.getType(); + } + + @Override public int getHandlerCodeAddress() { + return handler.getCodeAddress(); + } + + @Nullable @Override public TypeReference getExceptionTypeReference() { + return exceptionType; + } + }; + } + + static ExceptionHandler newExceptionHandler(@Nonnull final Label handler) { + return new BaseExceptionHandler() { + @Nullable @Override public String getExceptionType() { + return null; + } + + @Override public int getHandlerCodeAddress() { + return handler.getCodeAddress(); + } + }; + } + + static ExceptionHandler newExceptionHandler(@Nullable final String exceptionType, + @Nonnull final Label handler) { + if (exceptionType == null) { + return newExceptionHandler(handler); + } + return new BaseExceptionHandler() { + @Nullable @Override public String getExceptionType() { + return exceptionType; + } + + @Override public int getHandlerCodeAddress() { + return handler.getCodeAddress(); + } + }; + } +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderInstruction.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderInstruction.java new file mode 100644 index 00000000..d06f7a8b --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderInstruction.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012, 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.builder; + +import org.jf.dexlib2.Format; +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.Instruction; +import org.jf.dexlib2.util.Preconditions; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public abstract class BuilderInstruction implements Instruction { + @Nonnull protected final Opcode opcode; + + @Nullable MethodLocation location; + + protected BuilderInstruction(@Nonnull Opcode opcode) { + Preconditions.checkFormat(opcode, getFormat()); + this.opcode = opcode; + } + + @Nonnull public Opcode getOpcode() { + return opcode; + } + + public abstract Format getFormat(); + + public int getCodeUnits() { + return getFormat().size / 2; + } + + @Nonnull + public MethodLocation getLocation() { + if (location == null) { + throw new IllegalStateException("Cannot get the location of an instruction that hasn't been added to a " + + "method."); + } + return location; + } +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderOffsetInstruction.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderOffsetInstruction.java new file mode 100644 index 00000000..d75d7b67 --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderOffsetInstruction.java @@ -0,0 +1,67 @@ +/* + * Copyright 2013, 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.builder; + +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.OffsetInstruction; + +import javax.annotation.Nonnull; + +public abstract class BuilderOffsetInstruction extends BuilderInstruction implements OffsetInstruction { + @Nonnull + protected final Label target; + + public BuilderOffsetInstruction(@Nonnull Opcode opcode, + @Nonnull Label target) { + super(opcode); + this.target = target; + } + + @Override public int getCodeOffset() { + int codeOffset = internalGetCodeOffset(); + if ((this.getCodeUnits() == 1 && (codeOffset < Byte.MIN_VALUE || codeOffset > Byte.MAX_VALUE)) || + (this.getCodeUnits() == 2 && (codeOffset < Short.MIN_VALUE || codeOffset > Short.MAX_VALUE))) { + throw new IllegalStateException("Target is out of range"); + } + return codeOffset; + } + + + int internalGetCodeOffset() { + return target.getCodeAddress() - this.getLocation().getCodeAddress(); + } + + @Nonnull + public Label getTarget() { + return target; + } +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderSwitchPayload.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderSwitchPayload.java new file mode 100644 index 00000000..d760383e --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderSwitchPayload.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013, 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.builder; + +import org.jf.dexlib2.Opcode; +import org.jf.dexlib2.iface.instruction.SwitchPayload; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public abstract class BuilderSwitchPayload extends BuilderInstruction implements SwitchPayload { + @Nullable + MethodLocation referrer; + + protected BuilderSwitchPayload(@Nonnull Opcode opcode) { + super(opcode); + } + + @Nonnull + public MethodLocation getReferrer() { + if (referrer == null) { + throw new IllegalStateException("The referrer has not been set yet"); + } + return referrer; + } +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderTryBlock.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderTryBlock.java new file mode 100644 index 00000000..f8e94cf0 --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/BuilderTryBlock.java @@ -0,0 +1,82 @@ +/* + * Copyright 2013, 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.builder; + +import com.google.common.collect.ImmutableList; +import org.jf.dexlib2.base.BaseTryBlock; +import org.jf.dexlib2.iface.ExceptionHandler; +import org.jf.dexlib2.iface.reference.TypeReference; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; + +class BuilderTryBlock extends BaseTryBlock { + // We only ever have one exception handler per try block. They are later merged as needed in TryListBuilder + @Nonnull public final ExceptionHandler exceptionHandler; + @Nonnull public final Label start; + // The end location is exclusive, it should point to the codeAddress of the instruction immediately after the last + // covered instruction. + @Nonnull public final Label end; + + public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable String exceptionType, + @Nonnull Label handler) { + this.start = start; + this.end = end; + this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler); + } + + public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable TypeReference exceptionType, + @Nonnull Label handler) { + this.start = start; + this.end = end; + this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler); + } + + public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nonnull Label handler) { + this.start = start; + this.end = end; + this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(handler); + } + + @Override public int getStartCodeAddress() { + return start.getCodeAddress(); + } + + @Override public int getCodeUnitCount() { + return end.getCodeAddress() - start.getCodeAddress(); + } + + @Nonnull @Override public List getExceptionHandlers() { + return ImmutableList.of(exceptionHandler); + } +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/Label.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/Label.java new file mode 100644 index 00000000..ef19f91c --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/Label.java @@ -0,0 +1,62 @@ +/* + * Copyright 2013, 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.builder; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class Label { + @Nullable MethodLocation location; + + Label() { + } + + Label(MethodLocation location) { + this.location = location; + } + + public int getCodeAddress() { + return getLocation().getCodeAddress(); + } + + @Nonnull + public MethodLocation getLocation() { + if (location == null) { + throw new IllegalStateException("Cannot get the location of a label that hasn't been placed yet."); + } + return location; + } + + public boolean isPlaced() { + return location != null; + } +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodImplementationBuilder.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodImplementationBuilder.java new file mode 100644 index 00000000..4a1e989d --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodImplementationBuilder.java @@ -0,0 +1,154 @@ +/* + * Copyright 2013, 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.builder; + +import org.jf.dexlib2.iface.MethodImplementation; +import org.jf.dexlib2.iface.reference.StringReference; +import org.jf.dexlib2.iface.reference.TypeReference; +import org.jf.dexlib2.writer.builder.BuilderStringReference; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.HashMap; + +public class MethodImplementationBuilder { + // Contains all named labels - both placed and unplaced + private final HashMap labels = new HashMap(); + + @Nonnull + private final MutableMethodImplementation impl; + + private MethodLocation currentLocation; + + public MethodImplementationBuilder(int registerCount) { + this.impl = new MutableMethodImplementation(registerCount); + this.currentLocation = impl.instructionList.get(0); + } + + public MethodImplementation getMethodImplementation() { + return impl; + } + + /** + * Adds a new named label at the current location. + * + * Any previous unplaced references to a label of this name will now refer to this label/location + * + * @param name The name of the label to add + * @return A LabelRef representing the label + */ + @Nonnull + public Label addLabel(@Nonnull String name) { + Label label = labels.get(name); + + if (label != null) { + if (label.isPlaced()) { + throw new IllegalArgumentException("There is already a label with that name."); + } else { + currentLocation.getLabels().add(label); + } + } else { + label = currentLocation.addNewLabel(); + labels.put(name, label); + } + + return label; + } + + /** + * Get a reference to a label with the given name. + * + * If a label with that name has not been added yet, a new one is created, but is left + * in an unplaced state. It is assumed that addLabel(name) will be called at a later + * point to define the location of the label. + * + * @param name The name of the label to get + * @return A LabelRef representing the label + */ + @Nonnull + public Label getLabel(@Nonnull String name) { + Label label = labels.get(name); + if (label == null) { + label = new Label(); + labels.put(name, label); + } + return label; + } + + public void addCatch(@Nullable TypeReference type, @Nonnull Label from, + @Nonnull Label to, @Nonnull Label handler) { + impl.addCatch(type, from, to, handler); + } + + public void addCatch(@Nullable String type, @Nonnull Label from, @Nonnull Label to, + @Nonnull Label handler) { + impl.addCatch(type, from, to, handler); + } + + public void addCatch(@Nonnull Label from, @Nonnull Label to, @Nonnull Label handler) { + impl.addCatch(from, to, handler); + } + + public void addLineNumber(int lineNumber) { + currentLocation.addLineNumber(lineNumber); + } + + public void addStartLocal(int registerNumber, @Nullable StringReference name, @Nullable TypeReference type, + @Nullable StringReference signature) { + currentLocation.addStartLocal(registerNumber, name, type, signature); + } + + public void addEndLocal(int registerNumber) { + currentLocation.addEndLocal(registerNumber); + } + + public void addRestartLocal(int registerNumber) { + currentLocation.addRestartLocal(registerNumber); + } + + public void addPrologue() { + currentLocation.addPrologue(); + } + + public void addEpilogue() { + currentLocation.addEpilogue(); + } + + public void addSetSourceFile(@Nullable BuilderStringReference sourceFile) { + currentLocation.addSetSourceFile(sourceFile); + } + + public void addInstruction(@Nullable BuilderInstruction instruction) { + impl.addInstruction(instruction); + currentLocation = impl.instructionList.get(impl.instructionList.size()-1); + } +} diff --git a/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodLocation.java b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodLocation.java new file mode 100644 index 00000000..5d38ff55 --- /dev/null +++ b/brut.apktool.smali/dexlib2/src/main/java/org/jf/dexlib2/builder/MethodLocation.java @@ -0,0 +1,213 @@ +/* + * Copyright 2013, 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.builder; + +import com.google.common.collect.Lists; +import org.jf.dexlib2.builder.debug.*; +import org.jf.dexlib2.iface.instruction.Instruction; +import org.jf.dexlib2.iface.reference.StringReference; +import org.jf.dexlib2.iface.reference.TypeReference; +import org.jf.dexlib2.writer.builder.BuilderStringReference; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +public class MethodLocation { + @Nullable BuilderInstruction instruction; + int codeAddress; + int index; + + private List