From 266cc79f26aa8df4718f2309808f77a5426f266c Mon Sep 17 00:00:00 2001 From: Cristian Cadar Date: Sat, 23 May 2009 02:42:09 +0000 Subject: Changed bout to ktest. Kept "BOUT\n" as the header of test files, for backward compatibility. Also changed KLEE_RUNTEST to KTEST_FILE. Updated tutorial-1. git-svn-id: https://llvm.org/svn/llvm-project/klee/trunk@72312 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Basic/BOut.cpp | 236 -------------------------------------------------- lib/Basic/KTest.cpp | 236 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/Core/Executor.cpp | 8 +- lib/Core/Executor.h | 10 +-- lib/Core/SeedInfo.cpp | 8 +- lib/Core/SeedInfo.h | 12 +-- 6 files changed, 255 insertions(+), 255 deletions(-) delete mode 100644 lib/Basic/BOut.cpp create mode 100644 lib/Basic/KTest.cpp (limited to 'lib') diff --git a/lib/Basic/BOut.cpp b/lib/Basic/BOut.cpp deleted file mode 100644 index 42d17e27..00000000 --- a/lib/Basic/BOut.cpp +++ /dev/null @@ -1,236 +0,0 @@ -//===-- BOut.c ------------------------------------------------------------===// -// -// The KLEE Symbolic Virtual Machine -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "klee/Internal/ADT/BOut.h" - -#include -#include -#include - -#define BOUT_MAGIC "BOUT\n" -#define BOUT_MAGIC_SIZE 5 -#define BOUT_VERSION 2 - -/***/ - -static int read_uint32(FILE *f, unsigned *value_out) { - unsigned char data[4]; - if (fread(data, 4, 1, f)!=1) - return 0; - *value_out = (((((data[0]<<8) + data[1])<<8) + data[2])<<8) + data[3]; - return 1; -} - -static int write_uint32(FILE *f, unsigned value) { - unsigned char data[4]; - data[0] = value>>24; - data[1] = value>>16; - data[2] = value>> 8; - data[3] = value>> 0; - return fwrite(data, 1, 4, f)==4; -} - -static int read_string(FILE *f, char **value_out) { - unsigned len; - if (!read_uint32(f, &len)) - return 0; - *value_out = (char*) malloc(len+1); - if (!*value_out) - return 0; - if (fread(*value_out, len, 1, f)!=1) - return 0; - (*value_out)[len] = 0; - return 1; -} - -static int write_string(FILE *f, const char *value) { - unsigned len = strlen(value); - if (!write_uint32(f, len)) - return 0; - if (fwrite(value, len, 1, f)!=1) - return 0; - return 1; -} - -/***/ - - -unsigned bOut_getCurrentVersion() { - return BOUT_VERSION; -} - - -static int bOut_checkHeader(FILE *f) { - char header[BOUT_MAGIC_SIZE]; - if (fread(header, BOUT_MAGIC_SIZE, 1, f)!=1) - return 0; - if (memcmp(header, BOUT_MAGIC, BOUT_MAGIC_SIZE)) - return 0; - return 1; -} - -int bOut_isBOutFile(const char *path) { - FILE *f = fopen(path, "rb"); - int res; - - if (!f) - return 0; - res = bOut_checkHeader(f); - fclose(f); - - return res; -} - -BOut *bOut_fromFile(const char *path) { - FILE *f = fopen(path, "rb"); - BOut *res = 0; - unsigned i, version; - - if (!f) - goto error; - if (!bOut_checkHeader(f)) - goto error; - - res = (BOut*) calloc(1, sizeof(*res)); - if (!res) - goto error; - - if (!read_uint32(f, &version)) - goto error; - - if (version > bOut_getCurrentVersion()) - goto error; - - res->version = version; - - if (!read_uint32(f, &res->numArgs)) - goto error; - res->args = (char**) calloc(res->numArgs, sizeof(*res->args)); - if (!res->args) - goto error; - - for (i=0; inumArgs; i++) - if (!read_string(f, &res->args[i])) - goto error; - - if (version >= 2) { - if (!read_uint32(f, &res->symArgvs)) - goto error; - if (!read_uint32(f, &res->symArgvLen)) - goto error; - } - - if (!read_uint32(f, &res->numObjects)) - goto error; - res->objects = (BOutObject*) calloc(res->numObjects, sizeof(*res->objects)); - if (!res->objects) - goto error; - for (i=0; inumObjects; i++) { - BOutObject *o = &res->objects[i]; - if (!read_string(f, &o->name)) - goto error; - if (!read_uint32(f, &o->numBytes)) - goto error; - o->bytes = (unsigned char*) malloc(o->numBytes); - if (fread(o->bytes, o->numBytes, 1, f)!=1) - goto error; - } - - fclose(f); - - return res; - error: - if (res) { - if (res->args) { - for (i=0; inumArgs; i++) - if (res->args[i]) - free(res->args[i]); - free(res->args); - } - if (res->objects) { - for (i=0; inumObjects; i++) { - BOutObject *bo = &res->objects[i]; - if (bo->name) - free(bo->name); - if (bo->bytes) - free(bo->bytes); - } - free(res->objects); - } - free(res); - } - - if (f) fclose(f); - - return 0; -} - -int bOut_toFile(BOut *bo, const char *path) { - FILE *f = fopen(path, "wb"); - unsigned i; - - if (!f) - goto error; - if (fwrite(BOUT_MAGIC, strlen(BOUT_MAGIC), 1, f)!=1) - goto error; - if (!write_uint32(f, BOUT_VERSION)) - goto error; - - if (!write_uint32(f, bo->numArgs)) - goto error; - for (i=0; inumArgs; i++) { - if (!write_string(f, bo->args[i])) - goto error; - } - - if (!write_uint32(f, bo->symArgvs)) - goto error; - if (!write_uint32(f, bo->symArgvLen)) - goto error; - - if (!write_uint32(f, bo->numObjects)) - goto error; - for (i=0; inumObjects; i++) { - BOutObject *o = &bo->objects[i]; - if (!write_string(f, o->name)) - goto error; - if (!write_uint32(f, o->numBytes)) - goto error; - if (fwrite(o->bytes, o->numBytes, 1, f)!=1) - goto error; - } - - fclose(f); - - return 1; - error: - if (f) fclose(f); - - return 0; -} - -unsigned bOut_numBytes(BOut *bo) { - unsigned i, res = 0; - for (i=0; inumObjects; i++) - res += bo->objects[i].numBytes; - return res; -} - -void bOut_free(BOut *bo) { - unsigned i; - for (i=0; inumArgs; i++) - free(bo->args[i]); - free(bo->args); - for (i=0; inumObjects; i++) { - free(bo->objects[i].name); - free(bo->objects[i].bytes); - } - free(bo->objects); - free(bo); -} diff --git a/lib/Basic/KTest.cpp b/lib/Basic/KTest.cpp new file mode 100644 index 00000000..d17916f5 --- /dev/null +++ b/lib/Basic/KTest.cpp @@ -0,0 +1,236 @@ +//===-- KTest.cpp ---------------------------------------------------------===// +// +// The KLEE Symbolic Virtual Machine +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "klee/Internal/ADT/KTest.h" + +#include +#include +#include + +#define KTEST_MAGIC "BOUT\n" +#define KTEST_MAGIC_SIZE 5 +#define KTEST_VERSION 2 + +/***/ + +static int read_uint32(FILE *f, unsigned *value_out) { + unsigned char data[4]; + if (fread(data, 4, 1, f)!=1) + return 0; + *value_out = (((((data[0]<<8) + data[1])<<8) + data[2])<<8) + data[3]; + return 1; +} + +static int write_uint32(FILE *f, unsigned value) { + unsigned char data[4]; + data[0] = value>>24; + data[1] = value>>16; + data[2] = value>> 8; + data[3] = value>> 0; + return fwrite(data, 1, 4, f)==4; +} + +static int read_string(FILE *f, char **value_out) { + unsigned len; + if (!read_uint32(f, &len)) + return 0; + *value_out = (char*) malloc(len+1); + if (!*value_out) + return 0; + if (fread(*value_out, len, 1, f)!=1) + return 0; + (*value_out)[len] = 0; + return 1; +} + +static int write_string(FILE *f, const char *value) { + unsigned len = strlen(value); + if (!write_uint32(f, len)) + return 0; + if (fwrite(value, len, 1, f)!=1) + return 0; + return 1; +} + +/***/ + + +unsigned kTest_getCurrentVersion() { + return KTEST_VERSION; +} + + +static int kTest_checkHeader(FILE *f) { + char header[KTEST_MAGIC_SIZE]; + if (fread(header, KTEST_MAGIC_SIZE, 1, f)!=1) + return 0; + if (memcmp(header, KTEST_MAGIC, KTEST_MAGIC_SIZE)) + return 0; + return 1; +} + +int kTest_isKTestFile(const char *path) { + FILE *f = fopen(path, "rb"); + int res; + + if (!f) + return 0; + res = kTest_checkHeader(f); + fclose(f); + + return res; +} + +KTest *kTest_fromFile(const char *path) { + FILE *f = fopen(path, "rb"); + KTest *res = 0; + unsigned i, version; + + if (!f) + goto error; + if (!kTest_checkHeader(f)) + goto error; + + res = (KTest*) calloc(1, sizeof(*res)); + if (!res) + goto error; + + if (!read_uint32(f, &version)) + goto error; + + if (version > kTest_getCurrentVersion()) + goto error; + + res->version = version; + + if (!read_uint32(f, &res->numArgs)) + goto error; + res->args = (char**) calloc(res->numArgs, sizeof(*res->args)); + if (!res->args) + goto error; + + for (i=0; inumArgs; i++) + if (!read_string(f, &res->args[i])) + goto error; + + if (version >= 2) { + if (!read_uint32(f, &res->symArgvs)) + goto error; + if (!read_uint32(f, &res->symArgvLen)) + goto error; + } + + if (!read_uint32(f, &res->numObjects)) + goto error; + res->objects = (KTestObject*) calloc(res->numObjects, sizeof(*res->objects)); + if (!res->objects) + goto error; + for (i=0; inumObjects; i++) { + KTestObject *o = &res->objects[i]; + if (!read_string(f, &o->name)) + goto error; + if (!read_uint32(f, &o->numBytes)) + goto error; + o->bytes = (unsigned char*) malloc(o->numBytes); + if (fread(o->bytes, o->numBytes, 1, f)!=1) + goto error; + } + + fclose(f); + + return res; + error: + if (res) { + if (res->args) { + for (i=0; inumArgs; i++) + if (res->args[i]) + free(res->args[i]); + free(res->args); + } + if (res->objects) { + for (i=0; inumObjects; i++) { + KTestObject *bo = &res->objects[i]; + if (bo->name) + free(bo->name); + if (bo->bytes) + free(bo->bytes); + } + free(res->objects); + } + free(res); + } + + if (f) fclose(f); + + return 0; +} + +int kTest_toFile(KTest *bo, const char *path) { + FILE *f = fopen(path, "wb"); + unsigned i; + + if (!f) + goto error; + if (fwrite(KTEST_MAGIC, strlen(KTEST_MAGIC), 1, f)!=1) + goto error; + if (!write_uint32(f, KTEST_VERSION)) + goto error; + + if (!write_uint32(f, bo->numArgs)) + goto error; + for (i=0; inumArgs; i++) { + if (!write_string(f, bo->args[i])) + goto error; + } + + if (!write_uint32(f, bo->symArgvs)) + goto error; + if (!write_uint32(f, bo->symArgvLen)) + goto error; + + if (!write_uint32(f, bo->numObjects)) + goto error; + for (i=0; inumObjects; i++) { + KTestObject *o = &bo->objects[i]; + if (!write_string(f, o->name)) + goto error; + if (!write_uint32(f, o->numBytes)) + goto error; + if (fwrite(o->bytes, o->numBytes, 1, f)!=1) + goto error; + } + + fclose(f); + + return 1; + error: + if (f) fclose(f); + + return 0; +} + +unsigned kTest_numBytes(KTest *bo) { + unsigned i, res = 0; + for (i=0; inumObjects; i++) + res += bo->objects[i].numBytes; + return res; +} + +void kTest_free(KTest *bo) { + unsigned i; + for (i=0; inumArgs; i++) + free(bo->args[i]); + free(bo->args); + for (i=0; inumObjects; i++) { + free(bo->objects[i].name); + free(bo->objects[i].bytes); + } + free(bo->objects); + free(bo); +} diff --git a/lib/Core/Executor.cpp b/lib/Core/Executor.cpp index d3409908..df8fdba8 100644 --- a/lib/Core/Executor.cpp +++ b/lib/Core/Executor.cpp @@ -34,7 +34,7 @@ #include "klee/util/ExprPPrinter.h" #include "klee/util/ExprUtil.h" #include "klee/Config/config.h" -#include "klee/Internal/ADT/BOut.h" +#include "klee/Internal/ADT/KTest.h" #include "klee/Internal/ADT/RNG.h" #include "klee/Internal/Module/Cell.h" #include "klee/Internal/Module/InstructionInfoTable.h" @@ -2227,7 +2227,7 @@ void Executor::run(ExecutionState &initialState) { if (usingSeeds) { std::vector &v = seedMap[&initialState]; - for (std::vector::const_iterator it = usingSeeds->begin(), + for (std::vector::const_iterator it = usingSeeds->begin(), ie = usingSeeds->end(); it != ie; ++it) v.push_back(SeedInfo(*it)); @@ -2973,7 +2973,7 @@ void Executor::executeMakeSymbolic(ExecutionState &state, for (std::vector::iterator siit = it->second.begin(), siie = it->second.end(); siit != siie; ++siit) { SeedInfo &si = *siit; - BOutObject *obj = si.getNextInput(mo, + KTestObject *obj = si.getNextInput(mo, NamedSeedMatching); if (!obj) { @@ -3019,7 +3019,7 @@ void Executor::executeMakeSymbolic(ExecutionState &state, if (replayPosition >= replayOut->numObjects) { terminateStateOnError(state, "replay count mismatch", "user.err"); } else { - BOutObject *obj = &replayOut->objects[replayPosition++]; + KTestObject *obj = &replayOut->objects[replayPosition++]; if (obj->numBytes != mo->size) { terminateStateOnError(state, "replay size mismatch", "user.err"); } else { diff --git a/lib/Core/Executor.h b/lib/Core/Executor.h index 76868291..9fa63a04 100644 --- a/lib/Core/Executor.h +++ b/lib/Core/Executor.h @@ -22,7 +22,7 @@ #include #include -struct BOut; +struct KTest; namespace llvm { class BasicBlock; @@ -134,7 +134,7 @@ private: /// When non-null the bindings that will be used for calls to /// klee_make_symbolic in order replay. - const struct BOut *replayOut; + const struct KTest *replayOut; /// When non-null a list of branch decisions to be used for replay. const std::vector *replayPath; /// The index into the current \ref replayOut or \ref replayPath @@ -143,7 +143,7 @@ private: /// When non-null a list of "seed" inputs which will be used to /// drive execution. - const std::vector *usingSeeds; + const std::vector *usingSeeds; /// Disables forking, instead a random path is chosen. Enabled as /// needed to control memory usage. \see fork() @@ -386,7 +386,7 @@ public: symPathWriter = tsw; } - virtual void setReplayOut(const struct BOut *out) { + virtual void setReplayOut(const struct KTest *out) { assert(!replayPath && "cannot replay both buffer and path"); replayOut = out; replayPosition = 0; @@ -401,7 +401,7 @@ public: virtual const llvm::Module * setModule(llvm::Module *module, const ModuleOptions &opts); - virtual void useSeeds(const std::vector *seeds) { + virtual void useSeeds(const std::vector *seeds) { usingSeeds = seeds; } diff --git a/lib/Core/SeedInfo.cpp b/lib/Core/SeedInfo.cpp index d76d75dc..e6d88bac 100644 --- a/lib/Core/SeedInfo.cpp +++ b/lib/Core/SeedInfo.cpp @@ -16,17 +16,17 @@ #include "klee/ExecutionState.h" #include "klee/Expr.h" #include "klee/util/ExprUtil.h" -#include "klee/Internal/ADT/BOut.h" +#include "klee/Internal/ADT/KTest.h" using namespace klee; -BOutObject *SeedInfo::getNextInput(const MemoryObject *mo, +KTestObject *SeedInfo::getNextInput(const MemoryObject *mo, bool byName) { if (byName) { unsigned i; for (i=0; inumObjects; ++i) { - BOutObject *obj = &input->objects[i]; + KTestObject *obj = &input->objects[i]; if (std::string(obj->name) == mo->name) if (used.insert(obj).second) return obj; @@ -38,7 +38,7 @@ BOutObject *SeedInfo::getNextInput(const MemoryObject *mo, if (!used.count(&input->objects[i])) break; if (inumObjects) { - BOutObject *obj = &input->objects[i]; + KTestObject *obj = &input->objects[i]; if (obj->numBytes == mo->size) { used.insert(obj); klee_warning_once(mo, "using seed input %s[%d] for: %s (no name match)", diff --git a/lib/Core/SeedInfo.h b/lib/Core/SeedInfo.h index dd151ed0..0acb130b 100644 --- a/lib/Core/SeedInfo.h +++ b/lib/Core/SeedInfo.h @@ -13,8 +13,8 @@ #include "klee/util/Assignment.h" extern "C" { - struct BOut; - struct BOutObject; + struct KTest; + struct KTestObject; } namespace klee { @@ -24,17 +24,17 @@ namespace klee { class SeedInfo { public: Assignment assignment; - BOut *input; + KTest *input; unsigned inputPosition; - std::set used; + std::set used; public: explicit - SeedInfo(BOut *_input) : assignment(true), + SeedInfo(KTest *_input) : assignment(true), input(_input), inputPosition(0) {} - BOutObject *getNextInput(const MemoryObject *mo, + KTestObject *getNextInput(const MemoryObject *mo, bool byName); /// Patch the seed so that condition is satisfied while retaining as -- cgit 1.4.1