Skip to content

Commit

Permalink
Optimization: Added Number class
Browse files Browse the repository at this point in the history
  • Loading branch information
merendamattia committed Mar 1, 2024
1 parent 890e377 commit 3e40c48
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 183 deletions.
1 change: 1 addition & 0 deletions src/main/java/it/unipr/EVMLiSA.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ public void run() {
int extra = 120000;
long blocks = smartContracts.size() / CORES * 20000;
long timeToWait = smartContracts.size() * millisPerSmartContract + extra + blocks;
timeToWait = timeToWait * 10;

// Statistics
long minutes = (timeToWait / 1000) / 60;
Expand Down
66 changes: 34 additions & 32 deletions src/main/java/it/unipr/analysis/EVMAbstractState.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package it.unipr.analysis;

import java.math.BigInteger;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

import org.apache.commons.lang3.tuple.Pair;

import it.unipr.analysis.operator.JumpiOperator;
import it.unive.lisa.analysis.BaseLattice;
import it.unive.lisa.analysis.Lattice;
Expand All @@ -27,14 +17,22 @@
import it.unive.lisa.symbolic.value.ValueExpression;
import it.unive.lisa.symbolic.value.operator.unary.LogicalNegation;
import it.unive.lisa.symbolic.value.operator.unary.UnaryOperator;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.commons.lang3.tuple.Pair;

public class EVMAbstractState implements ValueDomain<EVMAbstractState>, BaseLattice<EVMAbstractState> {

private static final EVMAbstractState TOP = new EVMAbstractState(true, "");
private static final EVMAbstractState BOTTOM = new EVMAbstractState(new AbstractStackSet().bottom(),
new Memory().bottom(), KIntegerSet.BOTTOM);
private final boolean isTop;

/**
* The address of the running contract.
*/
Expand Down Expand Up @@ -69,7 +67,7 @@ private EVMAbstractState(boolean isTop, String contractAddress) {
this.stacks = new AbstractStackSet();
this.memory = new Memory();
this.mu_i = KIntegerSet.ZERO;

CONTRACT_ADDRESS = contractAddress;
}

Expand Down Expand Up @@ -153,7 +151,7 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
}
case "PushOperator": { // PUSH
AbstractStackSet result = new AbstractStackSet(new HashSet<>(stacks.size()), false);
KIntegerSet toPush = new KIntegerSet(this.toBigDecimal(un.getExpression()));
KIntegerSet toPush = new KIntegerSet(toBigInteger(un.getExpression()));

for (AbstractStack stack : stacks) {
AbstractStack resultStack = stack.clone();
Expand All @@ -165,8 +163,8 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
}
case "AddressOperator": { // ADDRESS
AbstractStackSet result = new AbstractStackSet(new HashSet<>(stacks.size()), false);
KIntegerSet hex = new KIntegerSet(toBigDecimal(CONTRACT_ADDRESS));
KIntegerSet hex = new KIntegerSet(toBigInteger(CONTRACT_ADDRESS));

for (AbstractStack stack : stacks) {
AbstractStack resultStack = stack.clone();
resultStack.push(hex);
Expand Down Expand Up @@ -728,10 +726,10 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
if (target.isTop() || indexOfByte.isTop()) {
resultStack.push(KIntegerSet.NUMERIC_TOP);
} else {
for (BigInteger value : target) {
for (Number value : target) {
byte[] valueAsByteArray = value.toByteArray();

for (BigInteger index : indexOfByte) {
for (Number index : indexOfByte) {
int intIndex = index.intValue();

if (intIndex <= 0 || intIndex >= valueAsByteArray.length) {
Expand Down Expand Up @@ -992,9 +990,9 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
} else {
KIntegerSet current_mu_i_lub = KIntegerSet.BOTTOM;

for (BigInteger os : offset) {
BigInteger thirtyTwo = BigInteger.valueOf(32);
BigInteger current_mu_i = os.add(thirtyTwo)
for (Number os : offset) {
Number thirtyTwo = new Number(32);
Number current_mu_i = os.add(thirtyTwo)
.divide(thirtyTwo);

memoryResult = memory.putState(os, value);
Expand Down Expand Up @@ -1026,11 +1024,11 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
} else {
KIntegerSet current_mu_i_lub = KIntegerSet.BOTTOM;

for (BigInteger os : offset) {
BigInteger current_mu_i = os.add(BigInteger.valueOf(1))
.divide(BigInteger.valueOf(32));
for (Number os : offset) {
Number current_mu_i = os.add(new Number(1))
.divide(new Number(32));

memoryResult = memory.putState(os, value.mod(new KIntegerSet(BigInteger.valueOf(256))));
memoryResult = memory.putState(os, value.mod(new KIntegerSet(new Number(256))));

current_mu_i_lub = current_mu_i_lub.lub(new KIntegerSet(current_mu_i));
}
Expand Down Expand Up @@ -1721,9 +1719,10 @@ public EVMAbstractState assume(ValueExpression expression, ProgramPoint src, Pro
UnaryOperator op = un.getOperator();

if (op instanceof JumpiOperator) { // JUMPI

@SuppressWarnings("unchecked")
Pair<Set<AbstractStack>, Set<AbstractStack>> split = ((Pair<Set<AbstractStack>, Set<AbstractStack>>) ((Constant) un.getExpression()).getValue());
Pair<Set<AbstractStack>, Set<AbstractStack>> split = ((Pair<Set<AbstractStack>,
Set<AbstractStack>>) ((Constant) un.getExpression()).getValue());
if (split.getLeft().isEmpty() && split.getRight().isEmpty())
return top();
else if (split.getLeft().isEmpty())
Expand All @@ -1741,7 +1740,10 @@ else if (split.getLeft().isEmpty())
if (wrappedOperator instanceof JumpiOperator) { // !JUMPI

@SuppressWarnings("unchecked")
Pair<Set<AbstractStack>, Set<AbstractStack>> split = ((Pair<Set<AbstractStack>, Set<AbstractStack>>) ((Constant) ((UnaryExpression) wrappedExpr).getExpression()).getValue());
Pair<Set<AbstractStack>,
Set<AbstractStack>> split = ((Pair<Set<AbstractStack>, Set<
AbstractStack>>) ((Constant) ((UnaryExpression) wrappedExpr).getExpression())
.getValue());
if (split.getLeft().isEmpty() && split.getRight().isEmpty())
return top();
else if (split.getRight().isEmpty())
Expand Down Expand Up @@ -1823,17 +1825,17 @@ public boolean isBottom() {
*
* @return the BigInteger corresponding to the memory word
*/
private BigInteger toBigDecimal(SymbolicExpression expression) {
private Number toBigInteger(SymbolicExpression expression) {
Constant c = (Constant) expression;
String hex = (String) c.getValue();
return toBigDecimal(hex);
return toBigInteger(hex);
}
private BigInteger toBigDecimal(String str) {

private Number toBigInteger(String str) {
String hexadecimal = str.substring(2);
BigInteger bigIntVal = new BigInteger(hexadecimal, 16);
// BigDecimal bigDecimalVal = new BigDecimal(bigIntVal);
return bigIntVal;
return new Number(bigIntVal);
}

@Override
Expand Down
Loading

0 comments on commit 3e40c48

Please sign in to comment.