Skip to content

Commit 709b645

Browse files
committed
func(vm): optimize selfdestruct restriction
1 parent def125e commit 709b645

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static org.tron.common.utils.ByteUtil.parseWord;
1717
import static org.tron.common.utils.ByteUtil.stripLeadingZeroes;
1818
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
19+
import static org.tron.core.vm.VMConstant.SIG_LENGTH;
1920

2021
import com.google.protobuf.ByteString;
2122

@@ -202,7 +203,7 @@ public class PrecompiledContracts {
202203
public static PrecompiledContract getOptimizedContractForConstant(PrecompiledContract contract) {
203204
try {
204205
Constructor<?> constructor = contract.getClass().getDeclaredConstructor();
205-
return (PrecompiledContracts.PrecompiledContract) constructor.newInstance();
206+
return (PrecompiledContracts.PrecompiledContract) constructor.newInstance();
206207
} catch (Exception e) {
207208
throw new RuntimeException(e);
208209
}
@@ -395,6 +396,20 @@ private static byte[][] extractBytesArray(DataWord[] words, int offset, byte[] d
395396
return bytesArray;
396397
}
397398

399+
private static byte[][] extractSigArray(DataWord[] words, int offset, byte[] data) {
400+
if (offset > words.length - 1) {
401+
return new byte[0][];
402+
}
403+
int len = words[offset].intValueSafe();
404+
byte[][] bytesArray = new byte[len][];
405+
for (int i = 0; i < len; i++) {
406+
int bytesOffset = words[offset + i + 1].intValueSafe() / WORD_SIZE;
407+
bytesArray[i] = extractBytes(data, (bytesOffset + offset + 2) * WORD_SIZE,
408+
SIG_LENGTH);
409+
}
410+
return bytesArray;
411+
}
412+
398413
private static byte[] extractBytes(byte[] data, int offset, int len) {
399414
return Arrays.copyOfRange(data, offset, offset + len);
400415
}
@@ -936,8 +951,15 @@ public Pair<Boolean, byte[]> execute(byte[] rawData) {
936951
byte[] hash = Sha256Hash.hash(CommonParameter
937952
.getInstance().isECKeyCryptoEngine(), combine);
938953

939-
byte[][] signatures = extractBytesArray(
940-
words, words[3].intValueSafe() / WORD_SIZE, rawData);
954+
if (VMConfig.allowTvmSelfdestructRestriction()) {
955+
int sigArraySize = words[words[3].intValueSafe() / WORD_SIZE].intValueSafe();
956+
if (sigArraySize > MAX_SIZE) {
957+
return Pair.of(true, DATA_FALSE);
958+
}
959+
}
960+
byte[][] signatures = VMConfig.allowTvmSelfdestructRestriction() ?
961+
extractSigArray(words, words[3].intValueSafe() / WORD_SIZE, rawData) :
962+
extractBytesArray(words, words[3].intValueSafe() / WORD_SIZE, rawData);
941963

942964
if (signatures.length == 0 || signatures.length > MAX_SIZE) {
943965
return Pair.of(true, DATA_FALSE);
@@ -1021,8 +1043,18 @@ private Pair<Boolean, byte[]> doExecute(byte[] data)
10211043
throws InterruptedException, ExecutionException {
10221044
DataWord[] words = DataWord.parseArray(data);
10231045
byte[] hash = words[0].getData();
1024-
byte[][] signatures = extractBytesArray(
1025-
words, words[1].intValueSafe() / WORD_SIZE, data);
1046+
1047+
if (VMConfig.allowTvmSelfdestructRestriction()) {
1048+
int sigArraySize = words[words[1].intValueSafe() / WORD_SIZE].intValueSafe();
1049+
int addrArraySize = words[words[2].intValueSafe() / WORD_SIZE].intValueSafe();
1050+
if (sigArraySize > MAX_SIZE || addrArraySize > MAX_SIZE) {
1051+
return Pair.of(true, DATA_FALSE);
1052+
}
1053+
}
1054+
1055+
byte[][] signatures = VMConfig.allowTvmSelfdestructRestriction() ?
1056+
extractSigArray(words, words[1].intValueSafe() / WORD_SIZE, data) :
1057+
extractBytesArray(words, words[1].intValueSafe() / WORD_SIZE, data);
10261058
byte[][] addresses = extractBytes32Array(
10271059
words, words[2].intValueSafe() / WORD_SIZE);
10281060
int cnt = signatures.length;

actuator/src/main/java/org/tron/core/vm/VMConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class VMConstant {
44

55
public static final int CONTRACT_NAME_LENGTH = 32;
66
public static final int MIN_TOKEN_ID = 1_000_000;
7+
public static final int SIG_LENGTH = 65;
78

89
// Numbers
910
public static final int ONE_HUNDRED = 100;

actuator/src/main/java/org/tron/core/vm/program/Memory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void write(int address, byte[] data, int dataSize, boolean limited) {
9898

9999
public void extendAndWrite(int address, int allocSize, byte[] data) {
100100
extend(address, allocSize);
101-
write(address, data, data.length, false);
101+
write(address, data, allocSize, false);
102102
}
103103

104104
public void extend(int address, int size) {

actuator/src/main/java/org/tron/core/vm/program/Program.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,11 @@ public void callToPrecompiledAddress(MessageCall msg,
17461746
}
17471747
}
17481748

1749-
this.memorySave(msg.getOutDataOffs().intValue(), out.getRight());
1749+
if (VMConfig.allowTvmSelfdestructRestriction()) {
1750+
this.memorySave(msg.getOutDataOffs().intValueSafe(), msg.getOutDataSize().intValueSafe(), out.getRight());
1751+
} else {
1752+
this.memorySave(msg.getOutDataOffs().intValue(), out.getRight());
1753+
}
17501754
}
17511755
}
17521756

0 commit comments

Comments
 (0)