Skip to content

Commit

Permalink
Some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
VincenzoArceri committed Feb 27, 2024
1 parent d985961 commit ea2cc7b
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 140 deletions.
40 changes: 25 additions & 15 deletions src/main/java/it/unipr/analysis/AbstractStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class AbstractStack implements ValueDomain<AbstractStack>, BaseLattice<Ab
public AbstractStack() {
this.stack = new LinkedList<KIntegerSet>(Collections.nCopies(STACK_LIMIT, KIntegerSet.BOTTOM));

// for (int i = 0; i < STACK_LIMIT; i++)
// stack.add(KIntegerSet.BOTTOM);
// for (int i = 0; i < STACK_LIMIT; i++)
// stack.add(KIntegerSet.BOTTOM);
}

/**
Expand Down Expand Up @@ -138,11 +138,11 @@ public AbstractStack wideningAux(AbstractStack other) throws SemanticException {

@Override
public AbstractStack top() {
// LinkedList<KIntegerSet> result = new LinkedList<>();
//
// for (int i = 0; i < STACK_LIMIT; i++)
// result.addLast(KIntegerSet.TOP);
LinkedList<KIntegerSet> result = new LinkedList<KIntegerSet>(Collections.nCopies(STACK_LIMIT, KIntegerSet.TOP));
LinkedList<KIntegerSet> result = new LinkedList<>();

for (int i = 0; i < STACK_LIMIT; i++)
result.addLast(KIntegerSet.TOP);
// LinkedList<KIntegerSet> result = new LinkedList<KIntegerSet>(Collections.nCopies(STACK_LIMIT, KIntegerSet.TOP));

return new AbstractStack(result);
}
Expand All @@ -157,7 +157,7 @@ public boolean isTop() {
if (isBottom())
return false;
else if (this.stack.stream()
.anyMatch(element -> !element.isTop()))
.anyMatch(element -> !element.isTop()))
return false;
else return true;
}
Expand Down Expand Up @@ -214,8 +214,8 @@ else if (isTop())

private static LinkedList<KIntegerSet> clone(LinkedList<KIntegerSet> list) {
LinkedList<KIntegerSet> result = new LinkedList<>();
for (int i = 0; i < list.size(); i++)
result.add(list.get(i).copy());
for (KIntegerSet item : list)
result.add(item.copy());

return result;
}
Expand Down Expand Up @@ -265,20 +265,30 @@ public List<KIntegerSet> getStack() {

@Override
public AbstractStack lubAux(AbstractStack other) throws SemanticException {
// Otherwise, let's build a new SymbolicStack
LinkedList<KIntegerSet> result = new LinkedList<>();

for (int i = 0; i < STACK_LIMIT; i++)
result.addLast(this.stack.get(i).lub(other.stack.get(i)));
Iterator<KIntegerSet> thisIterator = this.stack.iterator();
Iterator<KIntegerSet> otherIterator = other.stack.iterator();

while (thisIterator.hasNext() && otherIterator.hasNext()) {
KIntegerSet thisElement = thisIterator.next();
KIntegerSet otherElement = otherIterator.next();
result.addLast(thisElement.lub(otherElement));
}

return new AbstractStack(result);
}

@Override
public boolean lessOrEqualAux(AbstractStack other) throws SemanticException {
for (int i = 0; i < STACK_LIMIT; i++)
if (!this.stack.get(i).lessOrEqual(other.stack.get(i)))
Iterator<KIntegerSet> thisIterator = this.stack.iterator();
Iterator<KIntegerSet> otherIterator = other.stack.iterator();

while (thisIterator.hasNext() && otherIterator.hasNext()) {
if (!thisIterator.next().lessOrEqual(otherIterator.next())) {
return false;
}
}

return true;
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/it/unipr/analysis/AbstractStackSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ public AbstractStackSet clone() {
return TOP;
else if (isBottom())
return BOTTOM;
AbstractStackSet result = new AbstractStackSet(new HashSet<AbstractStack>(), false);
for (AbstractStack stack : elements())
result.add(stack.clone());
return result;
return new AbstractStackSet(new HashSet<>(this.elements), false);

// AbstractStackSet result = new AbstractStackSet(new HashSet<AbstractStack>(), false);
// for (AbstractStack stack : elements())
// result.add(stack.clone());
// return result;
}

@Override
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/it/unipr/analysis/EVMAbstractState.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo

for (AbstractStack stack : stacks) {
AbstractStack resultStack = stack.clone();
BigDecimal valueToPush = this.toBigDecimal(un.getExpression());
BigInteger valueToPush = this.toBigDecimal(un.getExpression());

resultStack.push(new KIntegerSet(valueToPush));
result.add(resultStack);
Expand Down Expand Up @@ -342,7 +342,7 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
for (AbstractStack stack : stacks) {
AbstractStack resultStack = stack.clone();
Integer i = (Integer) ((Constant) un.getExpression()).getValue();
resultStack.push(new KIntegerSet(new BigDecimal(i)));
resultStack.push(new KIntegerSet(BigInteger.valueOf(i)));
result.add(resultStack);
}

Expand Down Expand Up @@ -721,10 +721,10 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
if (target.isTop() || indexOfByte.isTop()) {
resultStack.push(KIntegerSet.TOP);
} else {
for (BigDecimal value : target) {
byte[] valueAsByteArray = value.unscaledValue().toByteArray();
for (BigInteger value : target) {
byte[] valueAsByteArray = value.toByteArray();

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

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

for (BigDecimal os : offset) {
BigDecimal thirtyTwo = new BigDecimal(32);
BigDecimal current_mu_i = os.add(thirtyTwo)
.divide(thirtyTwo, RoundingMode.UP);
for (BigInteger os : offset) {
BigInteger thirtyTwo = BigInteger.valueOf(32);
BigInteger current_mu_i = os.add(thirtyTwo)
.divide(thirtyTwo);

memoryResult = memory.putState(os, value);

Expand Down Expand Up @@ -1019,11 +1019,11 @@ public EVMAbstractState smallStepSemantics(ValueExpression expression, ProgramPo
} else {
KIntegerSet current_mu_i_lub = KIntegerSet.BOTTOM;

for (BigDecimal os : offset) {
BigDecimal current_mu_i = os.add(new BigDecimal(1))
.divide(new BigDecimal(32), RoundingMode.UP);
for (BigInteger os : offset) {
BigInteger current_mu_i = os.add(BigInteger.valueOf(1))
.divide(BigInteger.valueOf(32));

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

current_mu_i_lub = current_mu_i_lub.lub(new KIntegerSet(current_mu_i));
}
Expand Down Expand Up @@ -1831,13 +1831,13 @@ public boolean isBottom() {
*
* @return the BigInteger corresponding to the memory word
*/
private BigDecimal toBigDecimal(SymbolicExpression expression) {
private BigInteger toBigDecimal(SymbolicExpression expression) {
Constant c = (Constant) expression;
String hex = (String) c.getValue();
String hexadecimal = hex.substring(2);
BigInteger bigIntVal = new BigInteger(hexadecimal, 16);
BigDecimal bigDecimalVal = new BigDecimal(bigIntVal);
return bigDecimalVal;
// BigDecimal bigDecimalVal = new BigDecimal(bigIntVal);
return bigIntVal;
}

@Override
Expand Down
Loading

0 comments on commit ea2cc7b

Please sign in to comment.