|
| 1 | +package org.tron.common.runtime.vm; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.math.BigInteger; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.bouncycastle.util.encoders.Hex; |
| 12 | +import org.junit.Assert; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.tron.common.runtime.TVMTestResult; |
| 16 | +import org.tron.common.runtime.TvmTestUtils; |
| 17 | +import org.tron.common.utils.WalletUtil; |
| 18 | +import org.tron.common.utils.client.utils.AbiUtil; |
| 19 | +import org.tron.core.exception.ContractExeException; |
| 20 | +import org.tron.core.exception.ContractValidateException; |
| 21 | +import org.tron.core.exception.ReceiptCheckErrException; |
| 22 | +import org.tron.core.exception.VMIllegalException; |
| 23 | +import org.tron.core.vm.config.ConfigLoader; |
| 24 | +import org.tron.protos.Protocol.Transaction; |
| 25 | + |
| 26 | +@Slf4j |
| 27 | +public class TvmIssueVerifierTest extends VMTestBase { |
| 28 | + |
| 29 | + private static final int WORD_SIZE = 32; |
| 30 | + |
| 31 | + private static final String ABI = loadResource("solidity/TvmIssueVerifier.abi"); |
| 32 | + |
| 33 | + private static final String BYTECODE = loadResource("solidity/TvmIssueVerifier.bin"); |
| 34 | + |
| 35 | + @Before |
| 36 | + public void enableVmFeatures() { |
| 37 | + ConfigLoader.disable = false; |
| 38 | + manager.getDynamicPropertiesStore().saveAllowTvmTransferTrc10(1); |
| 39 | + manager.getDynamicPropertiesStore().saveAllowTvmConstantinople(1); |
| 40 | + manager.getDynamicPropertiesStore().saveAllowTvmIstanbul(1); |
| 41 | + manager.getDynamicPropertiesStore().saveAllowTvmLondon(1); |
| 42 | + manager.getDynamicPropertiesStore().saveAllowTvmCompatibleEvm(1); |
| 43 | + manager.getDynamicPropertiesStore().saveAllowTvmOsaka(1); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void verifyTvmOsakaFixesWithSolidity() |
| 48 | + throws ContractExeException, ReceiptCheckErrException, |
| 49 | + VMIllegalException, ContractValidateException { |
| 50 | + byte[] verifierAddress = deployVerifier(); |
| 51 | + |
| 52 | + manager.getDynamicPropertiesStore().saveAllowTvmOsaka(0); |
| 53 | + |
| 54 | + TVMTestResult modExpResult = trigger(verifierAddress, "modexpZeroModulus()", |
| 55 | + Collections.emptyList(), 100_000_000L); |
| 56 | + byte[] modExpReturn = modExpResult.getRuntime().getResult().getHReturn(); |
| 57 | + Assert.assertNull(modExpResult.getRuntime().getRuntimeError()); |
| 58 | + Assert.assertEquals(BigInteger.ONE, word(modExpReturn, 0)); |
| 59 | + Assert.assertEquals("MODEXP zero modulus currently returns empty returndata", |
| 60 | + BigInteger.ZERO, word(modExpReturn, 1)); |
| 61 | + |
| 62 | + TVMTestResult create2Result = trigger(verifierAddress, |
| 63 | + "failedCreate2KeepsPriorReturnData(bytes,uint256)", Arrays.asList("00", 7L), |
| 64 | + 100_000_000L); |
| 65 | + byte[] create2Return = create2Result.getRuntime().getResult().getHReturn(); |
| 66 | + Assert.assertNull(create2Result.getRuntime().getRuntimeError()); |
| 67 | + Assert.assertEquals(BigInteger.valueOf(32), word(create2Return, 0)); |
| 68 | + Assert.assertEquals(BigInteger.ZERO, word(create2Return, 1)); |
| 69 | + Assert.assertEquals("failed CREATE2 keeps the previous 32-byte return data buffer", |
| 70 | + BigInteger.valueOf(32), word(create2Return, 2)); |
| 71 | + |
| 72 | + TVMTestResult preOsakaCreate2SuccessResult = trigger(verifierAddress, |
| 73 | + "successfulCreate2KeepsPriorReturnData(bytes,uint256)", |
| 74 | + Arrays.asList(initCodeReturningRuntime("00"), 8L), 100_000_000L); |
| 75 | + byte[] preOsakaCreate2SuccessReturn = |
| 76 | + preOsakaCreate2SuccessResult.getRuntime().getResult().getHReturn(); |
| 77 | + Assert.assertNull(preOsakaCreate2SuccessResult.getRuntime().getRuntimeError()); |
| 78 | + Assert.assertEquals(BigInteger.valueOf(32), word(preOsakaCreate2SuccessReturn, 0)); |
| 79 | + Assert.assertTrue(word(preOsakaCreate2SuccessReturn, 1).signum() != 0); |
| 80 | + Assert.assertEquals(BigInteger.valueOf(32), word(preOsakaCreate2SuccessReturn, 2)); |
| 81 | + Assert.assertEquals(BigInteger.ONE, word(preOsakaCreate2SuccessReturn, 3)); |
| 82 | + |
| 83 | + manager.getDynamicPropertiesStore().saveAllowTvmOsaka(1); |
| 84 | + |
| 85 | + modExpResult = trigger(verifierAddress, "modexpZeroModulus()", |
| 86 | + Collections.emptyList(), 100_000_000L); |
| 87 | + modExpReturn = modExpResult.getRuntime().getResult().getHReturn(); |
| 88 | + Assert.assertNull(modExpResult.getRuntime().getRuntimeError()); |
| 89 | + Assert.assertEquals(BigInteger.ONE, word(modExpReturn, 0)); |
| 90 | + Assert.assertEquals("MODEXP zero modulus returns modLen bytes after Osaka", |
| 91 | + BigInteger.ONE, word(modExpReturn, 1)); |
| 92 | + |
| 93 | + create2Result = trigger(verifierAddress, |
| 94 | + "failedCreate2KeepsPriorReturnData(bytes,uint256)", Arrays.asList("00", 7L), |
| 95 | + 100_000_000L); |
| 96 | + create2Return = create2Result.getRuntime().getResult().getHReturn(); |
| 97 | + Assert.assertNull(create2Result.getRuntime().getRuntimeError()); |
| 98 | + Assert.assertEquals(BigInteger.valueOf(32), word(create2Return, 0)); |
| 99 | + Assert.assertEquals(BigInteger.ZERO, word(create2Return, 1)); |
| 100 | + Assert.assertEquals("failed CREATE2 clears the previous return data buffer after Osaka", |
| 101 | + BigInteger.ZERO, word(create2Return, 2)); |
| 102 | + |
| 103 | + TVMTestResult create2SuccessResult = trigger(verifierAddress, |
| 104 | + "successfulCreate2KeepsPriorReturnData(bytes,uint256)", |
| 105 | + Arrays.asList(initCodeReturningRuntime("00"), 9L), 100_000_000L); |
| 106 | + byte[] create2SuccessReturn = create2SuccessResult.getRuntime().getResult().getHReturn(); |
| 107 | + Assert.assertNull(create2SuccessResult.getRuntime().getRuntimeError()); |
| 108 | + Assert.assertEquals(BigInteger.valueOf(32), word(create2SuccessReturn, 0)); |
| 109 | + Assert.assertTrue(word(create2SuccessReturn, 1).signum() != 0); |
| 110 | + Assert.assertEquals("successful CREATE2 clears the previous return data buffer after Osaka", |
| 111 | + BigInteger.ZERO, word(create2SuccessReturn, 2)); |
| 112 | + Assert.assertEquals(BigInteger.ONE, word(create2SuccessReturn, 3)); |
| 113 | + } |
| 114 | + |
| 115 | + private byte[] deployVerifier() |
| 116 | + throws ContractExeException, ReceiptCheckErrException, |
| 117 | + VMIllegalException, ContractValidateException { |
| 118 | + byte[] owner = Hex.decode(OWNER_ADDRESS); |
| 119 | + Transaction trx = TvmTestUtils.generateDeploySmartContractAndGetTransaction( |
| 120 | + "TvmIssueVerifier", owner, ABI, BYTECODE, 0, 1_000_000_000L, 0, null); |
| 121 | + byte[] contractAddress = WalletUtil.generateContractAddress(trx); |
| 122 | + runtime = TvmTestUtils.processTransactionAndReturnRuntime(trx, rootRepository, null); |
| 123 | + Assert.assertNull(runtime.getRuntimeError()); |
| 124 | + return contractAddress; |
| 125 | + } |
| 126 | + |
| 127 | + private TVMTestResult trigger(byte[] contractAddress, String method, List<Object> args, |
| 128 | + long feeLimit) |
| 129 | + throws ContractExeException, ReceiptCheckErrException, |
| 130 | + VMIllegalException, ContractValidateException { |
| 131 | + String input = AbiUtil.parseMethod(method, args); |
| 132 | + return TvmTestUtils.triggerContractAndReturnTvmTestResult(Hex.decode(OWNER_ADDRESS), |
| 133 | + contractAddress, Hex.decode(input), 0, feeLimit, manager, null); |
| 134 | + } |
| 135 | + |
| 136 | + private static BigInteger word(byte[] data, int index) { |
| 137 | + int start = index * WORD_SIZE; |
| 138 | + return new BigInteger(1, Arrays.copyOfRange(data, start, start + WORD_SIZE)); |
| 139 | + } |
| 140 | + |
| 141 | + private static String initCodeReturningRuntime(String runtimeCode) { |
| 142 | + byte[] runtime = Hex.decode(runtimeCode); |
| 143 | + Assert.assertTrue(runtime.length <= 255); |
| 144 | + |
| 145 | + byte[] initCode = new byte[12 + runtime.length]; |
| 146 | + initCode[0] = 0x60; |
| 147 | + initCode[1] = (byte) runtime.length; |
| 148 | + initCode[2] = 0x60; |
| 149 | + initCode[3] = 0x0c; |
| 150 | + initCode[4] = 0x60; |
| 151 | + initCode[5] = 0x00; |
| 152 | + initCode[6] = 0x39; |
| 153 | + initCode[7] = 0x60; |
| 154 | + initCode[8] = (byte) runtime.length; |
| 155 | + initCode[9] = 0x60; |
| 156 | + initCode[10] = 0x00; |
| 157 | + initCode[11] = (byte) 0xf3; |
| 158 | + System.arraycopy(runtime, 0, initCode, 12, runtime.length); |
| 159 | + |
| 160 | + return Hex.toHexString(initCode); |
| 161 | + } |
| 162 | + |
| 163 | + private static String loadResource(String resource) { |
| 164 | + try (InputStream in = TvmIssueVerifierTest.class.getClassLoader() |
| 165 | + .getResourceAsStream(resource)) { |
| 166 | + if (in == null) { |
| 167 | + throw new IllegalStateException("Missing test resource: " + resource); |
| 168 | + } |
| 169 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 170 | + byte[] buffer = new byte[4096]; |
| 171 | + int read; |
| 172 | + while ((read = in.read(buffer)) >= 0) { |
| 173 | + out.write(buffer, 0, read); |
| 174 | + } |
| 175 | + return out.toString("UTF-8").trim(); |
| 176 | + } catch (IOException e) { |
| 177 | + throw new IllegalStateException("Cannot read test resource: " + resource, e); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments