|
| 1 | +package org.tron.core.vm.program; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Random; |
| 8 | +import java.util.Set; |
| 9 | +import org.junit.Test; |
| 10 | +import org.tron.core.vm.Op; |
| 11 | + |
| 12 | +public class JumpDestBitmapTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void bitmapMatchesTheOldJumpDestSet() { |
| 16 | + assertSame(new byte[0]); |
| 17 | + assertSame(new byte[] {op(Op.JUMPDEST)}); |
| 18 | + assertSame(new byte[] {op(Op.PUSH1), op(Op.JUMPDEST), op(Op.JUMPDEST)}); |
| 19 | + assertSame(new byte[] {op(Op.PUSH1), op(Op.JUMPDEST)}); |
| 20 | + assertSame(truncatedPush32()); |
| 21 | + assertSame(everyPushWidth()); |
| 22 | + assertSame(allJumpDests(300)); |
| 23 | + |
| 24 | + ProgramPrecompile analysis = ProgramPrecompile.compile(new byte[] {op(Op.JUMPDEST)}); |
| 25 | + assertEquals(false, analysis.hasJumpDest(-1)); |
| 26 | + assertEquals(false, analysis.hasJumpDest(1)); |
| 27 | + assertEquals(false, analysis.hasJumpDest(Integer.MIN_VALUE)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void singleJumpDestDoesNotSetOtherBits() { |
| 32 | + for (int length = 1; length <= 65; length++) { |
| 33 | + for (int dest = 0; dest < length; dest++) { |
| 34 | + byte[] ops = new byte[length]; |
| 35 | + ops[dest] = op(Op.JUMPDEST); |
| 36 | + ProgramPrecompile analysis = ProgramPrecompile.compile(ops); |
| 37 | + for (int pc = -1; pc <= length; pc++) { |
| 38 | + assertEquals("length " + length + " dest " + dest + " pc " + pc, |
| 39 | + pc == dest, analysis.hasJumpDest(pc)); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void randomBytecodeMatchesTheOldJumpDestSet() { |
| 47 | + Random random = new Random(0x5b60617fL); |
| 48 | + for (int sample = 0; sample < 10000; sample++) { |
| 49 | + byte[] ops = new byte[random.nextInt(8193)]; |
| 50 | + random.nextBytes(ops); |
| 51 | + // Exercise both arbitrary bytecode and bytecode with densely packed destinations. |
| 52 | + if ((sample & 1) != 0) { |
| 53 | + for (int pc = 0; pc < ops.length; pc++) { |
| 54 | + if (random.nextInt(4) == 0) { |
| 55 | + ops[pc] = op(Op.JUMPDEST); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + assertSame(ops); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static void assertSame(byte[] ops) { |
| 64 | + ProgramPrecompile analysis = ProgramPrecompile.compile(ops); |
| 65 | + Set<Integer> expected = oldJumpDests(ops); |
| 66 | + for (int pc = -2; pc <= ops.length + 2; pc++) { |
| 67 | + assertEquals(ops.length + " pc " + pc, expected.contains(pc), analysis.hasJumpDest(pc)); |
| 68 | + } |
| 69 | + assertEquals(false, analysis.hasJumpDest(Integer.MIN_VALUE)); |
| 70 | + assertEquals(false, analysis.hasJumpDest(Integer.MAX_VALUE)); |
| 71 | + } |
| 72 | + |
| 73 | + private static Set<Integer> oldJumpDests(byte[] ops) { |
| 74 | + Set<Integer> jumpDest = new HashSet<>(); |
| 75 | + for (int i = 0; i < ops.length; ++i) { |
| 76 | + int op = ops[i] & 0xff; |
| 77 | + if (op == Op.JUMPDEST) { |
| 78 | + jumpDest.add(i); |
| 79 | + } |
| 80 | + if (op >= Op.PUSH1 && op <= Op.PUSH32) { |
| 81 | + i += op - Op.PUSH1 + 1; |
| 82 | + } |
| 83 | + } |
| 84 | + return jumpDest; |
| 85 | + } |
| 86 | + |
| 87 | + private static byte[] truncatedPush32() { |
| 88 | + byte[] ops = new byte[10]; |
| 89 | + ops[0] = op(Op.PUSH32); |
| 90 | + ops[3] = op(Op.JUMPDEST); |
| 91 | + return ops; |
| 92 | + } |
| 93 | + |
| 94 | + private static byte[] everyPushWidth() { |
| 95 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 96 | + for (int width = 1; width <= 32; width++) { |
| 97 | + for (int align = 0; align < 8; align++) { |
| 98 | + for (int pad = 0; pad < align; pad++) { |
| 99 | + out.write(0); |
| 100 | + } |
| 101 | + out.write(op(Op.PUSH1 + width - 1)); |
| 102 | + for (int n = 0; n < width; n++) { |
| 103 | + out.write(n == width - 1 ? op(Op.JUMPDEST) : 0); |
| 104 | + } |
| 105 | + out.write(op(Op.JUMPDEST)); |
| 106 | + } |
| 107 | + } |
| 108 | + return out.toByteArray(); |
| 109 | + } |
| 110 | + |
| 111 | + private static byte op(int opcode) { |
| 112 | + return (byte) opcode; |
| 113 | + } |
| 114 | + |
| 115 | + private static byte[] allJumpDests(int length) { |
| 116 | + byte[] ops = new byte[length]; |
| 117 | + for (int i = 0; i < ops.length; i++) { |
| 118 | + ops[i] = op(Op.JUMPDEST); |
| 119 | + } |
| 120 | + return ops; |
| 121 | + } |
| 122 | +} |
0 commit comments