001package daikon.tools.runtimechecker;
002
003import java.util.ArrayList;
004import java.util.List;
005import org.checkerframework.checker.lock.qual.GuardedBy;
006
007/**
008 * If a class has been instrumented with the instrumenter, invariant violations are added to the
009 * {@code violations} list.
010 */
011@SuppressWarnings("JavaLangClash")
012public class Runtime {
013
014  /** A list of throwables seen when attempting to evaluate properties. */
015  public static List<Throwable> internalInvariantEvaluationErrors = new ArrayList<>();
016
017  private static @GuardedBy("Runtime.class") List<Violation> violations =
018      new ArrayList<Violation>();
019
020  // The number of times that an invariant was checked (whether the
021  // check succeeded or failed).
022  public static long numEvaluations = 0;
023
024  // The number of entry program points traversed.
025  public static long numPptEntries = 0;
026
027  // The number of normal-exit program points traversed.
028  public static long numNormalPptExits = 0;
029
030  // The number of exceptional-exit program points traversed.
031  public static long numExceptionalPptExits = 0;
032
033  /** Returns the list of violations. */
034  public static synchronized List<Violation> getViolations() {
035    List<Violation> retval = new ArrayList<>();
036    for (Violation v : violations) {
037      retval.add(v);
038    }
039    return retval;
040  }
041
042  /** Empty the violations list. */
043  public static synchronized void resetViolations() {
044    violations = new ArrayList<Violation>();
045  }
046
047  /** True if the violations list is empty. */
048  public static synchronized boolean violationsEmpty() {
049    return violations.isEmpty();
050  }
051
052  /** Add a violation to the violations list. */
053  public static synchronized void violationsAdd(Violation v) {
054    violations.add(v);
055  }
056
057  // Works for non-negative values
058  public static final boolean isPowerOfTwo(int x) {
059    if (x == 0) {
060      return true;
061    }
062    // If x is a power of two, then x - 1 has no bits in common with x
063    // OTOH, if x is not a power of two, then x and x - 1 have the same
064    // most-significant bit set, so they have at least one bit in common.
065    return (x & (x - 1)) == 0;
066  }
067
068  private static int largestNonPointerValue = 100000;
069
070  private static int smallestNonPointerValue = -100000;
071
072  public static final boolean isWithinPointerRange(int value) {
073    if (value == 0) {
074      return true;
075    }
076    return (value >= largestNonPointerValue) || (value <= smallestNonPointerValue);
077  }
078}