com.android.dex.util.ExceptionWithContext Java Examples

The following examples show how to use com.android.dex.util.ExceptionWithContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example #2
Source File: OffsettedItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public final void writeTo(DexFile file, AnnotatedOutput out) {
    out.alignTo(alignment);

    try {
        if (writeSize < 0) {
            throw new UnsupportedOperationException(
                    "writeSize is unknown");
        }
        out.assertCursor(getAbsoluteOffset());
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while writing " + this);
    }

    writeTo0(file, out);
}
 
Example #3
Source File: DebugInfoEncoder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this (PositionList, LocalList) pair into a state machine
 * sequence.
 *
 * @return {@code non-null;} encoded byte sequence without padding and
 * terminated with a {@code 0x00} byte
 */
public byte[] convert() {
    try {
        byte[] ret;
        ret = convert0();

        if (DEBUG) {
            for (int i = 0 ; i < ret.length; i++) {
                System.err.printf("byte %02x\n", (0xff & ret[i]));
            }
        }

        return ret;
    } catch (IOException ex) {
        throw ExceptionWithContext
                .withContext(ex, "...while encoding debug info");
    }
}
 
Example #4
Source File: LocalsArraySet.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void annotate(ExceptionWithContext ex) {
    ex.addContext("(locals array set; primary)");
    primary.annotate(ex);

    int sz = secondaries.size();
    for (int label = 0; label < sz; label++) {
        LocalsArray la = secondaries.get(label);

        if (la != null) {
            ex.addContext("(locals array set: primary for caller "
                    + Hex.u2(label) + ')');

            la.getPrimary().annotate(ex);
        }
    }
}
 
Example #5
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example #6
Source File: OffsettedItem.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public final void writeTo(DexFile file, AnnotatedOutput out) {
    out.alignTo(alignment);

    try {
        if (writeSize < 0) {
            throw new UnsupportedOperationException(
                    "writeSize is unknown");
        }
        out.assertCursor(getAbsoluteOffset());
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while writing " + this);
    }

    writeTo0(file, out);
}
 
Example #7
Source File: DebugInfoEncoder.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this (PositionList, LocalList) pair into a state machine
 * sequence.
 *
 * @return {@code non-null;} encoded byte sequence without padding and
 * terminated with a {@code 0x00} byte
 */
public byte[] convert() {
    try {
        byte[] ret;
        ret = convert0();

        if (DEBUG) {
            for (int i = 0 ; i < ret.length; i++) {
                System.err.printf("byte %02x\n", (0xff & ret[i]));
            }
        }

        return ret;
    } catch (IOException ex) {
        throw ExceptionWithContext
                .withContext(ex, "...while encoding debug info");
    }
}
 
Example #8
Source File: LocalsArraySet.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void annotate(ExceptionWithContext ex) {
    ex.addContext("(locals array set; primary)");
    primary.annotate(ex);

    int sz = secondaries.size();
    for (int label = 0; label < sz; label++) {
        LocalsArray la = secondaries.get(label);

        if (la != null) {
            ex.addContext("(locals array set: primary for caller "
                    + Hex.u2(label) + ')');

            la.getPrimary().annotate(ex);
        }
    }
}
 
Example #9
Source File: DebugInfoDecoder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example #10
Source File: OffsettedItem.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public final void writeTo(DexFile file, AnnotatedOutput out) {
    out.alignTo(alignment);

    try {
        if (writeSize < 0) {
            throw new UnsupportedOperationException(
                    "writeSize is unknown");
        }
        out.assertCursor(getAbsoluteOffset());
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while writing " + this);
    }

    writeTo0(file, out);
}
 
Example #11
Source File: DebugInfoEncoder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this (PositionList, LocalList) pair into a state machine
 * sequence.
 *
 * @return {@code non-null;} encoded byte sequence without padding and
 * terminated with a {@code 0x00} byte
 */
public byte[] convert() {
    try {
        byte[] ret;
        ret = convert0();

        if (DEBUG) {
            for (int i = 0 ; i < ret.length; i++) {
                System.err.printf("byte %02x\n", (0xff & ret[i]));
            }
        }

        return ret;
    } catch (IOException ex) {
        throw ExceptionWithContext
                .withContext(ex, "...while encoding debug info");
    }
}
 
Example #12
Source File: LocalsArraySet.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void annotate(ExceptionWithContext ex) {
    ex.addContext("(locals array set; primary)");
    primary.annotate(ex);

    int sz = secondaries.size();
    for (int label = 0; label < sz; label++) {
        LocalsArray la = secondaries.get(label);

        if (la != null) {
            ex.addContext("(locals array set: primary for caller "
                    + Hex.u2(label) + ')');

            la.getPrimary().annotate(ex);
        }
    }
}
 
