2019-09-17 21:00:59 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2020-05-10 11:16:32 +02:00
|
|
|
* Copyright (C) 2007-2020 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-13 13:44:48 +02:00
|
|
|
#include "config.h"
|
2019-09-17 21:00:59 +02:00
|
|
|
#include "exprengine.h"
|
2020-04-13 13:44:48 +02:00
|
|
|
#include "library.h"
|
|
|
|
#include "platform.h"
|
2019-09-17 21:00:59 +02:00
|
|
|
#include "settings.h"
|
2020-04-13 13:44:48 +02:00
|
|
|
#include "token.h"
|
2019-09-17 21:00:59 +02:00
|
|
|
#include "tokenize.h"
|
|
|
|
#include "testsuite.h"
|
|
|
|
|
2019-09-22 21:14:20 +02:00
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
class TestExprEngine : public TestFixture {
|
|
|
|
public:
|
|
|
|
TestExprEngine() : TestFixture("TestExprEngine") {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void run() OVERRIDE {
|
2019-10-02 17:59:04 +02:00
|
|
|
#ifdef USE_Z3
|
2019-12-24 21:14:14 +01:00
|
|
|
TEST_CASE(annotation1);
|
2020-01-21 18:55:07 +01:00
|
|
|
TEST_CASE(annotation2);
|
2019-12-24 21:14:14 +01:00
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
TEST_CASE(expr1);
|
|
|
|
TEST_CASE(expr2);
|
|
|
|
TEST_CASE(expr3);
|
|
|
|
TEST_CASE(expr4);
|
|
|
|
TEST_CASE(expr5);
|
2019-10-09 11:24:57 +02:00
|
|
|
TEST_CASE(expr6);
|
2019-10-09 20:17:26 +02:00
|
|
|
TEST_CASE(expr7);
|
2019-09-21 21:15:51 +02:00
|
|
|
TEST_CASE(exprAssign1);
|
2019-09-23 20:27:13 +02:00
|
|
|
TEST_CASE(exprAssign2); // Truncation
|
2019-09-17 21:00:59 +02:00
|
|
|
|
|
|
|
TEST_CASE(if1);
|
2019-12-24 15:51:45 +01:00
|
|
|
TEST_CASE(if2);
|
|
|
|
TEST_CASE(if3);
|
2019-12-30 12:53:59 +01:00
|
|
|
TEST_CASE(if4);
|
2020-05-10 22:50:23 +02:00
|
|
|
TEST_CASE(if5);
|
2019-09-17 21:00:59 +02:00
|
|
|
TEST_CASE(ifelse1);
|
|
|
|
|
2019-10-06 19:58:51 +02:00
|
|
|
TEST_CASE(switch1);
|
2020-05-01 15:15:24 +02:00
|
|
|
TEST_CASE(switch2);
|
2019-10-06 19:58:51 +02:00
|
|
|
|
2019-10-07 17:44:26 +02:00
|
|
|
TEST_CASE(while1);
|
|
|
|
TEST_CASE(while2);
|
2019-10-14 19:41:32 +02:00
|
|
|
TEST_CASE(while3);
|
2020-04-30 21:05:34 +02:00
|
|
|
TEST_CASE(while4);
|
2020-05-15 20:58:33 +02:00
|
|
|
TEST_CASE(while5);
|
2019-10-07 17:44:26 +02:00
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
TEST_CASE(array1);
|
|
|
|
TEST_CASE(array2);
|
|
|
|
TEST_CASE(array3);
|
|
|
|
TEST_CASE(array4);
|
|
|
|
TEST_CASE(arrayInit1);
|
|
|
|
TEST_CASE(arrayInit2);
|
|
|
|
TEST_CASE(arrayUninit);
|
|
|
|
|
|
|
|
TEST_CASE(floatValue1);
|
|
|
|
TEST_CASE(floatValue2);
|
2019-12-30 19:47:18 +01:00
|
|
|
TEST_CASE(floatValue3);
|
2019-09-29 15:00:54 +02:00
|
|
|
|
|
|
|
TEST_CASE(functionCall1);
|
|
|
|
TEST_CASE(functionCall2);
|
|
|
|
TEST_CASE(functionCall3);
|
2020-01-15 15:23:58 +01:00
|
|
|
TEST_CASE(functionCall4);
|
2019-09-17 21:00:59 +02:00
|
|
|
|
2020-04-28 17:16:13 +02:00
|
|
|
TEST_CASE(functionCallContract1);
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
TEST_CASE(int1);
|
2019-09-29 08:26:09 +02:00
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
TEST_CASE(pointer1);
|
2019-12-29 16:26:11 +01:00
|
|
|
TEST_CASE(pointer2);
|
2019-09-17 21:00:59 +02:00
|
|
|
TEST_CASE(pointerAlias1);
|
|
|
|
TEST_CASE(pointerAlias2);
|
2019-09-22 15:58:55 +02:00
|
|
|
TEST_CASE(pointerAlias3);
|
|
|
|
TEST_CASE(pointerAlias4);
|
2019-09-23 18:10:06 +02:00
|
|
|
TEST_CASE(pointerNull1);
|
2019-10-02 17:59:04 +02:00
|
|
|
|
2019-12-29 18:42:35 +01:00
|
|
|
TEST_CASE(structMember1);
|
|
|
|
TEST_CASE(structMember2);
|
2020-04-30 21:05:34 +02:00
|
|
|
TEST_CASE(structMember3);
|
2020-05-15 21:34:52 +02:00
|
|
|
|
|
|
|
TEST_CASE(ternaryOperator1);
|
2019-10-02 17:59:04 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-13 09:50:25 +02:00
|
|
|
static void replace(std::string& str, const std::string& from, const std::string& to) {
|
|
|
|
size_t pos = 0;
|
|
|
|
while ((pos = str.find(from, pos)) != std::string::npos)
|
|
|
|
str.replace(pos, from.length(), to);
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:21:10 +02:00
|
|
|
static std::string cleanupExpr(std::string rawexpr) {
|
|
|
|
std::string ret;
|
|
|
|
std::istringstream istr(rawexpr);
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(istr, line)) {
|
|
|
|
if (line.empty())
|
|
|
|
continue;
|
|
|
|
line = line.substr(line.find_first_not_of(" "));
|
|
|
|
if (line.compare(0,13,"(declare-fun ") == 0)
|
|
|
|
continue;
|
|
|
|
if (line == "(solver")
|
|
|
|
continue;
|
|
|
|
if (line.compare(0,9,"(assert (") == 0) {
|
|
|
|
line.erase(0,8);
|
|
|
|
line.erase(line.size()-1);
|
|
|
|
}
|
2020-05-13 09:56:21 +02:00
|
|
|
replace(line, "(fp.gt ", "(> ");
|
|
|
|
replace(line, "(fp.lt ", "(< ");
|
2020-05-08 12:21:10 +02:00
|
|
|
int par = 0;
|
|
|
|
for (int pos = 0; pos < line.size(); ++pos) {
|
|
|
|
if (line[pos] == '(')
|
|
|
|
par++;
|
|
|
|
else if (line[pos] == ')')
|
|
|
|
--par;
|
|
|
|
}
|
|
|
|
if (par < 0)
|
|
|
|
line.erase(line.size() - 1);
|
|
|
|
ret += line + "\n";
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
std::string expr(const char code[], const std::string &binop) {
|
|
|
|
Settings settings;
|
|
|
|
settings.platform(cppcheck::Platform::Unix64);
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
std::string ret;
|
2019-10-02 21:47:00 +02:00
|
|
|
std::function<void(const Token *, const ExprEngine::Value &, ExprEngine::DataBase *)> f = [&](const Token *tok, const ExprEngine::Value &value, ExprEngine::DataBase *dataBase) {
|
2019-10-02 17:59:04 +02:00
|
|
|
if (tok->str() != binop)
|
|
|
|
return;
|
2020-04-04 11:44:59 +02:00
|
|
|
const auto *b = dynamic_cast<const ExprEngine::BinOpResult *>(&value);
|
2019-10-02 17:59:04 +02:00
|
|
|
if (!b)
|
|
|
|
return;
|
2020-05-08 12:21:10 +02:00
|
|
|
ret += TestExprEngine::cleanupExpr(b->getExpr(dataBase));
|
2019-10-02 17:59:04 +02:00
|
|
|
};
|
|
|
|
std::vector<ExprEngine::Callback> callbacks;
|
|
|
|
callbacks.push_back(f);
|
2019-12-27 19:05:10 +01:00
|
|
|
std::ostringstream trace;
|
2020-04-28 17:16:13 +02:00
|
|
|
ExprEngine::executeAllFunctions(this, &tokenizer, &settings, callbacks, trace);
|
2019-10-02 17:59:04 +02:00
|
|
|
return ret;
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 17:16:13 +02:00
|
|
|
std::string functionCallContractExpr(const char code[], const Settings &s) {
|
|
|
|
Settings settings;
|
|
|
|
settings.bugHunting = true;
|
|
|
|
settings.debugBugHunting = true;
|
|
|
|
settings.functionContracts = s.functionContracts;
|
|
|
|
settings.platform(cppcheck::Platform::Unix64);
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
std::vector<ExprEngine::Callback> callbacks;
|
|
|
|
std::ostringstream trace;
|
|
|
|
ExprEngine::executeAllFunctions(this, &tokenizer, &settings, callbacks, trace);
|
|
|
|
std::string ret = trace.str();
|
|
|
|
std::string::size_type pos1 = ret.find("checkContract:{");
|
|
|
|
std::string::size_type pos2 = ret.find("}", pos1);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
return "Error:" + ret;
|
2020-05-08 12:21:10 +02:00
|
|
|
return TestExprEngine::cleanupExpr(ret.substr(pos1, pos2+1-pos1));
|
2020-04-28 17:16:13 +02:00
|
|
|
}
|
|
|
|
|
2019-09-22 15:58:55 +02:00
|
|
|
std::string getRange(const char code[], const std::string &str, int linenr = 0) {
|
2019-09-17 21:00:59 +02:00
|
|
|
Settings settings;
|
|
|
|
settings.platform(cppcheck::Platform::Unix64);
|
2019-09-22 10:56:49 +02:00
|
|
|
settings.library.smartPointers.insert("std::shared_ptr");
|
2019-09-17 21:00:59 +02:00
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
std::string ret;
|
2019-10-02 21:47:00 +02:00
|
|
|
std::function<void(const Token *, const ExprEngine::Value &, ExprEngine::DataBase *)> f = [&](const Token *tok, const ExprEngine::Value &value, ExprEngine::DataBase *dataBase) {
|
|
|
|
(void)dataBase;
|
2019-09-22 15:58:55 +02:00
|
|
|
if ((linenr == 0 || linenr == tok->linenr()) && tok->expressionString() == str) {
|
2019-09-21 14:17:16 +02:00
|
|
|
if (!ret.empty())
|
|
|
|
ret += ",";
|
2019-09-17 21:00:59 +02:00
|
|
|
ret += value.getRange();
|
2019-09-21 14:17:16 +02:00
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
};
|
|
|
|
std::vector<ExprEngine::Callback> callbacks;
|
|
|
|
callbacks.push_back(f);
|
2019-12-27 19:05:10 +01:00
|
|
|
std::ostringstream trace;
|
2020-04-28 17:16:13 +02:00
|
|
|
ExprEngine::executeAllFunctions(this, &tokenizer, &settings, callbacks, trace);
|
2019-09-17 21:00:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
std::string trackExecution(const char code[]) {
|
|
|
|
Settings settings;
|
2020-01-14 21:17:07 +01:00
|
|
|
settings.bugHunting = true;
|
|
|
|
settings.debugBugHunting = true;
|
2019-09-29 15:00:54 +02:00
|
|
|
settings.platform(cppcheck::Platform::Unix64);
|
|
|
|
settings.library.smartPointers.insert("std::shared_ptr");
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
2019-12-27 19:05:10 +01:00
|
|
|
std::vector<ExprEngine::Callback> callbacks;
|
2019-09-29 15:00:54 +02:00
|
|
|
std::ostringstream ret;
|
2020-04-28 17:16:13 +02:00
|
|
|
ExprEngine::executeAllFunctions(this, &tokenizer, &settings, callbacks, ret);
|
2019-09-29 15:00:54 +02:00
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
2019-12-24 21:14:14 +01:00
|
|
|
void annotation1() {
|
|
|
|
const char code[] = "void f(__cppcheck_low__(100) short x) {\n"
|
|
|
|
" return x < 10;\n"
|
|
|
|
"}";
|
|
|
|
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(>= $1 100)\n" // <- annotation
|
|
|
|
"(and (>= $1 (- 32768)) (<= $1 32767))\n"
|
|
|
|
"(< $1 10)\n"
|
|
|
|
"z3::unsat\n";
|
2019-12-24 21:14:14 +01:00
|
|
|
|
|
|
|
ASSERT_EQUALS(expected, expr(code, "<"));
|
|
|
|
}
|
|
|
|
|
2020-01-21 18:55:07 +01:00
|
|
|
void annotation2() {
|
|
|
|
const char code[] = "__cppcheck_low__(100) short x;\n"
|
|
|
|
" void f() {\n"
|
|
|
|
" return x < 10;\n"
|
|
|
|
"}";
|
|
|
|
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(>= $1 100)\n" // <- annotation
|
|
|
|
"(and (>= $1 (- 32768)) (<= $1 32767))\n"
|
|
|
|
"(< $1 10)\n"
|
|
|
|
"z3::unsat\n";
|
2020-01-21 18:55:07 +01:00
|
|
|
|
|
|
|
ASSERT_EQUALS(expected, expr(code, "<"));
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void expr1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("-32768:32767", getRange("void f(short x) { a = x; }", "x"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr2() {
|
2019-10-02 17:59:04 +02:00
|
|
|
ASSERT_EQUALS("($1)+($1)", getRange("void f(short x) { a = x + x; }", "x+x"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr3() {
|
2019-10-02 17:59:04 +02:00
|
|
|
ASSERT_EQUALS("($1)+($1)", getRange("int f(short x) { int a = x + x; return a; }", "return a"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr4() {
|
2019-10-02 17:59:04 +02:00
|
|
|
ASSERT_EQUALS("($1)-($1)", getRange("int f(short x) { int a = x - x; return a; }", "return a"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void expr5() {
|
2019-10-02 17:59:04 +02:00
|
|
|
ASSERT_EQUALS("($1)+($2)", getRange("void f(short a, short b, short c, short d) { if (a+b<c+d) {} }", "a+b"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 11:24:57 +02:00
|
|
|
void expr6() {
|
|
|
|
const char code[] = "void f(unsigned char x) {\n"
|
|
|
|
" unsigned char result = 8 - x;\n"
|
|
|
|
" result > 1000;"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
ASSERT_EQUALS("(8)-($1)", getRange(code, "8-x"));
|
|
|
|
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $1 0) (<= $1 255))\n"
|
|
|
|
"(> (- 8 $1) 1000)\n"
|
|
|
|
"z3::unsat\n",
|
2019-10-09 11:24:57 +02:00
|
|
|
expr(code, ">"));
|
|
|
|
}
|
|
|
|
|
2019-10-09 20:17:26 +02:00
|
|
|
void expr7() {
|
|
|
|
const char code[] = "void f(bool a, bool b, int c) {\n"
|
|
|
|
" if (a||b) {}\n"
|
|
|
|
" c > 1000;"
|
|
|
|
"}";
|
|
|
|
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(or (distinct $1 0) (distinct $2 0))\n"
|
|
|
|
"(and (>= $3 (- 2147483648)) (<= $3 2147483647))\n"
|
|
|
|
"(and (>= $1 0) (<= $1 1))\n"
|
|
|
|
"(and (>= $2 0) (<= $2 1))\n"
|
|
|
|
"(> $3 1000)\n"
|
2019-10-09 20:17:26 +02:00
|
|
|
"z3::sat\n"
|
2020-05-08 12:21:10 +02:00
|
|
|
"(= (ite (or (distinct $1 0) (distinct $2 0)) 1 0) 0)\n"
|
|
|
|
"(and (>= $3 (- 2147483648)) (<= $3 2147483647))\n"
|
|
|
|
"(and (>= $1 0) (<= $1 1))\n"
|
|
|
|
"(and (>= $2 0) (<= $2 1))\n"
|
|
|
|
"(> $3 1000)\n"
|
|
|
|
"z3::sat\n",
|
2019-10-09 20:17:26 +02:00
|
|
|
expr(code, ">"));
|
|
|
|
}
|
|
|
|
|
2019-09-21 21:15:51 +02:00
|
|
|
void exprAssign1() {
|
2019-10-02 17:59:04 +02:00
|
|
|
ASSERT_EQUALS("($1)+(1)", getRange("void f(unsigned char a) { a += 1; }", "a+=1"));
|
2019-09-21 21:15:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 20:27:13 +02:00
|
|
|
void exprAssign2() {
|
|
|
|
ASSERT_EQUALS("2", getRange("void f(unsigned char x) { x = 258; int a = x }", "a=x"));
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void if1() {
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(< $1 $2)\n"
|
|
|
|
"(and (>= $1 (- 2147483648)) (<= $1 2147483647))\n"
|
|
|
|
"(and (>= $2 (- 2147483648)) (<= $2 2147483647))\n"
|
|
|
|
"(= $1 $2)\n"
|
|
|
|
"z3::unsat\n",
|
2019-10-02 21:47:00 +02:00
|
|
|
expr("void f(int x, int y) { if (x < y) return x == y; }", "=="));
|
2019-09-20 21:27:51 +02:00
|
|
|
}
|
|
|
|
|
2019-12-24 15:51:45 +01:00
|
|
|
void if2() {
|
|
|
|
const char code[] = "void foo(int x) {\n"
|
|
|
|
" if (x > 0 && x == 20) {}\n"
|
|
|
|
"}";
|
|
|
|
// In expression "x + x < 20", "x" is greater than 0
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(> $1 0)\n"
|
|
|
|
"(and (>= $1 (- 2147483648)) (<= $1 2147483647))\n"
|
|
|
|
"(= $1 20)\n"
|
|
|
|
"z3::sat\n";
|
2019-12-24 15:51:45 +01:00
|
|
|
ASSERT_EQUALS(expected, expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
|
|
|
void if3() {
|
|
|
|
const char code[] = "void foo(int x) {\n"
|
|
|
|
" if (x > 0 || x == 20) {}\n"
|
|
|
|
"}";
|
|
|
|
// In expression "x + x < 20", "x" is greater than 0
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(<= $1 0)\n"
|
|
|
|
"(and (>= $1 (- 2147483648)) (<= $1 2147483647))\n"
|
|
|
|
"(= $1 20)\n"
|
|
|
|
"z3::unsat\n"; // "x == 20" is unsat
|
2019-12-24 15:51:45 +01:00
|
|
|
ASSERT_EQUALS(expected, expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
2019-12-30 12:53:59 +01:00
|
|
|
void if4() {
|
|
|
|
const char code[] = "void foo(unsigned int x, unsigned int y) {\n"
|
|
|
|
" unsigned int z = y;"
|
|
|
|
" if (x < z) { return z == 0; }\n"
|
|
|
|
"}";
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(< $1 $2)\n"
|
|
|
|
"(>= $2 0)\n"
|
|
|
|
"(>= $1 0)\n"
|
|
|
|
"(= $2 0)\n"
|
|
|
|
"z3::unsat\n";
|
2019-12-30 12:53:59 +01:00
|
|
|
ASSERT_EQUALS(expected, expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:50:23 +02:00
|
|
|
void if5() {
|
|
|
|
ASSERT_EQUALS("(> |$2:0| 12)\n"
|
|
|
|
"(and (>= |$2:0| (- 2147483648)) (<= |$2:0| 2147483647))\n"
|
|
|
|
"(= |$2:0| 5)\n"
|
|
|
|
"z3::unsat\n",
|
|
|
|
expr("void foo(const int *x) { if (f1() && *x > 12) dostuff(*x == 5); }", "=="));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void ifelse1() {
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(<= $1 5)\n"
|
|
|
|
"(and (>= $1 (- 32768)) (<= $1 32767))\n"
|
|
|
|
"(= (+ $1 2) 40)\n"
|
|
|
|
"z3::unsat\n",
|
2019-10-02 21:47:00 +02:00
|
|
|
expr("void f(short x) { if (x > 5) ; else if (x+2==40); }", "=="));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
|
2019-10-06 19:58:51 +02:00
|
|
|
void switch1() {
|
|
|
|
const char code[] = "void f(int x) {\n"
|
|
|
|
" switch (x) {\n"
|
|
|
|
" case 1: x==3; break;\n"
|
|
|
|
" case 2: x>0; break;\n"
|
|
|
|
" };\n"
|
|
|
|
" x<=4;\n"
|
|
|
|
"}";
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(= $1 1)\n"
|
|
|
|
"(and (>= $1 (- 2147483648)) (<= $1 2147483647))\n"
|
|
|
|
"(= $1 3)\n"
|
|
|
|
"z3::unsat\n",
|
2019-10-06 19:58:51 +02:00
|
|
|
expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:15:24 +02:00
|
|
|
void switch2() {
|
|
|
|
const char code[] = "void foo(char type, int mcc) {\n"
|
|
|
|
" switch (type) {\n"
|
|
|
|
" case '1':\n"
|
|
|
|
" case '3':\n"
|
|
|
|
" break;\n"
|
|
|
|
" default:\n"
|
|
|
|
" return false;\n"
|
|
|
|
" }\n"
|
|
|
|
" p[0] = mcc == 0;\n"
|
|
|
|
"}";
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(= $1 49)\n"
|
|
|
|
"(and (>= $2 (- 2147483648)) (<= $2 2147483647))\n"
|
|
|
|
"(and (>= $1 (- 128)) (<= $1 127))\n"
|
|
|
|
"(= $2 0)\n"
|
2020-05-01 15:15:24 +02:00
|
|
|
"z3::sat\n"
|
2020-05-08 12:21:10 +02:00
|
|
|
"(= $1 51)\n"
|
|
|
|
"(and (>= $2 (- 2147483648)) (<= $2 2147483647))\n"
|
|
|
|
"(and (>= $1 (- 128)) (<= $1 127))\n"
|
|
|
|
"(= $2 0)\n"
|
|
|
|
"z3::sat\n",
|
2020-05-01 15:15:24 +02:00
|
|
|
expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:44:26 +02:00
|
|
|
void while1() {
|
|
|
|
const char code[] = "void f(int y) {\n"
|
|
|
|
" int x = 0;\n"
|
|
|
|
" while (x < y)\n"
|
|
|
|
" x = x + 34;\n"
|
2019-10-09 11:24:57 +02:00
|
|
|
" x == 340;\n"
|
2019-10-07 17:44:26 +02:00
|
|
|
"}";
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $2 (- 2147483648)) (<= $2 2147483647))\n"
|
|
|
|
"(= (+ $2 34) 340)\n"
|
|
|
|
"z3::sat\n",
|
2019-10-07 17:44:26 +02:00
|
|
|
expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
|
|
|
void while2() {
|
|
|
|
const char code[] = "void f(int y) {\n"
|
|
|
|
" int x = 0;\n"
|
|
|
|
" while (x < y)\n"
|
|
|
|
" x++;\n"
|
|
|
|
" x == 1;\n"
|
|
|
|
"}";
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $2 (- 2147483648)) (<= $2 2147483647))\n"
|
|
|
|
"(= $2 1)\n"
|
|
|
|
"z3::sat\n",
|
2019-10-07 17:44:26 +02:00
|
|
|
expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
2019-10-14 19:41:32 +02:00
|
|
|
void while3() {
|
|
|
|
const char code[] = "struct AB {int a; int b;};\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (1)\n"
|
|
|
|
" ab.a = 3;\n"
|
|
|
|
" ab.a == 0;\n"
|
|
|
|
"}";
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(= 3 0)\n"
|
|
|
|
"z3::unsat\n",
|
2019-10-14 19:41:32 +02:00
|
|
|
expr(code, "=="));
|
|
|
|
}
|
2019-10-06 19:58:51 +02:00
|
|
|
|
2020-04-30 21:05:34 +02:00
|
|
|
void while4() {
|
|
|
|
const char code[] = "void f(const char *host, int *len) {\n"
|
|
|
|
" while (*host)\n"
|
|
|
|
" *len = 0;\n"
|
|
|
|
" *len == 0;\n"
|
|
|
|
"}";
|
|
|
|
// Currently the *len gets a BailoutValue in the loop
|
|
|
|
ASSERT_EQUALS("", expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
2020-05-15 20:58:33 +02:00
|
|
|
void while5() {
|
|
|
|
const char code[] = "void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" while (cond)\n"
|
|
|
|
" x += 4;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT(getRange(code, "x", 4).find("?") != std::string::npos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void array1() {
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(= 5 0)\nz3::unsat\n",
|
2019-10-02 17:59:04 +02:00
|
|
|
expr("int f() { int arr[10]; arr[4] = 5; return arr[4]==0; }", "=="));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void array2() {
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= |$3:4| 0) (<= |$3:4| 255))\n"
|
|
|
|
"(= |$3:4| 365)\n"
|
|
|
|
"z3::unsat\n",
|
2019-10-02 17:59:04 +02:00
|
|
|
expr("void dostuff(unsigned char *); int f() { unsigned char arr[10] = \"\"; dostuff(arr); return arr[4] == 365; }", "=="));
|
2019-09-24 20:10:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void array3() {
|
2019-10-02 17:59:04 +02:00
|
|
|
const char code[] = "void f(unsigned char x) { int arr[10]; arr[4] = 43; return arr[x] == 12; }";
|
|
|
|
ASSERT_EQUALS("?,43", getRange(code, "arr[x]"));
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $1 0) (<= $1 255))\n"
|
|
|
|
"(= (ite (= $1 4) 43 0) 12)\n"
|
|
|
|
"z3::unsat\n",
|
2019-10-03 20:16:06 +02:00
|
|
|
expr(code, "=="));
|
2019-09-26 10:03:58 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void array4() {
|
|
|
|
const char code[] = "int buf[10];\n"
|
|
|
|
"void f() { int x = buf[0]; }";
|
2019-09-29 21:19:21 +02:00
|
|
|
ASSERT_EQUALS("2:16: $2:0=-2147483648:2147483647\n"
|
|
|
|
"2:20: $2=-2147483648:2147483647\n"
|
|
|
|
"2:26: { buf=($1,size=10,[:]=$2) x=$2:0}\n",
|
2019-09-29 15:00:54 +02:00
|
|
|
trackExecution(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrayInit1() {
|
2019-09-21 14:17:16 +02:00
|
|
|
ASSERT_EQUALS("0", getRange("inf f() { char arr[10] = \"\"; return arr[4]; }", "arr[4]"));
|
2019-09-21 11:36:34 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void arrayInit2() {
|
2019-09-24 20:10:51 +02:00
|
|
|
ASSERT_EQUALS("66", getRange("void f() { char str[] = \"hello\"; str[0] = \'B\'; }", "str[0]=\'B\'"));
|
2019-09-21 19:34:06 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void arrayUninit() {
|
2019-09-22 15:58:55 +02:00
|
|
|
ASSERT_EQUALS("?", getRange("int f() { int arr[10]; return arr[4]; }", "arr[4]"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void floatValue1() {
|
2019-12-30 19:47:18 +01:00
|
|
|
ASSERT_EQUALS("-inf:inf", getRange("float f; void func() { f=f; }", "f=f"));
|
2019-09-29 15:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void floatValue2() {
|
2019-10-02 17:59:04 +02:00
|
|
|
ASSERT_EQUALS("(29.0)/(2.0)", getRange("void func() { float f = 29.0; f = f / 2.0; }", "f/2.0"));
|
2019-09-29 15:00:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 19:47:18 +01:00
|
|
|
void floatValue3() {
|
|
|
|
const char code[] = "void foo(float f) { return f > 12.0; }";
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(> $1 |12.0|)\n"
|
|
|
|
"z3::sat\n";
|
2019-12-30 19:47:18 +01:00
|
|
|
ASSERT_EQUALS(expected, expr(code, ">"));
|
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
|
2019-09-29 15:00:54 +02:00
|
|
|
void functionCall1() {
|
|
|
|
ASSERT_EQUALS("-2147483648:2147483647", getRange("int atoi(const char *p); void f() { int x = atoi(a); x = x; }", "x=x"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void functionCall2() {
|
|
|
|
const char code[] = "namespace NS {\n"
|
|
|
|
" short getValue();\n"
|
|
|
|
"}"
|
|
|
|
"void f() {\n"
|
|
|
|
" short value = NS::getValue();\n"
|
|
|
|
" value = value;\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_EQUALS("-32768:32767", getRange(code, "value=value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void functionCall3() {
|
|
|
|
ASSERT_EQUALS("-2147483648:2147483647", getRange("int fgets(int, const char *, void *); void f() { int x = -1; fgets(stdin, \"%d\", &x); x=x; }", "x=x"));
|
|
|
|
}
|
|
|
|
|
2020-01-15 15:23:58 +01:00
|
|
|
void functionCall4() {
|
|
|
|
ASSERT_EQUALS("1:2147483647", getRange("void f() { sizeof(data); }", "sizeof(data)"));
|
|
|
|
}
|
|
|
|
|
2020-04-28 17:16:13 +02:00
|
|
|
void functionCallContract1() {
|
|
|
|
const char code[] = "void foo(int x);\n"
|
|
|
|
"void bar(unsigned short x) { foo(x); }";
|
|
|
|
|
|
|
|
Settings s;
|
|
|
|
s.functionContracts["foo(x)"] = "x < 1000";
|
|
|
|
|
|
|
|
ASSERT_EQUALS("checkContract:{\n"
|
2020-05-08 12:21:10 +02:00
|
|
|
"(ite (< $2 1000) false true)\n"
|
|
|
|
"(= $2 $1)\n"
|
|
|
|
"(and (>= $2 (- 2147483648)) (<= $2 2147483647))\n"
|
|
|
|
"(and (>= $1 0) (<= $1 65535))\n"
|
|
|
|
"}\n",
|
2020-04-28 17:16:13 +02:00
|
|
|
functionCallContractExpr(code, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
void int1() {
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $1 (- 2147483648)) (<= $1 2147483647))\n"
|
|
|
|
"(= (+ 2 $1) 3)\n"
|
|
|
|
"z3::sat\n",
|
2019-10-02 17:59:04 +02:00
|
|
|
expr("void f(int x) { return 2+x==3; }", "=="));
|
2019-09-29 08:26:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:04 +02:00
|
|
|
|
|
|
|
void pointer1() {
|
2019-10-03 21:08:42 +02:00
|
|
|
const char code[] = "void f(unsigned char *p) { return *p == 7; }";
|
2019-12-31 20:31:31 +01:00
|
|
|
ASSERT_EQUALS("size=$1,[:]=?,null", getRange(code, "p"));
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $3 0) (<= $3 255))\n"
|
|
|
|
"(= $3 7)\n"
|
|
|
|
"z3::sat\n",
|
2019-12-29 16:26:11 +01:00
|
|
|
expr(code, "=="));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointer2() {
|
|
|
|
const char code[] = "void f(unsigned char *p) { return p[2] == 7; }";
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $3 0) (<= $3 255))\n"
|
|
|
|
"(= $3 7)\n"
|
|
|
|
"z3::sat\n",
|
2019-10-03 21:08:42 +02:00
|
|
|
expr(code, "=="));
|
2019-09-29 08:26:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 21:00:59 +02:00
|
|
|
void pointerAlias1() {
|
2019-09-22 15:58:55 +02:00
|
|
|
ASSERT_EQUALS("3", getRange("int f() { int x; int *p = &x; x = 3; return *p; }", "return*p"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pointerAlias2() {
|
2019-09-22 15:58:55 +02:00
|
|
|
ASSERT_EQUALS("1", getRange("int f() { int x; int *p = &x; *p = 1; return *p; }", "return*p"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointerAlias3() {
|
|
|
|
ASSERT_EQUALS("7", getRange("int f() {\n"
|
|
|
|
" int x = 18;\n"
|
|
|
|
" int *p = &x;\n"
|
|
|
|
" *p = 7;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}", "x", 5));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pointerAlias4() {
|
|
|
|
ASSERT_EQUALS("71", getRange("int f() { int x[10]; int *p = x+3; *p = 71; return x[3]; }", "x[3]"));
|
2019-09-17 21:00:59 +02:00
|
|
|
}
|
2019-09-23 18:10:06 +02:00
|
|
|
|
|
|
|
void pointerNull1() {
|
2019-10-02 17:59:04 +02:00
|
|
|
ASSERT_EQUALS("1", getRange("void f(void *p) { p = NULL; p += 1; }", "p+=1"));
|
2019-09-23 18:10:06 +02:00
|
|
|
}
|
2019-10-02 17:59:04 +02:00
|
|
|
|
|
|
|
|
2019-12-29 18:42:35 +01:00
|
|
|
void structMember1() {
|
2020-05-08 12:21:10 +02:00
|
|
|
ASSERT_EQUALS("(and (>= $2 0) (<= $2 255))\n"
|
|
|
|
"(and (>= $3 0) (<= $3 255))\n"
|
|
|
|
"(= (+ $2 $3) 0)\n"
|
|
|
|
"z3::sat\n",
|
2019-10-02 17:59:04 +02:00
|
|
|
expr("struct S {\n"
|
|
|
|
" unsigned char a;\n"
|
|
|
|
" unsigned char b;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f(struct S s) { return s.a + s.b == 0; }", "=="));
|
|
|
|
}
|
|
|
|
|
2019-12-29 18:42:35 +01:00
|
|
|
void structMember2() {
|
|
|
|
const char code[] = "struct S { int x; };\n"
|
|
|
|
"void foo(struct S *s) { return s->x == 123; }";
|
|
|
|
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(and (>= $3 (- 2147483648)) (<= $3 2147483647))\n"
|
|
|
|
"(= $3 123)\n"
|
|
|
|
"z3::sat\n";
|
2019-12-29 18:42:35 +01:00
|
|
|
|
|
|
|
ASSERT_EQUALS(expected, expr(code, "=="));
|
|
|
|
}
|
2020-04-30 21:05:34 +02:00
|
|
|
|
|
|
|
void structMember3() {
|
|
|
|
const char code[] = "struct S { int x; };\n"
|
|
|
|
"void foo(struct S *s) {\n"
|
|
|
|
" s->x = iter->second.data;\n" // assign some unknown value
|
|
|
|
" return s->x == 1;\n"
|
|
|
|
"}";
|
|
|
|
|
2020-05-08 12:21:10 +02:00
|
|
|
const char expected[] = "(and (>= $3 (- 2147483648)) (<= $3 2147483647))\n"
|
|
|
|
"(= $3 1)\n"
|
|
|
|
"z3::sat\n";
|
2020-04-30 21:05:34 +02:00
|
|
|
|
|
|
|
ASSERT_EQUALS(expected, expr(code, "=="));
|
|
|
|
}
|
2020-05-15 21:34:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
void ternaryOperator1() {
|
|
|
|
const char code[] = "void foo(signed char x) {\n"
|
|
|
|
" x = (x > 0) ? (0==x) : 0;\n"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
const char expected[] = "(> $1 0)\n"
|
|
|
|
"(and (>= $1 (- 128)) (<= $1 127))\n"
|
|
|
|
"(= 0 $1)\n"
|
|
|
|
"z3::unsat\n";
|
|
|
|
|
|
|
|
ASSERT_EQUALS(expected, expr(code, "=="));
|
|
|
|
}
|
2019-09-17 21:00:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestExprEngine)
|