klee
TreeStream.cpp
Go to the documentation of this file.
1//===-- TreeStream.cpp ----------------------------------------------------===//
2//
3// The KLEE Symbolic Virtual Machine
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#define DEBUG_TYPE "TreeStreamWriter"
11#include "klee/ADT/TreeStream.h"
12
13#include "klee/Support/Debug.h"
14
15#include <cassert>
16#include <iomanip>
17#include <fstream>
18#include <iterator>
19#include <map>
20
21#include "llvm/Support/raw_ostream.h"
22#include <string.h>
23
24using namespace klee;
25
27
28TreeStreamWriter::TreeStreamWriter(const std::string &_path)
29 : lastID(0),
30 bufferCount(0),
31 path(_path),
32 output(new std::ofstream(path.c_str(),
33 std::ios::out | std::ios::binary)),
34 ids(1) {
35 if (!output->good()) {
36 delete output;
37 output = 0;
38 }
39}
40
42 flush();
43 delete output;
44}
45
47 return !!output;
48}
49
51 return open(TreeOStream(*this, 0));
52}
53
55 assert(output && os.writer==this);
57 unsigned id = ids++;
58 output->write(reinterpret_cast<const char*>(&os.id), 4);
59 unsigned tag = id | (1<<31);
60 output->write(reinterpret_cast<const char*>(&tag), 4);
61 return TreeOStream(*this, id);
62}
63
64void TreeStreamWriter::write(TreeOStream &os, const char *s, unsigned size) {
65 if (bufferCount &&
66 (os.id!=lastID || size+bufferCount>bufferSize))
68 if (bufferCount) { // (os.id==lastID && size+bufferCount<=bufferSize)
69 memcpy(&buffer[bufferCount], s, size);
70 bufferCount += size;
71 } else if (size<bufferSize) {
72 lastID = os.id;
73 memcpy(buffer, s, size);
74 bufferCount = size;
75 } else {
76 output->write(reinterpret_cast<const char*>(&os.id), 4);
77 output->write(reinterpret_cast<const char*>(&size), 4);
78 output->write(s, size);
79 }
80}
81
83 if (bufferCount) {
84 output->write(reinterpret_cast<const char*>(&lastID), 4);
85 output->write(reinterpret_cast<const char*>(&bufferCount), 4);
86 output->write(buffer, bufferCount);
87 bufferCount = 0;
88 }
89}
90
93 output->flush();
94}
95
97 std::vector<unsigned char> &out) {
98 assert(streamID>0 && streamID<ids);
99 flush();
100
101 std::ifstream is(path.c_str(),
102 std::ios::in | std::ios::binary);
103 assert(is.good());
104 KLEE_DEBUG(llvm::errs() << "finding chain for: " << streamID << "\n");
105
106 std::map<unsigned,unsigned> parents;
107 std::vector<unsigned> roots;
108 for (;;) {
109 assert(is.good());
110 unsigned id;
111 unsigned tag;
112 is.read(reinterpret_cast<char*>(&id), 4);
113 is.read(reinterpret_cast<char*>(&tag), 4);
114 if (tag&(1<<31)) { // fork
115 unsigned child = tag ^ (1<<31);
116
117 if (child==streamID) {
118 roots.push_back(child);
119 while (id) {
120 roots.push_back(id);
121 std::map<unsigned, unsigned>::iterator it = parents.find(id);
122 assert(it!=parents.end());
123 id = it->second;
124 }
125 break;
126 } else {
127 parents.insert(std::make_pair(child,id));
128 }
129 } else {
130 unsigned size = tag;
131 while (size--) is.get();
132 }
133 }
134 KLEE_DEBUG({
135 llvm::errs() << "roots: ";
136 for (size_t i = 0, e = roots.size(); i < e; ++i) {
137 llvm::errs() << roots[i] << " ";
138 }
139 llvm::errs() << "\n";
140 });
141 is.seekg(0, std::ios::beg);
142 for (;;) {
143 unsigned id;
144 unsigned tag;
145 is.read(reinterpret_cast<char*>(&id), 4);
146 is.read(reinterpret_cast<char*>(&tag), 4);
147 if (!is.good()) break;
148 if (tag&(1<<31)) { // fork
149 unsigned child = tag ^ (1<<31);
150 if (id==roots.back() && roots.size()>1 && child==roots[roots.size()-2])
151 roots.pop_back();
152 } else {
153 unsigned size = tag;
154 if (id==roots.back()) {
155 while (size--) out.push_back(is.get());
156 } else {
157 while (size--) is.get();
158 }
159 }
160 }
161}
162
164
166 : writer(0),
167 id(0) {
168}
169
171 : writer(&_writer),
172 id(_id) {
173}
174
176}
177
178unsigned TreeOStream::getID() const {
179 assert(writer);
180 return id;
181}
182
183void TreeOStream::write(const char *buffer, unsigned size) {
184 assert(writer);
185 writer->write(*this, buffer, size);
186}
187
188TreeOStream &TreeOStream::operator<<(const std::string &s) {
189 assert(writer);
190 write(s.c_str(), s.size());
191 return *this;
192}
193
195 assert(writer);
196 writer->flush();
197}
198
TreeStreamWriter * writer
Definition: TreeStream.h:57
void write(const char *buffer, unsigned size)
Definition: TreeStream.cpp:183
unsigned getID() const
Definition: TreeStream.cpp:178
TreeOStream & operator<<(const std::string &s)
Definition: TreeStream.cpp:188
TreeOStream open()
Definition: TreeStream.cpp:50
void write(TreeOStream &os, const char *s, unsigned size)
Definition: TreeStream.cpp:64
friend class TreeOStream
Definition: TreeStream.h:24
void readStream(TreeStreamID id, std::vector< unsigned char > &out)
Definition: TreeStream.cpp:96
std::ofstream * output
Definition: TreeStream.h:31
static const unsigned bufferSize
Definition: TreeStream.h:22
char buffer[bufferSize]
Definition: TreeStream.h:27
Definition: main.cpp:291
unsigned TreeStreamID
Definition: TreeStream.h:18