Example #13
Source File: DebugInfoDecoder.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Validates an encoded debug info stream against data used to encode it,
 * throwing an exception if they do not match. Used to validate the
 * encoder.
 *
 * @param info encoded debug info
 * @param file {@code non-null;} file to refer to during decoding
 * @param ref {@code non-null;} method whose info is being decoded
 * @param code {@code non-null;} original code object that was encoded
 * @param isStatic whether the method is static
 */
public static void validateEncode(byte[] info, DexFile file,
        CstMethodRef ref, DalvCode code, boolean isStatic) {
    PositionList pl = code.getPositions();
    LocalList ll = code.getLocals();
    DalvInsnList insns = code.getInsns();
    int codeSize = insns.codeSize();
    int countRegisters = insns.getRegistersSize();

    try {
        validateEncode0(info, codeSize, countRegisters,
                isStatic, ref, file, pl, ll);
    } catch (RuntimeException ex) {
        System.err.println("instructions:");
        insns.debugPrint(System.err, "  ", true);
        System.err.println("local list:");
        ll.debugPrint(System.err, "  ");
        throw ExceptionWithContext.withContext(ex,
                "while processing " + ref.toHuman());
    }
}
 
Example #14
Source File: OffsettedItem.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public final void writeTo(DexFile file, AnnotatedOutput out) {
    out.alignTo(alignment);

    try {
        if (writeSize < 0) {
            throw new UnsupportedOperationException(
                    "writeSize is unknown");
        }
        out.assertCursor(getAbsoluteOffset());
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while writing " + this);
    }

    writeTo0(file, out);
}
 
Example #15
Source File: DebugInfoEncoder.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Converts this (PositionList, LocalList) pair into a state machine
 * sequence.
 *
 * @return {@code non-null;} encoded byte sequence without padding and
 * terminated with a {@code 0x00} byte
 */
public byte[] convert() {
    try {
        byte[] ret;
        ret = convert0();

        if (DEBUG) {
            for (int i = 0 ; i < ret.length; i++) {
                System.err.printf("byte %02x\n", (0xff & ret[i]));
            }
        }

        return ret;
    } catch (IOException ex) {
        throw ExceptionWithContext
                .withContext(ex, "...while encoding debug info");
    }
}
 
Example #16
Source File: LocalsArraySet.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void annotate(ExceptionWithContext ex) {
    ex.addContext("(locals array set; primary)");
    primary.annotate(ex);

    int sz = secondaries.size();
    for (int label = 0; label < sz; label++) {
        LocalsArray la = secondaries.get(label);

        if (la != null) {
            ex.addContext("(locals array set: primary for caller "
                    + Hex.u2(label) + ')');

            la.getPrimary().annotate(ex);
        }
    }
}
 
Example #17
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the debug info sequence.
 */
public void decode() {
    try {
        decode0();
    } catch (Exception ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while decoding debug info");
    }
}
 
Example #18
Source File: MixedItemSection.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Places all the items in this instance at particular offsets. This
 * will call {@link OffsettedItem#place} on each item. If an item
 * does not know its write size before the call to {@code place},
 * it is that call which is responsible for setting the write size.
 * This method may only be called once per instance; subsequent calls
 * will throw an exception.
 */
public void placeItems() {
    throwIfNotPrepared();

    switch (sort) {
        case INSTANCE: {
            Collections.sort(items);
            break;
        }
        case TYPE: {
            Collections.sort(items, TYPE_SORTER);
            break;
        }
    }

    int sz = items.size();
    int outAt = 0;
    for (int i = 0; i < sz; i++) {
        OffsettedItem one = items.get(i);
        try {
            int placedAt = one.place(this, outAt);

            if (placedAt < outAt) {
                throw new RuntimeException("bogus place() result for " +
                        one);
            }

            outAt = placedAt + one.writeSize();
        } catch (RuntimeException ex) {
            throw ExceptionWithContext.withContext(ex,
                    "...while placing " + one);
        }
    }

    writeSize = outAt;
}
 
Example #19
Source File: CodeItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #writeTo0} which writes out the actual bytecode.
 *
 * @param file {@code non-null;} file we are part of
 * @param out {@code non-null;} where to write to
 */
private void writeCodes(DexFile file, AnnotatedOutput out) {
    DalvInsnList insns = code.getInsns();

    try {
        insns.writeTo(out);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex, "...while writing " +
                "instructions for " + ref.toHuman());
    }
}
 
