about summary refs log tree commit diff homepage
path: root/lib/Core/PTreeWriter.cpp
blob: a8067a5d0b94a4307b33eaa43b1eedd6435bed55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//===-- PTreeWriter.cpp ---------------------------------------------------===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "PTreeWriter.h"

#include "PTree.h"
#include "klee/Support/ErrorHandling.h"
#include "klee/Support/OptionCategories.h"

#include "llvm/Support/CommandLine.h"

namespace {
llvm::cl::opt<unsigned> BatchSize(
    "ptree-batch-size", llvm::cl::init(100U),
    llvm::cl::desc("Number of process tree nodes to batch for writing, "
                   "see --write-ptree (default=100)"),
    llvm::cl::cat(klee::PTreeCat));
} // namespace

using namespace klee;

void prepare_statement(sqlite3 *db, const std::string &query, sqlite3_stmt **stmt) {
  int result;
#ifdef SQLITE_PREPARE_PERSISTENT
  result = sqlite3_prepare_v3(db, query.c_str(), -1, SQLITE_PREPARE_PERSISTENT,
                              stmt, nullptr);
#else
  result = sqlite3_prepare_v3(db, query.c_str(), -1, 0, stmt, nullptr);
#endif
  if (result != SQLITE_OK) {
    klee_warning("Process tree database: can't prepare query: %s [%s]",
                 sqlite3_errmsg(db), query.c_str());
    sqlite3_close(db);
    klee_error("Process tree database: can't prepare query: %s", query.c_str());
  }
}

PTreeWriter::PTreeWriter(const std::string &dbPath) {
  // create database file
  if (sqlite3_open(dbPath.c_str(), &db) != SQLITE_OK)
    klee_error("Can't create process tree database: %s", sqlite3_errmsg(db));

  // - set options: asynchronous + WAL
  char *errMsg = nullptr;
  if (sqlite3_exec(db, "PRAGMA synchronous = OFF;", nullptr, nullptr,
                   &errMsg) != SQLITE_OK) {
    klee_warning("Process tree database: can't set option: %s", errMsg);
    sqlite3_free(errMsg);
  }
  if (sqlite3_exec(db, "PRAGMA journal_mode = WAL;", nullptr, nullptr,
                   &errMsg) != SQLITE_OK) {
    klee_warning("Process tree database: can't set option: %s", errMsg);
    sqlite3_free(errMsg);
  }

  // - create table
  std::string query =
      "CREATE TABLE IF NOT EXISTS nodes ("
      "ID INT PRIMARY KEY, stateID INT, leftID INT, rightID INT,"
      "asmLine INT, kind INT);";
  char *zErr = nullptr;
  if (sqlite3_exec(db, query.c_str(), nullptr, nullptr, &zErr) != SQLITE_OK) {
    klee_warning("Process tree database: initialisation error: %s", zErr);
    sqlite3_free(zErr);
    sqlite3_close(db);
    klee_error("Process tree database: initialisation error.");
  }

  // create prepared statements
  // - insertStmt
  query = "INSERT INTO nodes VALUES (?, ?, ?, ?, ?, ?);";
  prepare_statement(db, query, &insertStmt);
  // - transactionBeginStmt
  query = "BEGIN TRANSACTION";
  prepare_statement(db, query, &transactionBeginStmt);
  // - transactionCommitStmt
  query = "COMMIT TRANSACTION";
  prepare_statement(db, query, &transactionCommitStmt);

  // begin transaction
  if (sqlite3_step(transactionBeginStmt) != SQLITE_DONE) {
    klee_warning("Process tree database: can't begin transaction: %s",
                 sqlite3_errmsg(db));
  }
  if (sqlite3_reset(transactionBeginStmt) != SQLITE_OK) {
    klee_warning("Process tree database: can't reset transaction: %s",
                 sqlite3_errmsg(db));
  }
}

PTreeWriter::~PTreeWriter() {
  batchCommit(!flushed);

  // finalize prepared statements
  sqlite3_finalize(insertStmt);
  sqlite3_finalize(transactionBeginStmt);
  sqlite3_finalize(transactionCommitStmt);

  // commit
  if (sqlite3_exec(db, "END TRANSACTION", nullptr, nullptr, nullptr) !=
      SQLITE_OK) {
    klee_warning("Process tree database: can't end transaction: %s",
                 sqlite3_errmsg(db));
  }

  if (sqlite3_close(db) != SQLITE_OK) {
    klee_warning("Process tree database: can't close database: %s",
                 sqlite3_errmsg(db));
  }
}

void PTreeWriter::batchCommit(bool force) {
  ++batch;
  if (batch < BatchSize && !force)
    return;

  // commit and begin transaction
  if (sqlite3_step(transactionCommitStmt) != SQLITE_DONE) {
    klee_warning("Process tree database: transaction commit error: %s",
                 sqlite3_errmsg(db));
  }

  if (sqlite3_reset(transactionCommitStmt) != SQLITE_OK) {
    klee_warning("Process tree database: transaction reset error: %s",
                 sqlite3_errmsg(db));
  }

  if (sqlite3_step(transactionBeginStmt) != SQLITE_DONE) {
    klee_warning("Process tree database: transaction begin error: %s",
                 sqlite3_errmsg(db));
  }

  if (sqlite3_reset(transactionBeginStmt) != SQLITE_OK) {
    klee_warning("Process tree database: transaction reset error: %s",
                 sqlite3_errmsg(db));
  }

  batch = 0;
  flushed = true;
}

void PTreeWriter::write(const AnnotatedPTreeNode &node) {
  unsigned rc = 0;

  // bind values (SQLITE_OK is defined as 0 - just check success once at the
  // end)
  rc |= sqlite3_bind_int64(insertStmt, 1, node.id);
  rc |= sqlite3_bind_int(insertStmt, 2, node.stateID);
  rc |= sqlite3_bind_int64(
      insertStmt, 3,
      node.left.getPointer()
          ? (static_cast<AnnotatedPTreeNode *>(node.left.getPointer()))->id
          : 0);
  rc |= sqlite3_bind_int64(
      insertStmt, 4,
      node.right.getPointer()
          ? (static_cast<AnnotatedPTreeNode *>(node.right.getPointer()))->id
          : 0);
  rc |= sqlite3_bind_int(insertStmt, 5, node.asmLine);
  std::uint8_t value{0};
  if (std::holds_alternative<BranchType>(node.kind)) {
    value = static_cast<std::uint8_t>(std::get<BranchType>(node.kind));
  } else if (std::holds_alternative<StateTerminationType>(node.kind)) {
    value =
        static_cast<std::uint8_t>(std::get<StateTerminationType>(node.kind));
  } else {
    assert(false && "PTreeWriter: Illegal node kind!");
  }
  rc |= sqlite3_bind_int(insertStmt, 6, value);
  if (rc != SQLITE_OK) {
    // This is either a programming error (e.g. SQLITE_MISUSE) or we ran out of
    // resources (e.g. SQLITE_NOMEM). Calling sqlite3_errmsg() after a possible
    // successful call above is undefined, hence no error message here.
    klee_error("Process tree database: can't persist data for node: %u",
               node.id);
  }

  // insert
  if (sqlite3_step(insertStmt) != SQLITE_DONE) {
    klee_warning("Process tree database: can't persist data for node: %u: %s",
                 node.id, sqlite3_errmsg(db));
  }

  if (sqlite3_reset(insertStmt) != SQLITE_OK) {
    klee_warning("Process tree database: error reset node: %u: %s", node.id,
                 sqlite3_errmsg(db));
  }

  batchCommit();
}