All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.java.se.ProgramState Maven / Gradle / Ivy

There is a newer version: 8.9.0.37768
Show newest version
/*
 * SonarQube Java
 * Copyright (C) 2012-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.java.se;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.sonar.java.collections.AVLTree;
import org.sonar.java.collections.PMap;
import org.sonar.java.se.constraint.BooleanConstraint;
import org.sonar.java.se.constraint.Constraint;
import org.sonar.java.se.constraint.ConstraintManager;
import org.sonar.java.se.constraint.ObjectConstraint;
import org.sonar.java.se.symbolicvalues.BinaryRelation;
import org.sonar.java.se.symbolicvalues.SymbolicValue;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.VariableTree;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class ProgramState {

  public static class Pop {

    public final ProgramState state;
    public final List values;

    public Pop(ProgramState programState, List result) {
      state = programState;
      values = result;
    }

  }

  private int hashCode;

  private final int constraintSize;
  public static final ProgramState EMPTY_STATE = new ProgramState(
    AVLTree.create(),
    AVLTree.create(),
    AVLTree.create()
      .put(SymbolicValue.NULL_LITERAL, ObjectConstraint.nullConstraint())
      .put(SymbolicValue.TRUE_LITERAL, BooleanConstraint.TRUE)
      .put(SymbolicValue.FALSE_LITERAL, BooleanConstraint.FALSE),
    AVLTree.create(),
    Lists.newLinkedList());

  private final PMap visitedPoints;

  private final Deque stack;
  private final PMap values;
  private final PMap references;
  private final PMap constraints;

  private ProgramState(PMap values, PMap references,
    PMap constraints, PMap visitedPoints,
    Deque stack) {
    this.values = values;
    this.references = references;
    this.constraints = constraints;
    this.visitedPoints = visitedPoints;
    this.stack = stack;
    constraintSize = 3;
  }

  private ProgramState(ProgramState ps, Deque newStack) {
    values = ps.values;
    references = ps.references;
    constraints = ps.constraints;
    constraintSize = ps.constraintSize;
    visitedPoints = ps.visitedPoints;
    stack = newStack;
  }

  private ProgramState(ProgramState ps, PMap newConstraints) {
    values = ps.values;
    references = ps.references;
    constraints = newConstraints;
    constraintSize = ps.constraintSize + 1;
    visitedPoints = ps.visitedPoints;
    this.stack = ps.stack;
  }

  ProgramState stackValue(SymbolicValue sv) {
    Deque newStack = new LinkedList<>(stack);
    newStack.push(sv);
    return new ProgramState(this, newStack);
  }

  ProgramState clearStack() {
    return unstackValue(stack.size()).state;
  }

  public Pop unstackValue(int nbElements) {
    if (nbElements == 0) {
      return new Pop(this, Collections.emptyList());
    }
    Preconditions.checkArgument(stack.size() >= nbElements, nbElements);
    Deque newStack = new LinkedList<>(stack);
    List result = Lists.newArrayList();
    for (int i = 0; i < nbElements; i++) {
      result.add(newStack.pop());
    }
    return new Pop(new ProgramState(this, newStack), result);
  }

  public SymbolicValue peekValue() {
    return stack.peek();
  }

  public List peekValues(int n) {
    if (n > stack.size()) {
      throw new IllegalStateException("At least " + n + " values were expected on the stack!");
    }
    return ImmutableList.copyOf(stack).subList(0, n);
  }

  int numberOfTimeVisited(ExplodedGraph.ProgramPoint programPoint) {
    Integer count = visitedPoints.get(programPoint);
    return count == null ? 0 : count;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ProgramState that = (ProgramState) o;
    return Objects.equals(values, that.values) &&
      Objects.equals(constraints, that.constraints) &&
      Objects.equals(peekValue(), that.peekValue());
  }

  @Override
  public int hashCode() {
    if (hashCode == 0) {
      hashCode = Objects.hash(values, constraints, peekValue());
    }
    return hashCode;
  }

  @Override
  public String toString() {
    return "{" + values.toString() + "}  {" + constraints.toString() + "}" + " { " + stack.toString() + " }";
  }

  public ProgramState addConstraint(SymbolicValue symbolicValue, Constraint constraint) {
    PMap newConstraints = constraints.put(symbolicValue, constraint);
    if (newConstraints != constraints) {
      return new ProgramState(this, newConstraints);
    }
    return this;
  }

  ProgramState put(Symbol symbol, SymbolicValue value) {
    if (symbol.isUnknown()) {
      return this;
    }
    SymbolicValue oldValue = values.get(symbol);
    if (oldValue == null || oldValue != value) {
      PMap newReferences = references;
      if (oldValue != null) {
        newReferences = decreaseReference(newReferences, oldValue);
      }
      newReferences = increaseReference(newReferences, value);
      PMap newValues = values.put(symbol, value);
      return new ProgramState(newValues, newReferences, constraints, visitedPoints, stack);
    }
    return this;
  }

  private static PMap decreaseReference(PMap givenReferences, SymbolicValue sv) {
    Integer value = givenReferences.get(sv);
    Preconditions.checkNotNull(value);
    return givenReferences.put(sv, value - 1);
  }

  private static PMap increaseReference(PMap givenReferences, SymbolicValue sv) {
    Integer value = givenReferences.get(sv);
    if (value == null) {
      return givenReferences.put(sv, 1);
    } else {
      return givenReferences.put(sv, value + 1);
    }
  }

  private static boolean isDisposable(SymbolicValue symbolicValue, @Nullable Object constraint) {
    return SymbolicValue.isDisposable(symbolicValue) && (constraint == null || !(constraint instanceof ObjectConstraint) || ((ObjectConstraint) constraint).isDisposable());
  }

  private static boolean inStack(Deque stack, SymbolicValue symbolicValue) {
    for (SymbolicValue value : stack) {
      if (value.equals(symbolicValue) || value.references(symbolicValue)) {
        return true;
      }
    }
    return false;
  }

  private static boolean isLocalVariable(Symbol symbol) {
    return symbol.isVariableSymbol() && symbol.owner().isMethodSymbol();
  }

  public ProgramState cleanupDeadSymbols(Set liveVariables) {
    PMap newValues = values;
    PMap newReferences = references;
    PMap newConstraints = constraints;
    boolean newProgramState = false;
    for (Iterator> iter = newValues.entriesIterator(); iter.hasNext();) {
      Map.Entry next = iter.next();
      Symbol symbol = next.getKey();
      if (isLocalVariable(symbol) && !liveVariables.contains(symbol)) {
        if (!newProgramState) {
          newProgramState = true;
        }
        SymbolicValue symbolicValue = next.getValue();
        newValues = newValues.remove(symbol);
        newReferences = decreaseReference(newReferences, symbolicValue);
        if (!isReachable(symbolicValue, newReferences) && isDisposable(symbolicValue, newConstraints.get(symbolicValue)) && !inStack(stack, symbolicValue)) {
          newConstraints = newConstraints.remove(symbolicValue);
          newReferences = newReferences.remove(symbolicValue);
        }
      }
    }
    return newProgramState ? new ProgramState(newValues, newReferences, newConstraints, visitedPoints, stack) : this;
  }

  public ProgramState cleanupConstraints() {
    PMap newConstraints = constraints;
    PMap newReferences = references;
    boolean newProgramState = false;
    for (Iterator> iter = newConstraints.entriesIterator(); iter.hasNext();) {
      Map.Entry next = iter.next();
      SymbolicValue symbolicValue = next.getKey();
      if (!isReachable(symbolicValue, newReferences) && isDisposable(symbolicValue, next.getValue()) && !inStack(stack, symbolicValue)) {
        if (!newProgramState) {
          newProgramState = true;
        }
        newConstraints = newConstraints.remove(symbolicValue);
        newReferences = newReferences.remove(symbolicValue);
      }
    }
    return newProgramState ? new ProgramState(values, newReferences, newConstraints, visitedPoints, stack) : this;
  }

  public ProgramState resetFieldValues(ConstraintManager constraintManager) {
    final List variableTrees = new ArrayList<>();
    values.forEach(new PMap.Consumer() {
      @Override
      public void accept(Symbol symbol, SymbolicValue value) {
        if (isField(symbol)) {
          VariableTree variable = ((Symbol.VariableSymbol) symbol).declaration();
          if (variable != null) {
            variableTrees.add(variable);
          }
        }
      }
    });
    if (variableTrees.isEmpty()) {
      return this;
    }
    PMap newValues = values;
    PMap newReferences = references;
    for (VariableTree variableTree : variableTrees) {
      Symbol symbol = variableTree.symbol();
      SymbolicValue oldValue = newValues.get(symbol);
      if (oldValue != null) {
        newReferences = decreaseReference(newReferences, oldValue);
      }
      SymbolicValue newValue = constraintManager.createSymbolicValue(variableTree);
      newValues = newValues.put(symbol, newValue);
      newReferences = increaseReference(newReferences, newValue);
    }
    return new ProgramState(newValues, newReferences, constraints, visitedPoints, stack);
  }

  public static boolean isField(Symbol symbol) {
    return symbol.isVariableSymbol() && !symbol.owner().isMethodSymbol();
  }

  private static boolean isReachable(SymbolicValue symbolicValue, PMap references) {
    Integer integer = references.get(symbolicValue);
    return integer != null && integer > 0;
  }

  public boolean canReach(SymbolicValue symbolicValue) {
    return isReachable(symbolicValue, references);
  }

  public ProgramState visitedPoint(ExplodedGraph.ProgramPoint programPoint, int nbOfVisit) {
    return new ProgramState(values, references, constraints, visitedPoints.put(programPoint, nbOfVisit), stack);
  }

  @CheckForNull
  public Constraint getConstraint(SymbolicValue sv) {
    return constraints.get(sv);
  }

  public int constraintsSize() {
    return constraintSize;
  }

  @CheckForNull
  public SymbolicValue getValue(Symbol symbol) {
    return values.get(symbol);
  }

  public Map getValuesWithConstraints(final Object state) {
    final Map result = new HashMap<>();
    constraints.forEach(new PMap.Consumer() {
      @Override
      public void accept(SymbolicValue value, Constraint valueConstraint) {
        if (valueConstraint instanceof ObjectConstraint) {
          ObjectConstraint constraint = (ObjectConstraint) valueConstraint;
          if (constraint.hasStatus(state)) {
            result.put(value, constraint);
          }
        }
      }
    });
    return result;
  }

  public List getFieldConstraints(final Object state) {
    final Set valuesAssignedToFields = getFieldValues();
    final List result = new ArrayList<>();
    constraints.forEach(new PMap.Consumer() {
      @Override
      public void accept(SymbolicValue value, Constraint valueConstraint) {
        if (valueConstraint instanceof ObjectConstraint && !valuesAssignedToFields.contains(value)) {
          ObjectConstraint constraint = (ObjectConstraint) valueConstraint;
          if (constraint.hasStatus(state)) {
            result.add(constraint);
          }
        }
      }
    });
    return result;
  }

  public Set getFieldValues() {
    final Set fieldValues = new HashSet<>();
    values.forEach(new PMap.Consumer() {
      @Override
      public void accept(Symbol key, SymbolicValue value) {
        if (isField(key)) {
          fieldValues.add(value);
        }
      }
    });
    return fieldValues;
  }

  public List getKnownRelations() {
    final List knownRelations = new ArrayList<>();
    constraints.forEach(new PMap.Consumer() {
      @Override
      public void accept(SymbolicValue value, Constraint constraint) {
        BinaryRelation relation = value.binaryRelation();
        if (relation != null) {
          if (BooleanConstraint.TRUE.equals(constraint)) {
            knownRelations.add(relation);
          } else if (BooleanConstraint.FALSE.equals(constraint)) {
            knownRelations.add(relation.inverse());
          }
        }
      }
    });
    return knownRelations;
  }

  @CheckForNull
  public ObjectConstraint getConstraintWithStatus(SymbolicValue value, Object aState) {
    final Object constraint = getConstraint(value.wrappedValue());
    if (constraint instanceof ObjectConstraint) {
      ObjectConstraint oConstraint = (ObjectConstraint) constraint;
      if (oConstraint.hasStatus(aState)) {
        return oConstraint;
      }
    }
    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy