ROSE  0.10.7.0
BinarySerialIo.h
1 #ifndef Rose_BinaryAnalysis_SerialIo_H
2 #define Rose_BinaryAnalysis_SerialIo_H
3 
4 #include <rosePublicConfig.h>
5 #ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
6 
7 #include <Progress.h>
8 #include <RoseException.h>
9 #include <boost/filesystem.hpp>
10 #include <boost/lexical_cast.hpp>
11 #include <boost/thread.hpp>
12 #include <Sawyer/Message.h>
13 #include <Sawyer/ProgressBar.h>
14 #include <Sawyer/Synchronization.h>
15 
16 // Define this if you need to debug SerialIo -- it causes everything to run in the calling thread and avoid catching exceptions.
17 //#define ROSE_DEBUG_SERIAL_IO
18 
19 #if defined(BOOST_WINDOWS)
20  // Lacks POSIX file system, so we can't monitor the I/O progress
21  #undef ROSE_SUPPORTS_SERIAL_IO
22 #elif !defined(ROSE_HAVE_BOOST_SERIALIZATION_LIB)
23  // Lacks Boost's serialization library, which is how we convert objects to bytes and vice versa
24  #undef ROSE_SUPPORTS_SERIAL_IO
25 #elif defined(__clang__)
26  #define ROSE_SUPPORTS_SERIAL_IO /*supported*/
27 #elif defined(__GNUC__)
28  #if __GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNU_C_PATCHLEVEL__ <= 40204
29  // GCC <= 4.2.4 gets segfaults compiling this file
30  #undef ROSE_SUPPORTS_SERIAL_IO
31  #else
32  #define ROSE_SUPPORTS_SERIAL_IO /*supported*/
33  #endif
34 #else
35  #define ROSE_SUPPORTS_SERIAL_IO /*supported*/
36 #endif
37 
38 #ifdef ROSE_SUPPORTS_SERIAL_IO
39 #include <boost/archive/binary_iarchive.hpp>
40 #include <boost/archive/binary_oarchive.hpp>
41 #include <boost/archive/text_iarchive.hpp>
42 #include <boost/archive/text_oarchive.hpp>
43 #include <boost/archive/xml_iarchive.hpp>
44 #include <boost/archive/xml_oarchive.hpp>
45 #include <boost/iostreams/device/file_descriptor.hpp>
46 #include <boost/iostreams/stream.hpp>
47 #endif
48 
49 namespace Rose {
50 namespace BinaryAnalysis {
51 
52 namespace Partitioner2 {
53 class Partitioner;
54 }
55 
57 // SerialIo
59 
117 public:
120 
122  enum Format {
130  };
131 
133  enum Savable {
134  NO_OBJECT = 0x00000000,
135  PARTITIONER = 0x00000001,
136  AST = 0x00000002,
137  END_OF_DATA = 0x0000fffe,
138  ERROR = 0x0000ffff,
139  USER_DEFINED = 0x00010000,
140  USER_DEFINED_LAST = 0xffffffff
141  };
142 
144  class Exception: public Rose::Exception {
145  public:
147  explicit Exception(const std::string &s): Rose::Exception(s) {}
148  ~Exception() throw() {}
149  };
150 
151 private:
152  mutable SAWYER_THREAD_TRAITS::Mutex mutex_; // protects the following data members
153  Format format_;
154  Progress::Ptr progress_;
155  bool isOpen_;
156  Savable objectType_;
157 
158 protected:
159  Sawyer::ProgressBar<size_t> progressBar_;
160 
161  // We use low-level file descriptors under an std::stream I/O interface in order to provide progress reports. This
162  // allows one thread to be reading from or writing to the file and another thread to monitor the file position to
163  // report progress. The C++ standard library doesn't have an API for wrapping file descriptors in a stream interface,
164  // so we use Boost.
165  int fd_;
166 
167 protected:
168  SerialIo()
169  : format_(BINARY), progress_(Progress::instance()), isOpen_(false), objectType_(NO_OBJECT),
170  progressBar_(mlog[Sawyer::Message::MARCH]), fd_(-1) {
171  init();
172  progressBar_.suffix(" bytes");
173  }
174 
175 public:
178 
186  virtual ~SerialIo();
187 
191  static Savable userSavable(unsigned offset);
192 
201  Format format() const;
202  void format(Format);
213  Progress::Ptr progress() const;
214  void progress(const Progress::Ptr&);
225  virtual void open(const boost::filesystem::path&) = 0;
226 
238  virtual void close() = 0;
239 
245  bool isOpen() const;
246 
251  Savable objectType() const;
252 
253 protected:
254  // Set or clear the isOpen flag.
255  void setIsOpen(bool b);
256 
257  // Set object type to ERROR to indicate that a read was unsuccessful
258  void objectType(Savable);
259 
260 private:
261  void init();
262 };
263 
264 
266 // SerialOutput
268 
272 class SerialOutput: public SerialIo {
273 public:
275 
276 private:
277 #ifdef ROSE_SUPPORTS_SERIAL_IO
278  boost::iostreams::file_descriptor_sink device_;
279  boost::iostreams::stream<boost::iostreams::file_descriptor_sink> file_;
280  boost::archive::binary_oarchive *binary_archive_;
281  boost::archive::text_oarchive *text_archive_;
282  boost::archive::xml_oarchive *xml_archive_;
283 #endif
284 
285 protected:
286 #ifdef ROSE_SUPPORTS_SERIAL_IO
287  SerialOutput(): binary_archive_(NULL), text_archive_(NULL), xml_archive_(NULL) {}
288 #else
289  SerialOutput() {}
290 #endif
291 
292 public:
293  ~SerialOutput();
294  void open(const boost::filesystem::path &fileName) ROSE_OVERRIDE;
295  void close() ROSE_OVERRIDE;
296 
301  static Ptr instance() { return Ptr(new SerialOutput); }
302 
313 
325  void saveAst(SgAsmNode*);
326  void saveAst(SgBinaryComposite*);
327 private:
328  // The saveAstHelper is what actually gets called for the functions above. It doesn't descriminate between nodes that
329  // support serialization and those that don't, which is why it's private. Use only the public functions because they'll
330  // give you a nice compiler error if you try to save an Ast node type that isn't supported.
331  void saveAstHelper(SgNode*);
332 public:
333 
348  template<class T>
349  void saveObject(Savable objectTypeId, const T &object) {
350  if (!isOpen())
351  throw Exception("cannot save object when no file is open");
352  if (ERROR == objectType())
353  throw Exception("cannot save object because stream is in error state");
354 
355 #ifndef ROSE_SUPPORTS_SERIAL_IO
356  throw Exception("binary state files are not supported in this configuration");
357 #elif defined(ROSE_DEBUG_SERIAL_IO)
358  std::string errorMessage;
359  asyncSave(objectTypeId, object, &errorMessage);
360 #else
361  // A different thread saves the object while this thread updates the progress
362  std::string errorMessage;
363  boost::thread worker(startWorker<T>, this, objectTypeId, &object, &errorMessage);
364  boost::chrono::milliseconds timeout((unsigned)(1000 * Sawyer::ProgressBarSettings::minimumUpdateInterval()));
365  progressBar_.prefix("writing");
366  while (!worker.try_join_for(timeout)) {
367  off_t cur = ::lseek(fd_, 0, SEEK_CUR);
368  if (-1 == cur) {
369  ++progressBar_; // so a spinner moves
370  } else {
371  progressBar_.value(cur);
372  if (Progress::Ptr p = progress())
373  p->update(Progress::Report(cur, NAN));
374  }
375  }
376  if (!errorMessage.empty())
377  throw Exception(errorMessage);
378 #endif
379  }
380 
381 private:
382  template<class T>
383  static void startWorker(SerialOutput *saver, Savable objectTypeId, const T *object, std::string *errorMessage) {
384  ASSERT_not_null(object);
385  saver->asyncSave(objectTypeId, *object, errorMessage);
386  }
387 
388  // This might run in its own thread.
389  template<class T>
390  void asyncSave(Savable objectTypeId, const T &object, std::string *errorMessage) {
391  ASSERT_not_null(errorMessage);
392 #ifndef ROSE_SUPPORTS_SERIAL_IO
393  ASSERT_not_reachable("not supported in this configuration");
394 #else
395 #if !defined(ROSE_DEBUG_SERIAL_IO)
396  try {
397 #endif
398  objectType(ERROR);
399  switch (format()) {
400  case BINARY:
401  ASSERT_not_null(binary_archive_);
402  *binary_archive_ <<BOOST_SERIALIZATION_NVP(objectTypeId);
403  *binary_archive_ <<BOOST_SERIALIZATION_NVP(object);
404  break;
405  case TEXT:
406  ASSERT_not_null(text_archive_);
407  *text_archive_ <<BOOST_SERIALIZATION_NVP(objectTypeId);
408  *text_archive_ <<BOOST_SERIALIZATION_NVP(object);
409  break;
410  case XML:
411  ASSERT_not_null(xml_archive_);
412  *xml_archive_ <<BOOST_SERIALIZATION_NVP(objectTypeId);
413  *xml_archive_ <<BOOST_SERIALIZATION_NVP(object);
414  break;
415  }
416  objectType(objectTypeId);
417 #if !defined(ROSE_DEBUG_SERIAL_IO)
418  } catch (const Exception &e) {
419  *errorMessage = e.what();
420  } catch (...) {
421  *errorMessage = "failed to write object to output stream";
422  }
423 #endif
424 #endif
425  }
426 };
427 
428 
430 // SerialInput
432 
436 class SerialInput: public SerialIo {
437 public:
439 
440 private:
441 #ifdef ROSE_SUPPORTS_SERIAL_IO
442  size_t fileSize_;
443  boost::iostreams::file_descriptor_source device_;
444  boost::iostreams::stream<boost::iostreams::file_descriptor_source> file_;
445  boost::archive::binary_iarchive *binary_archive_;
446  boost::archive::text_iarchive *text_archive_;
447  boost::archive::xml_iarchive *xml_archive_;
448 #endif
449 
450 protected:
451 #ifdef ROSE_SUPPORTS_SERIAL_IO
452  SerialInput(): fileSize_(0), binary_archive_(NULL), text_archive_(NULL), xml_archive_(NULL) {}
453 #else
454  SerialInput() {}
455 #endif
456 
457 public:
458  ~SerialInput();
459  void open(const boost::filesystem::path &fileName) ROSE_OVERRIDE;
460  void close() ROSE_OVERRIDE;
461 
466  static Ptr instance() { return Ptr(new SerialInput); }
467 
476 
484 
492  SgNode* loadAst();
493 
500  template<class T>
501  T loadObject(Savable objectTypeId) {
502  T object;
503  loadObject<T>(objectTypeId, object);
504  return object;
505  }
506  template<class T>
507  void loadObject(Savable objectTypeId, T &object) {
508  if (!isOpen())
509  throw Exception("cannot load object when no file is open");
510 
511 #ifndef ROSE_SUPPORTS_SERIAL_IO
512  throw Exception("binary state files are not supported in this configuration");
513 #else
514  if (ERROR == objectType())
515  throw Exception("cannot read object because stream is in error state");
516  if (objectType() != objectTypeId) {
517  throw Exception("unexpected object type (expected " + boost::lexical_cast<std::string>(objectTypeId) +
518  " but read " + boost::lexical_cast<std::string>(objectType()) + ")");
519  }
520  objectType(ERROR); // in case of exception
521  std::string errorMessage;
522 #ifdef ROSE_DEBUG_SERIAL_IO
523  asyncLoad(object, &errorMessage);
524 #else
525  boost::thread worker(startWorker<T>, this, &object, &errorMessage);
526  boost::chrono::milliseconds timeout((unsigned)(1000 * Sawyer::ProgressBarSettings::minimumUpdateInterval()));
527  progressBar_.prefix("reading");
528  while (!worker.try_join_for(timeout)) {
529  if (fileSize_ > 0) {
530  off_t cur = ::lseek(fd_, 0, SEEK_CUR);
531  if (cur != -1) {
532  progressBar_.value(cur);
533  if (Progress::Ptr p = progress())
534  p->update(Progress::Report(cur, fileSize_));
535  }
536  } else {
537  ++progressBar_; // so the spinner moves
538  }
539  }
540  if (!errorMessage.empty())
541  throw Exception(errorMessage);
542 #endif
543  advanceObjectType();
544 #endif
545  }
548 private:
549  template<class T>
550  static void startWorker(SerialInput *loader, T *object, std::string *errorMessage) {
551  loader->asyncLoad(*object, errorMessage);
552  }
553 
554  // Might run in its own thread
555  template<class T>
556  void asyncLoad(T &object, std::string *errorMessage) {
557  ASSERT_not_null(errorMessage);
558 #ifndef ROSE_SUPPORTS_SERIAL_IO
559  ASSERT_not_reachable("not supported in this configuration");
560 #else
561 #if !defined(ROSE_DEBUG_SERIAL_IO)
562  try {
563 #endif
564  switch (format()) {
565  case BINARY:
566  ASSERT_not_null(binary_archive_);
567  *binary_archive_ >>object;
568  break;
569  case TEXT:
570  ASSERT_not_null(text_archive_);
571  *text_archive_ >>object;
572  break;
573  case XML:
574  ASSERT_not_null(xml_archive_);
575  *xml_archive_ >>BOOST_SERIALIZATION_NVP(object);
576  break;
577  }
578 #if !defined(ROSE_DEBUG_SERIAL_IO)
579  } catch (const Exception &e) {
580  *errorMessage = e.what();
581  } catch (...) {
582  *errorMessage = "failed to read object from input stream";
583  }
584 #endif
585 #endif
586  }
587 
588 protected:
589  // Read the next object type from the input stream
590  void advanceObjectType();
591 };
592 
593 } // namespace
594 } // namespace
595 
596 #endif
597 #endif
Savable
Types of objects that can be saved.
A single progress report.
Definition: Progress.h:179
double minimumUpdateInterval()
Minimum time between updates.
Base class for all binary analysis IR nodes.
Textual binary state files use a custom format (Boost serialization format) that stores the data as A...
void savePartitioner(const Partitioner2::Partitioner &)
Save a binary analysis partitioner.
static Savable userSavable(unsigned offset)
Create a new Savable enum constant.
void close() ROSE_OVERRIDE
Detach a file.
Collection of streams.
Definition: Message.h:1600
First user-defined object number.
void saveObject(Savable objectTypeId, const T &object)
Save an object to the output stream.
Format format() const
Property: File format.
Output binary analysis state.
Rose::BinaryAnalysis::Partitioner2::Partitioner.
void saveAst(SgAsmNode *)
Save a binary AST.
static Sawyer::Message::Facility mlog
Message facility.
Main namespace for the ROSE library.
Base class for binary state input and output.
Progress::Ptr progress() const
Property: Progress reporter.
SgNode * loadAst()
Load an AST from the input stream.
Reference-counting intrusive smart pointer.
Definition: SharedPointer.h:68
Name space for the entire library.
Object type for newly-initialized serializers.
Savable objectType() const
Type ID for next object.
Exception(const std::string &s)
Construct an exception with an error message.
virtual void open(const boost::filesystem::path &)=0
Attach a file.
Marks that the stream has encountered an error condition.
void open(const boost::filesystem::path &fileName) ROSE_OVERRIDE
Attach a file.
void close() ROSE_OVERRIDE
Detach a file.
static Ptr instance()
Factory method to create a new instance.
The states are stored as XML, which is a very verbose and slow format.
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:9247
static Ptr instance()
Factory method to create a new instance.
void open(const boost::filesystem::path &fileName) ROSE_OVERRIDE
Attach a file.
ValueType value() const
Value for the progress bar.
Definition: ProgressBar.h:159
T loadObject(Savable objectTypeId)
Load an object from the input stream.
Partitioner2::Partitioner loadPartitioner()
Load a partitioner from the input stream.
bool isOpen() const
Whether a file is attached.
const std::string & prefix() const
String to show before the beginning of the bar.
Definition: ProgressBar.h:290
void loadObject(Savable objectTypeId, T &object)
Load an object from the input stream.
virtual ~SerialIo()
Destructor.
Suffix suffix()
Property: suffix.
Definition: ProgressBar.h:190
Base class for reference counted objects.
Definition: SharedObject.h:64
A general, thread-safe way to report progress made on some task.
Definition: Progress.h:165
Marks the end of the data stream.
Format
Format of the state file.
Partitions instructions into basic blocks and functions.
Definition: Partitioner.h:321
Base class for all ROSE exceptions.
Definition: RoseException.h:9
Binary state files are smaller and faster than the other formats, but are not portable across archite...
virtual void close()=0
Detach a file.
Input binary analysis state.
Sawyer::SharedPointer< SerialIo > Ptr
Reference-counting pointer.
Savable nextObjectType()
Type of next object in the input stream.