Example #20
Source File: DebugInfoItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void place0(Section addedTo, int offset) {
    // Encode the data and note the size.

    try {
        encoded = encode(addedTo.getFile(), null, null, null, false);
        setWriteSize(encoded.length);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while placing debug info for " + ref.toHuman());
    }
}
 
Example #21
Source File: OneLocalsArray.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void annotate(ExceptionWithContext ex) {
    for (int i = 0; i < locals.length; i++) {
        TypeBearer type = locals[i];
        String s = (type == null) ? "<invalid>" : type.toString();
        ex.addContext("locals[" + Hex.u2(i) + "]: " + s);
    }
}
 
Example #22
Source File: ExecutionStack.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Annotates (adds context to) the given exception with information
 * about this instance.
 *
 * @param ex {@code non-null;} the exception to annotate
 */
public void annotate(ExceptionWithContext ex) {
    int limit = stackPtr - 1;

    for (int i = 0; i <= limit; i++) {
        String idx = (i == limit) ? "top0" : Hex.u2(limit - i);

        ex.addContext("stack[" + idx + "]: " +
                      stackElementString(stack[i]));
    }
}
 
Example #23
Source File: ByteArrayAnnotatedOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void assertCursor(int expectedCursor) {
    if (cursor != expectedCursor) {
        throw new ExceptionWithContext("expected cursor " +
                expectedCursor + "; actual value: " + cursor);
    }
}
 
Example #24
Source File: DebugInfoDecoder.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the debug info sequence.
 */
public void decode() {
    try {
        decode0();
    } catch (Exception ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while decoding debug info");
    }
}
 
Example #25
Source File: MixedItemSection.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Places all the items in this instance at particular offsets. This
 * will call {@link OffsettedItem#place} on each item. If an item
 * does not know its write size before the call to {@code place},
 * it is that call which is responsible for setting the write size.
 * This method may only be called once per instance; subsequent calls
 * will throw an exception.
 */
public void placeItems() {
    throwIfNotPrepared();

    switch (sort) {
        case INSTANCE: {
            Collections.sort(items);
            break;
        }
        case TYPE: {
            Collections.sort(items, TYPE_SORTER);
            break;
        }
    }

    int sz = items.size();
    int outAt = 0;
    for (int i = 0; i < sz; i++) {
        OffsettedItem one = items.get(i);
        try {
            int placedAt = one.place(this, outAt);

            if (placedAt < outAt) {
                throw new RuntimeException("bogus place() result for " +
                        one);
            }

            outAt = placedAt + one.writeSize();
        } catch (RuntimeException ex) {
            throw ExceptionWithContext.withContext(ex,
                    "...while placing " + one);
        }
    }

    writeSize = outAt;
}
 
Example #26
Source File: CodeItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #writeTo0} which writes out the actual bytecode.
 *
 * @param file {@code non-null;} file we are part of
 * @param out {@code non-null;} where to write to
 */
private void writeCodes(DexFile file, AnnotatedOutput out) {
    DalvInsnList insns = code.getInsns();

    try {
        insns.writeTo(out);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex, "...while writing " +
                "instructions for " + ref.toHuman());
    }
}
 
Example #27
Source File: DebugInfoItem.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void place0(Section addedTo, int offset) {
    // Encode the data and note the size.

    try {
        encoded = encode(addedTo.getFile(), null, null, null, false);
        setWriteSize(encoded.length);
    } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex,
                "...while placing debug info for " + ref.toHuman());
    }
}
 
Example #28
Source File: OneLocalsArray.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void annotate(ExceptionWithContext ex) {
    for (int i = 0; i < locals.length; i++) {
        TypeBearer type = locals[i];
        String s = (type == null) ? "<invalid>" : type.toString();
        ex.addContext("locals[" + Hex.u2(i) + "]: " + s);
    }
}
 
Example #29
Source File: ExecutionStack.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Annotates (adds context to) the given exception with information
 * about this instance.
 *
 * @param ex {@code non-null;} the exception to annotate
 */
public void annotate(ExceptionWithContext ex) {
    int limit = stackPtr - 1;

    for (int i = 0; i <= limit; i++) {
        String idx = (i == limit) ? "top0" : Hex.u2(limit - i);

        ex.addContext("stack[" + idx + "]: " +
                      stackElementString(stack[i]));
    }
}
 
Example #30
Source File: ByteArrayAnnotatedOutput.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void assertCursor(int expectedCursor) {
    if (cursor != expectedCursor) {
        throw new ExceptionWithContext("expected cursor " +
                expectedCursor + "; actual value: " + cursor);
    }
}