Example #1
0
void Fuzzer::UpdateCorpusDistribution() {
  size_t N = Corpus.size();
  std::vector<double> Intervals(N + 1);
  std::vector<double> Weights(N);
  std::iota(Intervals.begin(), Intervals.end(), 0);
  std::iota(Weights.begin(), Weights.end(), 1);
  CorpusDistribution = std::piecewise_constant_distribution<double>(
      Intervals.begin(), Intervals.end(), Weights.begin());
}
Example #2
0
pBuffer BufferSource::
        read( const Interval& I )
{
    Interval myInterval = buffer_->getInterval();
    Intervals i(I.first, I.first+1);
    if (i & myInterval)
    {
        return buffer_;
    }

    return zeros((Intervals(I) - myInterval).fetchFirstInterval());
}
Intervals intersect(const Intervals &i1, const Intervals &i2)
{
    std::vector<Interval> seedIntervals;
    for(std::list<Interval>::const_iterator it = i1.m_intervals.begin(); it != i1.m_intervals.end(); ++it)
    {
        for(std::list<Interval>::const_iterator it2 = i2.m_intervals.begin(); it2 != i2.m_intervals.end(); ++it2)
        {
            if(overlap(*it, *it2))
                seedIntervals.push_back(Iintersect(*it, *it2));
        }
    }
    return Intervals(seedIntervals);
}
bool DataFlowTrace::Init(const std::string &DirPath,
                         std::string *FocusFunction,
                         Random &Rand) {
  if (DirPath.empty()) return false;
  Printf("INFO: DataFlowTrace: reading from '%s'\n", DirPath.c_str());
  Vector<SizedFile> Files;
  GetSizedFilesFromDir(DirPath, &Files);
  std::string L;
  size_t FocusFuncIdx = SIZE_MAX;
  Vector<std::string> FunctionNames;

  // Read functions.txt
  std::ifstream IF(DirPlusFile(DirPath, kFunctionsTxt));
  size_t NumFunctions = 0;
  while (std::getline(IF, L, '\n')) {
    FunctionNames.push_back(L);
    NumFunctions++;
    if (*FocusFunction == L)
      FocusFuncIdx = NumFunctions - 1;
  }

  if (*FocusFunction == "auto") {
    // AUTOFOCUS works like this:
    // * reads the coverage data from the DFT files.
    // * assigns weights to functions based on coverage.
    // * chooses a random function according to the weights.
    ReadCoverage(DirPath);
    auto Weights = Coverage.FunctionWeights(NumFunctions);
    Vector<double> Intervals(NumFunctions + 1);
    std::iota(Intervals.begin(), Intervals.end(), 0);
    auto Distribution = std::piecewise_constant_distribution<double>(
        Intervals.begin(), Intervals.end(), Weights.begin());
    FocusFuncIdx = static_cast<size_t>(Distribution(Rand));
    *FocusFunction = FunctionNames[FocusFuncIdx];
    assert(FocusFuncIdx < NumFunctions);
    Printf("INFO: AUTOFOCUS: %zd %s\n", FocusFuncIdx,
           FunctionNames[FocusFuncIdx].c_str());
    for (size_t i = 0; i < NumFunctions; i++) {
      if (!Weights[i]) continue;
      Printf("  [%zd] W %g\tBB-tot %u\tBB-cov %u\tEntryFreq %u:\t%s\n", i,
             Weights[i], Coverage.GetNumberOfBlocks(i),
             Coverage.GetNumberOfCoveredBlocks(i), Coverage.GetCounter(i, 0),
             FunctionNames[i].c_str());
    }
  }

  if (!NumFunctions || FocusFuncIdx == SIZE_MAX || Files.size() <= 1)
    return false;

  // Read traces.
  size_t NumTraceFiles = 0;
  size_t NumTracesWithFocusFunction = 0;
  for (auto &SF : Files) {
    auto Name = Basename(SF.File);
    if (Name == kFunctionsTxt) continue;
    NumTraceFiles++;
    // Printf("=== %s\n", Name.c_str());
    std::ifstream IF(SF.File);
    while (std::getline(IF, L, '\n')) {
      size_t FunctionNum = 0;
      std::string DFTString;
      if (ParseDFTLine(L, &FunctionNum, &DFTString) &&
          FunctionNum == FocusFuncIdx) {
        NumTracesWithFocusFunction++;

        if (FunctionNum >= NumFunctions)
          return ParseError("N is greater than the number of functions", L);
        Traces[Name] = DFTStringToVector(DFTString);
        // Print just a few small traces.
        if (NumTracesWithFocusFunction <= 3 && DFTString.size() <= 16)
          Printf("%s => |%s|\n", Name.c_str(), std::string(DFTString).c_str());
        break; // No need to parse the following lines.
      }
    }
  }
  assert(NumTraceFiles == Files.size() - 1);
  Printf("INFO: DataFlowTrace: %zd trace files, %zd functions, "
         "%zd traces with focus function\n",
         NumTraceFiles, NumFunctions, NumTracesWithFocusFunction);
  return true;
}
Intervals PolynomialIntervalSolver::findPolyIntervals(const Polynomial &poly)
{
    const double eps = 1e-8;
    
    int leadcoeff=0;
    std::vector<Interval> empty;
    std::vector<Interval> all;
    all. push_back(Interval(-std::numeric_limits<double>::infinity(),
                            std::numeric_limits<double>::infinity()));
    
    const std::vector<double> &coeffs = poly.getCoeffs();
    int deg = coeffs.size()-1;
    
    for(int i=0; i<(int)coeffs.size(); i++)
        assert(!isnan(coeffs[i]));
    
    // get rid of leading 0s
//    for(leadcoeff=0; leadcoeff < (int)coeffs.size() && fabs(coeffs[leadcoeff]) < eps; leadcoeff++)
//    {
//        deg--;
//    }
    
    // check for the zero polynomial
    if(deg < 0)
    {
        return Intervals(empty);
    }
    
    // check for constant polynomial
    if(deg == 0)
    {
        double val = poly.evaluate(0);
        if(val > 0)
        {
            return Intervals(all);
        }
        return Intervals(empty);
    }
    
    // nonconstant polynomial... rpoly time!!!
    assert(deg <= 6);
    double zeror[6];
    double zeroi[6];
    int numroots = rf.rpoly(&coeffs[leadcoeff], deg, zeror, zeroi);
    
    std::vector<double> roots;
    for(int i=0; i<numroots; i++)
        if( fabs(zeroi[i]) < eps )
            roots.push_back(zeror[i]);
    
    // no roots: check at 0
    if(roots.size() == 0)
    {
        double val = poly.evaluate(0);
        if(val > 0)
            return Intervals(all);
        return Intervals(empty);
    }
    
    std::sort(roots.begin(), roots.end());
    
    std::vector<Interval> intervals;
    
    for(int i=0; i<(int)roots.size(); i++)
    {
        if(i == 0)
        {
            //check poly on (-inf, r)
            
            double t = roots[i]-1;
            double val = poly.evaluate(t);
            if(val > 0)
            {
                intervals.push_back(Interval(-std::numeric_limits<double>::infinity(),
                                             roots[i]));
            }
        }
        if(i == (int)roots.size()-1)
        {
            //check poly on (r, inf)
            double t = roots[i]+1;
            double val = poly.evaluate(t);
            if(val > 0)
            {
                intervals.push_back(Interval(roots[i],
                                             std::numeric_limits<double>::infinity()));
            }
        }
        
        if(i < (int)roots.size()-1)
        {
            // check poly on (r, r+1)
            double t = 0.5*(roots[i]+roots[i+1]);
            double val = poly.evaluate(t);
            if(val > 0)
            {
                intervals.push_back(Interval(roots[i],
                                             roots[i+1]));
            }
        }
    }
    return Intervals(intervals);
}