2019-09-17 21:00:59 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 Cppcheck team.
|
2019-09-17 21:00:59 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-04-30 12:18:49 +02:00
|
|
|
/**
|
|
|
|
* @brief This is the ExprEngine component in Cppcheck. Its job is to
|
|
|
|
* convert the C/C++ code into expressions that the Z3 prover understands.
|
|
|
|
* We can then ask Z3 prover for instance if variable "x" can be 123 and
|
|
|
|
* the Z3 prover can tell us that.
|
|
|
|
*
|
|
|
|
* Overview
|
|
|
|
* ========
|
|
|
|
*
|
|
|
|
* The ExprEngine performs a "abstract execution" of each function.
|
|
|
|
* - ExprEngine performs "forward" analysis only. It starts at the top
|
|
|
|
* of the functions.
|
|
|
|
* - There is a abstract program state `Data::memory`.
|
|
|
|
* - The constraints are stored in the vector `Data::constraints`.
|
|
|
|
*
|
|
|
|
* Abstract program state
|
|
|
|
* ======================
|
|
|
|
*
|
|
|
|
* The map `Data::memory` contains the abstract values of all variables
|
|
|
|
* that are used in the current function scope.
|
|
|
|
*
|
|
|
|
* Use `--debug-bug-hunting --verbose` to dump out `Data::memory`.
|
|
|
|
* Example output:
|
|
|
|
* 2:5: { x=$1 y=$2}
|
|
|
|
* Explanation:
|
|
|
|
* At line 2, column 5: The memory has two variables. Variable x has the
|
|
|
|
* value $1. Variable y has the value $2.
|
|
|
|
*
|
|
|
|
* Different value names:
|
|
|
|
* - Typical abstract value has name that starts with "$". The number is
|
|
|
|
* just a incremented value.
|
|
|
|
* - If a variable has a known value then the concrete value is written.
|
|
|
|
* Example: `{ x=1 }`.
|
|
|
|
* - For an uninitialized value the output says "?". For example: `{ a=? }`
|
|
|
|
* - For buffers the output is something like `{ buf=($3,size=10,[:]=?,[$1]=$2) }`
|
|
|
|
* The first item "$3" is the name of the buffer value.
|
|
|
|
* The second item says that the size of this buffer is 10.
|
|
|
|
* After that comes `[index]=value` items that show what values buffer items have:
|
|
|
|
* `[:]=?` means that all items are uninitialized.
|
|
|
|
* `[$1]=$2` means that the buffer item at index "$1" has value "$2".
|
|
|
|
*
|
|
|
|
* Abstract execution
|
|
|
|
* ==================
|
|
|
|
*
|
|
|
|
* The function:
|
2020-06-20 23:00:39 +02:00
|
|
|
* static std::string execute(const Token *start, const Token *end, Data &data)
|
2020-04-30 12:18:49 +02:00
|
|
|
*
|
|
|
|
* Perform abstract execution of the code from `start` to `end`. The
|
|
|
|
* `data` is modified during the abstract execution.
|
|
|
|
*
|
|
|
|
* Each astTop token is executed. From that, operands are executed
|
|
|
|
* recursively in the "execute.." functions. The result of an operand is
|
|
|
|
* a abstract value.
|
|
|
|
*
|
|
|
|
* Branches
|
|
|
|
* --------
|
|
|
|
*
|
|
|
|
* Imagine:
|
|
|
|
* code1
|
|
|
|
* if (x > 0)
|
|
|
|
* code2
|
|
|
|
* else
|
|
|
|
* code3
|
|
|
|
* code4
|
|
|
|
*
|
|
|
|
* When "if" is reached.. the current `data` is branched into `thenData`
|
|
|
|
* and `elseData`.
|
|
|
|
* For "thenData" a constraint is added: x>0
|
|
|
|
* For "elseData" a constraint is added: !(x>0)
|
|
|
|
*
|
|
|
|
* Then analysis of `thenData` and `elseData` will continue separately,
|
|
|
|
* by recursive execution. The "code4" block will be analysed both with
|
|
|
|
* `thenData` and `elseData`.
|
|
|
|
*
|
|
|
|
* Z3
|
|
|
|
* ==
|
|
|
|
*
|
|
|
|
* The ExprEngine will not execute Z3 unless a check wants it to.
|
|
|
|
*
|
|
|
|
* The abstract values and all their constraints is added to a Z3 solver
|
|
|
|
* object and after that Z3 can tell us if some condition can be true.
|
|
|
|
*
|
|
|
|
* Z3 is a SMT solver:
|
|
|
|
* https://en.wikipedia.org/wiki/Satisfiability_modulo_theories
|
|
|
|
*
|
|
|
|
* In SMT:
|
|
|
|
* - all variables are "constant". A variable can not be changed or assigned.
|
|
|
|
* - There is no "execution". The solver considers all equations simultaneously.
|
|
|
|
*
|
|
|
|
* Simple example (TestExpr::expr6):
|
|
|
|
*
|
|
|
|
* void f(unsigned char x)
|
|
|
|
* {
|
|
|
|
* unsigned char y = 8 - x;\n"
|
|
|
|
* y > 1000;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* If a check wants to know if "y > 1000" can be true, ExprEngine will
|
|
|
|
* generate this Z3 input:
|
|
|
|
*
|
|
|
|
* (declare-fun $1 () Int)
|
|
|
|
* (assert (and (>= $1 0) (<= $1 255)))
|
|
|
|
* (assert (> (- 8 $1) 1000))
|
|
|
|
*
|
|
|
|
* A symbol "$1" is created.
|
|
|
|
* assert that "$1" is a value 0-255.
|
|
|
|
* assert that "8-$1" is greater than 1000.
|
|
|
|
*
|
|
|
|
* Z3 can now determine if these assertions are possible or not. In this
|
|
|
|
* case these assertions are not possible, there is no value for $1 between
|
|
|
|
* 0-255 that means that "8-$1" is greater than 1000.
|
|
|
|
*/
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
#include "exprengine.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
|
2019-09-19 20:18:39 +02:00
|
|
|
#include "astutils.h"
|
2020-06-19 13:16:48 +02:00
|
|
|
#include "bughuntingchecks.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errorlogger.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "library.h"
|
|
|
|
#include "mathlib.h"
|
|
|
|
#include "platform.h"
|
2019-09-17 21:00:59 +02:00
|
|
|
#include "settings.h"
|
|
|
|
#include "symboldatabase.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "token.h"
|
2019-09-17 21:00:59 +02:00
|
|
|
#include "tokenize.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "tokenlist.h"
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2021-04-03 21:30:50 +02:00
|
|
|
#include <cctype>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <climits>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <ctime>
|
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
2019-09-22 21:14:20 +02:00
|
|
|
#include <limits>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <list>
|
2019-09-17 21:00:59 +02:00
|
|
|
#include <memory>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <set>
|
2020-12-22 20:21:57 +01:00
|
|
|
#include <tuple>
|
2022-01-27 19:03:20 +01:00
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
#ifdef USE_Z3
|
|
|
|
#include <z3++.h>
|
2020-05-12 16:57:07 +02:00
|
|
|
#include <z3_version.h>
|
|
|
|
#define GET_VERSION_INT(A,B,C) ((A) * 10000 + (B) * 100 + (C))
|
|
|
|
#define Z3_VERSION_INT GET_VERSION_INT(Z3_MAJOR_VERSION, Z3_MINOR_VERSION, Z3_BUILD_NUMBER)
|
2019-10-02 17:59:04 +02:00
|
|
|
#endif
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2022-01-03 20:55:46 +01:00
|
|
|
const uint32_t MAX_BUFFER_SIZE = ~0U >> 1;
|
2022-01-03 17:10:01 +01:00
|
|
|
#define CONTRACT 1
|
2022-01-03 12:46:33 +01:00
|
|
|
|
2019-10-08 20:13:25 +02:00
|
|
|
namespace {
|
2020-06-28 17:39:14 +02:00
|
|
|
struct ExprEngineException {
|
|
|
|
ExprEngineException(const Token *tok, const std::string &what) : tok(tok), what(what) {}
|
2019-10-08 20:13:25 +02:00
|
|
|
const Token *tok;
|
|
|
|
const std::string what;
|
|
|
|
};
|
2020-07-14 11:22:42 +02:00
|
|
|
struct TerminateExpression {};
|
2019-10-08 20:13:25 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:00:39 +02:00
|
|
|
static std::string str(ExprEngine::ValuePtr val)
|
|
|
|
{
|
2020-12-14 12:32:30 +01:00
|
|
|
const char *typestr = "???UnknownValueType???";
|
2020-12-12 18:29:05 +01:00
|
|
|
switch (val->type) {
|
|
|
|
case ExprEngine::ValueType::AddressOfValue:
|
|
|
|
typestr = "AddressOfValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::ArrayValue:
|
|
|
|
typestr = "ArrayValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::UninitValue:
|
|
|
|
typestr = "UninitValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::IntRange:
|
|
|
|
typestr = "IntRange";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::FloatRange:
|
|
|
|
typestr = "FloatRange";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::ConditionalValue:
|
|
|
|
typestr = "ConditionalValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::StringLiteralValue:
|
|
|
|
typestr = "StringLiteralValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::StructValue:
|
|
|
|
typestr = "StructValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::BinOpResult:
|
|
|
|
typestr = "BinOpResult";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::IntegerTruncation:
|
|
|
|
typestr = "IntegerTruncation";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::FunctionCallArgumentValues:
|
|
|
|
typestr = "FunctionCallArgumentValues";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::BailoutValue:
|
|
|
|
typestr = "BailoutValue";
|
|
|
|
break;
|
2020-12-26 13:46:28 +01:00
|
|
|
}
|
2020-06-20 23:00:39 +02:00
|
|
|
|
2022-01-03 12:46:33 +01:00
|
|
|
return val->name + "=" + std::string(typestr) + "(" + val->getRange() + ")";
|
2020-06-20 23:00:39 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 08:49:32 +02:00
|
|
|
static size_t extfind(const std::string &str, const std::string &what, size_t pos)
|
|
|
|
{
|
|
|
|
int indent = 0;
|
|
|
|
for (; pos < str.size(); ++pos) {
|
|
|
|
if (indent <= 0 && str[pos] == what[0])
|
|
|
|
return pos;
|
|
|
|
else if (str[pos] == '\"') {
|
|
|
|
++pos;
|
|
|
|
while (pos < str.size()) {
|
|
|
|
if (str[pos] == '\"')
|
|
|
|
break;
|
|
|
|
if (pos == '\\')
|
|
|
|
++pos;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
} else if (str[pos] == '(')
|
|
|
|
++indent;
|
|
|
|
else if (str[pos] == ')')
|
|
|
|
--indent;
|
|
|
|
}
|
|
|
|
return std::string::npos;
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
std::string ExprEngine::str(int128_t value)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2019-09-17 22:28:36 +02:00
|
|
|
#ifdef __GNUC__
|
2019-09-17 21:00:59 +02:00
|
|
|
if (value == (int)value) {
|
|
|
|
ostr << (int) value;
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
if (value < 0) {
|
|
|
|
ostr << "-";
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t high = value >> 64;
|
|
|
|
uint64_t low = value;
|
|
|
|
if (high > 0)
|
|
|
|
ostr << "h" << std::hex << high << "l";
|
|
|
|
ostr << std::hex << low;
|
2019-09-17 22:28:36 +02:00
|
|
|
#else
|
|
|
|
ostr << value;
|
|
|
|
#endif
|
2019-09-17 21:00:59 +02:00
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr getValueRangeFromValueType(const std::string &name, const ValueType *vt, const cppcheck::Platform &platform);
|
|
|
|
|
|
|
|
namespace {
|
2019-09-19 20:18:39 +02:00
|
|
|
class TrackExecution {
|
|
|
|
public:
|
2020-06-28 10:52:50 +02:00
|
|
|
TrackExecution() : mDataIndexCounter(0), mAbortLine(-1) {}
|
2020-04-28 17:16:13 +02:00
|
|
|
|
2019-09-19 20:18:39 +02:00
|
|
|
int getNewDataIndex() {
|
2020-06-28 10:52:50 +02:00
|
|
|
return mDataIndexCounter++;
|
2019-09-19 20:18:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 20:48:33 +02:00
|
|
|
void symbolRange(const Token *tok, ExprEngine::ValuePtr value) {
|
2019-09-26 21:43:29 +02:00
|
|
|
if (!tok || !value)
|
2019-09-19 20:18:39 +02:00
|
|
|
return;
|
2020-04-28 17:16:13 +02:00
|
|
|
if (tok->index() == 0)
|
|
|
|
return;
|
2019-09-26 20:48:33 +02:00
|
|
|
const std::string &symbolicExpression = value->getSymbolicExpression();
|
2020-12-13 15:51:20 +01:00
|
|
|
if (std::isdigit(symbolicExpression[0]) || value->type == ExprEngine::ValueType::BinOpResult || value->type == ExprEngine::ValueType::UninitValue)
|
2019-09-26 20:48:33 +02:00
|
|
|
return;
|
2019-09-27 08:17:46 +02:00
|
|
|
if (mSymbols.find(symbolicExpression) != mSymbols.end())
|
2019-09-26 20:48:33 +02:00
|
|
|
return;
|
2019-09-27 08:17:46 +02:00
|
|
|
mSymbols.insert(symbolicExpression);
|
2020-12-12 18:29:05 +01:00
|
|
|
mMap[tok].push_back(str(value));
|
2019-09-19 20:18:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void state(const Token *tok, const std::string &s) {
|
2020-04-28 22:18:02 +02:00
|
|
|
mMap[tok].push_back(s);
|
2019-09-19 20:18:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void print(std::ostream &out) {
|
2019-09-19 20:18:39 +02:00
|
|
|
std::set<std::pair<int,int>> locations;
|
2020-04-28 22:18:02 +02:00
|
|
|
for (auto it : mMap) {
|
2019-09-20 07:04:58 +02:00
|
|
|
locations.insert(std::pair<int,int>(it.first->linenr(), it.first->column()));
|
2019-09-19 20:18:39 +02:00
|
|
|
}
|
|
|
|
for (const std::pair<int,int> &loc : locations) {
|
|
|
|
int lineNumber = loc.first;
|
|
|
|
int column = loc.second;
|
2020-04-28 22:18:02 +02:00
|
|
|
for (auto &it : mMap) {
|
2019-09-19 20:18:39 +02:00
|
|
|
const Token *tok = it.first;
|
|
|
|
if (lineNumber != tok->linenr())
|
|
|
|
continue;
|
2019-09-26 21:00:36 +02:00
|
|
|
if (column != tok->column())
|
|
|
|
continue;
|
2019-09-19 20:18:39 +02:00
|
|
|
const std::vector<std::string> &dumps = it.second;
|
|
|
|
for (const std::string &dump : dumps)
|
2019-09-29 15:00:54 +02:00
|
|
|
out << lineNumber << ":" << column << ": " << dump << "\n";
|
2019-09-19 20:18:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 21:00:53 +01:00
|
|
|
|
2021-09-15 20:28:58 +02:00
|
|
|
void report(std::ostream &out, const Scope *functionScope) const {
|
2019-12-22 21:00:53 +01:00
|
|
|
int linenr = -1;
|
|
|
|
std::string code;
|
|
|
|
for (const Token *tok = functionScope->bodyStart->next(); tok != functionScope->bodyEnd; tok = tok->next()) {
|
|
|
|
if (tok->linenr() > linenr) {
|
|
|
|
if (!code.empty())
|
|
|
|
out << getStatus(linenr) << " " << code << std::endl;
|
|
|
|
linenr = tok->linenr();
|
|
|
|
code.clear();
|
|
|
|
}
|
|
|
|
code += " " + tok->str();
|
|
|
|
}
|
|
|
|
|
|
|
|
out << getStatus(linenr) << " " << code << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAbortLine(int linenr) {
|
|
|
|
if (linenr > 0 && (mAbortLine == -1 || linenr < mAbortLine))
|
|
|
|
mAbortLine = linenr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addError(int linenr) {
|
|
|
|
mErrors.insert(linenr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isAllOk() const {
|
|
|
|
return mErrors.empty();
|
|
|
|
}
|
2020-04-28 22:09:01 +02:00
|
|
|
|
|
|
|
void addMissingContract(const std::string &f) {
|
|
|
|
mMissingContracts.insert(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<std::string> getMissingContracts() const {
|
|
|
|
return mMissingContracts;
|
|
|
|
}
|
2020-12-12 18:29:05 +01:00
|
|
|
|
|
|
|
void ifSplit(const Token *tok, unsigned int thenIndex, unsigned int elseIndex) {
|
2022-01-03 12:46:33 +01:00
|
|
|
mMap[tok].push_back("D" + std::to_string(thenIndex) + ": Split. Then:D" + std::to_string(thenIndex) + " Else:D" + std::to_string(elseIndex));
|
2020-12-12 18:29:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:18:39 +02:00
|
|
|
private:
|
2019-12-22 21:00:53 +01:00
|
|
|
const char *getStatus(int linenr) const {
|
|
|
|
if (mErrors.find(linenr) != mErrors.end())
|
|
|
|
return "ERROR";
|
|
|
|
if (mAbortLine > 0 && linenr >= mAbortLine)
|
|
|
|
return "--";
|
|
|
|
return "ok";
|
|
|
|
}
|
|
|
|
|
2020-04-28 22:18:02 +02:00
|
|
|
std::map<const Token *, std::vector<std::string>> mMap;
|
2020-04-28 17:16:13 +02:00
|
|
|
|
2020-06-28 10:52:50 +02:00
|
|
|
int mDataIndexCounter;
|
2019-12-22 21:00:53 +01:00
|
|
|
int mAbortLine;
|
2019-09-27 08:17:46 +02:00
|
|
|
std::set<std::string> mSymbols;
|
2019-12-22 21:00:53 +01:00
|
|
|
std::set<int> mErrors;
|
2020-04-28 22:09:01 +02:00
|
|
|
std::set<std::string> mMissingContracts;
|
2019-09-19 20:18:39 +02:00
|
|
|
};
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
class Data : public ExprEngine::DataBase {
|
2019-09-17 21:00:59 +02:00
|
|
|
public:
|
2020-04-28 17:16:13 +02:00
|
|
|
Data(int *symbolValueIndex, ErrorLogger *errorLogger, const Tokenizer *tokenizer, const Settings *settings, const std::string ¤tFunction, const std::vector<ExprEngine::Callback> &callbacks, TrackExecution *trackExecution)
|
2020-04-27 09:08:50 +02:00
|
|
|
: DataBase(currentFunction, settings)
|
2019-09-29 15:00:54 +02:00
|
|
|
, symbolValueIndex(symbolValueIndex)
|
2020-04-28 17:16:13 +02:00
|
|
|
, errorLogger(errorLogger)
|
2019-09-17 21:00:59 +02:00
|
|
|
, tokenizer(tokenizer)
|
2019-09-19 20:18:39 +02:00
|
|
|
, callbacks(callbacks)
|
2020-06-19 10:27:59 +02:00
|
|
|
, recursion(0)
|
2020-06-21 18:28:07 +02:00
|
|
|
, startTime(std::time(nullptr))
|
2019-09-19 20:18:39 +02:00
|
|
|
, mTrackExecution(trackExecution)
|
2019-09-20 06:12:20 +02:00
|
|
|
, mDataIndex(trackExecution->getNewDataIndex()) {}
|
2020-06-27 18:09:43 +02:00
|
|
|
|
|
|
|
Data(const Data &old)
|
|
|
|
: DataBase(old.currentFunction, old.settings)
|
2020-06-27 20:59:10 +02:00
|
|
|
, memory(old.memory)
|
2020-06-27 18:09:43 +02:00
|
|
|
, symbolValueIndex(old.symbolValueIndex)
|
|
|
|
, errorLogger(old.errorLogger)
|
|
|
|
, tokenizer(old.tokenizer)
|
|
|
|
, callbacks(old.callbacks)
|
2020-06-27 21:54:38 +02:00
|
|
|
, constraints(old.constraints)
|
2020-06-27 18:09:43 +02:00
|
|
|
, recursion(old.recursion)
|
|
|
|
, startTime(old.startTime)
|
|
|
|
, mTrackExecution(old.mTrackExecution)
|
|
|
|
, mDataIndex(mTrackExecution->getNewDataIndex()) {
|
|
|
|
for (auto &it: memory) {
|
|
|
|
if (!it.second)
|
|
|
|
continue;
|
|
|
|
if (auto oldValue = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(it.second))
|
|
|
|
it.second = std::make_shared<ExprEngine::ArrayValue>(getNewSymbolName(), *oldValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 17:51:32 +01:00
|
|
|
using Memory = std::map<nonneg int, ExprEngine::ValuePtr>;
|
2019-09-17 21:00:59 +02:00
|
|
|
Memory memory;
|
2019-09-19 20:18:39 +02:00
|
|
|
int * const symbolValueIndex;
|
2020-04-28 17:16:13 +02:00
|
|
|
ErrorLogger *errorLogger;
|
2019-09-19 20:18:39 +02:00
|
|
|
const Tokenizer * const tokenizer;
|
2019-09-17 21:00:59 +02:00
|
|
|
const std::vector<ExprEngine::Callback> &callbacks;
|
2019-10-03 08:48:05 +02:00
|
|
|
std::vector<ExprEngine::ValuePtr> constraints;
|
2020-06-19 10:27:59 +02:00
|
|
|
int recursion;
|
2020-06-21 18:28:07 +02:00
|
|
|
std::time_t startTime;
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
bool isC() const override {
|
2020-06-28 13:41:27 +02:00
|
|
|
return tokenizer->isC();
|
|
|
|
}
|
2022-02-10 23:02:24 +01:00
|
|
|
bool isCPP() const override {
|
2020-06-28 13:41:27 +02:00
|
|
|
return tokenizer->isCPP();
|
|
|
|
}
|
2020-06-27 22:11:12 +02:00
|
|
|
|
2022-01-03 17:10:01 +01:00
|
|
|
#ifdef CONTRACT
|
2021-08-07 20:51:18 +02:00
|
|
|
ExprEngine::ValuePtr executeContract(const Function *function, ExprEngine::ValuePtr (*executeExpression)(const Token*, Data&)) {
|
2020-04-28 17:16:13 +02:00
|
|
|
const auto it = settings->functionContracts.find(function->fullName());
|
2020-04-27 09:08:50 +02:00
|
|
|
if (it == settings->functionContracts.end())
|
2020-04-28 17:16:13 +02:00
|
|
|
return ExprEngine::ValuePtr();
|
2020-04-27 09:08:50 +02:00
|
|
|
const std::string &expects = it->second;
|
|
|
|
TokenList tokenList(settings);
|
|
|
|
std::istringstream istr(expects);
|
|
|
|
tokenList.createTokens(istr);
|
|
|
|
tokenList.createAst();
|
|
|
|
SymbolDatabase *symbolDatabase = const_cast<SymbolDatabase*>(tokenizer->getSymbolDatabase());
|
|
|
|
for (Token *tok = tokenList.front(); tok; tok = tok->next()) {
|
|
|
|
for (const Variable &arg: function->argumentList) {
|
|
|
|
if (arg.name() == tok->str()) {
|
|
|
|
tok->variable(&arg);
|
|
|
|
tok->varId(arg.declarationId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
symbolDatabase->setValueTypeInTokenList(false, tokenList.front());
|
2020-04-28 17:16:13 +02:00
|
|
|
return executeExpression(tokenList.front()->astTop(), *this);
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
void contractConstraints(const Function *function, ExprEngine::ValuePtr (*executeExpression)(const Token*, Data&)) {
|
2020-04-28 17:16:13 +02:00
|
|
|
auto value = executeContract(function, executeExpression);
|
|
|
|
if (value)
|
|
|
|
constraints.push_back(value);
|
2020-04-27 09:08:50 +02:00
|
|
|
}
|
2022-01-03 17:10:01 +01:00
|
|
|
#endif
|
2020-04-27 09:08:50 +02:00
|
|
|
|
2019-09-26 20:48:33 +02:00
|
|
|
void assignValue(const Token *tok, unsigned int varId, ExprEngine::ValuePtr value) {
|
2019-10-07 17:44:26 +02:00
|
|
|
if (varId == 0)
|
|
|
|
return;
|
2019-09-26 20:48:33 +02:00
|
|
|
mTrackExecution->symbolRange(tok, value);
|
2019-09-27 13:12:16 +02:00
|
|
|
if (value) {
|
|
|
|
if (auto arr = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(value)) {
|
2020-06-16 22:36:36 +02:00
|
|
|
for (const auto &dim: arr->size)
|
|
|
|
mTrackExecution->symbolRange(tok, dim);
|
2019-09-27 13:12:16 +02:00
|
|
|
for (const auto &indexAndValue: arr->data)
|
|
|
|
mTrackExecution->symbolRange(tok, indexAndValue.value);
|
2019-09-27 13:30:09 +02:00
|
|
|
} else if (auto s = std::dynamic_pointer_cast<ExprEngine::StructValue>(value)) {
|
|
|
|
for (const auto &m: s->member)
|
|
|
|
mTrackExecution->symbolRange(tok, m.second);
|
2019-09-27 13:12:16 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 20:48:33 +02:00
|
|
|
memory[varId] = value;
|
|
|
|
}
|
|
|
|
|
2019-09-26 21:35:29 +02:00
|
|
|
void assignStructMember(const Token *tok, ExprEngine::StructValue *structVal, const std::string &memberName, ExprEngine::ValuePtr value) {
|
|
|
|
mTrackExecution->symbolRange(tok, value);
|
|
|
|
structVal->member[memberName] = value;
|
|
|
|
}
|
|
|
|
|
2020-01-09 18:50:29 +01:00
|
|
|
void functionCall() {
|
|
|
|
// Remove values for global variables
|
|
|
|
const SymbolDatabase *symbolDatabase = tokenizer->getSymbolDatabase();
|
|
|
|
for (std::map<nonneg int, ExprEngine::ValuePtr>::iterator it = memory.begin(); it != memory.end();) {
|
|
|
|
unsigned int varid = it->first;
|
|
|
|
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
|
2020-01-15 18:35:55 +01:00
|
|
|
if (var && var->isGlobal())
|
2020-01-09 18:50:29 +01:00
|
|
|
it = memory.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string getNewSymbolName() final {
|
2019-09-17 21:00:59 +02:00
|
|
|
return "$" + std::to_string(++(*symbolValueIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ExprEngine::ArrayValue> getArrayValue(const Token *tok) {
|
|
|
|
const Memory::iterator it = memory.find(tok->varId());
|
|
|
|
if (it != memory.end())
|
|
|
|
return std::dynamic_pointer_cast<ExprEngine::ArrayValue>(it->second);
|
2020-01-02 06:16:36 +01:00
|
|
|
if (tok->varId() == 0 || !tok->variable())
|
2019-09-21 14:17:16 +02:00
|
|
|
return std::shared_ptr<ExprEngine::ArrayValue>();
|
2019-09-29 15:00:54 +02:00
|
|
|
auto val = std::make_shared<ExprEngine::ArrayValue>(this, tok->variable());
|
|
|
|
assignValue(tok, tok->varId(), val);
|
2019-09-21 14:17:16 +02:00
|
|
|
return val;
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:18:39 +02:00
|
|
|
ExprEngine::ValuePtr getValue(unsigned int varId, const ValueType *valueType, const Token *tok) {
|
2019-09-17 21:00:59 +02:00
|
|
|
const Memory::const_iterator it = memory.find(varId);
|
|
|
|
if (it != memory.end())
|
|
|
|
return it->second;
|
|
|
|
if (!valueType)
|
|
|
|
return ExprEngine::ValuePtr();
|
2020-10-08 17:24:19 +02:00
|
|
|
|
|
|
|
// constant value..
|
|
|
|
const Variable *var = tokenizer->getSymbolDatabase()->getVariableFromVarId(varId);
|
|
|
|
if (var && valueType->constness == 1 && Token::Match(var->nameToken(), "%var% =")) {
|
|
|
|
const Token *initExpr = var->nameToken()->next()->astOperand2();
|
|
|
|
if (initExpr && initExpr->hasKnownIntValue()) {
|
|
|
|
auto intval = initExpr->getKnownIntValue();
|
|
|
|
return std::make_shared<ExprEngine::IntRange>(std::to_string(intval), intval, intval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
ExprEngine::ValuePtr value = getValueRangeFromValueType(getNewSymbolName(), valueType, *settings);
|
2019-09-19 20:18:39 +02:00
|
|
|
if (value) {
|
2020-01-21 18:55:07 +01:00
|
|
|
if (tok->variable() && tok->variable()->nameToken())
|
|
|
|
addConstraints(value, tok->variable()->nameToken());
|
2019-09-26 20:48:33 +02:00
|
|
|
assignValue(tok, varId, value);
|
2019-09-19 20:18:39 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
return value;
|
|
|
|
}
|
2019-09-19 20:18:39 +02:00
|
|
|
|
2020-04-28 17:16:13 +02:00
|
|
|
void trackCheckContract(const Token *tok, const std::string &solverOutput) {
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "checkContract:{\n";
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
std::istringstream istr(solverOutput);
|
|
|
|
while (std::getline(istr, line))
|
|
|
|
os << " " << line << "\n";
|
|
|
|
|
|
|
|
os << "}";
|
|
|
|
|
|
|
|
mTrackExecution->state(tok, os.str());
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:18:39 +02:00
|
|
|
void trackProgramState(const Token *tok) {
|
|
|
|
if (memory.empty())
|
|
|
|
return;
|
|
|
|
const SymbolDatabase * const symbolDatabase = tokenizer->getSymbolDatabase();
|
|
|
|
std::ostringstream s;
|
2022-01-03 12:46:33 +01:00
|
|
|
s << "D" << mDataIndex << ":" << "memory:{";
|
2020-12-12 21:23:38 +01:00
|
|
|
bool first = true;
|
2019-09-19 20:18:39 +02:00
|
|
|
for (auto mem : memory) {
|
|
|
|
ExprEngine::ValuePtr value = mem.second;
|
2019-09-27 14:36:33 +02:00
|
|
|
const Variable *var = symbolDatabase->getVariableFromVarId(mem.first);
|
|
|
|
if (!var)
|
|
|
|
continue;
|
2020-12-12 21:23:38 +01:00
|
|
|
if (!first)
|
|
|
|
s << " ";
|
|
|
|
first = false;
|
|
|
|
s << var->name() << "=";
|
2019-09-20 21:27:51 +02:00
|
|
|
if (!value)
|
|
|
|
s << "(null)";
|
2019-09-26 10:07:31 +02:00
|
|
|
else if (value->name[0] == '$' && value->getSymbolicExpression() != value->name)
|
2019-09-26 10:03:58 +02:00
|
|
|
s << "(" << value->name << "," << value->getSymbolicExpression() << ")";
|
2019-09-19 20:18:39 +02:00
|
|
|
else
|
|
|
|
s << value->name;
|
|
|
|
}
|
|
|
|
s << "}";
|
2020-12-12 21:23:38 +01:00
|
|
|
|
|
|
|
if (!constraints.empty()) {
|
|
|
|
s << " constraints:{";
|
|
|
|
first = true;
|
|
|
|
for (auto constraint: constraints) {
|
|
|
|
if (!first)
|
|
|
|
s << " ";
|
|
|
|
first = false;
|
|
|
|
s << constraint->getSymbolicExpression();
|
|
|
|
}
|
|
|
|
s << "}";
|
|
|
|
}
|
2019-09-19 20:18:39 +02:00
|
|
|
mTrackExecution->state(tok, s.str());
|
|
|
|
}
|
2019-10-02 21:47:00 +02:00
|
|
|
|
2020-04-28 22:09:01 +02:00
|
|
|
void addMissingContract(const std::string &f) {
|
|
|
|
mTrackExecution->addMissingContract(f);
|
|
|
|
}
|
|
|
|
|
2019-10-02 21:47:00 +02:00
|
|
|
ExprEngine::ValuePtr notValue(ExprEngine::ValuePtr v) {
|
|
|
|
auto b = std::dynamic_pointer_cast<ExprEngine::BinOpResult>(v);
|
|
|
|
if (b) {
|
|
|
|
std::string binop;
|
|
|
|
if (b->binop == "==")
|
|
|
|
binop = "!=";
|
|
|
|
else if (b->binop == "!=")
|
|
|
|
binop = "==";
|
|
|
|
else if (b->binop == ">=")
|
|
|
|
binop = "<";
|
|
|
|
else if (b->binop == "<=")
|
|
|
|
binop = ">";
|
|
|
|
else if (b->binop == ">")
|
|
|
|
binop = "<=";
|
|
|
|
else if (b->binop == "<")
|
|
|
|
binop = ">=";
|
|
|
|
if (!binop.empty())
|
|
|
|
return std::make_shared<ExprEngine::BinOpResult>(binop, b->op1, b->op2);
|
|
|
|
}
|
2020-12-21 12:32:26 +01:00
|
|
|
if (std::dynamic_pointer_cast<ExprEngine::FloatRange>(v)) {
|
|
|
|
auto zero = std::make_shared<ExprEngine::FloatRange>("0.0", 0.0, 0.0);
|
|
|
|
return std::make_shared<ExprEngine::BinOpResult>("==", v, zero);
|
|
|
|
}
|
2019-10-02 21:47:00 +02:00
|
|
|
auto zero = std::make_shared<ExprEngine::IntRange>("0", 0, 0);
|
|
|
|
return std::make_shared<ExprEngine::BinOpResult>("==", v, zero);
|
|
|
|
}
|
|
|
|
|
2019-10-03 08:48:05 +02:00
|
|
|
void addConstraint(ExprEngine::ValuePtr condValue, bool trueCond) {
|
2019-10-06 14:47:34 +02:00
|
|
|
if (!condValue)
|
|
|
|
return;
|
2019-10-02 21:47:00 +02:00
|
|
|
if (trueCond)
|
2019-10-03 08:48:05 +02:00
|
|
|
constraints.push_back(condValue);
|
2019-10-02 21:47:00 +02:00
|
|
|
else
|
2019-10-03 08:48:05 +02:00
|
|
|
constraints.push_back(notValue(condValue));
|
2019-10-02 21:47:00 +02:00
|
|
|
}
|
2019-10-06 19:58:51 +02:00
|
|
|
|
|
|
|
void addConstraint(ExprEngine::ValuePtr lhsValue, ExprEngine::ValuePtr rhsValue, bool equals) {
|
|
|
|
if (!lhsValue || !rhsValue)
|
|
|
|
return;
|
|
|
|
constraints.push_back(std::make_shared<ExprEngine::BinOpResult>(equals?"==":"!=", lhsValue, rhsValue));
|
|
|
|
}
|
|
|
|
|
2019-12-24 21:14:14 +01:00
|
|
|
void addConstraints(ExprEngine::ValuePtr value, const Token *tok) {
|
2022-01-03 17:10:01 +01:00
|
|
|
#ifdef CONTRACT
|
2019-12-24 21:14:14 +01:00
|
|
|
MathLib::bigint low;
|
|
|
|
if (tok->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, &low))
|
|
|
|
addConstraint(std::make_shared<ExprEngine::BinOpResult>(">=", value, std::make_shared<ExprEngine::IntRange>(std::to_string(low), low, low)), true);
|
|
|
|
|
|
|
|
MathLib::bigint high;
|
|
|
|
if (tok->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, &high))
|
|
|
|
addConstraint(std::make_shared<ExprEngine::BinOpResult>("<=", value, std::make_shared<ExprEngine::IntRange>(std::to_string(high), high, high)), true);
|
2022-01-03 17:10:01 +01:00
|
|
|
#endif
|
2019-12-24 21:14:14 +01:00
|
|
|
}
|
|
|
|
|
2020-06-19 10:27:59 +02:00
|
|
|
void reportError(const Token *tok,
|
|
|
|
Severity::SeverityType severity,
|
|
|
|
const char id[],
|
|
|
|
const std::string &text,
|
|
|
|
CWE cwe,
|
|
|
|
bool inconclusive,
|
|
|
|
bool incomplete,
|
2022-02-10 23:02:24 +01:00
|
|
|
const std::string &functionName) override {
|
2020-06-19 10:27:59 +02:00
|
|
|
if (errorPath.empty())
|
|
|
|
mTrackExecution->addError(tok->linenr());
|
|
|
|
|
|
|
|
ErrorPath e = errorPath;
|
|
|
|
e.push_back(ErrorPathItem(tok, text));
|
2021-02-24 22:00:06 +01:00
|
|
|
ErrorMessage errmsg(e, &tokenizer->list, severity, id, text, cwe, inconclusive ? Certainty::inconclusive : Certainty::normal, true);
|
2020-06-19 10:27:59 +02:00
|
|
|
errmsg.incomplete = incomplete;
|
|
|
|
errmsg.function = functionName.empty() ? currentFunction : functionName;
|
|
|
|
errorLogger->reportErr(errmsg);
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:00:39 +02:00
|
|
|
std::string str() const {
|
|
|
|
std::ostringstream ret;
|
|
|
|
std::map<std::string, ExprEngine::ValuePtr> vars;
|
2020-08-23 20:53:44 +02:00
|
|
|
for (const auto &mem: memory) {
|
2020-06-21 08:49:32 +02:00
|
|
|
if (!mem.second)
|
|
|
|
continue;
|
2020-06-20 23:00:39 +02:00
|
|
|
const Variable *var = tokenizer->getSymbolDatabase()->getVariableFromVarId(mem.first);
|
|
|
|
if (var && var->isLocal())
|
|
|
|
continue;
|
|
|
|
ret << " @" << mem.first << ":" << mem.second->name;
|
|
|
|
getSymbols(vars, mem.second);
|
|
|
|
}
|
2020-08-23 20:53:44 +02:00
|
|
|
for (const auto &var: vars) {
|
2020-06-20 23:00:39 +02:00
|
|
|
if (var.second->name[0] == '$')
|
|
|
|
ret << " " << ::str(var.second);
|
|
|
|
}
|
2020-08-23 20:53:44 +02:00
|
|
|
for (const auto &c: constraints)
|
2020-06-20 23:00:39 +02:00
|
|
|
ret << " (" << c->getSymbolicExpression() << ")";
|
|
|
|
ret << std::endl;
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void load(const std::string &s) {
|
|
|
|
std::vector<ImportData> importData;
|
|
|
|
parsestr(s, &importData);
|
|
|
|
//simplify(importData);
|
|
|
|
|
|
|
|
if (importData.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::map<std::string, ExprEngine::ValuePtr> symbols;
|
|
|
|
for (auto mem: memory) {
|
|
|
|
getSymbols(symbols, mem.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: combined symbolvalue
|
|
|
|
std::map<int, std::string> combinedMemory;
|
|
|
|
for (const ImportData &d: importData) {
|
|
|
|
for (const auto &mem: d.mem) {
|
|
|
|
auto c = combinedMemory.find(mem.first);
|
|
|
|
if (c == combinedMemory.end()) {
|
|
|
|
combinedMemory[mem.first] = mem.second;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c->second == mem.second)
|
|
|
|
continue;
|
|
|
|
if (c->second == "?" || mem.second == "?")
|
|
|
|
c->second = "?";
|
|
|
|
else
|
|
|
|
c->second.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &mem: combinedMemory) {
|
|
|
|
int varid = mem.first;
|
|
|
|
const std::string &name = mem.second;
|
|
|
|
auto it = memory.find(varid);
|
2020-06-21 08:49:32 +02:00
|
|
|
if (it != memory.end() && it->second && it->second->name == name)
|
2020-06-20 23:00:39 +02:00
|
|
|
continue;
|
|
|
|
if (name.empty()) {
|
|
|
|
if (it != memory.end())
|
|
|
|
memory.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto it2 = symbols.find(name);
|
|
|
|
if (it2 != symbols.end()) {
|
|
|
|
memory[varid] = it2->second;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (name == "?") {
|
|
|
|
auto uninitValue = std::make_shared<ExprEngine::UninitValue>();
|
|
|
|
symbols[name] = uninitValue;
|
|
|
|
memory[varid] = uninitValue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (std::isdigit(name[0])) {
|
|
|
|
long long v = std::stoi(name);
|
|
|
|
auto intRange = std::make_shared<ExprEngine::IntRange>(name, v, v);
|
|
|
|
symbols[name] = intRange;
|
|
|
|
memory[varid] = intRange;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO: handle this value..
|
|
|
|
if (it != memory.end())
|
|
|
|
memory.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 18:29:05 +01:00
|
|
|
static void ifSplit(const Token *tok, const Data& thenData, const Data& elseData) {
|
|
|
|
thenData.mTrackExecution->ifSplit(tok, thenData.mDataIndex, elseData.mDataIndex);
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:18:39 +02:00
|
|
|
private:
|
|
|
|
TrackExecution * const mTrackExecution;
|
2019-09-20 06:12:20 +02:00
|
|
|
const int mDataIndex;
|
2020-06-20 23:00:39 +02:00
|
|
|
|
|
|
|
struct ImportData {
|
|
|
|
std::map<int, std::string> mem;
|
|
|
|
std::map<std::string, std::string> sym;
|
|
|
|
std::vector<std::string> constraints;
|
|
|
|
};
|
|
|
|
|
2021-01-09 20:32:38 +01:00
|
|
|
static void parsestr(const std::string &s, std::vector<ImportData> *importData) {
|
2020-06-20 23:00:39 +02:00
|
|
|
std::string line;
|
|
|
|
std::istringstream istr(s);
|
|
|
|
while (std::getline(istr, line)) {
|
|
|
|
if (line.empty())
|
|
|
|
continue;
|
|
|
|
line += " ";
|
|
|
|
ImportData d;
|
|
|
|
for (std::string::size_type pos = 0; pos < line.size();) {
|
|
|
|
pos = line.find_first_not_of(" ", pos);
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
break;
|
|
|
|
if (line[pos] == '@') {
|
|
|
|
++pos;
|
|
|
|
std::string::size_type colon = line.find(":", pos);
|
|
|
|
std::string::size_type end = line.find(" ", colon);
|
|
|
|
const std::string lhs = line.substr(pos, colon-pos);
|
|
|
|
pos = colon + 1;
|
|
|
|
const std::string rhs = line.substr(pos, end-pos);
|
|
|
|
d.mem[std::stoi(lhs)] = rhs;
|
|
|
|
pos = end;
|
|
|
|
} else if (line[pos] == '$') {
|
|
|
|
const std::string::size_type eq = line.find("=", pos);
|
|
|
|
const std::string lhs = line.substr(pos, eq-pos);
|
|
|
|
pos = eq + 1;
|
2020-06-21 08:49:32 +02:00
|
|
|
const std::string::size_type end = extfind(line, " ", pos);
|
2020-06-20 23:00:39 +02:00
|
|
|
const std::string rhs = line.substr(pos, end-pos);
|
|
|
|
pos = end;
|
|
|
|
d.sym[lhs] = rhs;
|
|
|
|
} else if (line[pos] == '(') {
|
2020-06-21 08:49:32 +02:00
|
|
|
const std::string::size_type end = extfind(line, " ", pos);
|
2020-06-20 23:00:39 +02:00
|
|
|
const std::string c = line.substr(pos, end-pos);
|
|
|
|
pos = end;
|
|
|
|
d.constraints.push_back(c);
|
2020-07-14 10:25:00 +02:00
|
|
|
} else {
|
|
|
|
throw ExprEngineException(nullptr, "Internal Error: Data::parsestr(), line:" + line);
|
2020-06-20 23:00:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
importData->push_back(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getSymbols(std::map<std::string, ExprEngine::ValuePtr> &symbols, ExprEngine::ValuePtr val) const {
|
|
|
|
if (!val)
|
|
|
|
return;
|
|
|
|
symbols[val->name] = val;
|
|
|
|
if (auto arrayValue = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(val)) {
|
|
|
|
for (auto sizeValue: arrayValue->size)
|
|
|
|
getSymbols(symbols, sizeValue);
|
|
|
|
for (auto indexValue: arrayValue->data) {
|
|
|
|
getSymbols(symbols, indexValue.index);
|
|
|
|
getSymbols(symbols, indexValue.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (auto structValue = std::dynamic_pointer_cast<ExprEngine::StructValue>(val)) {
|
|
|
|
for (auto memberNameValue: structValue->member)
|
|
|
|
getSymbols(symbols, memberNameValue.second);
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:17:06 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
// work around "undefined reference to `__muloti4'" linker error - see https://bugs.llvm.org/show_bug.cgi?id=16404
|
|
|
|
__attribute__((no_sanitize("undefined")))
|
|
|
|
#endif
|
2019-09-25 18:33:21 +02:00
|
|
|
static ExprEngine::ValuePtr simplifyValue(ExprEngine::ValuePtr origValue)
|
|
|
|
{
|
|
|
|
auto b = std::dynamic_pointer_cast<ExprEngine::BinOpResult>(origValue);
|
|
|
|
if (!b)
|
|
|
|
return origValue;
|
|
|
|
if (!b->op1 || !b->op2)
|
|
|
|
return origValue;
|
|
|
|
auto intRange1 = std::dynamic_pointer_cast<ExprEngine::IntRange>(b->op1);
|
2019-10-02 17:59:04 +02:00
|
|
|
auto intRange2 = std::dynamic_pointer_cast<ExprEngine::IntRange>(b->op2);
|
2019-09-25 18:33:21 +02:00
|
|
|
if (intRange1 && intRange2 && intRange1->minValue == intRange1->maxValue && intRange2->minValue == intRange2->maxValue) {
|
|
|
|
const std::string &binop = b->binop;
|
|
|
|
int128_t v;
|
|
|
|
if (binop == "+")
|
|
|
|
v = intRange1->minValue + intRange2->minValue;
|
|
|
|
else if (binop == "-")
|
|
|
|
v = intRange1->minValue - intRange2->minValue;
|
|
|
|
else if (binop == "*")
|
|
|
|
v = intRange1->minValue * intRange2->minValue;
|
|
|
|
else if (binop == "/" && intRange2->minValue != 0)
|
|
|
|
v = intRange1->minValue / intRange2->minValue;
|
|
|
|
else if (binop == "%" && intRange2->minValue != 0)
|
|
|
|
v = intRange1->minValue % intRange2->minValue;
|
|
|
|
else
|
|
|
|
return origValue;
|
|
|
|
return std::make_shared<ExprEngine::IntRange>(ExprEngine::str(v), v, v);
|
|
|
|
}
|
|
|
|
return origValue;
|
|
|
|
}
|
|
|
|
|
2019-09-28 16:16:36 +02:00
|
|
|
static ExprEngine::ValuePtr translateUninitValueToRange(ExprEngine::ValuePtr value, const ::ValueType *valueType, Data &data)
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
return value;
|
|
|
|
if (value->type == ExprEngine::ValueType::UninitValue) {
|
|
|
|
auto rangeValue = getValueRangeFromValueType(data.getNewSymbolName(), valueType, *data.settings);
|
|
|
|
if (rangeValue)
|
|
|
|
return rangeValue;
|
|
|
|
}
|
|
|
|
if (auto conditionalValue = std::dynamic_pointer_cast<ExprEngine::ConditionalValue>(value)) {
|
|
|
|
if (conditionalValue->values.size() == 1 && conditionalValue->values[0].second && conditionalValue->values[0].second->type == ExprEngine::ValueType::UninitValue) {
|
|
|
|
auto rangeValue = getValueRangeFromValueType(data.getNewSymbolName(), valueType, *data.settings);
|
|
|
|
if (rangeValue)
|
|
|
|
return rangeValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2019-09-25 18:33:21 +02:00
|
|
|
|
2019-09-28 19:28:12 +02:00
|
|
|
static int128_t truncateInt(int128_t value, int bits, char sign)
|
|
|
|
{
|
|
|
|
value = value & (((int128_t)1 << bits) - 1);
|
|
|
|
// Sign extension
|
|
|
|
if (sign == 's' && value & (1ULL << (bits - 1)))
|
|
|
|
value |= ~(((int128_t)1 << bits) - 1);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-12-29 16:26:11 +01:00
|
|
|
ExprEngine::ArrayValue::ArrayValue(const std::string &name, ExprEngine::ValuePtr size, ExprEngine::ValuePtr value, bool pointer, bool nullPointer, bool uninitPointer)
|
2019-09-26 10:03:58 +02:00
|
|
|
: Value(name, ExprEngine::ValueType::ArrayValue)
|
2019-12-29 16:26:11 +01:00
|
|
|
, pointer(pointer), nullPointer(nullPointer), uninitPointer(uninitPointer)
|
2019-09-25 18:33:21 +02:00
|
|
|
{
|
2020-06-16 22:56:01 +02:00
|
|
|
this->size.push_back(size);
|
2019-09-25 18:33:21 +02:00
|
|
|
assign(ExprEngine::ValuePtr(), value);
|
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
ExprEngine::ArrayValue::ArrayValue(DataBase *data, const Variable *var)
|
|
|
|
: Value(data->getNewSymbolName(), ExprEngine::ValueType::ArrayValue)
|
2021-10-28 17:20:45 +02:00
|
|
|
, pointer(var && var->isPointer()), nullPointer(var && var->isPointer()), uninitPointer(var && var->isPointer())
|
2019-09-25 18:33:21 +02:00
|
|
|
{
|
|
|
|
if (var) {
|
|
|
|
for (const auto &dim : var->dimensions()) {
|
2020-06-16 22:36:36 +02:00
|
|
|
if (dim.known)
|
|
|
|
size.push_back(std::make_shared<ExprEngine::IntRange>(std::to_string(dim.num), dim.num, dim.num));
|
|
|
|
else
|
|
|
|
size.push_back(std::make_shared<ExprEngine::IntRange>(data->getNewSymbolName(), 1, ExprEngine::ArrayValue::MAXSIZE));
|
2019-09-25 18:33:21 +02:00
|
|
|
}
|
2020-06-16 22:36:36 +02:00
|
|
|
} else {
|
|
|
|
size.push_back(std::make_shared<ExprEngine::IntRange>(data->getNewSymbolName(), 1, ExprEngine::ArrayValue::MAXSIZE));
|
2019-09-25 18:33:21 +02:00
|
|
|
}
|
2020-06-16 22:36:36 +02:00
|
|
|
|
2020-12-15 07:11:13 +01:00
|
|
|
const Token *initToken = var ? var->nameToken() : nullptr;
|
2020-12-14 22:15:10 +01:00
|
|
|
while (initToken && initToken->str() != "=")
|
|
|
|
initToken = initToken->astParent();
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
ValuePtr val;
|
2020-12-14 22:15:10 +01:00
|
|
|
if (var && !var->isGlobal() && !var->isStatic() && !(var->isArgument() && var->isConst()) && !initToken)
|
2019-09-29 15:00:54 +02:00
|
|
|
val = std::make_shared<ExprEngine::UninitValue>();
|
2019-09-29 17:32:26 +02:00
|
|
|
else if (var && var->valueType()) {
|
|
|
|
::ValueType vt(*var->valueType());
|
|
|
|
vt.pointer = 0;
|
|
|
|
val = getValueRangeFromValueType(data->getNewSymbolName(), &vt, *data->settings);
|
2019-09-29 15:00:54 +02:00
|
|
|
}
|
|
|
|
assign(ExprEngine::ValuePtr(), val);
|
2019-09-25 18:33:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 18:09:43 +02:00
|
|
|
ExprEngine::ArrayValue::ArrayValue(const std::string &name, const ExprEngine::ArrayValue &arrayValue)
|
|
|
|
: Value(name, ExprEngine::ValueType::ArrayValue)
|
|
|
|
, pointer(arrayValue.pointer), nullPointer(arrayValue.nullPointer), uninitPointer(arrayValue.uninitPointer)
|
2020-06-27 20:59:10 +02:00
|
|
|
, data(arrayValue.data), size(arrayValue.size)
|
2021-08-07 20:51:18 +02:00
|
|
|
{}
|
2020-06-27 18:09:43 +02:00
|
|
|
|
|
|
|
|
2019-12-29 16:26:11 +01:00
|
|
|
std::string ExprEngine::ArrayValue::getRange() const
|
|
|
|
{
|
|
|
|
std::string r = getSymbolicExpression();
|
|
|
|
if (nullPointer)
|
|
|
|
r += std::string(r.empty() ? "" : ",") + "null";
|
|
|
|
if (uninitPointer)
|
|
|
|
r += std::string(r.empty() ? "" : ",") + "->?";
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void ExprEngine::ArrayValue::assign(ExprEngine::ValuePtr index, ExprEngine::ValuePtr value)
|
|
|
|
{
|
2019-09-25 18:33:21 +02:00
|
|
|
if (!index)
|
|
|
|
data.clear();
|
|
|
|
if (value) {
|
2020-12-16 19:06:20 +01:00
|
|
|
if (index) {
|
|
|
|
// Remove old item that will be "overwritten"
|
|
|
|
for (size_t i = 0; i < data.size(); ++i) {
|
|
|
|
if (data[i].index && data[i].index->name == index->name) {
|
|
|
|
data.erase(data.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 18:33:21 +02:00
|
|
|
ExprEngine::ArrayValue::IndexAndValue indexAndValue = {index, value};
|
|
|
|
data.push_back(indexAndValue);
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 22:22:16 +02:00
|
|
|
void ExprEngine::ArrayValue::clear()
|
|
|
|
{
|
2019-09-25 18:33:21 +02:00
|
|
|
data.clear();
|
|
|
|
ExprEngine::ArrayValue::IndexAndValue indexAndValue = {
|
|
|
|
ExprEngine::ValuePtr(), std::make_shared<ExprEngine::IntRange>("0", 0, 0)
|
|
|
|
};
|
|
|
|
data.push_back(indexAndValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEqual(ExprEngine::ValuePtr v1, ExprEngine::ValuePtr v2)
|
|
|
|
{
|
|
|
|
if (!v1 || !v2)
|
|
|
|
return !v1 && !v2;
|
2019-09-26 10:03:58 +02:00
|
|
|
return v1->name == v2->name;
|
2019-09-24 22:22:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 18:33:21 +02:00
|
|
|
static bool isNonOverlapping(ExprEngine::ValuePtr v1, ExprEngine::ValuePtr v2)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2019-09-25 18:33:21 +02:00
|
|
|
if (!v1 || !v2)
|
|
|
|
return false; // Don't know!
|
|
|
|
auto intRange1 = std::dynamic_pointer_cast<ExprEngine::IntRange>(v1);
|
|
|
|
auto intRange2 = std::dynamic_pointer_cast<ExprEngine::IntRange>(v2);
|
|
|
|
if (intRange1 && intRange2 && (intRange1->minValue > intRange2->maxValue || intRange1->maxValue < intRange2->maxValue))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:03:58 +02:00
|
|
|
ExprEngine::ConditionalValue::Vector ExprEngine::ArrayValue::read(ExprEngine::ValuePtr index) const
|
2019-09-25 18:33:21 +02:00
|
|
|
{
|
2019-09-26 10:03:58 +02:00
|
|
|
ExprEngine::ConditionalValue::Vector ret;
|
2019-10-04 17:58:00 +02:00
|
|
|
if (!index)
|
|
|
|
return ret;
|
2020-04-04 11:44:59 +02:00
|
|
|
for (const auto &indexAndValue : data) {
|
2019-10-08 20:13:25 +02:00
|
|
|
if (::isEqual(index, indexAndValue.index))
|
2019-09-25 18:33:21 +02:00
|
|
|
ret.clear();
|
|
|
|
if (isNonOverlapping(index, indexAndValue.index))
|
|
|
|
continue;
|
2019-09-26 10:03:58 +02:00
|
|
|
// Array contains string literal data...
|
|
|
|
if (!indexAndValue.index && indexAndValue.value->type == ExprEngine::ValueType::StringLiteralValue) {
|
2019-09-25 18:33:21 +02:00
|
|
|
auto stringLiteral = std::dynamic_pointer_cast<ExprEngine::StringLiteralValue>(indexAndValue.value);
|
|
|
|
if (!stringLiteral) {
|
2019-09-26 10:03:58 +02:00
|
|
|
ret.push_back(std::pair<ValuePtr,ValuePtr>(indexAndValue.index, std::make_shared<ExprEngine::IntRange>("", -128, 128)));
|
2019-09-25 18:33:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (auto i = std::dynamic_pointer_cast<ExprEngine::IntRange>(index)) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (i->minValue >= 0 && i->minValue == i->maxValue) {
|
2019-09-25 18:33:21 +02:00
|
|
|
int c = 0;
|
|
|
|
if (i->minValue < stringLiteral->size())
|
|
|
|
c = stringLiteral->string[i->minValue];
|
2019-09-26 10:03:58 +02:00
|
|
|
ret.push_back(std::pair<ValuePtr,ValuePtr>(indexAndValue.index, std::make_shared<ExprEngine::IntRange>(std::to_string(c), c, c)));
|
2019-09-25 18:33:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int cmin = 0, cmax = 0;
|
|
|
|
for (char c : stringLiteral->string) {
|
|
|
|
if (c < cmin)
|
|
|
|
cmin = c;
|
|
|
|
else if (c > cmax)
|
|
|
|
cmax = c;
|
|
|
|
}
|
2019-09-26 10:03:58 +02:00
|
|
|
ret.push_back(std::pair<ValuePtr,ValuePtr>(indexAndValue.index, std::make_shared<ExprEngine::IntRange>("", cmin, cmax)));
|
2019-09-25 18:33:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-09-29 21:19:21 +02:00
|
|
|
|
|
|
|
// Rename IntRange
|
|
|
|
if (auto i = std::dynamic_pointer_cast<ExprEngine::IntRange>(indexAndValue.value)) {
|
|
|
|
ret.push_back(std::pair<ValuePtr,ValuePtr>(indexAndValue.index, std::make_shared<ExprEngine::IntRange>(indexAndValue.value->name + ":" + index->name, i->minValue, i->maxValue)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:03:58 +02:00
|
|
|
ret.push_back(std::pair<ValuePtr,ValuePtr>(indexAndValue.index, indexAndValue.value));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
2019-09-26 10:03:58 +02:00
|
|
|
|
|
|
|
if (ret.size() == 1)
|
|
|
|
ret[0].first = ExprEngine::ValuePtr();
|
|
|
|
else if (ret.size() == 2 && !ret[0].first) {
|
|
|
|
ret[0].first = std::make_shared<ExprEngine::BinOpResult>("!=", index, ret[1].first);
|
|
|
|
ret[1].first = std::make_shared<ExprEngine::BinOpResult>("==", index, ret[1].first);
|
|
|
|
} else {
|
|
|
|
// FIXME!!
|
|
|
|
ret.clear();
|
|
|
|
}
|
|
|
|
|
2019-09-25 18:33:21 +02:00
|
|
|
return ret;
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 10:03:58 +02:00
|
|
|
std::string ExprEngine::ConditionalValue::getSymbolicExpression() const
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "{";
|
|
|
|
bool first = true;
|
|
|
|
for (auto condvalue : values) {
|
|
|
|
ValuePtr cond = condvalue.first;
|
|
|
|
ValuePtr value = condvalue.second;
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
ostr << ",";
|
|
|
|
first = false;
|
|
|
|
ostr << "{"
|
|
|
|
<< (cond ? cond->getSymbolicExpression() : std::string("(null)"))
|
|
|
|
<< ","
|
|
|
|
<< value->getSymbolicExpression()
|
|
|
|
<< "}";
|
|
|
|
}
|
|
|
|
ostr << "}";
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ExprEngine::ArrayValue::getSymbolicExpression() const
|
2019-09-21 14:17:16 +02:00
|
|
|
{
|
2019-09-25 18:33:21 +02:00
|
|
|
std::ostringstream ostr;
|
2020-06-16 22:36:36 +02:00
|
|
|
if (size.empty())
|
|
|
|
ostr << "(null)";
|
|
|
|
else {
|
|
|
|
for (const auto &dim: size)
|
|
|
|
ostr << "[" << (dim ? dim->name : std::string("(null)")) << "]";
|
|
|
|
}
|
2020-04-04 11:44:59 +02:00
|
|
|
for (const auto &indexAndValue : data) {
|
2019-09-25 18:33:21 +02:00
|
|
|
ostr << ",["
|
|
|
|
<< (!indexAndValue.index ? std::string(":") : indexAndValue.index->name)
|
2020-12-14 12:32:30 +01:00
|
|
|
<< "]=";
|
|
|
|
if (indexAndValue.value->type == ExprEngine::ValueType::StructValue)
|
|
|
|
ostr << "("
|
|
|
|
<< indexAndValue.value->name
|
|
|
|
<< ","
|
|
|
|
<< indexAndValue.value->getSymbolicExpression()
|
|
|
|
<< ")";
|
|
|
|
else
|
|
|
|
ostr << indexAndValue.value->name;
|
2019-09-21 14:17:16 +02:00
|
|
|
}
|
2019-09-25 18:33:21 +02:00
|
|
|
return ostr.str();
|
2019-09-21 14:17:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 19:39:12 +02:00
|
|
|
std::string ExprEngine::StructValue::getSymbolicExpression() const
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "{";
|
|
|
|
bool first = true;
|
|
|
|
for (const auto& m: member) {
|
|
|
|
const std::string &memberName = m.first;
|
|
|
|
auto memberValue = m.second;
|
|
|
|
if (!first)
|
|
|
|
ostr << ",";
|
|
|
|
first = false;
|
|
|
|
ostr << memberName << "=" << (memberValue ? memberValue->getSymbolicExpression() : std::string("(null)"));
|
|
|
|
}
|
|
|
|
ostr << "}";
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
std::string ExprEngine::IntegerTruncation::getSymbolicExpression() const
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2019-10-02 17:59:04 +02:00
|
|
|
return sign + std::to_string(bits) + "(" + inputValue->getSymbolicExpression() + ")";
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
#ifdef USE_Z3
|
2019-10-06 14:47:34 +02:00
|
|
|
|
2020-12-07 06:27:14 +01:00
|
|
|
class ExprData {
|
|
|
|
public:
|
2021-01-05 17:51:32 +01:00
|
|
|
using ValueExpr = std::map<std::string, z3::expr>;
|
|
|
|
using AssertionList = std::vector<z3::expr>;
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
class BailoutValueException : public ExprEngineException {
|
2020-12-07 06:27:14 +01:00
|
|
|
public:
|
|
|
|
BailoutValueException() : ExprEngineException(nullptr, "Incomplete analysis") {}
|
|
|
|
};
|
|
|
|
|
2019-10-05 16:33:40 +02:00
|
|
|
z3::context context;
|
2019-10-02 17:59:04 +02:00
|
|
|
ValueExpr valueExpr;
|
|
|
|
AssertionList assertionList;
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
void addAssertions(z3::solver &solver) const {
|
|
|
|
for (auto assertExpr : assertionList)
|
|
|
|
solver.add(assertExpr);
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 06:27:14 +01:00
|
|
|
void addConstraints(z3::solver &solver, const Data* data) {
|
|
|
|
for (auto constraint : data->constraints) {
|
|
|
|
try {
|
|
|
|
solver.add(getConstraintExpr(constraint));
|
2021-08-07 20:51:18 +02:00
|
|
|
} catch (const BailoutValueException &) {}
|
2020-12-07 06:27:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 12:53:59 +01:00
|
|
|
z3::expr addInt(const std::string &name, int128_t minValue, int128_t maxValue) {
|
|
|
|
z3::expr e = context.int_const(name.c_str());
|
|
|
|
valueExpr.emplace(name, e);
|
|
|
|
if (minValue >= INT_MIN && maxValue <= INT_MAX)
|
|
|
|
assertionList.push_back(e >= int(minValue) && e <= int(maxValue));
|
|
|
|
else if (maxValue <= INT_MAX)
|
|
|
|
assertionList.push_back(e <= int(maxValue));
|
|
|
|
else if (minValue >= INT_MIN)
|
|
|
|
assertionList.push_back(e >= int(minValue));
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:47:18 +01:00
|
|
|
z3::expr addFloat(const std::string &name) {
|
2020-12-22 08:17:14 +01:00
|
|
|
z3::expr e = z3_fp_const(name);
|
2019-12-30 19:47:18 +01:00
|
|
|
valueExpr.emplace(name, e);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
z3::expr getExpr(const ExprEngine::BinOpResult *b) {
|
|
|
|
auto op1 = getExpr(b->op1);
|
|
|
|
auto op2 = getExpr(b->op2);
|
|
|
|
|
2020-12-22 17:15:11 +01:00
|
|
|
// floating point promotion
|
|
|
|
if (b->binop != "&&" && b->binop != "||" && b->binop != "<<" && b->binop != ">>") {
|
|
|
|
if (z3_is_fp(op1) || z3_is_fp(op2)) {
|
|
|
|
z3_to_fp(op1);
|
|
|
|
z3_to_fp(op2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
if (b->binop == "+")
|
|
|
|
return op1 + op2;
|
|
|
|
if (b->binop == "-")
|
|
|
|
return op1 - op2;
|
|
|
|
if (b->binop == "*")
|
|
|
|
return op1 * op2;
|
|
|
|
if (b->binop == "/")
|
|
|
|
return op1 / op2;
|
|
|
|
if (b->binop == "%")
|
2020-05-12 17:01:16 +02:00
|
|
|
#if Z3_VERSION_INT >= GET_VERSION_INT(4,8,5)
|
2019-10-06 14:47:34 +02:00
|
|
|
return op1 % op2;
|
2020-01-16 18:59:28 +01:00
|
|
|
#else
|
|
|
|
return op1 - (op1 / op2) * op2;
|
2020-01-15 19:46:00 +01:00
|
|
|
#endif
|
2019-10-06 14:47:34 +02:00
|
|
|
if (b->binop == "==")
|
2019-10-09 20:17:26 +02:00
|
|
|
return int_expr(op1) == int_expr(op2);
|
2019-10-06 14:47:34 +02:00
|
|
|
if (b->binop == "!=")
|
|
|
|
return op1 != op2;
|
|
|
|
if (b->binop == ">=")
|
|
|
|
return op1 >= op2;
|
|
|
|
if (b->binop == "<=")
|
|
|
|
return op1 <= op2;
|
|
|
|
if (b->binop == ">")
|
|
|
|
return op1 > op2;
|
|
|
|
if (b->binop == "<")
|
|
|
|
return op1 < op2;
|
|
|
|
if (b->binop == "&&")
|
2019-10-09 20:17:26 +02:00
|
|
|
return bool_expr(op1) && bool_expr(op2);
|
2019-10-06 14:47:34 +02:00
|
|
|
if (b->binop == "||")
|
2019-10-09 20:17:26 +02:00
|
|
|
return bool_expr(op1) || bool_expr(op2);
|
2019-10-14 17:20:20 +02:00
|
|
|
if (b->binop == "<<")
|
|
|
|
return op1 * z3::pw(context.int_val(2), op2);
|
|
|
|
if (b->binop == ">>")
|
|
|
|
return op1 / z3::pw(context.int_val(2), op2);
|
2020-06-28 17:39:14 +02:00
|
|
|
throw ExprEngineException(nullptr, "Internal error: Unhandled operator " + b->binop);
|
2019-10-06 14:47:34 +02:00
|
|
|
}
|
2019-09-23 20:27:13 +02:00
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
z3::expr getExpr(ExprEngine::ValuePtr v) {
|
2019-10-09 22:16:30 +02:00
|
|
|
if (!v)
|
2020-06-28 17:39:14 +02:00
|
|
|
throw ExprEngineException(nullptr, "Can not solve expressions, operand value is null");
|
2020-12-07 06:27:14 +01:00
|
|
|
if (v->type == ExprEngine::ValueType::BailoutValue)
|
|
|
|
throw BailoutValueException();
|
2019-10-06 14:47:34 +02:00
|
|
|
if (auto intRange = std::dynamic_pointer_cast<ExprEngine::IntRange>(v)) {
|
|
|
|
if (intRange->name[0] != '$')
|
2020-12-22 08:09:12 +01:00
|
|
|
return z3_int_val(intRange->minValue);
|
2019-10-06 14:47:34 +02:00
|
|
|
auto it = valueExpr.find(v->name);
|
|
|
|
if (it != valueExpr.end())
|
|
|
|
return it->second;
|
2019-12-30 12:53:59 +01:00
|
|
|
return addInt(v->name, intRange->minValue, intRange->maxValue);
|
2019-10-06 14:47:34 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2019-12-30 19:47:18 +01:00
|
|
|
if (auto floatRange = std::dynamic_pointer_cast<ExprEngine::FloatRange>(v)) {
|
2020-12-21 13:23:08 +01:00
|
|
|
if (floatRange->name[0] != '$')
|
2020-12-22 08:09:12 +01:00
|
|
|
return z3_fp_val(floatRange->minValue, floatRange->name);
|
2020-12-21 13:23:08 +01:00
|
|
|
|
2019-12-30 19:47:18 +01:00
|
|
|
auto it = valueExpr.find(v->name);
|
|
|
|
if (it != valueExpr.end())
|
|
|
|
return it->second;
|
|
|
|
return addFloat(v->name);
|
|
|
|
}
|
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
if (auto b = std::dynamic_pointer_cast<ExprEngine::BinOpResult>(v)) {
|
|
|
|
return getExpr(b.get());
|
|
|
|
}
|
2019-09-22 21:14:20 +02:00
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
if (auto c = std::dynamic_pointer_cast<ExprEngine::ConditionalValue>(v)) {
|
2019-10-10 20:29:36 +02:00
|
|
|
if (c->values.empty())
|
2020-06-28 17:39:14 +02:00
|
|
|
throw ExprEngineException(nullptr, "ConditionalValue is empty");
|
2019-10-10 20:29:36 +02:00
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
if (c->values.size() == 1)
|
|
|
|
return getExpr(c->values[0].second);
|
2019-09-22 21:14:20 +02:00
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
return z3::ite(getExpr(c->values[1].first),
|
|
|
|
getExpr(c->values[1].second),
|
|
|
|
getExpr(c->values[0].second));
|
|
|
|
}
|
2019-10-03 20:16:06 +02:00
|
|
|
|
2019-10-06 17:43:30 +02:00
|
|
|
if (auto integerTruncation = std::dynamic_pointer_cast<ExprEngine::IntegerTruncation>(v)) {
|
|
|
|
return getExpr(integerTruncation->inputValue);
|
|
|
|
//return getExpr(integerTruncation->inputValue) & ((1 << integerTruncation->bits) - 1);
|
|
|
|
}
|
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
if (v->type == ExprEngine::ValueType::UninitValue)
|
|
|
|
return context.int_val(0);
|
2019-10-03 20:16:06 +02:00
|
|
|
|
2020-06-28 17:39:14 +02:00
|
|
|
throw ExprEngineException(nullptr, "Internal error: Unhandled value type");
|
2019-10-06 14:47:34 +02:00
|
|
|
}
|
2019-10-03 20:16:06 +02:00
|
|
|
|
2019-10-06 14:47:34 +02:00
|
|
|
z3::expr getConstraintExpr(ExprEngine::ValuePtr v) {
|
|
|
|
if (v->type == ExprEngine::ValueType::IntRange)
|
|
|
|
return (getExpr(v) != 0);
|
2020-05-27 19:37:07 +02:00
|
|
|
return bool_expr(getExpr(v));
|
2019-10-06 14:47:34 +02:00
|
|
|
}
|
2019-10-09 20:17:26 +02:00
|
|
|
|
|
|
|
z3::expr bool_expr(z3::expr e) {
|
|
|
|
if (e.is_bool())
|
|
|
|
return e;
|
2020-12-21 12:32:26 +01:00
|
|
|
|
|
|
|
// Workaround for z3 bug: https://github.com/Z3Prover/z3/issues/4905
|
2020-12-22 08:09:12 +01:00
|
|
|
if (z3_is_fp(e))
|
|
|
|
return e != z3_fp_val(0.0, "0.0");
|
2020-12-21 12:32:26 +01:00
|
|
|
|
2019-10-09 20:17:26 +02:00
|
|
|
return e != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
z3::expr int_expr(z3::expr e) {
|
|
|
|
if (e.is_bool())
|
|
|
|
return z3::ite(e, context.int_val(1), context.int_val(0));
|
|
|
|
return e;
|
|
|
|
}
|
2020-12-22 08:09:12 +01:00
|
|
|
|
|
|
|
// Wrapper functions for Z3 interface. Instead of having ifdefs embedded
|
|
|
|
// in the code we have wrapper functions with ifdefs. The code that use
|
|
|
|
// these will be cleaner and hopefully more robust.
|
|
|
|
|
|
|
|
z3::expr z3_fp_const(const std::string &name) {
|
|
|
|
return context.real_const(name.c_str());
|
|
|
|
}
|
|
|
|
|
2020-12-22 15:17:36 +01:00
|
|
|
z3::expr z3_fp_val(long double value, std::string name) {
|
2020-12-22 08:09:12 +01:00
|
|
|
(void)value;
|
2020-12-22 15:17:36 +01:00
|
|
|
while (name.size() > 1 && (name.back() == 'f' || name.back() == 'F' || name.back() == 'l' || name.back() == 'L'))
|
|
|
|
name.erase(name.size() - 1);
|
2020-12-22 08:09:12 +01:00
|
|
|
return context.real_val(name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool z3_is_fp(z3::expr e) const {
|
|
|
|
return e.is_real();
|
|
|
|
}
|
|
|
|
|
2020-12-22 17:15:11 +01:00
|
|
|
void z3_to_fp(z3::expr &e) {
|
|
|
|
if (e.is_int())
|
2020-12-22 18:05:21 +01:00
|
|
|
e = z3::to_real(e);
|
2020-12-22 21:30:15 +01:00
|
|
|
}
|
2020-12-22 18:05:21 +01:00
|
|
|
|
2020-12-22 17:15:11 +01:00
|
|
|
|
2020-12-22 08:17:14 +01:00
|
|
|
z3::expr z3_int_val(int128_t value) {
|
2020-12-22 08:09:12 +01:00
|
|
|
#if Z3_VERSION_INT >= GET_VERSION_INT(4,7,1)
|
|
|
|
return context.int_val(int64_t(value));
|
|
|
|
#else
|
|
|
|
return context.int_val((long long)(value));
|
|
|
|
#endif
|
|
|
|
}
|
2019-10-06 14:47:34 +02:00
|
|
|
};
|
2019-10-02 17:59:04 +02:00
|
|
|
#endif
|
2019-10-05 16:33:40 +02:00
|
|
|
|
2022-01-03 12:46:33 +01:00
|
|
|
bool ExprEngine::UninitValue::isUninit(const DataBase *dataBase) const {
|
|
|
|
const Data *data = dynamic_cast<const Data *>(dataBase);
|
|
|
|
if (data->constraints.empty())
|
|
|
|
return true;
|
|
|
|
#ifdef USE_Z3
|
|
|
|
// Check the value against the constraints
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
try {
|
|
|
|
exprData.addConstraints(solver, data);
|
|
|
|
exprData.addAssertions(solver);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// The value may or may not be uninitialized
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::IntRange::isEqual(const DataBase *dataBase, int value) const
|
2019-10-05 16:33:40 +02:00
|
|
|
{
|
|
|
|
if (value < minValue || value > maxValue)
|
|
|
|
return false;
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
const Data *data = dynamic_cast<const Data *>(dataBase);
|
2019-10-05 16:33:40 +02:00
|
|
|
if (data->constraints.empty())
|
|
|
|
return true;
|
|
|
|
#ifdef USE_Z3
|
|
|
|
// Check the value against the constraints
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
2019-10-06 18:26:40 +02:00
|
|
|
try {
|
2019-12-30 12:53:59 +01:00
|
|
|
z3::expr e = exprData.addInt(name, minValue, maxValue);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, data);
|
2019-12-30 12:53:59 +01:00
|
|
|
exprData.addAssertions(solver);
|
2019-10-06 18:26:40 +02:00
|
|
|
solver.add(e == value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
2019-10-09 09:42:00 +02:00
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
2019-10-06 18:26:40 +02:00
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-10-06 18:26:40 +02:00
|
|
|
}
|
2019-10-05 16:33:40 +02:00
|
|
|
#else
|
|
|
|
// The value may or may not be in range
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::IntRange::isGreaterThan(const DataBase *dataBase, int value) const
|
2019-12-26 15:39:08 +01:00
|
|
|
{
|
2020-01-21 20:19:51 +01:00
|
|
|
if (maxValue <= value)
|
2019-12-26 15:39:08 +01:00
|
|
|
return false;
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
const Data *data = dynamic_cast<const Data *>(dataBase);
|
2019-12-26 15:39:08 +01:00
|
|
|
if (data->constraints.empty())
|
|
|
|
return true;
|
|
|
|
#ifdef USE_Z3
|
|
|
|
// Check the value against the constraints
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
try {
|
2019-12-30 19:47:18 +01:00
|
|
|
z3::expr e = exprData.addInt(name, minValue, maxValue);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, data);
|
2019-12-30 12:53:59 +01:00
|
|
|
exprData.addAssertions(solver);
|
2019-12-26 15:39:08 +01:00
|
|
|
solver.add(e > value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-12-26 15:39:08 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// The value may or may not be in range
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::IntRange::isLessThan(const DataBase *dataBase, int value) const
|
2019-12-26 15:39:08 +01:00
|
|
|
{
|
2020-01-21 20:19:51 +01:00
|
|
|
if (minValue >= value)
|
2019-12-26 15:39:08 +01:00
|
|
|
return false;
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
const Data *data = dynamic_cast<const Data *>(dataBase);
|
2019-12-26 15:39:08 +01:00
|
|
|
if (data->constraints.empty())
|
|
|
|
return true;
|
|
|
|
#ifdef USE_Z3
|
|
|
|
// Check the value against the constraints
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
try {
|
2019-12-30 19:47:18 +01:00
|
|
|
z3::expr e = exprData.addInt(name, minValue, maxValue);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, data);
|
2019-12-30 12:53:59 +01:00
|
|
|
exprData.addAssertions(solver);
|
2019-12-26 15:39:08 +01:00
|
|
|
solver.add(e < value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-12-26 15:39:08 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// The value may or may not be in range
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::FloatRange::isEqual(const DataBase *dataBase, int value) const
|
2019-12-30 19:47:18 +01:00
|
|
|
{
|
2019-12-31 09:01:58 +01:00
|
|
|
if (MathLib::isFloat(name)) {
|
|
|
|
float f = MathLib::toDoubleNumber(name);
|
|
|
|
return value >= f - 0.00001 && value <= f + 0.00001;
|
|
|
|
}
|
2020-12-22 20:21:57 +01:00
|
|
|
const Data *data = dynamic_cast<const Data *>(dataBase);
|
2020-12-22 09:25:54 +01:00
|
|
|
if (data->constraints.empty())
|
|
|
|
return true;
|
2019-12-30 19:47:18 +01:00
|
|
|
#ifdef USE_Z3
|
|
|
|
// Check the value against the constraints
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
try {
|
|
|
|
z3::expr e = exprData.addFloat(name);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, data);
|
2019-12-30 19:47:18 +01:00
|
|
|
exprData.addAssertions(solver);
|
2020-12-21 12:32:26 +01:00
|
|
|
// Workaround for z3 bug: https://github.com/Z3Prover/z3/issues/4905
|
|
|
|
#if Z3_VERSION_INT >= GET_VERSION_INT(4,8,0)
|
|
|
|
z3::expr val_e = exprData.context.fpa_val(static_cast<double>(value));
|
|
|
|
#else
|
|
|
|
z3::expr val_e = exprData.context.real_val(value);
|
|
|
|
#endif // Z3_VERSION_INT
|
|
|
|
solver.add(e == val_e);
|
2019-12-30 19:47:18 +01:00
|
|
|
return solver.check() != z3::unsat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-12-30 19:47:18 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// The value may or may not be in range
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::FloatRange::isGreaterThan(const DataBase *dataBase, int value) const
|
2019-12-30 19:47:18 +01:00
|
|
|
{
|
|
|
|
if (value < minValue || value > maxValue)
|
|
|
|
return false;
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
const Data *data = dynamic_cast<const Data *>(dataBase);
|
2019-12-30 19:47:18 +01:00
|
|
|
if (data->constraints.empty())
|
|
|
|
return true;
|
2019-12-31 09:01:58 +01:00
|
|
|
if (MathLib::isFloat(name))
|
|
|
|
return value > MathLib::toDoubleNumber(name);
|
2019-12-30 19:47:18 +01:00
|
|
|
#ifdef USE_Z3
|
|
|
|
// Check the value against the constraints
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
try {
|
|
|
|
z3::expr e = exprData.addFloat(name);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, data);
|
2019-12-30 19:47:18 +01:00
|
|
|
exprData.addAssertions(solver);
|
|
|
|
solver.add(e > value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-12-30 19:47:18 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// The value may or may not be in range
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::FloatRange::isLessThan(const DataBase *dataBase, int value) const
|
2019-12-30 19:47:18 +01:00
|
|
|
{
|
|
|
|
if (value < minValue || value > maxValue)
|
|
|
|
return false;
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
const Data *data = dynamic_cast<const Data *>(dataBase);
|
2019-12-30 19:47:18 +01:00
|
|
|
if (data->constraints.empty())
|
|
|
|
return true;
|
2019-12-31 09:01:58 +01:00
|
|
|
if (MathLib::isFloat(name))
|
|
|
|
return value < MathLib::toDoubleNumber(name);
|
2019-12-30 19:47:18 +01:00
|
|
|
#ifdef USE_Z3
|
|
|
|
// Check the value against the constraints
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
try {
|
|
|
|
z3::expr e = exprData.addFloat(name);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, data);
|
2019-12-30 19:47:18 +01:00
|
|
|
exprData.addAssertions(solver);
|
|
|
|
solver.add(e < value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-12-30 19:47:18 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// The value may or may not be in range
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::BinOpResult::isEqual(const ExprEngine::DataBase *dataBase, int value) const
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2019-10-02 17:59:04 +02:00
|
|
|
#ifdef USE_Z3
|
2020-12-12 17:33:10 +01:00
|
|
|
try {
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
z3::expr e = exprData.getExpr(this);
|
|
|
|
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
|
|
|
exprData.addAssertions(solver);
|
|
|
|
solver.add(exprData.int_expr(e) == value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3:" << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
}
|
2019-10-02 17:59:04 +02:00
|
|
|
#else
|
2019-10-02 21:47:00 +02:00
|
|
|
(void)dataBase;
|
2019-10-02 17:59:04 +02:00
|
|
|
(void)value;
|
|
|
|
return false;
|
|
|
|
#endif
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::BinOpResult::isGreaterThan(const ExprEngine::DataBase *dataBase, int value) const
|
2019-10-08 20:13:25 +02:00
|
|
|
{
|
|
|
|
#ifdef USE_Z3
|
2019-10-09 09:42:00 +02:00
|
|
|
try {
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
z3::expr e = exprData.getExpr(this);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
2019-12-30 12:53:59 +01:00
|
|
|
exprData.addAssertions(solver);
|
2019-10-09 09:42:00 +02:00
|
|
|
solver.add(e > value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3:" << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-10-09 09:42:00 +02:00
|
|
|
}
|
2019-10-08 20:13:25 +02:00
|
|
|
#else
|
|
|
|
(void)dataBase;
|
|
|
|
(void)value;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::BinOpResult::isLessThan(const ExprEngine::DataBase *dataBase, int value) const
|
2019-10-08 20:13:25 +02:00
|
|
|
{
|
|
|
|
#ifdef USE_Z3
|
2019-10-09 09:42:00 +02:00
|
|
|
try {
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
z3::expr e = exprData.getExpr(this);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
2019-12-30 12:53:59 +01:00
|
|
|
exprData.addAssertions(solver);
|
2019-10-09 09:42:00 +02:00
|
|
|
solver.add(e < value);
|
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3:" << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2019-10-09 09:42:00 +02:00
|
|
|
}
|
2019-10-08 20:13:25 +02:00
|
|
|
#else
|
|
|
|
(void)dataBase;
|
|
|
|
(void)value;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool ExprEngine::BinOpResult::isTrue(const ExprEngine::DataBase *dataBase) const
|
2020-12-05 11:47:57 +01:00
|
|
|
{
|
|
|
|
#ifdef USE_Z3
|
|
|
|
try {
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
z3::expr e = exprData.getExpr(this);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
2020-12-05 11:47:57 +01:00
|
|
|
exprData.addAssertions(solver);
|
2020-12-12 15:22:33 +01:00
|
|
|
solver.add(exprData.int_expr(e) != 0);
|
2020-12-05 11:47:57 +01:00
|
|
|
return solver.check() == z3::sat;
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3:" << exception << std::endl;
|
|
|
|
return true; // Safe option is to return true
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
return true; // Safe option is to return true
|
|
|
|
} catch (const ExprEngineException &) {
|
|
|
|
return true; // Safe option is to return true
|
2020-12-05 11:47:57 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)dataBase;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-10-02 21:47:00 +02:00
|
|
|
std::string ExprEngine::BinOpResult::getExpr(ExprEngine::DataBase *dataBase) const
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2019-10-02 17:59:04 +02:00
|
|
|
#ifdef USE_Z3
|
2019-10-09 20:17:26 +02:00
|
|
|
try {
|
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
z3::expr e = exprData.getExpr(this);
|
2020-12-07 06:27:14 +01:00
|
|
|
exprData.addConstraints(solver, dynamic_cast<const Data *>(dataBase));
|
2019-12-30 12:53:59 +01:00
|
|
|
exprData.addAssertions(solver);
|
2019-10-09 20:17:26 +02:00
|
|
|
solver.add(e);
|
|
|
|
std::ostringstream os;
|
|
|
|
os << solver;
|
|
|
|
switch (solver.check()) {
|
|
|
|
case z3::sat:
|
2020-05-08 12:21:10 +02:00
|
|
|
os << "\nz3::sat\n";
|
2019-10-09 20:17:26 +02:00
|
|
|
break;
|
|
|
|
case z3::unsat:
|
2020-05-08 12:21:10 +02:00
|
|
|
os << "\nz3::unsat\n";
|
2019-10-09 20:17:26 +02:00
|
|
|
break;
|
|
|
|
case z3::unknown:
|
2020-05-08 12:21:10 +02:00
|
|
|
os << "\nz3::unknown\n";
|
2019-10-09 20:17:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::ostringstream os;
|
2020-05-08 12:21:10 +02:00
|
|
|
os << "\nz3:" << exception << "\n";
|
2019-10-09 20:17:26 +02:00
|
|
|
return os.str();
|
2019-10-09 11:24:57 +02:00
|
|
|
}
|
2019-10-02 17:59:04 +02:00
|
|
|
#else
|
2019-10-02 21:47:00 +02:00
|
|
|
(void)dataBase;
|
2019-10-02 17:59:04 +02:00
|
|
|
return "";
|
|
|
|
#endif
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
// Todo: This is taken from ValueFlow and modified.. we should reuse it
|
2019-09-23 20:27:13 +02:00
|
|
|
static int getIntBitsFromValueType(const ValueType *vt, const cppcheck::Platform &platform)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2019-09-23 20:27:13 +02:00
|
|
|
if (!vt)
|
|
|
|
return 0;
|
2019-09-17 21:00:59 +02:00
|
|
|
|
|
|
|
switch (vt->type) {
|
|
|
|
case ValueType::Type::BOOL:
|
2019-09-23 20:27:13 +02:00
|
|
|
return 1;
|
2019-09-17 21:00:59 +02:00
|
|
|
case ValueType::Type::CHAR:
|
2019-09-23 20:27:13 +02:00
|
|
|
return platform.char_bit;
|
2019-09-17 21:00:59 +02:00
|
|
|
case ValueType::Type::SHORT:
|
2019-09-23 20:27:13 +02:00
|
|
|
return platform.short_bit;
|
2019-09-17 21:00:59 +02:00
|
|
|
case ValueType::Type::INT:
|
2019-09-23 20:27:13 +02:00
|
|
|
return platform.int_bit;
|
2019-09-17 21:00:59 +02:00
|
|
|
case ValueType::Type::LONG:
|
2019-09-23 20:27:13 +02:00
|
|
|
return platform.long_bit;
|
2019-09-17 21:00:59 +02:00
|
|
|
case ValueType::Type::LONGLONG:
|
2019-09-23 20:27:13 +02:00
|
|
|
return platform.long_long_bit;
|
2019-09-17 21:00:59 +02:00
|
|
|
default:
|
2019-09-23 20:27:13 +02:00
|
|
|
return 0;
|
2020-04-21 17:27:51 +02:00
|
|
|
}
|
2019-09-23 20:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr getValueRangeFromValueType(const std::string &name, const ValueType *vt, const cppcheck::Platform &platform)
|
|
|
|
{
|
|
|
|
if (!vt || !(vt->isIntegral() || vt->isFloat()) || vt->pointer)
|
|
|
|
return ExprEngine::ValuePtr();
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2019-09-23 20:27:13 +02:00
|
|
|
int bits = getIntBitsFromValueType(vt, platform);
|
2019-09-17 21:00:59 +02:00
|
|
|
if (bits == 1) {
|
|
|
|
return std::make_shared<ExprEngine::IntRange>(name, 0, 1);
|
2019-09-23 20:27:13 +02:00
|
|
|
} else if (bits > 1) {
|
2019-09-17 21:00:59 +02:00
|
|
|
if (vt->sign == ValueType::Sign::UNSIGNED) {
|
|
|
|
return std::make_shared<ExprEngine::IntRange>(name, 0, ((int128_t)1 << bits) - 1);
|
|
|
|
} else {
|
|
|
|
return std::make_shared<ExprEngine::IntRange>(name, -((int128_t)1 << (bits - 1)), ((int128_t)1 << (bits - 1)) - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:47:18 +01:00
|
|
|
if (vt->isFloat())
|
|
|
|
return std::make_shared<ExprEngine::FloatRange>(name, -std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
|
|
|
|
|
|
|
|
return ExprEngine::ValuePtr();
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 21:48:36 +02:00
|
|
|
static ExprEngine::ValuePtr getValueRangeFromValueType(const ValueType *valueType, Data &data)
|
|
|
|
{
|
2020-12-14 17:53:28 +01:00
|
|
|
if (valueType && valueType->pointer) {
|
2020-12-14 18:04:12 +01:00
|
|
|
ExprEngine::ValuePtr val = std::make_shared<ExprEngine::BailoutValue>();
|
2020-12-14 17:53:28 +01:00
|
|
|
auto bufferSize = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), 1, ExprEngine::ArrayValue::MAXSIZE);
|
|
|
|
return std::make_shared<ExprEngine::ArrayValue>(data.getNewSymbolName(), bufferSize, val, true, true, false);
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:48:36 +02:00
|
|
|
if (!valueType || valueType->pointer)
|
|
|
|
return ExprEngine::ValuePtr();
|
|
|
|
if (valueType->container) {
|
|
|
|
ExprEngine::ValuePtr value;
|
|
|
|
if (valueType->container->stdStringLike)
|
|
|
|
value = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), -128, 127);
|
|
|
|
else if (valueType->containerTypeToken) {
|
|
|
|
ValueType vt = ValueType::parseDecl(valueType->containerTypeToken, data.settings);
|
|
|
|
value = getValueRangeFromValueType(&vt, data);
|
|
|
|
} else
|
|
|
|
return ExprEngine::ValuePtr();
|
2020-10-04 11:21:00 +02:00
|
|
|
auto bufferSize = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), 0, ExprEngine::ArrayValue::MAXSIZE);
|
2020-06-21 21:48:36 +02:00
|
|
|
return std::make_shared<ExprEngine::ArrayValue>(data.getNewSymbolName(), bufferSize, value, false, false, false);
|
|
|
|
}
|
|
|
|
return getValueRangeFromValueType(data.getNewSymbolName(), valueType, *data.settings);
|
|
|
|
}
|
|
|
|
|
2019-10-02 21:47:00 +02:00
|
|
|
static void call(const std::vector<ExprEngine::Callback> &callbacks, const Token *tok, ExprEngine::ValuePtr value, Data *dataBase)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
|
|
|
if (value) {
|
|
|
|
for (ExprEngine::Callback f : callbacks) {
|
2019-10-09 22:16:30 +02:00
|
|
|
try {
|
|
|
|
f(tok, *value, dataBase);
|
2020-06-28 17:39:14 +02:00
|
|
|
} catch (const ExprEngineException &e) {
|
|
|
|
throw ExprEngineException(tok, e.what);
|
2019-10-09 22:16:30 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr executeExpression(const Token *tok, Data &data);
|
2020-04-28 17:16:13 +02:00
|
|
|
static ExprEngine::ValuePtr executeExpression1(const Token *tok, Data &data);
|
2020-06-20 23:00:39 +02:00
|
|
|
static std::string execute(const Token *start, const Token *end, Data &data);
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2020-06-16 22:36:36 +02:00
|
|
|
static ExprEngine::ValuePtr calculateArrayIndex(const Token *tok, Data &data, const ExprEngine::ArrayValue &arrayValue)
|
|
|
|
{
|
|
|
|
int nr = 1;
|
|
|
|
const Token *tok2 = tok;
|
|
|
|
while (Token::simpleMatch(tok2->astOperand1(), "[")) {
|
|
|
|
tok2 = tok2->astOperand1();
|
|
|
|
nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprEngine::ValuePtr totalIndex;
|
|
|
|
ExprEngine::ValuePtr dim;
|
|
|
|
while (Token::simpleMatch(tok, "[")) {
|
|
|
|
auto rawIndex = executeExpression(tok->astOperand2(), data);
|
|
|
|
|
|
|
|
ExprEngine::ValuePtr index;
|
|
|
|
if (dim)
|
|
|
|
index = simplifyValue(std::make_shared<ExprEngine::BinOpResult>("*", dim, rawIndex));
|
|
|
|
else
|
|
|
|
index = rawIndex;
|
|
|
|
|
|
|
|
if (!totalIndex)
|
|
|
|
totalIndex = index;
|
|
|
|
else
|
|
|
|
totalIndex = simplifyValue(std::make_shared<ExprEngine::BinOpResult>("+", index, totalIndex));
|
|
|
|
|
|
|
|
if (arrayValue.size.size() >= nr) {
|
|
|
|
if (arrayValue.size[nr-1]) {
|
|
|
|
if (!dim)
|
|
|
|
dim = arrayValue.size[nr-1];
|
|
|
|
else
|
|
|
|
dim = simplifyValue(std::make_shared<ExprEngine::BinOpResult>("*", dim, arrayValue.size[nr-1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nr--;
|
|
|
|
tok = tok->astOperand1();
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalIndex;
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
static ExprEngine::ValuePtr executeReturn(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
ExprEngine::ValuePtr retval = executeExpression(tok->astOperand1(), data);
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok, retval, &data);
|
2019-09-17 21:00:59 +02:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:27:13 +02:00
|
|
|
static ExprEngine::ValuePtr truncateValue(ExprEngine::ValuePtr val, const ValueType *valueType, Data &data)
|
|
|
|
{
|
2019-09-24 13:27:53 +02:00
|
|
|
if (!valueType)
|
|
|
|
return val;
|
2019-09-23 20:27:13 +02:00
|
|
|
if (valueType->pointer != 0)
|
|
|
|
return val;
|
|
|
|
if (!valueType->isIntegral())
|
|
|
|
return val; // TODO
|
|
|
|
|
|
|
|
int bits = getIntBitsFromValueType(valueType, *data.settings);
|
|
|
|
if (bits == 0)
|
|
|
|
// TODO
|
|
|
|
return val;
|
|
|
|
|
|
|
|
if (auto range = std::dynamic_pointer_cast<ExprEngine::IntRange>(val)) {
|
|
|
|
if (range->minValue == range->maxValue) {
|
2019-09-28 19:28:12 +02:00
|
|
|
int128_t newValue = truncateInt(range->minValue, bits, valueType->sign == ValueType::Sign::SIGNED ? 's' : 'u');
|
2019-09-23 20:27:13 +02:00
|
|
|
if (newValue == range->minValue)
|
|
|
|
return val;
|
|
|
|
return std::make_shared<ExprEngine::IntRange>(ExprEngine::str(newValue), newValue, newValue);
|
|
|
|
}
|
|
|
|
if (auto typeRange = getValueRangeFromValueType("", valueType, *data.settings)) {
|
|
|
|
auto typeIntRange = std::dynamic_pointer_cast<ExprEngine::IntRange>(typeRange);
|
|
|
|
if (typeIntRange) {
|
|
|
|
if (range->minValue >= typeIntRange->minValue && range->maxValue <= typeIntRange->maxValue)
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<ExprEngine::IntegerTruncation>(data.getNewSymbolName(), val, bits, valueType->sign == ValueType::Sign::SIGNED ? 's' : 'u');
|
|
|
|
}
|
|
|
|
// TODO
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:40:20 +02:00
|
|
|
static void assignExprValue(const Token *expr, ExprEngine::ValuePtr value, Data &data)
|
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return;
|
|
|
|
if (expr->varId() > 0) {
|
|
|
|
data.assignValue(expr, expr->varId(), value);
|
|
|
|
} else if (expr->str() == "[") {
|
|
|
|
// Find array token
|
|
|
|
const Token *arrayToken = expr;
|
|
|
|
while (Token::simpleMatch(arrayToken, "["))
|
|
|
|
arrayToken = arrayToken->astOperand1();
|
|
|
|
if (!arrayToken)
|
|
|
|
return;
|
|
|
|
if (auto arrayValue = data.getArrayValue(arrayToken)) {
|
|
|
|
// Is it array initialization?
|
|
|
|
if (arrayToken->variable() && arrayToken->variable()->nameToken() == arrayToken) {
|
|
|
|
if (value->type == ExprEngine::ValueType::StringLiteralValue)
|
|
|
|
arrayValue->assign(ExprEngine::ValuePtr(), value);
|
|
|
|
} else {
|
|
|
|
auto indexValue = calculateArrayIndex(expr, data, *arrayValue);
|
2020-06-28 17:28:40 +02:00
|
|
|
bool loopAssign = false;
|
|
|
|
if (auto loopValue = std::dynamic_pointer_cast<ExprEngine::IntRange>(indexValue)) {
|
|
|
|
if (loopValue->loopScope == expr->scope()) {
|
|
|
|
loopAssign = true;
|
|
|
|
for (auto i = loopValue->minValue; i <= loopValue->maxValue; ++i)
|
|
|
|
arrayValue->assign(std::make_shared<ExprEngine::IntRange>(ExprEngine::str(i), i, i), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!loopAssign)
|
|
|
|
arrayValue->assign(indexValue, value);
|
2020-06-19 20:40:20 +02:00
|
|
|
}
|
2020-12-05 20:39:54 +01:00
|
|
|
} else {
|
|
|
|
const Token * const indexToken = expr->astOperand2();
|
|
|
|
auto indexValue = executeExpression(indexToken, data);
|
|
|
|
call(data.callbacks, indexToken, indexValue, &data);
|
2020-06-19 20:40:20 +02:00
|
|
|
}
|
|
|
|
} else if (expr->isUnaryOp("*")) {
|
|
|
|
auto pval = executeExpression(expr->astOperand1(), data);
|
|
|
|
if (pval && pval->type == ExprEngine::ValueType::AddressOfValue) {
|
|
|
|
auto val = std::dynamic_pointer_cast<ExprEngine::AddressOfValue>(pval);
|
|
|
|
if (val)
|
|
|
|
data.assignValue(expr, val->varId, value);
|
2020-12-13 19:53:29 +01:00
|
|
|
} else if (pval && pval->type == ExprEngine::ValueType::ArrayValue) {
|
|
|
|
auto arrayValue = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(pval);
|
|
|
|
auto indexValue = std::make_shared<ExprEngine::IntRange>("0", 0, 0);
|
|
|
|
arrayValue->assign(indexValue, value);
|
2020-06-19 20:40:20 +02:00
|
|
|
} else if (pval && pval->type == ExprEngine::ValueType::BinOpResult) {
|
|
|
|
auto b = std::dynamic_pointer_cast<ExprEngine::BinOpResult>(pval);
|
|
|
|
if (b && b->binop == "+") {
|
|
|
|
std::shared_ptr<ExprEngine::ArrayValue> arr;
|
|
|
|
ExprEngine::ValuePtr offset;
|
|
|
|
if (b->op1->type == ExprEngine::ValueType::ArrayValue) {
|
|
|
|
arr = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(b->op1);
|
|
|
|
offset = b->op2;
|
|
|
|
} else {
|
|
|
|
arr = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(b->op2);
|
|
|
|
offset = b->op1;
|
|
|
|
}
|
|
|
|
if (arr && offset) {
|
|
|
|
arr->assign(offset, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (Token::Match(expr, ". %name%")) {
|
|
|
|
auto structVal = executeExpression(expr->astOperand1(), data);
|
|
|
|
if (structVal && structVal->type == ExprEngine::ValueType::StructValue)
|
|
|
|
data.assignStructMember(expr, &*std::static_pointer_cast<ExprEngine::StructValue>(structVal), expr->next()->str(), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
static ExprEngine::ValuePtr executeAssign(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
ExprEngine::ValuePtr rhsValue = executeExpression(tok->astOperand2(), data);
|
2019-10-23 18:42:40 +02:00
|
|
|
|
2020-04-30 21:05:34 +02:00
|
|
|
if (!rhsValue) {
|
2020-04-30 21:49:27 +02:00
|
|
|
const ValueType * const vt1 = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr;
|
|
|
|
const ValueType * const vt2 = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr;
|
|
|
|
|
2020-06-21 21:48:36 +02:00
|
|
|
rhsValue = getValueRangeFromValueType(vt1, data);
|
|
|
|
if (!rhsValue && vt2 && vt2->pointer == 0) {
|
|
|
|
rhsValue = getValueRangeFromValueType(vt2, data);
|
|
|
|
if (rhsValue)
|
|
|
|
call(data.callbacks, tok->astOperand2(), rhsValue, &data);
|
2020-04-30 21:05:34 +02:00
|
|
|
}
|
2020-12-14 17:53:28 +01:00
|
|
|
if (!rhsValue)
|
|
|
|
rhsValue = std::make_shared<ExprEngine::BailoutValue>();
|
2019-10-23 18:42:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-24 13:27:53 +02:00
|
|
|
ExprEngine::ValuePtr assignValue;
|
2019-09-21 21:15:51 +02:00
|
|
|
if (tok->str() == "=")
|
2019-09-24 13:27:53 +02:00
|
|
|
assignValue = rhsValue;
|
2019-09-21 21:15:51 +02:00
|
|
|
else {
|
|
|
|
// "+=" => "+"
|
|
|
|
std::string binop(tok->str());
|
|
|
|
binop = binop.substr(0, binop.size() - 1);
|
|
|
|
ExprEngine::ValuePtr lhsValue = executeExpression(tok->astOperand1(), data);
|
2019-09-25 18:33:21 +02:00
|
|
|
assignValue = simplifyValue(std::make_shared<ExprEngine::BinOpResult>(binop, lhsValue, rhsValue));
|
2019-09-21 21:15:51 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
|
|
|
|
const Token *lhsToken = tok->astOperand1();
|
2021-10-29 19:23:01 +02:00
|
|
|
if (lhsToken)
|
|
|
|
assignValue = truncateValue(assignValue, lhsToken->valueType(), data);
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok, assignValue, &data);
|
2019-09-24 13:27:53 +02:00
|
|
|
|
2020-06-19 20:40:20 +02:00
|
|
|
assignExprValue(lhsToken, assignValue, data);
|
|
|
|
|
2019-09-24 13:27:53 +02:00
|
|
|
return assignValue;
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 17:35:35 +02:00
|
|
|
static ExprEngine::ValuePtr executeIncDec(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
ExprEngine::ValuePtr beforeValue = executeExpression(tok->astOperand1(), data);
|
|
|
|
ExprEngine::ValuePtr assignValue = simplifyValue(std::make_shared<ExprEngine::BinOpResult>(tok->str().substr(0,1), beforeValue, std::make_shared<ExprEngine::IntRange>("1", 1, 1)));
|
2020-06-19 20:40:20 +02:00
|
|
|
assignExprValue(tok->astOperand1(), assignValue, data);
|
|
|
|
auto retVal = (precedes(tok, tok->astOperand1())) ? assignValue : beforeValue;
|
|
|
|
call(data.callbacks, tok, retVal, &data);
|
|
|
|
return retVal;
|
2020-06-19 17:35:35 +02:00
|
|
|
}
|
2020-04-28 17:16:13 +02:00
|
|
|
|
|
|
|
#ifdef USE_Z3
|
2020-04-28 22:29:16 +02:00
|
|
|
static void checkContract(Data &data, const Token *tok, const Function *function, const std::vector<ExprEngine::ValuePtr> &argValues)
|
|
|
|
{
|
2022-01-03 17:10:01 +01:00
|
|
|
#ifdef CONTRACT
|
2020-04-28 17:16:13 +02:00
|
|
|
ExprData exprData;
|
|
|
|
z3::solver solver(exprData.context);
|
|
|
|
try {
|
|
|
|
// Invert contract, we want to know if the contract might not be met
|
2020-12-07 06:27:14 +01:00
|
|
|
try {
|
|
|
|
solver.add(z3::ite(exprData.getConstraintExpr(data.executeContract(function, executeExpression1)), exprData.context.bool_val(false), exprData.context.bool_val(true)));
|
|
|
|
} catch (const ExprData::BailoutValueException &) {
|
|
|
|
throw ExprEngineException(tok, "Internal error: Bailout value used");
|
|
|
|
}
|
2020-04-28 17:16:13 +02:00
|
|
|
|
|
|
|
bool bailoutValue = false;
|
|
|
|
for (nonneg int i = 0; i < argValues.size(); ++i) {
|
|
|
|
const Variable *argvar = function->getArgumentVar(i);
|
|
|
|
if (!argvar || !argvar->nameToken())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ExprEngine::ValuePtr argValue = argValues[i];
|
|
|
|
if (!argValue || argValue->type == ExprEngine::ValueType::BailoutValue) {
|
|
|
|
bailoutValue = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argValue && argValue->type == ExprEngine::ValueType::IntRange) {
|
|
|
|
solver.add(exprData.getExpr(data.getValue(argvar->declarationId(), nullptr, nullptr)) == exprData.getExpr(argValue));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bailoutValue) {
|
|
|
|
for (auto constraint : data.constraints)
|
|
|
|
solver.add(exprData.getConstraintExpr(constraint));
|
|
|
|
|
|
|
|
exprData.addAssertions(solver);
|
|
|
|
|
|
|
|
// Log solver expressions for debugging/testing purposes
|
|
|
|
std::ostringstream os;
|
|
|
|
os << solver;
|
|
|
|
data.trackCheckContract(tok, os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bailoutValue || solver.check() == z3::sat) {
|
2020-06-19 10:27:59 +02:00
|
|
|
const char id[] = "bughuntingFunctionCall";
|
2020-04-28 17:16:13 +02:00
|
|
|
const auto contractIt = data.settings->functionContracts.find(function->fullName());
|
|
|
|
const std::string functionName = contractIt->first;
|
|
|
|
const std::string functionExpects = contractIt->second;
|
2020-06-19 10:27:59 +02:00
|
|
|
data.reportError(tok,
|
|
|
|
Severity::SeverityType::error,
|
|
|
|
id,
|
|
|
|
"Function '" + function->name() + "' is called, can not determine that its contract '" + functionExpects + "' is always met.",
|
|
|
|
CWE(0),
|
|
|
|
false,
|
|
|
|
bailoutValue,
|
|
|
|
functionName);
|
2020-04-28 17:16:13 +02:00
|
|
|
}
|
|
|
|
} catch (const z3::exception &exception) {
|
|
|
|
std::cerr << "z3: " << exception << std::endl;
|
2020-12-07 06:27:14 +01:00
|
|
|
} catch (const ExprEngineException &) {
|
2020-06-19 10:27:59 +02:00
|
|
|
const char id[] = "internalErrorInExprEngine";
|
2020-04-28 22:09:01 +02:00
|
|
|
const auto contractIt = data.settings->functionContracts.find(function->fullName());
|
2020-06-19 10:27:59 +02:00
|
|
|
const std::string functionName = contractIt->first;
|
2020-04-28 22:09:01 +02:00
|
|
|
const std::string functionExpects = contractIt->second;
|
2020-06-19 10:27:59 +02:00
|
|
|
data.reportError(tok,
|
|
|
|
Severity::SeverityType::error,
|
|
|
|
id,
|
|
|
|
"Function '" + function->name() + "' is called, can not determine that its contract '" + functionExpects + "' is always met.",
|
|
|
|
CWE(0),
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
functionName);
|
2020-04-28 17:16:13 +02:00
|
|
|
}
|
2022-01-03 17:10:01 +01:00
|
|
|
#endif
|
2020-04-28 17:16:13 +02:00
|
|
|
}
|
2020-04-28 22:29:16 +02:00
|
|
|
#endif
|
2020-04-28 17:16:13 +02:00
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
static ExprEngine::ValuePtr executeFunctionCall(const Token *tok, Data &data)
|
|
|
|
{
|
2020-01-15 15:23:58 +01:00
|
|
|
if (Token::simpleMatch(tok->previous(), "sizeof (")) {
|
|
|
|
ExprEngine::ValuePtr retVal;
|
|
|
|
if (tok->hasKnownIntValue()) {
|
|
|
|
const MathLib::bigint value = tok->getKnownIntValue();
|
|
|
|
retVal = std::make_shared<ExprEngine::IntRange>(std::to_string(value), value, value);
|
|
|
|
} else {
|
|
|
|
retVal = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), 1, 0x7fffffff);
|
|
|
|
}
|
|
|
|
call(data.callbacks, tok, retVal, &data);
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2020-06-21 15:15:43 +02:00
|
|
|
bool hasBody = tok->astOperand1()->function() && tok->astOperand1()->function()->hasBody();
|
|
|
|
if (hasBody) {
|
2020-12-13 09:33:40 +01:00
|
|
|
const Scope *functionScope = tok->scope();
|
|
|
|
while (functionScope->isExecutable() && functionScope->type != Scope::ScopeType::eFunction)
|
|
|
|
functionScope = functionScope->nestedIn;
|
|
|
|
if (functionScope == tok->astOperand1()->function()->functionScope)
|
|
|
|
hasBody = false;
|
2020-06-21 15:15:43 +02:00
|
|
|
for (const auto &errorPathItem: data.errorPath) {
|
|
|
|
if (errorPathItem.first == tok) {
|
|
|
|
hasBody = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 18:38:02 +02:00
|
|
|
|
2020-06-20 23:00:39 +02:00
|
|
|
const std::vector<const Token *> &argTokens = getArguments(tok);
|
2019-09-24 22:22:16 +02:00
|
|
|
std::vector<ExprEngine::ValuePtr> argValues;
|
2020-06-20 23:00:39 +02:00
|
|
|
for (const Token *argtok : argTokens) {
|
2020-06-19 18:38:02 +02:00
|
|
|
auto val = hasBody ? executeExpression1(argtok, data) : executeExpression(argtok, data);
|
2019-09-24 22:22:16 +02:00
|
|
|
argValues.push_back(val);
|
2020-06-19 18:38:02 +02:00
|
|
|
if (hasBody)
|
|
|
|
continue;
|
2019-09-24 19:53:33 +02:00
|
|
|
if (!argtok->valueType() || (argtok->valueType()->constness & 1) == 1)
|
|
|
|
continue;
|
2019-09-21 19:34:06 +02:00
|
|
|
if (auto arrayValue = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(val)) {
|
|
|
|
ValueType vt(*argtok->valueType());
|
|
|
|
vt.pointer = 0;
|
2020-06-21 21:48:36 +02:00
|
|
|
auto anyVal = getValueRangeFromValueType(&vt, data);
|
2019-09-25 18:33:21 +02:00
|
|
|
arrayValue->assign(ExprEngine::ValuePtr(), anyVal);
|
2019-09-24 19:53:33 +02:00
|
|
|
} else if (auto addressOf = std::dynamic_pointer_cast<ExprEngine::AddressOfValue>(val)) {
|
|
|
|
ValueType vt(*argtok->valueType());
|
|
|
|
vt.pointer = 0;
|
|
|
|
if (vt.isIntegral() && argtok->valueType()->pointer == 1)
|
2020-06-21 21:48:36 +02:00
|
|
|
data.assignValue(argtok, addressOf->varId, getValueRangeFromValueType(&vt, data));
|
2019-09-21 19:34:06 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 22:22:16 +02:00
|
|
|
|
2020-12-05 11:47:57 +01:00
|
|
|
call(data.callbacks, tok, std::make_shared<ExprEngine::FunctionCallArgumentValues>(argValues), &data);
|
|
|
|
|
2020-04-28 17:16:13 +02:00
|
|
|
if (tok->astOperand1()->function()) {
|
2020-06-19 10:27:59 +02:00
|
|
|
const Function *function = tok->astOperand1()->function();
|
|
|
|
const std::string &functionName = function->fullName();
|
2022-01-03 17:10:01 +01:00
|
|
|
#ifdef CONTRACT
|
2020-04-28 22:09:01 +02:00
|
|
|
const auto contractIt = data.settings->functionContracts.find(functionName);
|
|
|
|
if (contractIt != data.settings->functionContracts.end()) {
|
2020-04-28 22:29:16 +02:00
|
|
|
#ifdef USE_Z3
|
2020-06-19 10:27:59 +02:00
|
|
|
checkContract(data, tok, function, argValues);
|
2020-04-28 22:29:16 +02:00
|
|
|
#endif
|
2020-04-28 22:09:01 +02:00
|
|
|
} else if (!argValues.empty()) {
|
|
|
|
bool bailout = false;
|
2020-08-23 20:53:44 +02:00
|
|
|
for (const auto &v: argValues)
|
2020-04-28 22:09:01 +02:00
|
|
|
bailout |= (v && v->type == ExprEngine::ValueType::BailoutValue);
|
|
|
|
if (!bailout)
|
|
|
|
data.addMissingContract(functionName);
|
|
|
|
}
|
2022-01-03 17:10:01 +01:00
|
|
|
#endif
|
2020-06-19 10:27:59 +02:00
|
|
|
|
|
|
|
// Execute subfunction..
|
2020-06-21 15:15:43 +02:00
|
|
|
if (hasBody) {
|
2020-06-21 08:49:32 +02:00
|
|
|
const Scope * const functionScope = function->functionScope;
|
2020-06-19 10:27:59 +02:00
|
|
|
int argnr = 0;
|
2020-06-20 23:00:39 +02:00
|
|
|
std::map<const Token *, nonneg int> refs;
|
2020-06-19 10:27:59 +02:00
|
|
|
for (const Variable &arg: function->argumentList) {
|
2020-06-20 23:00:39 +02:00
|
|
|
if (argnr < argValues.size() && arg.declarationId() > 0) {
|
|
|
|
if (arg.isReference())
|
|
|
|
refs[argTokens[argnr]] = arg.declarationId();
|
|
|
|
else
|
2020-06-19 18:05:56 +02:00
|
|
|
argValues[argnr] = translateUninitValueToRange(argValues[argnr], arg.valueType(), data);
|
2020-06-19 10:27:59 +02:00
|
|
|
data.assignValue(function->functionScope->bodyStart, arg.declarationId(), argValues[argnr]);
|
2020-06-19 18:05:56 +02:00
|
|
|
}
|
2020-06-19 10:27:59 +02:00
|
|
|
// TODO default values!
|
|
|
|
argnr++;
|
|
|
|
}
|
|
|
|
data.contractConstraints(function, executeExpression1);
|
|
|
|
data.errorPath.push_back(ErrorPathItem(tok, "Calling " + function->name()));
|
2020-06-19 17:12:54 +02:00
|
|
|
try {
|
2020-06-20 23:00:39 +02:00
|
|
|
data.load(execute(functionScope->bodyStart, functionScope->bodyEnd, data));
|
|
|
|
for (auto ref: refs) {
|
|
|
|
auto v = data.getValue(ref.second, nullptr, nullptr);
|
|
|
|
assignExprValue(ref.first, v, data);
|
|
|
|
}
|
2020-06-28 17:39:14 +02:00
|
|
|
} catch (ExprEngineException &e) {
|
2020-06-21 15:15:43 +02:00
|
|
|
data.errorPath.pop_back();
|
2020-06-19 17:12:54 +02:00
|
|
|
e.tok = tok;
|
2020-06-22 12:26:41 +02:00
|
|
|
throw e;
|
2020-06-19 17:12:54 +02:00
|
|
|
}
|
2020-06-19 10:27:59 +02:00
|
|
|
data.errorPath.pop_back();
|
|
|
|
}
|
2020-04-28 17:16:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 21:20:59 +02:00
|
|
|
else if (const auto *f = data.settings->library.getAllocFuncInfo(tok->astOperand1())) {
|
|
|
|
if (!f->initData) {
|
|
|
|
const std::string name = data.getNewSymbolName();
|
2022-01-03 19:55:44 +01:00
|
|
|
auto size = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), 1, MAX_BUFFER_SIZE);
|
2020-06-28 21:20:59 +02:00
|
|
|
auto val = std::make_shared<ExprEngine::UninitValue>();
|
|
|
|
auto result = std::make_shared<ExprEngine::ArrayValue>(name, size, val, false, false, false);
|
|
|
|
call(data.callbacks, tok, result, &data);
|
|
|
|
data.functionCall();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:36:56 +02:00
|
|
|
auto result = getValueRangeFromValueType(tok->valueType(), data);
|
|
|
|
call(data.callbacks, tok, result, &data);
|
2020-01-09 18:50:29 +01:00
|
|
|
data.functionCall();
|
2020-06-28 21:36:56 +02:00
|
|
|
return result;
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr executeArrayIndex(const Token *tok, Data &data)
|
|
|
|
{
|
2020-09-05 21:09:11 +02:00
|
|
|
if (tok->tokType() == Token::eLambda)
|
|
|
|
throw ExprEngineException(tok, "FIXME: lambda");
|
2020-06-16 22:36:36 +02:00
|
|
|
const Token *tok2 = tok;
|
|
|
|
while (Token::simpleMatch(tok2->astOperand1(), "["))
|
|
|
|
tok2 = tok2->astOperand1();
|
|
|
|
auto arrayValue = data.getArrayValue(tok2->astOperand1());
|
2019-09-17 21:00:59 +02:00
|
|
|
if (arrayValue) {
|
2020-06-16 22:36:36 +02:00
|
|
|
auto indexValue = calculateArrayIndex(tok, data, *arrayValue);
|
2019-09-26 10:03:58 +02:00
|
|
|
auto conditionalValues = arrayValue->read(indexValue);
|
|
|
|
for (auto value: conditionalValues)
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok, value.second, &data);
|
2019-09-29 21:19:21 +02:00
|
|
|
if (conditionalValues.size() == 1 && !conditionalValues[0].first)
|
|
|
|
return conditionalValues[0].second;
|
2019-09-26 10:03:58 +02:00
|
|
|
return std::make_shared<ExprEngine::ConditionalValue>(data.getNewSymbolName(), conditionalValues);
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
2019-09-28 19:28:12 +02:00
|
|
|
|
|
|
|
// TODO: Pointer value..
|
|
|
|
executeExpression(tok->astOperand1(), data);
|
|
|
|
executeExpression(tok->astOperand2(), data);
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
return ExprEngine::ValuePtr();
|
|
|
|
}
|
|
|
|
|
2019-09-28 15:40:00 +02:00
|
|
|
static ExprEngine::ValuePtr executeCast(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
const Token *expr = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
|
|
|
|
|
|
|
|
auto val = executeExpression(expr, data);
|
|
|
|
|
|
|
|
if (expr->valueType() && expr->valueType()->type == ::ValueType::Type::VOID && expr->valueType()->pointer > 0) {
|
2020-01-09 21:25:23 +01:00
|
|
|
if (!tok->valueType() || expr->valueType()->pointer < tok->valueType()->pointer)
|
|
|
|
return std::make_shared<ExprEngine::UninitValue>();
|
|
|
|
|
2020-12-17 07:26:56 +01:00
|
|
|
auto range = std::make_shared<ExprEngine::UninitValue>();
|
2019-09-28 15:40:00 +02:00
|
|
|
|
|
|
|
if (tok->valueType()->pointer == 0)
|
|
|
|
return range;
|
|
|
|
|
2019-12-29 16:26:11 +01:00
|
|
|
bool uninitPointer = false, nullPointer = false;
|
|
|
|
if (val && val->type == ExprEngine::ValueType::ArrayValue) {
|
|
|
|
nullPointer = std::static_pointer_cast<ExprEngine::ArrayValue>(val)->nullPointer;
|
|
|
|
uninitPointer = std::static_pointer_cast<ExprEngine::ArrayValue>(val)->uninitPointer;
|
2019-09-28 15:40:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-03 12:46:33 +01:00
|
|
|
auto bufferSize = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), 1, MAX_BUFFER_SIZE);
|
2019-12-29 16:26:11 +01:00
|
|
|
return std::make_shared<ExprEngine::ArrayValue>(data.getNewSymbolName(), bufferSize, range, true, nullPointer, uninitPointer);
|
2019-09-28 15:40:00 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 19:17:36 +01:00
|
|
|
if (val) {
|
2019-09-28 15:40:00 +02:00
|
|
|
// TODO: Cast this..
|
2019-12-29 19:17:36 +01:00
|
|
|
call(data.callbacks, tok, val, &data);
|
2019-09-28 15:40:00 +02:00
|
|
|
return val;
|
2019-12-29 19:17:36 +01:00
|
|
|
}
|
2019-09-28 15:40:00 +02:00
|
|
|
|
2020-06-21 21:48:36 +02:00
|
|
|
val = getValueRangeFromValueType(tok->valueType(), data);
|
2019-12-29 19:17:36 +01:00
|
|
|
call(data.callbacks, tok, val, &data);
|
|
|
|
return val;
|
2019-09-28 15:40:00 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
static ExprEngine::ValuePtr executeDot(const Token *tok, Data &data)
|
|
|
|
{
|
2020-12-06 17:44:07 +01:00
|
|
|
if (!tok->astOperand1()) {
|
|
|
|
auto v = std::make_shared<ExprEngine::BailoutValue>();
|
2019-10-27 16:23:37 +01:00
|
|
|
call(data.callbacks, tok, v, &data);
|
|
|
|
return v;
|
|
|
|
}
|
2020-12-06 17:44:07 +01:00
|
|
|
std::shared_ptr<ExprEngine::StructValue> structValue = std::dynamic_pointer_cast<ExprEngine::StructValue>(executeExpression(tok->astOperand1(), data));
|
2019-09-29 08:26:09 +02:00
|
|
|
if (!structValue) {
|
|
|
|
if (tok->originalName() == "->") {
|
2019-12-29 16:26:11 +01:00
|
|
|
std::shared_ptr<ExprEngine::ArrayValue> pointerValue = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(data.getValue(tok->astOperand1()->varId(), nullptr, nullptr));
|
2019-12-29 18:42:35 +01:00
|
|
|
if (pointerValue && pointerValue->pointer && !pointerValue->data.empty()) {
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok->astOperand1(), pointerValue, &data);
|
2019-12-29 18:42:35 +01:00
|
|
|
auto indexValue = std::make_shared<ExprEngine::IntRange>("0", 0, 0);
|
|
|
|
ExprEngine::ValuePtr ret;
|
|
|
|
for (auto val: pointerValue->read(indexValue)) {
|
|
|
|
structValue = std::dynamic_pointer_cast<ExprEngine::StructValue>(val.second);
|
|
|
|
if (structValue) {
|
|
|
|
auto memberValue = structValue->getValueOfMember(tok->astOperand2()->str());
|
|
|
|
call(data.callbacks, tok, memberValue, &data);
|
|
|
|
if (!ret)
|
|
|
|
ret = memberValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2019-09-29 08:26:09 +02:00
|
|
|
} else {
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok->astOperand1(), data.getValue(tok->astOperand1()->varId(), nullptr, nullptr), &data);
|
2019-09-29 08:26:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 08:01:24 +02:00
|
|
|
|
|
|
|
auto v = getValueRangeFromValueType(tok->valueType(), data);
|
|
|
|
if (!v)
|
|
|
|
v = std::make_shared<ExprEngine::BailoutValue>();
|
|
|
|
call(data.callbacks, tok, v, &data);
|
|
|
|
return v;
|
2019-09-29 08:26:09 +02:00
|
|
|
}
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok->astOperand1(), structValue, &data);
|
2020-01-12 10:28:48 +01:00
|
|
|
ExprEngine::ValuePtr memberValue = structValue->getValueOfMember(tok->astOperand2()->str());
|
|
|
|
call(data.callbacks, tok, memberValue, &data);
|
|
|
|
return memberValue;
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 20:05:26 +02:00
|
|
|
static void streamReadSetValue(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
if (!tok || !tok->valueType())
|
|
|
|
return;
|
2022-01-03 12:46:33 +01:00
|
|
|
if (tok->varId() > 0 && tok->valueType()->pointer) {
|
|
|
|
const auto oldValue = data.getValue(tok->varId(), tok->valueType(), tok);
|
|
|
|
if (oldValue && (oldValue->isUninit(&data)))
|
|
|
|
call(data.callbacks, tok, oldValue, &data);
|
|
|
|
}
|
2020-06-21 21:48:36 +02:00
|
|
|
auto rangeValue = getValueRangeFromValueType(tok->valueType(), data);
|
2020-06-21 20:05:26 +02:00
|
|
|
if (rangeValue)
|
|
|
|
assignExprValue(tok, rangeValue, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr executeStreamRead(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
tok = tok->astOperand2();
|
|
|
|
while (Token::simpleMatch(tok, ">>")) {
|
|
|
|
streamReadSetValue(tok->astOperand1(), data);
|
|
|
|
tok = tok->astOperand2();
|
|
|
|
}
|
|
|
|
streamReadSetValue(tok, data);
|
|
|
|
return ExprEngine::ValuePtr();
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
static ExprEngine::ValuePtr executeBinaryOp(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
ExprEngine::ValuePtr v1 = executeExpression(tok->astOperand1(), data);
|
2019-12-24 15:51:45 +01:00
|
|
|
ExprEngine::ValuePtr v2;
|
2020-05-15 20:58:33 +02:00
|
|
|
|
2020-05-15 21:34:52 +02:00
|
|
|
if (tok->str() == "?") {
|
|
|
|
if (tok->astOperand1()->hasKnownIntValue()) {
|
|
|
|
if (tok->astOperand1()->getKnownIntValue())
|
|
|
|
v2 = executeExpression(tok->astOperand2()->astOperand1(), data);
|
|
|
|
else
|
|
|
|
v2 = executeExpression(tok->astOperand2()->astOperand2(), data);
|
|
|
|
call(data.callbacks, tok, v2, &data);
|
|
|
|
return v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data trueData(data);
|
|
|
|
trueData.addConstraint(v1, true);
|
|
|
|
auto trueValue = simplifyValue(executeExpression(tok->astOperand2()->astOperand1(), trueData));
|
|
|
|
|
|
|
|
Data falseData(data);
|
|
|
|
falseData.addConstraint(v1, false);
|
|
|
|
auto falseValue = simplifyValue(executeExpression(tok->astOperand2()->astOperand2(), falseData));
|
|
|
|
|
|
|
|
auto result = simplifyValue(std::make_shared<ExprEngine::BinOpResult>("?", v1, std::make_shared<ExprEngine::BinOpResult>(":", trueValue, falseValue)));
|
|
|
|
call(data.callbacks, tok, result, &data);
|
|
|
|
return result;
|
|
|
|
|
2020-05-15 20:58:33 +02:00
|
|
|
} else if (tok->str() == "&&" || tok->str() == "||") {
|
2019-12-24 15:51:45 +01:00
|
|
|
Data data2(data);
|
|
|
|
data2.addConstraint(v1, tok->str() == "&&");
|
|
|
|
v2 = executeExpression(tok->astOperand2(), data2);
|
|
|
|
} else {
|
|
|
|
v2 = executeExpression(tok->astOperand2(), data);
|
|
|
|
}
|
2020-05-15 20:58:33 +02:00
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
if (v1 && v2) {
|
2019-09-25 18:33:21 +02:00
|
|
|
auto result = simplifyValue(std::make_shared<ExprEngine::BinOpResult>(tok->str(), v1, v2));
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok, result, &data);
|
2019-09-17 21:00:59 +02:00
|
|
|
return result;
|
|
|
|
}
|
2020-05-10 22:50:23 +02:00
|
|
|
if (tok->str() == "&&" && (v1 || v2)) {
|
|
|
|
auto result = v1 ? v1 : v2;
|
|
|
|
call(data.callbacks, tok, result, &data);
|
|
|
|
return result;
|
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
return ExprEngine::ValuePtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr executeAddressOf(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
auto addr = std::make_shared<ExprEngine::AddressOfValue>(data.getNewSymbolName(), tok->astOperand1()->varId());
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok, addr, &data);
|
2019-09-17 21:00:59 +02:00
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr executeDeref(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
ExprEngine::ValuePtr pval = executeExpression(tok->astOperand1(), data);
|
2019-10-27 16:47:56 +01:00
|
|
|
if (!pval) {
|
2020-06-21 21:48:36 +02:00
|
|
|
auto v = getValueRangeFromValueType(tok->valueType(), data);
|
2019-10-27 16:47:56 +01:00
|
|
|
if (tok->astOperand1()->varId()) {
|
2019-12-29 16:26:11 +01:00
|
|
|
pval = std::make_shared<ExprEngine::ArrayValue>(data.getNewSymbolName(), ExprEngine::ValuePtr(), v, true, false, false);
|
2019-10-27 18:22:47 +01:00
|
|
|
data.assignValue(tok->astOperand1(), tok->astOperand1()->varId(), pval);
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
2019-10-27 16:47:56 +01:00
|
|
|
call(data.callbacks, tok, v, &data);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
auto addressOf = std::dynamic_pointer_cast<ExprEngine::AddressOfValue>(pval);
|
|
|
|
if (addressOf) {
|
|
|
|
auto val = data.getValue(addressOf->varId, tok->valueType(), tok);
|
|
|
|
call(data.callbacks, tok, val, &data);
|
|
|
|
return val;
|
|
|
|
}
|
2019-12-29 16:26:11 +01:00
|
|
|
auto pointer = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(pval);
|
2019-10-27 16:47:56 +01:00
|
|
|
if (pointer) {
|
2019-12-29 16:26:11 +01:00
|
|
|
auto indexValue = std::make_shared<ExprEngine::IntRange>("0", 0, 0);
|
|
|
|
auto conditionalValues = pointer->read(indexValue);
|
|
|
|
for (auto value: conditionalValues)
|
|
|
|
call(data.callbacks, tok, value.second, &data);
|
|
|
|
if (conditionalValues.size() == 1 && !conditionalValues[0].first)
|
|
|
|
return conditionalValues[0].second;
|
|
|
|
return std::make_shared<ExprEngine::ConditionalValue>(data.getNewSymbolName(), conditionalValues);
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
return ExprEngine::ValuePtr();
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:00:04 +01:00
|
|
|
static ExprEngine::ValuePtr executeNot(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
ExprEngine::ValuePtr v = executeExpression(tok->astOperand1(), data);
|
|
|
|
if (!v)
|
|
|
|
return v;
|
|
|
|
ExprEngine::ValuePtr zero = std::make_shared<ExprEngine::IntRange>("0", 0, 0);
|
|
|
|
auto result = simplifyValue(std::make_shared<ExprEngine::BinOpResult>("==", v, zero));
|
|
|
|
call(data.callbacks, tok, result, &data);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
static ExprEngine::ValuePtr executeVariable(const Token *tok, Data &data)
|
|
|
|
{
|
2019-09-19 20:18:39 +02:00
|
|
|
auto val = data.getValue(tok->varId(), tok->valueType(), tok);
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok, val, &data);
|
2019-09-17 21:00:59 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-09-23 18:10:06 +02:00
|
|
|
static ExprEngine::ValuePtr executeKnownMacro(const Token *tok, Data &data)
|
|
|
|
{
|
2022-01-03 12:46:33 +01:00
|
|
|
const auto intval = tok->getKnownIntValue();
|
|
|
|
auto val = std::make_shared<ExprEngine::IntRange>(std::to_string(intval), intval, intval);
|
2019-10-02 21:47:00 +02:00
|
|
|
call(data.callbacks, tok, val, &data);
|
2019-09-23 18:10:06 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-10-25 21:46:02 +02:00
|
|
|
static ExprEngine::ValuePtr executeNumber(const Token *tok, Data &data)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2019-09-22 21:14:20 +02:00
|
|
|
if (tok->valueType()->isFloat()) {
|
|
|
|
long double value = MathLib::toDoubleNumber(tok->str());
|
2019-10-25 21:46:02 +02:00
|
|
|
auto v = std::make_shared<ExprEngine::FloatRange>(tok->str(), value, value);
|
|
|
|
call(data.callbacks, tok, v, &data);
|
|
|
|
return v;
|
2019-09-22 21:14:20 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
int128_t value = MathLib::toLongNumber(tok->str());
|
2019-10-25 21:46:02 +02:00
|
|
|
auto v = std::make_shared<ExprEngine::IntRange>(tok->str(), value, value);
|
|
|
|
call(data.callbacks, tok, v, &data);
|
|
|
|
return v;
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 11:36:34 +02:00
|
|
|
static ExprEngine::ValuePtr executeStringLiteral(const Token *tok, Data &data)
|
|
|
|
{
|
2021-01-02 19:10:25 +01:00
|
|
|
const std::string& s = tok->str();
|
2019-09-21 11:36:34 +02:00
|
|
|
return std::make_shared<ExprEngine::StringLiteralValue>(data.getNewSymbolName(), s.substr(1, s.size()-2));
|
|
|
|
}
|
|
|
|
|
2019-09-28 16:16:36 +02:00
|
|
|
static ExprEngine::ValuePtr executeExpression1(const Token *tok, Data &data)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2021-01-02 19:10:25 +01:00
|
|
|
if (Settings::terminated())
|
2020-07-14 11:22:42 +02:00
|
|
|
throw TerminateExpression();
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
if (tok->str() == "return")
|
|
|
|
return executeReturn(tok, data);
|
|
|
|
|
2019-09-21 21:15:51 +02:00
|
|
|
if (tok->isAssignmentOp())
|
|
|
|
// TODO: Handle more operators
|
2019-09-17 21:00:59 +02:00
|
|
|
return executeAssign(tok, data);
|
2020-06-19 17:35:35 +02:00
|
|
|
|
|
|
|
if (tok->tokType() == Token::Type::eIncDecOp)
|
|
|
|
return executeIncDec(tok, data);
|
2019-09-17 21:00:59 +02:00
|
|
|
|
|
|
|
if (tok->astOperand1() && tok->astOperand2() && tok->str() == "[")
|
|
|
|
return executeArrayIndex(tok, data);
|
|
|
|
|
2019-09-28 15:40:00 +02:00
|
|
|
if (tok->str() == "(") {
|
|
|
|
if (!tok->isCast())
|
|
|
|
return executeFunctionCall(tok, data);
|
|
|
|
return executeCast(tok, data);
|
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
|
|
|
|
if (tok->str() == ".")
|
|
|
|
return executeDot(tok, data);
|
|
|
|
|
2019-10-23 18:23:25 +02:00
|
|
|
if (tok->str() == "::" && tok->hasKnownIntValue()) { // TODO handle :: better
|
|
|
|
auto v = tok->getKnownIntValue();
|
|
|
|
return std::make_shared<ExprEngine::IntRange>(std::to_string(v), v, v);
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:05:26 +02:00
|
|
|
if (data.tokenizer->isCPP() && tok->str() == ">>" && !tok->astParent() && tok->isBinaryOp() && Token::Match(tok->astOperand1(), "%name%|::"))
|
|
|
|
return executeStreamRead(tok, data);
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
if (tok->astOperand1() && tok->astOperand2())
|
|
|
|
return executeBinaryOp(tok, data);
|
|
|
|
|
|
|
|
if (tok->isUnaryOp("&") && Token::Match(tok->astOperand1(), "%var%"))
|
|
|
|
return executeAddressOf(tok, data);
|
|
|
|
|
|
|
|
if (tok->isUnaryOp("*"))
|
|
|
|
return executeDeref(tok, data);
|
|
|
|
|
2020-12-13 13:00:04 +01:00
|
|
|
if (tok->isUnaryOp("!"))
|
|
|
|
return executeNot(tok, data);
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
if (tok->varId())
|
|
|
|
return executeVariable(tok, data);
|
|
|
|
|
2019-09-23 18:10:06 +02:00
|
|
|
if (tok->isName() && tok->hasKnownIntValue())
|
|
|
|
return executeKnownMacro(tok, data);
|
|
|
|
|
2019-09-24 20:10:51 +02:00
|
|
|
if (tok->isNumber() || tok->tokType() == Token::Type::eChar)
|
2019-10-25 21:46:02 +02:00
|
|
|
return executeNumber(tok, data);
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2019-09-21 11:36:34 +02:00
|
|
|
if (tok->tokType() == Token::Type::eString)
|
|
|
|
return executeStringLiteral(tok, data);
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
return ExprEngine::ValuePtr();
|
|
|
|
}
|
|
|
|
|
2019-09-28 16:16:36 +02:00
|
|
|
static ExprEngine::ValuePtr executeExpression(const Token *tok, Data &data)
|
|
|
|
{
|
|
|
|
return translateUninitValueToRange(executeExpression1(tok, data), tok->valueType(), data);
|
|
|
|
}
|
|
|
|
|
2019-09-26 19:39:12 +02:00
|
|
|
static ExprEngine::ValuePtr createVariableValue(const Variable &var, Data &data);
|
|
|
|
|
2020-12-22 21:30:15 +01:00
|
|
|
static std::tuple<bool, bool> checkConditionBranches(const ExprEngine::ValuePtr &condValue, const Data &data)
|
|
|
|
{
|
2020-12-22 20:21:57 +01:00
|
|
|
bool canBeFalse = true;
|
|
|
|
bool canBeTrue = true;
|
|
|
|
if (auto b = std::dynamic_pointer_cast<ExprEngine::BinOpResult>(condValue)) {
|
|
|
|
canBeFalse = b->isEqual(&data, 0);
|
|
|
|
canBeTrue = b->isTrue(&data);
|
|
|
|
} else if (auto i = std::dynamic_pointer_cast<ExprEngine::IntRange>(condValue)) {
|
|
|
|
canBeFalse = i->isEqual(&data, 0);
|
|
|
|
canBeTrue = ExprEngine::BinOpResult("!=", i, std::make_shared<ExprEngine::IntRange>("0", 0, 0)).isTrue(&data);
|
|
|
|
} else if (std::dynamic_pointer_cast<ExprEngine::StringLiteralValue>(condValue)) {
|
|
|
|
canBeFalse = false;
|
|
|
|
canBeTrue = true;
|
|
|
|
} else if (auto f = std::dynamic_pointer_cast<ExprEngine::FloatRange>(condValue)) {
|
|
|
|
canBeFalse = f->isEqual(&data, 0);
|
|
|
|
canBeTrue = ExprEngine::BinOpResult("!=", f, std::make_shared<ExprEngine::FloatRange>("0.0", 0.0, 0.0)).isTrue(&data);
|
|
|
|
}
|
|
|
|
return std::make_tuple(canBeFalse, canBeTrue);
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:00:39 +02:00
|
|
|
static std::string execute(const Token *start, const Token *end, Data &data)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2020-06-19 10:27:59 +02:00
|
|
|
if (data.recursion > 20)
|
2020-05-23 11:30:54 +02:00
|
|
|
// FIXME
|
2020-06-20 23:00:39 +02:00
|
|
|
return data.str();
|
2020-05-23 11:30:54 +02:00
|
|
|
|
2020-06-19 10:27:59 +02:00
|
|
|
// Update data.recursion
|
|
|
|
struct Recursion {
|
|
|
|
Recursion(int *var, int value) : var(var), value(value) {
|
|
|
|
*var = value + 1;
|
|
|
|
}
|
|
|
|
~Recursion() {
|
|
|
|
if (*var >= value) *var = value;
|
|
|
|
}
|
|
|
|
int *var;
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
Recursion updateRecursion(&data.recursion, data.recursion);
|
|
|
|
|
2020-12-18 20:41:10 +01:00
|
|
|
const std::time_t stopTime = data.startTime + data.settings->bugHuntingCheckFunctionMaxTime;
|
2020-12-18 19:58:53 +01:00
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
for (const Token *tok = start; tok != end; tok = tok->next()) {
|
2020-12-13 09:13:11 +01:00
|
|
|
if (Token::Match(tok, "[;{}]")) {
|
2019-09-19 20:18:39 +02:00
|
|
|
data.trackProgramState(tok);
|
2020-12-13 09:13:11 +01:00
|
|
|
if (tok->str() == ";") {
|
|
|
|
const Token *prev = tok->previous();
|
|
|
|
while (prev && !Token::Match(prev, "[;{}]"))
|
|
|
|
prev = prev->previous();
|
|
|
|
if (Token::Match(prev, "[;{}] return|throw"))
|
|
|
|
return data.str();
|
|
|
|
}
|
2022-01-03 12:46:33 +01:00
|
|
|
while (Token::simpleMatch(tok, "} catch (") && Token::simpleMatch(tok->linkAt(2), ") {")) {
|
|
|
|
tok = tok->linkAt(2)->next()->link();
|
|
|
|
}
|
2020-12-18 19:58:53 +01:00
|
|
|
if (std::time(nullptr) > stopTime)
|
|
|
|
return "";
|
2020-12-13 09:13:11 +01:00
|
|
|
}
|
2019-09-29 17:28:12 +02:00
|
|
|
|
2020-07-19 16:54:44 +02:00
|
|
|
if (Token::simpleMatch(tok, "__CPPCHECK_BAILOUT__ ;"))
|
|
|
|
// This is intended for testing
|
|
|
|
throw ExprEngineException(tok, "__CPPCHECK_BAILOUT__");
|
|
|
|
|
2020-12-26 13:46:28 +01:00
|
|
|
if (Token::simpleMatch(tok, "while (") && Token::simpleMatch(tok->linkAt(1), ") ;") && tok->next()->astOperand1()->hasKnownIntValue() && tok->next()->astOperand1()->getKnownIntValue() == 0) {
|
2019-10-06 17:43:30 +02:00
|
|
|
tok = tok->tokAt(4);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-06 19:58:51 +02:00
|
|
|
if (tok->str() == "break") {
|
|
|
|
const Scope *scope = tok->scope();
|
|
|
|
while (scope->type == Scope::eIf || scope->type == Scope::eElse)
|
|
|
|
scope = scope->nestedIn;
|
|
|
|
tok = scope->bodyEnd;
|
2020-04-25 10:13:18 +02:00
|
|
|
if (!precedes(tok,end))
|
2020-06-20 23:00:39 +02:00
|
|
|
return data.str();
|
2019-10-06 19:58:51 +02:00
|
|
|
}
|
2019-10-06 17:43:30 +02:00
|
|
|
|
2022-01-03 12:46:33 +01:00
|
|
|
if (Token::simpleMatch(tok, "try {") && Token::simpleMatch(tok->linkAt(1), "} catch (")) {
|
|
|
|
const Token *catchTok = tok->linkAt(1);
|
|
|
|
while (Token::simpleMatch(catchTok, "} catch (")) {
|
|
|
|
Data catchData(data);
|
|
|
|
catchTok = catchTok->linkAt(2)->next();
|
|
|
|
execute(catchTok, end, catchData);
|
|
|
|
catchTok = catchTok->link();
|
|
|
|
}
|
|
|
|
}
|
2019-10-06 19:58:51 +02:00
|
|
|
|
2019-09-29 17:28:12 +02:00
|
|
|
// Variable declaration..
|
2019-09-26 19:39:12 +02:00
|
|
|
if (tok->variable() && tok->variable()->nameToken() == tok) {
|
2019-09-27 21:03:47 +02:00
|
|
|
if (Token::Match(tok, "%varid% ; %varid% =", tok->varId())) {
|
2019-09-29 08:26:09 +02:00
|
|
|
// if variable is not used in assignment rhs then we do not need to create a "confusing" variable value..
|
|
|
|
bool foundInRhs = false;
|
|
|
|
visitAstNodes(tok->tokAt(3)->astOperand2(), [&](const Token *rhs) {
|
|
|
|
if (rhs->varId()==tok->varId()) {
|
|
|
|
foundInRhs = true;
|
|
|
|
return ChildrenToVisit::done;
|
|
|
|
}
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
|
|
|
});
|
|
|
|
if (!foundInRhs) {
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-19 14:05:19 +02:00
|
|
|
data.assignValue(tok, tok->varId(), createVariableValue(*tok->variable(), data));
|
2019-12-31 14:57:42 +01:00
|
|
|
} else if (tok->variable()->isArray()) {
|
2019-09-29 15:00:54 +02:00
|
|
|
data.assignValue(tok, tok->varId(), std::make_shared<ExprEngine::ArrayValue>(&data, tok->variable()));
|
2019-09-26 19:39:12 +02:00
|
|
|
if (Token::Match(tok, "%name% ["))
|
|
|
|
tok = tok->linkAt(1);
|
|
|
|
} else if (Token::Match(tok, "%var% ;"))
|
2019-09-26 20:48:33 +02:00
|
|
|
data.assignValue(tok, tok->varId(), createVariableValue(*tok->variable(), data));
|
2019-10-06 19:58:51 +02:00
|
|
|
} else if (!tok->astParent() && (tok->astOperand1() || tok->astOperand2())) {
|
2019-09-17 21:00:59 +02:00
|
|
|
executeExpression(tok, data);
|
2019-10-06 19:58:51 +02:00
|
|
|
if (Token::Match(tok, "throw|return"))
|
2020-06-20 23:00:39 +02:00
|
|
|
return data.str();
|
2019-10-06 19:58:51 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2019-09-26 19:39:12 +02:00
|
|
|
else if (Token::simpleMatch(tok, "if (")) {
|
2019-10-06 19:58:51 +02:00
|
|
|
const Token *cond = tok->next()->astOperand2(); // TODO: C++17 condition
|
2019-10-02 21:47:00 +02:00
|
|
|
const ExprEngine::ValuePtr condValue = executeExpression(cond, data);
|
2020-12-12 15:22:33 +01:00
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bool canBeFalse, canBeTrue;
|
|
|
|
std::tie(canBeFalse, canBeTrue) = checkConditionBranches(condValue, data);
|
2020-12-12 15:22:33 +01:00
|
|
|
|
2020-06-20 23:00:39 +02:00
|
|
|
Data &thenData(data);
|
2019-10-02 21:47:00 +02:00
|
|
|
Data elseData(data);
|
2020-12-22 11:10:01 +01:00
|
|
|
if (canBeFalse && canBeTrue) { // Avoid that constraints are overspecified
|
|
|
|
thenData.addConstraint(condValue, true);
|
|
|
|
elseData.addConstraint(condValue, false);
|
|
|
|
}
|
2019-10-02 21:47:00 +02:00
|
|
|
|
2020-12-12 18:29:05 +01:00
|
|
|
Data::ifSplit(tok, thenData, elseData);
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
const Token *thenStart = tok->linkAt(1)->next();
|
|
|
|
const Token *thenEnd = thenStart->link();
|
2020-04-24 18:51:54 +02:00
|
|
|
|
2020-05-23 17:50:24 +02:00
|
|
|
const Token *exceptionToken = nullptr;
|
|
|
|
std::string exceptionMessage;
|
|
|
|
auto exec = [&](const Token *tok1, const Token *tok2, Data& data) {
|
|
|
|
try {
|
2020-06-19 10:27:59 +02:00
|
|
|
execute(tok1, tok2, data);
|
2020-06-28 17:39:14 +02:00
|
|
|
} catch (ExprEngineException &e) {
|
2020-05-23 17:50:24 +02:00
|
|
|
if (!exceptionToken || (e.tok && precedes(e.tok, exceptionToken))) {
|
|
|
|
exceptionToken = e.tok;
|
|
|
|
exceptionMessage = e.what;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-20 14:51:03 +01:00
|
|
|
if (canBeTrue)
|
2020-12-12 15:22:33 +01:00
|
|
|
exec(thenStart->next(), end, thenData);
|
2020-04-24 18:51:54 +02:00
|
|
|
|
2020-12-20 14:51:03 +01:00
|
|
|
if (canBeFalse) {
|
2020-12-12 15:22:33 +01:00
|
|
|
if (Token::simpleMatch(thenEnd, "} else {")) {
|
|
|
|
const Token *elseStart = thenEnd->tokAt(2);
|
|
|
|
exec(elseStart->next(), end, elseData);
|
|
|
|
} else {
|
|
|
|
exec(thenEnd, end, elseData);
|
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
2020-05-23 17:50:24 +02:00
|
|
|
|
|
|
|
if (exceptionToken)
|
2020-06-28 17:39:14 +02:00
|
|
|
throw ExprEngineException(exceptionToken, exceptionMessage);
|
2020-06-20 23:00:39 +02:00
|
|
|
|
2020-12-20 14:51:03 +01:00
|
|
|
return (canBeTrue ? thenData.str() : std::string()) +
|
|
|
|
(canBeFalse ? elseData.str() : std::string());
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-10-06 19:58:51 +02:00
|
|
|
else if (Token::simpleMatch(tok, "switch (")) {
|
|
|
|
auto condValue = executeExpression(tok->next()->astOperand2(), data); // TODO: C++17 condition
|
|
|
|
const Token *bodyStart = tok->linkAt(1)->next();
|
|
|
|
const Token *bodyEnd = bodyStart->link();
|
|
|
|
const Token *defaultStart = nullptr;
|
|
|
|
Data defaultData(data);
|
2020-05-23 17:50:24 +02:00
|
|
|
const Token *exceptionToken = nullptr;
|
|
|
|
std::string exceptionMessage;
|
2020-06-20 23:00:39 +02:00
|
|
|
std::ostringstream ret;
|
2020-05-23 17:50:24 +02:00
|
|
|
auto exec = [&](const Token *tok1, const Token *tok2, Data& data) {
|
|
|
|
try {
|
2020-06-19 10:27:59 +02:00
|
|
|
execute(tok1, tok2, data);
|
2020-06-20 23:00:39 +02:00
|
|
|
ret << data.str();
|
2020-06-28 17:39:14 +02:00
|
|
|
} catch (ExprEngineException &e) {
|
2020-05-23 17:50:24 +02:00
|
|
|
if (!exceptionToken || (e.tok && precedes(e.tok, exceptionToken))) {
|
|
|
|
exceptionToken = e.tok;
|
|
|
|
exceptionMessage = e.what;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-10-06 19:58:51 +02:00
|
|
|
for (const Token *tok2 = bodyStart->next(); tok2 != bodyEnd; tok2 = tok2->next()) {
|
|
|
|
if (tok2->str() == "{")
|
|
|
|
tok2 = tok2->link();
|
2020-05-01 15:15:24 +02:00
|
|
|
else if (Token::Match(tok2, "case %char%|%num% :")) {
|
|
|
|
const MathLib::bigint caseValue1 = tok2->next()->getKnownIntValue();
|
|
|
|
auto caseValue = std::make_shared<ExprEngine::IntRange>(MathLib::toString(caseValue1), caseValue1, caseValue1);
|
2019-10-06 19:58:51 +02:00
|
|
|
Data caseData(data);
|
|
|
|
caseData.addConstraint(condValue, caseValue, true);
|
|
|
|
defaultData.addConstraint(condValue, caseValue, false);
|
2020-05-23 17:50:24 +02:00
|
|
|
exec(tok2->tokAt(2), end, caseData);
|
|
|
|
} else if (Token::Match(tok2, "case %name% :") && !Token::Match(tok2->tokAt(3), ";| case")) {
|
|
|
|
Data caseData(data);
|
|
|
|
exec(tok2->tokAt(2), end, caseData);
|
2019-10-06 19:58:51 +02:00
|
|
|
} else if (Token::simpleMatch(tok2, "default :"))
|
|
|
|
defaultStart = tok2;
|
|
|
|
}
|
2020-05-23 17:50:24 +02:00
|
|
|
exec(defaultStart ? defaultStart : bodyEnd, end, defaultData);
|
|
|
|
if (exceptionToken)
|
2020-06-28 17:39:14 +02:00
|
|
|
throw ExprEngineException(exceptionToken, exceptionMessage);
|
2020-06-20 23:00:39 +02:00
|
|
|
return ret.str();
|
2019-10-06 19:58:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 13:41:27 +02:00
|
|
|
if (Token::simpleMatch(tok, "for (")) {
|
|
|
|
nonneg int varid;
|
2020-06-29 21:53:14 +02:00
|
|
|
bool hasKnownInitValue, partialCond;
|
2020-06-28 13:41:27 +02:00
|
|
|
MathLib::bigint initValue, stepValue, lastValue;
|
2020-06-29 21:53:14 +02:00
|
|
|
if (extractForLoopValues(tok, &varid, &hasKnownInitValue, &initValue, &partialCond, &stepValue, &lastValue) && hasKnownInitValue && !partialCond) {
|
2020-06-28 13:41:27 +02:00
|
|
|
auto loopValues = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), initValue, lastValue);
|
|
|
|
data.assignValue(tok, varid, loopValues);
|
|
|
|
tok = tok->linkAt(1);
|
2021-01-08 22:55:04 +01:00
|
|
|
if (tok->next()) {
|
|
|
|
loopValues->loopScope = tok->next()->scope();
|
|
|
|
// Check whether the condition expression is always false
|
|
|
|
if (initValue > lastValue) {
|
|
|
|
tok = tok->next()->link();
|
|
|
|
}
|
2020-12-26 21:29:50 +01:00
|
|
|
}
|
2020-06-28 13:41:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:44:26 +02:00
|
|
|
if (Token::Match(tok, "for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
|
2020-12-22 20:21:57 +01:00
|
|
|
const Token *cond = tok->next()->astOperand2();
|
|
|
|
const ExprEngine::ValuePtr condValue = executeExpression(cond, data);
|
|
|
|
|
|
|
|
bool canBeFalse = false, canBeTrue = true;
|
|
|
|
if (tok->str() == "while")
|
|
|
|
std::tie(canBeFalse, canBeTrue) = checkConditionBranches(condValue, data);
|
|
|
|
|
|
|
|
Data &bodyData(data);
|
|
|
|
Data noexecData(data);
|
|
|
|
if (canBeFalse && canBeTrue) { // Avoid that constraints are overspecified
|
|
|
|
bodyData.addConstraint(condValue, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Data::ifSplit(tok, bodyData, noexecData);
|
|
|
|
|
2019-10-07 17:44:26 +02:00
|
|
|
const Token *bodyStart = tok->linkAt(1)->next();
|
|
|
|
const Token *bodyEnd = bodyStart->link();
|
|
|
|
|
|
|
|
// TODO this is very rough code
|
2020-12-22 20:21:57 +01:00
|
|
|
if (canBeTrue) {
|
|
|
|
std::set<int> changedVariables;
|
|
|
|
for (const Token *tok2 = tok; tok2 != bodyEnd; tok2 = tok2->next()) {
|
|
|
|
if (Token::Match(tok2, "%assign%")) {
|
|
|
|
const Token *lhs = tok2->astOperand1();
|
|
|
|
while (Token::simpleMatch(lhs, "["))
|
|
|
|
lhs = lhs->astOperand1();
|
|
|
|
if (!lhs)
|
2020-06-28 17:39:14 +02:00
|
|
|
throw ExprEngineException(tok2, "Unhandled assignment in loop");
|
2020-12-22 20:21:57 +01:00
|
|
|
if (Token::Match(lhs, ". %name% =|[") && Token::simpleMatch(lhs->astOperand1(), ".")) {
|
|
|
|
const Token *structToken = lhs;
|
|
|
|
while (Token::Match(structToken, ".|["))
|
|
|
|
structToken = structToken->astOperand1();
|
|
|
|
if (Token::Match(structToken, "%var%")) {
|
|
|
|
bodyData.assignValue(structToken, structToken->varId(), std::make_shared<ExprEngine::BailoutValue>());
|
|
|
|
changedVariables.insert(structToken->varId());
|
|
|
|
continue;
|
2019-10-14 19:41:32 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 20:21:57 +01:00
|
|
|
if (Token::Match(lhs, ". %name% =|[") && lhs->astOperand1() && lhs->astOperand1()->valueType()) {
|
|
|
|
const Token *structToken = lhs->astOperand1();
|
|
|
|
if (!structToken->valueType() || !structToken->varId())
|
|
|
|
throw ExprEngineException(tok2, "Unhandled assignment in loop");
|
|
|
|
const Scope *structScope = structToken->valueType()->typeScope;
|
|
|
|
if (!structScope)
|
|
|
|
throw ExprEngineException(tok2, "Unhandled assignment in loop");
|
|
|
|
const std::string &memberName = tok2->previous()->str();
|
|
|
|
ExprEngine::ValuePtr memberValue;
|
|
|
|
for (const Variable &member : structScope->varlist) {
|
|
|
|
if (memberName == member.name() && member.valueType()) {
|
|
|
|
memberValue = createVariableValue(member, bodyData);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!memberValue)
|
|
|
|
throw ExprEngineException(tok2, "Unhandled assignment in loop");
|
2019-10-14 19:41:32 +02:00
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
ExprEngine::ValuePtr structVal1 = bodyData.getValue(structToken->varId(), structToken->valueType(), structToken);
|
|
|
|
if (!structVal1)
|
|
|
|
structVal1 = createVariableValue(*structToken->variable(), bodyData);
|
|
|
|
auto structVal = std::dynamic_pointer_cast<ExprEngine::StructValue>(structVal1);
|
|
|
|
if (!structVal) {
|
|
|
|
// Handle pointer to a struct
|
|
|
|
if (auto structPtr = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(structVal1)) {
|
|
|
|
if (structPtr->pointer && !structPtr->data.empty()) {
|
|
|
|
auto indexValue = std::make_shared<ExprEngine::IntRange>("0", 0, 0);
|
|
|
|
for (auto val: structPtr->read(indexValue)) {
|
|
|
|
structVal = std::dynamic_pointer_cast<ExprEngine::StructValue>(val.second);
|
|
|
|
}
|
2020-12-13 16:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 20:21:57 +01:00
|
|
|
if (!structVal)
|
|
|
|
throw ExprEngineException(tok2, "Unhandled assignment in loop");
|
2020-12-13 16:02:35 +01:00
|
|
|
}
|
2019-10-14 22:03:55 +02:00
|
|
|
|
2020-12-22 20:21:57 +01:00
|
|
|
bodyData.assignStructMember(tok2, &*structVal, memberName, memberValue);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (lhs->isUnaryOp("*") && lhs->astOperand1()->varId()) {
|
|
|
|
const Token *varToken = tok2->astOperand1()->astOperand1();
|
|
|
|
ExprEngine::ValuePtr val = bodyData.getValue(varToken->varId(), varToken->valueType(), varToken);
|
|
|
|
if (val && val->type == ExprEngine::ValueType::ArrayValue) {
|
|
|
|
// Try to assign "any" value
|
|
|
|
auto arrayValue = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(val);
|
|
|
|
arrayValue->assign(std::make_shared<ExprEngine::IntRange>("0", 0, 0), std::make_shared<ExprEngine::BailoutValue>());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!lhs->variable())
|
|
|
|
throw ExprEngineException(tok2, "Unhandled assignment in loop");
|
|
|
|
// give variable "any" value
|
|
|
|
int varid = lhs->varId();
|
|
|
|
if (changedVariables.find(varid) != changedVariables.end())
|
|
|
|
continue;
|
|
|
|
changedVariables.insert(varid);
|
|
|
|
auto oldValue = bodyData.getValue(varid, nullptr, nullptr);
|
2022-01-03 12:46:33 +01:00
|
|
|
if (oldValue && oldValue->isUninit(&bodyData))
|
2020-12-22 20:21:57 +01:00
|
|
|
call(bodyData.callbacks, lhs, oldValue, &bodyData);
|
|
|
|
if (oldValue && oldValue->type == ExprEngine::ValueType::ArrayValue) {
|
2020-04-30 21:05:34 +02:00
|
|
|
// Try to assign "any" value
|
2020-12-22 20:21:57 +01:00
|
|
|
auto arrayValue = std::dynamic_pointer_cast<ExprEngine::ArrayValue>(oldValue);
|
2022-01-03 19:55:44 +01:00
|
|
|
arrayValue->assign(std::make_shared<ExprEngine::IntRange>(bodyData.getNewSymbolName(), 0, MAX_BUFFER_SIZE), std::make_shared<ExprEngine::BailoutValue>());
|
2020-04-30 21:05:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-22 20:21:57 +01:00
|
|
|
bodyData.assignValue(tok2, varid, getValueRangeFromValueType(lhs->valueType(), bodyData));
|
2020-06-15 22:42:45 +02:00
|
|
|
continue;
|
2020-12-22 20:21:57 +01:00
|
|
|
} else if (Token::Match(tok2, "++|--") && tok2->astOperand1() && tok2->astOperand1()->variable()) {
|
|
|
|
// give variable "any" value
|
|
|
|
const Token *vartok = tok2->astOperand1();
|
|
|
|
int varid = vartok->varId();
|
|
|
|
if (changedVariables.find(varid) != changedVariables.end())
|
|
|
|
continue;
|
|
|
|
changedVariables.insert(varid);
|
|
|
|
auto oldValue = bodyData.getValue(varid, nullptr, nullptr);
|
|
|
|
if (oldValue && oldValue->type == ExprEngine::ValueType::UninitValue)
|
|
|
|
call(bodyData.callbacks, tok2, oldValue, &bodyData);
|
|
|
|
bodyData.assignValue(tok2, varid, getValueRangeFromValueType(vartok->valueType(), bodyData));
|
2020-06-15 22:42:45 +02:00
|
|
|
}
|
2019-10-07 17:44:26 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 20:21:57 +01:00
|
|
|
|
|
|
|
const Token *exceptionToken = nullptr;
|
|
|
|
std::string exceptionMessage;
|
|
|
|
auto exec = [&](const Token *tok1, const Token *tok2, Data& data) {
|
|
|
|
try {
|
|
|
|
execute(tok1, tok2, data);
|
|
|
|
} catch (ExprEngineException &e) {
|
|
|
|
if (!exceptionToken || (e.tok && precedes(e.tok, exceptionToken))) {
|
|
|
|
exceptionToken = e.tok;
|
|
|
|
exceptionMessage = e.what;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (canBeTrue)
|
|
|
|
exec(bodyStart->next(), end, bodyData);
|
|
|
|
if (canBeFalse)
|
|
|
|
exec(bodyEnd, end, noexecData);
|
|
|
|
|
|
|
|
if (exceptionToken)
|
|
|
|
throw ExprEngineException(exceptionToken, exceptionMessage);
|
|
|
|
|
|
|
|
return (canBeTrue ? bodyData.str() : std::string()) +
|
|
|
|
(canBeFalse ? noexecData.str() : std::string());
|
2019-10-07 17:44:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
if (Token::simpleMatch(tok, "} else {"))
|
|
|
|
tok = tok->linkAt(2);
|
|
|
|
}
|
2020-06-20 23:00:39 +02:00
|
|
|
|
|
|
|
return data.str();
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 17:16:13 +02:00
|
|
|
void ExprEngine::executeAllFunctions(ErrorLogger *errorLogger, const Tokenizer *tokenizer, const Settings *settings, const std::vector<ExprEngine::Callback> &callbacks, std::ostream &report)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
|
|
|
const SymbolDatabase *symbolDatabase = tokenizer->getSymbolDatabase();
|
|
|
|
for (const Scope *functionScope : symbolDatabase->functionScopes) {
|
2019-09-27 14:36:33 +02:00
|
|
|
try {
|
2020-04-28 17:16:13 +02:00
|
|
|
executeFunction(functionScope, errorLogger, tokenizer, settings, callbacks, report);
|
2020-06-28 17:39:14 +02:00
|
|
|
} catch (const ExprEngineException &e) {
|
2019-10-08 20:13:25 +02:00
|
|
|
// FIXME.. there should not be exceptions
|
|
|
|
std::string functionName = functionScope->function->name();
|
|
|
|
std::cout << "Verify: Aborted analysis of function '" << functionName << "':" << e.tok->linenr() << ": " << e.what << std::endl;
|
2019-09-28 10:59:28 +02:00
|
|
|
} catch (const std::exception &e) {
|
2019-09-27 14:36:33 +02:00
|
|
|
// FIXME.. there should not be exceptions
|
2019-09-28 10:59:28 +02:00
|
|
|
std::string functionName = functionScope->function->name();
|
2019-09-28 11:03:20 +02:00
|
|
|
std::cout << "Verify: Aborted analysis of function '" << functionName << "': " << e.what() << std::endl;
|
2020-07-14 11:22:42 +02:00
|
|
|
} catch (const TerminateExpression &) {
|
|
|
|
break;
|
2019-09-27 14:36:33 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:32:30 +01:00
|
|
|
static ExprEngine::ValuePtr createStructVal(const Token *tok, const Scope *structScope, bool uninitData, Data &data)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
2019-09-20 21:27:51 +02:00
|
|
|
if (!structScope)
|
|
|
|
return ExprEngine::ValuePtr();
|
2019-09-17 21:00:59 +02:00
|
|
|
std::shared_ptr<ExprEngine::StructValue> structValue = std::make_shared<ExprEngine::StructValue>(data.getNewSymbolName());
|
2019-09-26 19:39:12 +02:00
|
|
|
auto uninitValue = std::make_shared<ExprEngine::UninitValue>();
|
2019-09-17 21:00:59 +02:00
|
|
|
for (const Variable &member : structScope->varlist) {
|
2020-09-09 18:26:04 +02:00
|
|
|
if (uninitData && !member.isInit()) {
|
2019-09-26 19:39:12 +02:00
|
|
|
if (member.isPointer()) {
|
|
|
|
structValue->member[member.name()] = uninitValue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (member.valueType() && member.valueType()->type >= ::ValueType::Type::CHAR) {
|
|
|
|
structValue->member[member.name()] = uninitValue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-10-05 18:29:41 +02:00
|
|
|
if (member.valueType() && member.valueType()->isIntegral()) {
|
|
|
|
ExprEngine::ValuePtr memberValue = createVariableValue(member, data);
|
|
|
|
if (memberValue)
|
2020-12-14 12:32:30 +01:00
|
|
|
data.assignStructMember(tok, structValue.get(), member.name(), memberValue);
|
2019-10-05 18:29:41 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
return structValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExprEngine::ValuePtr createVariableValue(const Variable &var, Data &data)
|
|
|
|
{
|
2019-09-22 10:56:49 +02:00
|
|
|
if (!var.nameToken())
|
|
|
|
return ExprEngine::ValuePtr();
|
|
|
|
const ValueType *valueType = var.valueType();
|
|
|
|
if (!valueType || valueType->type == ValueType::Type::UNKNOWN_TYPE)
|
|
|
|
valueType = var.nameToken()->valueType();
|
2019-09-29 08:26:09 +02:00
|
|
|
if (!valueType || valueType->type == ValueType::Type::UNKNOWN_TYPE) {
|
|
|
|
// variable with unknown type
|
|
|
|
if (var.isLocal() && var.isPointer() && !var.isArray())
|
|
|
|
return std::make_shared<ExprEngine::UninitValue>();
|
2019-09-17 21:00:59 +02:00
|
|
|
return ExprEngine::ValuePtr();
|
2019-09-29 08:26:09 +02:00
|
|
|
}
|
2019-09-22 10:56:49 +02:00
|
|
|
|
|
|
|
if (valueType->pointer > 0) {
|
2019-09-29 08:26:09 +02:00
|
|
|
if (var.isLocal())
|
|
|
|
return std::make_shared<ExprEngine::UninitValue>();
|
2022-01-03 19:55:44 +01:00
|
|
|
auto bufferSize = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), 1, MAX_BUFFER_SIZE);
|
2019-12-29 18:42:35 +01:00
|
|
|
ExprEngine::ValuePtr pointerValue;
|
|
|
|
if (valueType->type == ValueType::Type::RECORD)
|
2020-12-14 12:32:30 +01:00
|
|
|
pointerValue = createStructVal(var.nameToken(), valueType->typeScope, var.isLocal() && !var.isStatic(), data);
|
2019-12-29 18:42:35 +01:00
|
|
|
else {
|
|
|
|
ValueType vt(*valueType);
|
|
|
|
vt.pointer = 0;
|
2019-12-31 17:51:58 +01:00
|
|
|
if (vt.constness & 1)
|
2020-06-21 21:48:36 +02:00
|
|
|
pointerValue = getValueRangeFromValueType(&vt, data);
|
2019-12-31 17:51:58 +01:00
|
|
|
else
|
|
|
|
pointerValue = std::make_shared<ExprEngine::UninitValue>();
|
2019-12-29 18:42:35 +01:00
|
|
|
}
|
2019-12-31 17:51:58 +01:00
|
|
|
return std::make_shared<ExprEngine::ArrayValue>(data.getNewSymbolName(), bufferSize, pointerValue, true, true, var.isLocal() && !var.isStatic());
|
2019-09-22 10:56:49 +02:00
|
|
|
}
|
2019-09-27 18:58:23 +02:00
|
|
|
if (var.isArray())
|
2019-09-29 15:00:54 +02:00
|
|
|
return std::make_shared<ExprEngine::ArrayValue>(&data, &var);
|
2019-12-30 19:47:18 +01:00
|
|
|
if (valueType->isIntegral() || valueType->isFloat()) {
|
2019-12-31 14:57:42 +01:00
|
|
|
ExprEngine::ValuePtr value;
|
|
|
|
if (var.isLocal() && !var.isStatic())
|
|
|
|
value = std::make_shared<ExprEngine::UninitValue>();
|
|
|
|
else
|
2020-06-21 21:48:36 +02:00
|
|
|
value = getValueRangeFromValueType(valueType, data);
|
2019-12-24 21:14:14 +01:00
|
|
|
data.addConstraints(value, var.nameToken());
|
|
|
|
return value;
|
|
|
|
}
|
2020-07-18 18:54:21 +02:00
|
|
|
if (valueType->type == ValueType::Type::RECORD) {
|
2020-12-08 13:34:46 +01:00
|
|
|
bool uninitData = true;
|
2020-07-18 18:54:21 +02:00
|
|
|
if (var.isLocal() && !var.isStatic()) {
|
2020-12-08 13:34:46 +01:00
|
|
|
uninitData = !valueType->typeScope ||
|
|
|
|
!valueType->typeScope->definedType ||
|
|
|
|
valueType->typeScope->definedType->needInitialization != Type::NeedInitialization::False;
|
2020-07-18 18:54:21 +02:00
|
|
|
}
|
2020-12-08 13:34:46 +01:00
|
|
|
if (var.isArgument() && var.isConst())
|
|
|
|
uninitData = false;
|
2020-12-14 12:32:30 +01:00
|
|
|
return createStructVal(var.nameToken(), valueType->typeScope, uninitData, data);
|
2020-07-18 18:54:21 +02:00
|
|
|
}
|
2019-09-22 10:56:49 +02:00
|
|
|
if (valueType->smartPointerType) {
|
2020-12-14 12:32:30 +01:00
|
|
|
auto structValue = createStructVal(var.nameToken(), valueType->smartPointerType->classScope, var.isLocal() && !var.isStatic(), data);
|
2022-01-03 19:55:44 +01:00
|
|
|
auto size = std::make_shared<ExprEngine::IntRange>(data.getNewSymbolName(), 1, MAX_BUFFER_SIZE);
|
2019-12-29 16:26:11 +01:00
|
|
|
return std::make_shared<ExprEngine::ArrayValue>(data.getNewSymbolName(), size, structValue, true, true, false);
|
2019-09-22 10:56:49 +02:00
|
|
|
}
|
2020-06-21 21:48:36 +02:00
|
|
|
return getValueRangeFromValueType(valueType, data);
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 17:16:13 +02:00
|
|
|
void ExprEngine::executeFunction(const Scope *functionScope, ErrorLogger *errorLogger, const Tokenizer *tokenizer, const Settings *settings, const std::vector<ExprEngine::Callback> &callbacks, std::ostream &report)
|
2019-09-17 21:00:59 +02:00
|
|
|
{
|
|
|
|
if (!functionScope->bodyStart)
|
|
|
|
return;
|
|
|
|
const Function *function = functionScope->function;
|
|
|
|
if (!function)
|
|
|
|
return;
|
2019-09-21 19:34:06 +02:00
|
|
|
if (functionScope->bodyStart->fileIndex() > 0)
|
|
|
|
// TODO.. what about functions in headers?
|
|
|
|
return;
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2020-04-27 09:08:50 +02:00
|
|
|
const std::string currentFunction = function->fullName();
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
int symbolValueIndex = 0;
|
2019-09-19 20:18:39 +02:00
|
|
|
TrackExecution trackExecution;
|
2020-04-28 17:16:13 +02:00
|
|
|
Data data(&symbolValueIndex, errorLogger, tokenizer, settings, currentFunction, callbacks, &trackExecution);
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2019-09-26 20:48:33 +02:00
|
|
|
for (const Variable &arg : function->argumentList)
|
|
|
|
data.assignValue(functionScope->bodyStart, arg.declarationId(), createVariableValue(arg, data));
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2022-01-03 17:10:01 +01:00
|
|
|
#ifdef CONTRACT
|
2020-04-27 09:08:50 +02:00
|
|
|
data.contractConstraints(function, executeExpression1);
|
2022-01-03 17:10:01 +01:00
|
|
|
#endif
|
2020-04-27 09:08:50 +02:00
|
|
|
|
2020-12-18 20:41:10 +01:00
|
|
|
const std::time_t stopTime = data.startTime + data.settings->bugHuntingCheckFunctionMaxTime;
|
2020-12-18 19:58:53 +01:00
|
|
|
|
2019-12-22 21:00:53 +01:00
|
|
|
try {
|
|
|
|
execute(functionScope->bodyStart, functionScope->bodyEnd, data);
|
2020-12-12 17:33:10 +01:00
|
|
|
} catch (const ExprEngineException &e) {
|
2020-05-15 20:58:33 +02:00
|
|
|
if (settings->debugBugHunting)
|
2020-12-13 16:54:22 +01:00
|
|
|
report << "ExprEngineException " << e.tok->linenr() << ":" << e.tok->column() << ": " << e.what << "\n";
|
2019-12-22 21:00:53 +01:00
|
|
|
trackExecution.setAbortLine(e.tok->linenr());
|
|
|
|
auto bailoutValue = std::make_shared<BailoutValue>();
|
2020-01-12 11:53:49 +01:00
|
|
|
for (const Token *tok = e.tok; tok != functionScope->bodyEnd; tok = tok->next()) {
|
2020-12-18 19:58:53 +01:00
|
|
|
if (std::time(nullptr) >= stopTime)
|
|
|
|
break;
|
2020-01-12 11:53:49 +01:00
|
|
|
if (Token::Match(tok, "return|throw|while|if|for (")) {
|
|
|
|
tok = tok->next();
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-22 21:00:53 +01:00
|
|
|
call(callbacks, tok, bailoutValue, &data);
|
2020-01-12 11:53:49 +01:00
|
|
|
}
|
2019-12-22 21:00:53 +01:00
|
|
|
}
|
2019-09-19 20:18:39 +02:00
|
|
|
|
2020-01-14 21:17:07 +01:00
|
|
|
const bool bugHuntingReport = !settings->bugHuntingReport.empty();
|
|
|
|
|
|
|
|
if (settings->debugBugHunting && (settings->verbose || callbacks.empty() || !trackExecution.isAllOk())) {
|
|
|
|
if (bugHuntingReport)
|
2019-12-27 19:05:10 +01:00
|
|
|
report << "[debug]" << std::endl;
|
|
|
|
trackExecution.print(report);
|
2019-12-28 22:09:16 +01:00
|
|
|
if (!callbacks.empty()) {
|
2020-01-14 21:17:07 +01:00
|
|
|
if (bugHuntingReport)
|
2019-12-28 22:09:16 +01:00
|
|
|
report << "[details]" << std::endl;
|
|
|
|
trackExecution.report(report, functionScope);
|
|
|
|
}
|
2019-09-19 20:18:39 +02:00
|
|
|
}
|
2019-12-22 21:00:53 +01:00
|
|
|
|
2020-01-18 07:25:39 +01:00
|
|
|
// Write a report
|
2020-01-14 21:17:07 +01:00
|
|
|
if (bugHuntingReport) {
|
2020-08-22 11:37:44 +02:00
|
|
|
std::set<std::string> intvars;
|
|
|
|
for (const Scope &scope: tokenizer->getSymbolDatabase()->scopeList) {
|
|
|
|
if (scope.isExecutable())
|
|
|
|
continue;
|
|
|
|
std::string path;
|
|
|
|
bool valid = true;
|
|
|
|
for (const Scope *s = &scope; s->type != Scope::ScopeType::eGlobal; s = s->nestedIn) {
|
|
|
|
if (s->isExecutable()) {
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
path = s->className + "::" + path;
|
|
|
|
}
|
|
|
|
if (!valid)
|
|
|
|
continue;
|
|
|
|
for (const Variable &var: scope.varlist) {
|
|
|
|
if (var.nameToken() && !var.nameToken()->hasCppcheckAttributes() && var.valueType() && var.valueType()->pointer == 0 && var.valueType()->constness == 0 && var.valueType()->isIntegral())
|
|
|
|
intvars.insert(path + var.name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const std::string &v: intvars)
|
|
|
|
report << "[intvar] " << v << std::endl;
|
2020-04-28 22:09:01 +02:00
|
|
|
for (const std::string &f: trackExecution.getMissingContracts())
|
|
|
|
report << "[missing contract] " << f << std::endl;
|
2019-12-27 19:05:10 +01:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExprEngine::runChecks(ErrorLogger *errorLogger, const Tokenizer *tokenizer, const Settings *settings)
|
|
|
|
{
|
2020-01-21 20:19:51 +01:00
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
std::vector<ExprEngine::Callback> callbacks;
|
2020-06-19 13:16:48 +02:00
|
|
|
addBughuntingChecks(&callbacks);
|
2019-12-27 19:05:10 +01:00
|
|
|
|
|
|
|
std::ostringstream report;
|
2020-04-28 17:16:13 +02:00
|
|
|
ExprEngine::executeAllFunctions(errorLogger, tokenizer, settings, callbacks, report);
|
2020-01-14 21:17:07 +01:00
|
|
|
if (settings->bugHuntingReport.empty())
|
2020-01-01 19:57:38 +01:00
|
|
|
std::cout << report.str();
|
|
|
|
else if (errorLogger)
|
2020-01-18 07:25:39 +01:00
|
|
|
errorLogger->bughuntingReport(report.str());
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
2020-05-27 19:37:07 +02:00
|
|
|
|
|
|
|
static void dumpRecursive(ExprEngine::ValuePtr val)
|
|
|
|
{
|
2020-05-27 21:31:40 +02:00
|
|
|
if (!val) {
|
2020-05-27 19:37:07 +02:00
|
|
|
std::cout << "NULL";
|
2020-05-27 21:31:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-05-27 19:37:07 +02:00
|
|
|
switch (val->type) {
|
|
|
|
case ExprEngine::ValueType::AddressOfValue:
|
|
|
|
std::cout << "AddressOfValue(" << std::dynamic_pointer_cast<ExprEngine::AddressOfValue>(val)->varId << ")";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::ArrayValue:
|
|
|
|
std::cout << "ArrayValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::BailoutValue:
|
|
|
|
std::cout << "BailoutValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::BinOpResult: {
|
|
|
|
auto b = std::dynamic_pointer_cast<ExprEngine::BinOpResult>(val);
|
|
|
|
std::cout << "(";
|
|
|
|
dumpRecursive(b->op1);
|
|
|
|
std::cout << " " << b->binop << " ";
|
|
|
|
dumpRecursive(b->op2);
|
|
|
|
std::cout << ")";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::ConditionalValue:
|
|
|
|
std::cout << "ConditionalValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::FloatRange:
|
|
|
|
std::cout << "FloatRange";
|
|
|
|
break;
|
2020-12-05 13:02:43 +01:00
|
|
|
case ExprEngine::ValueType::FunctionCallArgumentValues: {
|
|
|
|
std::cout << "FunctionCallArgumentValues(";
|
|
|
|
const char *sep = "";
|
|
|
|
for (auto arg: std::dynamic_pointer_cast<ExprEngine::FunctionCallArgumentValues>(val)->argValues) {
|
|
|
|
std::cout << sep;
|
|
|
|
sep = ",";
|
|
|
|
if (!arg)
|
|
|
|
std::cout << "NULL";
|
|
|
|
else
|
|
|
|
dumpRecursive(arg);
|
|
|
|
}
|
|
|
|
std::cout << ")";
|
|
|
|
}
|
|
|
|
break;
|
2020-05-27 19:37:07 +02:00
|
|
|
case ExprEngine::ValueType::IntRange:
|
|
|
|
std::cout << "IntRange";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::IntegerTruncation:
|
|
|
|
std::cout << "IntegerTruncation(";
|
|
|
|
dumpRecursive(std::dynamic_pointer_cast<ExprEngine::IntegerTruncation>(val)->inputValue);
|
|
|
|
std::cout << ")";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::StringLiteralValue:
|
|
|
|
std::cout << "StringLiteralValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::StructValue:
|
|
|
|
std::cout << "StructValue";
|
|
|
|
break;
|
|
|
|
case ExprEngine::ValueType::UninitValue:
|
|
|
|
std::cout << "UninitValue";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExprEngine::dump(ExprEngine::ValuePtr val)
|
|
|
|
{
|
|
|
|
dumpRecursive(val);
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|