From a53fade6e85394ef95dfaaa1c264149e85ea5451 Mon Sep 17 00:00:00 2001 From: Andrea Mattavelli Date: Wed, 22 Nov 2017 17:18:07 +0000 Subject: Added support for KLEE index-based array optimization --- include/klee/ArrayExprOptimizer.h | 70 +++++++++++++++++++++++++++ include/klee/ArrayExprRewriter.h | 43 +++++++++++++++++ include/klee/AssignmentGenerator.h | 58 +++++++++++++++++++++++ include/klee/util/ArrayExprVisitor.h | 91 ++++++++++++++++++++++++++++++++++++ 4 files changed, 262 insertions(+) create mode 100644 include/klee/ArrayExprOptimizer.h create mode 100644 include/klee/ArrayExprRewriter.h create mode 100644 include/klee/AssignmentGenerator.h create mode 100644 include/klee/util/ArrayExprVisitor.h (limited to 'include') diff --git a/include/klee/ArrayExprOptimizer.h b/include/klee/ArrayExprOptimizer.h new file mode 100644 index 00000000..c5eac212 --- /dev/null +++ b/include/klee/ArrayExprOptimizer.h @@ -0,0 +1,70 @@ +#ifndef KLEE_EXPROPTIMIZER_H +#define KLEE_EXPROPTIMIZER_H + +#include +#include +#include +#include +#include +#include + +#include "Expr.h" +#include "ExprBuilder.h" +#include "Constraints.h" +#include "Internal/Support/ErrorHandling.h" +#include "util/ArrayExprVisitor.h" +#include "util/Assignment.h" +#include "util/Ref.h" +#include "klee/AssignmentGenerator.h" +#include "klee/ExecutionState.h" + +#include "llvm/Support/TimeValue.h" +#include "klee/Internal/System/Time.h" + +#include + +#ifdef _LIBCPP_VERSION +#include +#include +#define unordered_map std::unordered_map +#define unordered_set std::unordered_set +#else +#include +#include +#define unordered_map std::tr1::unordered_map +#define unordered_set std::tr1::unordered_set +#endif + +namespace klee { + +enum ArrayOptimizationType { + NONE, + INDEX +}; + +extern llvm::cl::opt OptimizeArray; + +class Expr; +template class ref; +typedef std::map > > array2idx_ty; +typedef std::vector v_arr_ty; +typedef std::map, std::vector > > mapIndexOptimizedExpr_ty; + +class ExprOptimizer { +private: + unordered_map > cacheExprOptimized; + unordered_set cacheExprUnapplicable; + +public: + void optimizeExpr(const ref &e, ref &result, + bool valueOnly = false); + +private: + bool computeIndexes(array2idx_ty &arrays, const ref &e, + std::map, std::vector > > &idx_valIdx) + const; + +}; +} + +#endif diff --git a/include/klee/ArrayExprRewriter.h b/include/klee/ArrayExprRewriter.h new file mode 100644 index 00000000..dafcfef6 --- /dev/null +++ b/include/klee/ArrayExprRewriter.h @@ -0,0 +1,43 @@ +#ifndef LIB_EXPRREWRITER_H_ +#define LIB_EXPRREWRITER_H_ + +#include "klee/Expr.h" +#include "klee/CommandLine.h" +#include "klee/Constraints.h" +#include "klee/Internal/Support/ErrorHandling.h" +#include "klee/util/ArrayExprVisitor.h" +#include "klee/util/Assignment.h" +#include "klee/util/Ref.h" +#include "klee/AssignmentGenerator.h" + +namespace klee { + +typedef std::vector v_arr_ty; +typedef std::map > > array2idx_ty; +typedef std::map, std::vector > > mapIndexOptimizedExpr_ty; + +class ExprRewriter { +public: + static ref createOptExpr( + const ref &e, const array2idx_ty &arrays, + const std::map, std::vector > > &idx_valIdx); + +private: + static ref + rewrite(const ref &e, const array2idx_ty &arrays, + const std::map, std::vector > > &idx_valIdx); + + static ref + concatenateOrExpr(const std::vector >::const_iterator begin, + const std::vector >::const_iterator end); + + static ref createEqExpr(const ref &index, + const ref &valIndex); + + static ref createRangeExpr(const ref &index, + const ref &valStart, + const ref &valEnd); +}; +} + +#endif /* LIB_EXPRREWRITER_H_ */ diff --git a/include/klee/AssignmentGenerator.h b/include/klee/AssignmentGenerator.h new file mode 100644 index 00000000..835651b8 --- /dev/null +++ b/include/klee/AssignmentGenerator.h @@ -0,0 +1,58 @@ +#ifndef KLEE_ASSIGNMENTGENERATOR_H +#define KLEE_ASSIGNMENTGENERATOR_H + +#include +#include +#include +#include +#include +#include + +#include "Expr.h" +#include "Constraints.h" +#include "Internal/Support/ErrorHandling.h" +#include "util/ArrayExprVisitor.h" +#include "util/Assignment.h" +#include "util/Ref.h" + +#include "llvm/Support/TimeValue.h" +#include "klee/Internal/System/Time.h" + +namespace klee { + +class Expr; +template class ref; + +class AssignmentGenerator { +public: + static bool generatePartialAssignment(const ref &e, ref &val, + Assignment *&a); + +private: + static bool helperGenerateAssignment(const ref &e, ref &val, + Assignment *&a, Expr::Width width, + bool sign); + + static bool isReadExprAtOffset(ref e, const ReadExpr *base, + ref offset); + static ReadExpr *hasOrderedReads(ref e); + + static ref createSubExpr(const ref &l, ref &r); + static ref createAddExpr(const ref &l, ref &r); + static ref createMulExpr(const ref &l, ref &r); + static ref createDivExpr(const ref &l, ref &r, bool sign); + static ref createDivRem(const ref &l, ref &r, bool sign); + static ref createShlExpr(const ref &l, ref &r); + static ref createLShrExpr(const ref &l, ref &r); + static ref createAndExpr(const ref &l, ref &r); + static ref createExtractExpr(const ref &l, ref &r); + static ref createExtendExpr(const ref &l, ref &r); + + static std::vector getByteValue(ref &val); + static std::vector + getIndexedValue(const std::vector &c_val, ConstantExpr &index, + const unsigned int size); +}; +} + +#endif diff --git a/include/klee/util/ArrayExprVisitor.h b/include/klee/util/ArrayExprVisitor.h new file mode 100644 index 00000000..42eead84 --- /dev/null +++ b/include/klee/util/ArrayExprVisitor.h @@ -0,0 +1,91 @@ +#ifndef KLEE_ARRAYEXPRVISITOR_H_ +#define KLEE_ARRAYEXPRVISITOR_H_ + +#include "klee/util/ExprVisitor.h" +#include "klee/ExprBuilder.h" +#include "klee/CommandLine.h" + +#include +#ifdef _LIBCPP_VERSION +#include +#include +#define unordered_map std::unordered_map +#define unordered_set std::unordered_set +#else +#include +#include +#define unordered_map std::tr1::unordered_map +#define unordered_set std::tr1::unordered_set +#endif + +namespace klee { + +//------------------------------ HELPER FUNCTIONS ---------------------------// +class ArrayExprHelper { +private: + static bool isReadExprAtOffset(ref e, const ReadExpr *base, + ref offset); + +public: + static ReadExpr *hasOrderedReads(const ConcatExpr &ce); +}; + +//--------------------------- INDEX-BASED OPTIMIZATION-----------------------// +class ConstantArrayExprVisitor : public ExprVisitor { +private: + typedef std::map > > bindings_ty; + bindings_ty &arrays; + // Avoids adding the same index twice + unordered_set addedIndexes; + bool incompatible; + +protected: + Action visitConcat(const ConcatExpr &); + Action visitRead(const ReadExpr &); + +public: + ConstantArrayExprVisitor(bindings_ty &_arrays) + : arrays(_arrays), incompatible(false) {} + inline bool isIncompatible() { return incompatible; } +}; + +class IndexCompatibilityExprVisitor : public ExprVisitor { +private: + bool compatible; + bool inner; + +protected: + Action visitRead(const ReadExpr &); + Action visitURem(const URemExpr &); + Action visitSRem(const SRemExpr &); + Action visitOr(const OrExpr &); + +public: + IndexCompatibilityExprVisitor() : compatible(true), inner(false) {} + + inline bool isCompatible() { return compatible; } + inline bool hasInnerReads() { return inner; } +}; + +class IndexTransformationExprVisitor : public ExprVisitor { +private: + const Array *array; + Expr::Width width; + ref mul; + +protected: + Action visitConcat(const ConcatExpr &); + Action visitMul(const MulExpr &); + +public: + IndexTransformationExprVisitor(const Array *_array) + : array(_array), width(Expr::InvalidWidth) {} + + inline Expr::Width getWidth() { + return width == Expr::InvalidWidth ? Expr::Int8 : width; + } + inline ref getMul() { return mul; } +}; +} + +#endif -- cgit 1.4.1