about summary refs log tree commit diff homepage
path: root/lib/Support/TreeStream.cpp
blob: 4571861d3e9d2dd9e1ada0d690ec6b621cc37f10 (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
197
198
//===-- TreeStream.cpp ----------------------------------------------------===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "TreeStreamWriter"
#include "klee/ADT/TreeStream.h"

#include "klee/Support/Debug.h"

#include <cassert>
#include <iomanip>
#include <fstream>
#include <iterator>
#include <map>

#include "llvm/Support/raw_ostream.h"
#include <string.h>

using namespace klee;

///

TreeStreamWriter::TreeStreamWriter(const std::string &_path) 
  : lastID(0),
    bufferCount(0),
    path(_path),
    output(new std::ofstream(path.c_str(), 
                             std::ios::out | std::ios::binary)),
    ids(1) {
  if (!output->good()) {
    delete output;
    output = 0;
  }
}

TreeStreamWriter::~TreeStreamWriter() {
  flush();
  delete output;
}

bool TreeStreamWriter::good() {
  return !!output;
}

TreeOStream TreeStreamWriter::open() {
  return open(TreeOStream(*this, 0));
}

TreeOStream TreeStreamWriter::open(const TreeOStream &os) {
  assert(output && os.writer==this);
  flushBuffer();
  unsigned id = ids++;
  output->write(reinterpret_cast<const char*>(&os.id), 4);
  unsigned tag = id | (1<<31);
  output->write(reinterpret_cast<const char*>(&tag), 4);
  return TreeOStream(*this, id);
}

void TreeStreamWriter::write(TreeOStream &os, const char *s, unsigned size) {
  if (bufferCount && 
      (os.id!=lastID || size+bufferCount>bufferSize))
    flushBuffer();
  if (bufferCount) { // (os.id==lastID && size+bufferCount<=bufferSize)
    memcpy(&buffer[bufferCount], s, size);
    bufferCount += size;
  } else if (size<bufferSize) {
    lastID = os.id;
    memcpy(buffer, s, size);
    bufferCount = size;
  } else {
    output->write(reinterpret_cast<const char*>(&os.id), 4);
    output->write(reinterpret_cast<const char*>(&size), 4);
    output->write(s, size);
  }
}

void TreeStreamWriter::flushBuffer() {
  if (bufferCount) {    
    output->write(reinterpret_cast<const char*>(&lastID), 4);
    output->write(reinterpret_cast<const char*>(&bufferCount), 4);
    output->write(buffer, bufferCount);
    bufferCount = 0;
  }
}

void TreeStreamWriter::flush() {
  flushBuffer();
  output->flush();
}

void TreeStreamWriter::readStream(TreeStreamID streamID,
                                  std::vector<unsigned char> &out) {
  assert(streamID>0 && streamID<ids);
  flush();
  
  std::ifstream is(path.c_str(),
                   std::ios::in | std::ios::binary);
  assert(is.good());
  KLEE_DEBUG(llvm::errs() << "finding chain for: " << streamID << "\n");

  std::map<unsigned,unsigned> parents;
  std::vector<unsigned> roots;
  for (;;) {
    assert(is.good());
    unsigned id;
    unsigned tag;
    is.read(reinterpret_cast<char*>(&id), 4);
    is.read(reinterpret_cast<char*>(&tag), 4);
    if (tag&(1<<31)) { // fork
      unsigned child = tag ^ (1<<31);

      if (child==streamID) {
        roots.push_back(child);
        while (id) {
          roots.push_back(id);
          std::map<unsigned, unsigned>::iterator it = parents.find(id);
          assert(it!=parents.end());
          id = it->second;
        } 
        break;
      } else {
        parents.insert(std::make_pair(child,id));
      }
    } else {
      unsigned size = tag;
      while (size--) is.get();
    }
  }
  KLEE_DEBUG({
      llvm::errs() << "roots: ";
      for (size_t i = 0, e = roots.size(); i < e; ++i) {
        llvm::errs() << roots[i] << " ";
      }
      llvm::errs() << "\n";
    });
  is.seekg(0, std::ios::beg);
  for (;;) {
    unsigned id;
    unsigned tag;
    is.read(reinterpret_cast<char*>(&id), 4);
    is.read(reinterpret_cast<char*>(&tag), 4);
    if (!is.good()) break;
    if (tag&(1<<31)) { // fork
      unsigned child = tag ^ (1<<31);
      if (id==roots.back() && roots.size()>1 && child==roots[roots.size()-2])
        roots.pop_back();
    } else {
      unsigned size = tag;
      if (id==roots.back()) {
        while (size--) out.push_back(is.get());
      } else {
        while (size--) is.get();
      }
    }
  }  
}

///

TreeOStream::TreeOStream()
  : writer(0),
    id(0) {
}

TreeOStream::TreeOStream(TreeStreamWriter &_writer, unsigned _id)
  : writer(&_writer),
    id(_id) {
}

TreeOStream::~TreeOStream() {
}

unsigned TreeOStream::getID() const {
  assert(writer);
  return id;
}

void TreeOStream::write(const char *buffer, unsigned size) {
  assert(writer);
  writer->write(*this, buffer, size);
}

TreeOStream &TreeOStream::operator<<(const std::string &s) {
  assert(writer);
  write(s.c_str(), s.size());
  return *this;
}

void TreeOStream::flush() {
  assert(writer);
  writer->flush();
}