1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // basic_archive.cpp:
3 
4 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  See http://www.boost.org for updates, documentation, and revision history.
10 
11 #include <boost/config.hpp> // msvc 6.0 needs this to suppress warnings
12 
13 #include <boost/assert.hpp>
14 #include <set>
15 #include <list>
16 #include <vector>
17 #include <cstddef> // size_t, NULL
18 
19 #include <boost/config.hpp>
20 #if defined(BOOST_NO_STDC_NAMESPACE)
21 namespace std{
22     using ::size_t;
23 } // namespace std
24 #endif
25 
26 #include <boost/integer_traits.hpp>
27 
28 #define BOOST_ARCHIVE_SOURCE
29 // include this to prevent linker errors when the
30 // same modules are marked export and import.
31 #define BOOST_SERIALIZATION_SOURCE
32 #include <boost/serialization/config.hpp>
33 
34 #include <boost/serialization/state_saver.hpp>
35 #include <boost/serialization/throw_exception.hpp>
36 #include <boost/serialization/tracking.hpp>
37 
38 #include <boost/archive/archive_exception.hpp>
39 #include <boost/archive/detail/decl.hpp>
40 #include <boost/archive/basic_archive.hpp>
41 #include <boost/archive/detail/basic_iserializer.hpp>
42 #include <boost/archive/detail/basic_pointer_iserializer.hpp>
43 #include <boost/archive/detail/basic_iarchive.hpp>
44 
45 #include <boost/archive/detail/auto_link_archive.hpp>
46 
47 using namespace boost::serialization;
48 
49 namespace boost {
50 namespace archive {
51 namespace detail {
52 
53 class basic_iarchive_impl {
54     friend class basic_iarchive;
55     library_version_type m_archive_library_version;
56     unsigned int m_flags;
57 
58     //////////////////////////////////////////////////////////////////////
59     // information about each serialized object loaded
60     // indexed on object_id
61     struct aobject
62     {
63         void * address;
64         bool loaded_as_pointer;
65         class_id_type class_id;
aobjectboost::archive::detail::basic_iarchive_impl::aobject66         aobject(
67             void *a,
68             class_id_type class_id_
69         ) :
70             address(a),
71             loaded_as_pointer(false),
72             class_id(class_id_)
73         {}
aobjectboost::archive::detail::basic_iarchive_impl::aobject74         aobject() :
75             address(NULL),
76             loaded_as_pointer(false),
77             class_id(-2)
78         {}
79     };
80     typedef std::vector<aobject> object_id_vector_type;
81     object_id_vector_type object_id_vector;
82 
83     //////////////////////////////////////////////////////////////////////
84     // used to implement the reset_object_address operation.
85     struct moveable_objects {
86         object_id_type start;
87         object_id_type end;
88         object_id_type recent;
89         bool is_pointer;
moveable_objectsboost::archive::detail::basic_iarchive_impl::moveable_objects90         moveable_objects() :
91             start(0),
92             end(0),
93             recent(0),
94             is_pointer(false)
95         {}
96     } m_moveable_objects;
97 
98     void reset_object_address(
99         const void * new_address,
100         const void *old_address
101     );
102 
103     //////////////////////////////////////////////////////////////////////
104     // used by load object to look up class id given basic_serializer
105     struct cobject_type
106     {
107         const basic_iserializer * m_bis;
108         const class_id_type m_class_id;
cobject_typeboost::archive::detail::basic_iarchive_impl::cobject_type109         cobject_type(
110             std::size_t class_id,
111             const basic_iserializer & bis
112         ) :
113             m_bis(& bis),
114             m_class_id(class_id)
115         {}
cobject_typeboost::archive::detail::basic_iarchive_impl::cobject_type116         cobject_type(const cobject_type & rhs) :
117             m_bis(rhs.m_bis),
118             m_class_id(rhs.m_class_id)
119         {}
120         // the following cannot be defined because of the const
121         // member.  This will generate a link error if an attempt
122         // is made to assign.  This should never be necessary
123         cobject_type & operator=(const cobject_type & rhs);
operator <boost::archive::detail::basic_iarchive_impl::cobject_type124         bool operator<(const cobject_type &rhs) const
125         {
126             return *m_bis < *(rhs.m_bis);
127         }
128     };
129     typedef std::set<cobject_type> cobject_info_set_type;
130     cobject_info_set_type cobject_info_set;
131 
132     //////////////////////////////////////////////////////////////////////
133     // information about each serialized class indexed on class_id
134     class cobject_id
135     {
136     public:
operator =(const cobject_id & rhs)137         cobject_id & operator=(const cobject_id & rhs){
138             bis_ptr = rhs.bis_ptr;
139             bpis_ptr = rhs.bpis_ptr;
140             file_version = rhs.file_version;
141             tracking_level = rhs.tracking_level;
142             initialized = rhs.initialized;
143             return *this;
144         }
145         const basic_iserializer * bis_ptr;
146         const basic_pointer_iserializer * bpis_ptr;
147         version_type file_version;
148         tracking_type tracking_level;
149         bool initialized;
150 
cobject_id(const basic_iserializer & bis_)151         cobject_id(const basic_iserializer & bis_) :
152             bis_ptr(& bis_),
153             bpis_ptr(NULL),
154             file_version(0),
155             tracking_level(track_never),
156             initialized(false)
157         {}
cobject_id(const cobject_id & rhs)158         cobject_id(const cobject_id &rhs):
159             bis_ptr(rhs.bis_ptr),
160             bpis_ptr(rhs.bpis_ptr),
161             file_version(rhs.file_version),
162             tracking_level(rhs.tracking_level),
163             initialized(rhs.initialized)
164         {}
165     };
166     typedef std::vector<cobject_id> cobject_id_vector_type;
167     cobject_id_vector_type cobject_id_vector;
168 
169     //////////////////////////////////////////////////////////////////////
170     // address of the most recent object serialized as a poiner
171     // whose data itself is now pending serialization
172     struct pending {
173         void * object;
174         const basic_iserializer * bis;
175         version_type version;
pendingboost::archive::detail::basic_iarchive_impl::pending176         pending() :
177             object(NULL),
178             bis(NULL),
179             version(0)
180         {}
181     } m_pending;
182 
basic_iarchive_impl(unsigned int flags)183     basic_iarchive_impl(unsigned int flags) :
184         m_archive_library_version(BOOST_ARCHIVE_VERSION()),
185         m_flags(flags)
186     {}
set_library_version(library_version_type archive_library_version)187     void set_library_version(library_version_type archive_library_version){
188         m_archive_library_version = archive_library_version;
189     }
190     bool
191     track(
192         basic_iarchive & ar,
193         void * & t
194     );
195     void
196     load_preamble(
197         basic_iarchive & ar,
198         cobject_id & co
199     );
200     class_id_type register_type(
201         const basic_iserializer & bis
202     );
203 
204     // redirect through virtual functions to load functions for this archive
205     template<class T>
load(basic_iarchive & ar,T & t)206     void load(basic_iarchive & ar, T & t){
207         ar.vload(t);
208     }
209 
210 //public:
211     void
next_object_pointer(void * t)212     next_object_pointer(void * t){
213         m_pending.object = t;
214     }
215     void delete_created_pointers();
216     class_id_type register_type(
217         const basic_pointer_iserializer & bpis
218     );
219     void load_object(
220         basic_iarchive & ar,
221         void * t,
222         const basic_iserializer & bis
223     );
224     const basic_pointer_iserializer * load_pointer(
225         basic_iarchive & ar,
226         void * & t,
227         const basic_pointer_iserializer * bpis,
228         const basic_pointer_iserializer * (*finder)(
229             const boost::serialization::extended_type_info & type
230         )
231     );
232 };
233 
234 inline void
reset_object_address(void const * const new_address,void const * const old_address)235 basic_iarchive_impl::reset_object_address(
236     void const * const new_address,
237     void const * const old_address
238 ){
239     if(m_moveable_objects.is_pointer)
240         return;
241 
242     // this code handles a couple of situations.
243     // a) where reset_object_address is applied to an untracked object.
244     //    In such a case the call is really superfluous and its really an
245     //    an error.  But we don't have access to the types here so we can't
246     //    know that.  However, this code will effectively turn this situation
247     //    into a no-op and every thing will work fine - albeat with a small
248     //    execution time penalty.
249     // b) where the call to reset_object_address doesn't immediatly follow
250     //    the << operator to which it corresponds.  This would be a bad idea
251     //    but the code may work anyway.  Naturally, a bad practice on the part
252     //    of the programmer but we can't detect it - as above.  So maybe we
253     //    can save a few more people from themselves as above.
254     object_id_type i = m_moveable_objects.recent;
255     for(; i < m_moveable_objects.end; ++i){
256         if(old_address == object_id_vector[i].address)
257             break;
258     }
259     for(; i < m_moveable_objects.end; ++i){
260         void const * const this_address = object_id_vector[i].address;
261         // calculate displacement from this level
262         // warning - pointer arithmetic on void * is in herently non-portable
263         // but expected to work on all platforms in current usage
264         if(this_address > old_address){
265             std::size_t member_displacement
266                 = reinterpret_cast<std::size_t>(this_address)
267                 - reinterpret_cast<std::size_t>(old_address);
268             object_id_vector[i].address = reinterpret_cast<void *>(
269                 reinterpret_cast<std::size_t>(new_address) + member_displacement
270             );
271         }
272         else{
273             std::size_t member_displacement
274                 = reinterpret_cast<std::size_t>(old_address)
275                 - reinterpret_cast<std::size_t>(this_address);
276             object_id_vector[i].address = reinterpret_cast<void *>(
277                 reinterpret_cast<std::size_t>(new_address) - member_displacement
278             );
279        }
280     }
281 }
282 
283 inline void
delete_created_pointers()284 basic_iarchive_impl::delete_created_pointers()
285 {
286     object_id_vector_type::iterator i;
287     for(
288         i = object_id_vector.begin();
289         i != object_id_vector.end();
290         ++i
291     ){
292         if(i->loaded_as_pointer){
293             // borland complains without this minor hack
294             const int j = i->class_id;
295             const cobject_id & co = cobject_id_vector[j];
296             //const cobject_id & co = cobject_id_vector[i->class_id];
297             // with the appropriate input serializer,
298             // delete the indicated object
299             co.bis_ptr->destroy(i->address);
300         }
301     }
302 }
303 
304 inline class_id_type
register_type(const basic_iserializer & bis)305 basic_iarchive_impl::register_type(
306     const basic_iserializer & bis
307 ){
308     class_id_type cid(cobject_info_set.size());
309     cobject_type co(cid, bis);
310     std::pair<cobject_info_set_type::const_iterator, bool>
311         result = cobject_info_set.insert(co);
312 
313     if(result.second){
314         cobject_id_vector.push_back(cobject_id(bis));
315         BOOST_ASSERT(cobject_info_set.size() == cobject_id_vector.size());
316     }
317     cid = result.first->m_class_id;
318     // borland complains without this minor hack
319     const int tid = cid;
320     cobject_id & coid = cobject_id_vector[tid];
321     coid.bpis_ptr = bis.get_bpis_ptr();
322     return cid;
323 }
324 
325 void
load_preamble(basic_iarchive & ar,cobject_id & co)326 basic_iarchive_impl::load_preamble(
327     basic_iarchive & ar,
328     cobject_id & co
329 ){
330     if(! co.initialized){
331         if(co.bis_ptr->class_info()){
332             class_id_optional_type cid(class_id_type(0));
333             load(ar, cid);    // to be thrown away
334             load(ar, co.tracking_level);
335             load(ar, co.file_version);
336         }
337         else{
338             // override tracking with indicator from class information
339             co.tracking_level = co.bis_ptr->tracking(m_flags);
340             co.file_version = version_type(
341                 co.bis_ptr->version()
342             );
343         }
344         co.initialized = true;
345     }
346 }
347 
348 bool
track(basic_iarchive & ar,void * & t)349 basic_iarchive_impl::track(
350     basic_iarchive & ar,
351     void * & t
352 ){
353     object_id_type oid;
354     load(ar, oid);
355 
356     // if its a reference to a old object
357     if(object_id_type(object_id_vector.size()) > oid){
358         // we're done
359         t = object_id_vector[oid].address;
360         return false;
361     }
362     return true;
363 }
364 
365 inline void
load_object(basic_iarchive & ar,void * t,const basic_iserializer & bis)366 basic_iarchive_impl::load_object(
367     basic_iarchive & ar,
368     void * t,
369     const basic_iserializer & bis
370 ){
371     m_moveable_objects.is_pointer = false;
372     serialization::state_saver<bool> ss_is_pointer(m_moveable_objects.is_pointer);
373     // if its been serialized through a pointer and the preamble's been done
374     if(t == m_pending.object && & bis == m_pending.bis){
375         // read data
376         (bis.load_object_data)(ar, t, m_pending.version);
377         return;
378     }
379 
380     const class_id_type cid = register_type(bis);
381     const int i = cid;
382     cobject_id & co = cobject_id_vector[i];
383 
384     load_preamble(ar, co);
385 
386     // save the current move stack position in case we want to truncate it
387     boost::serialization::state_saver<object_id_type> ss_start(m_moveable_objects.start);
388 
389     // note: extra line used to evade borland issue
390     const bool tracking = co.tracking_level;
391 
392     object_id_type this_id;
393     m_moveable_objects.start =
394     this_id = object_id_type(object_id_vector.size());
395 
396     // if we tracked this object when the archive was saved
397     if(tracking){
398         // if it was already read
399         if(!track(ar, t))
400             // we're done
401             return;
402         // add a new enty into the tracking list
403         object_id_vector.push_back(aobject(t, cid));
404         // and add an entry for this object
405         m_moveable_objects.end = object_id_type(object_id_vector.size());
406     }
407     // read data
408     (bis.load_object_data)(ar, t, co.file_version);
409     m_moveable_objects.recent = this_id;
410 }
411 
412 inline const basic_pointer_iserializer *
load_pointer(basic_iarchive & ar,void * & t,const basic_pointer_iserializer * bpis_ptr,const basic_pointer_iserializer * (* finder)(const boost::serialization::extended_type_info & type_))413 basic_iarchive_impl::load_pointer(
414     basic_iarchive &ar,
415     void * & t,
416     const basic_pointer_iserializer * bpis_ptr,
417     const basic_pointer_iserializer * (*finder)(
418         const boost::serialization::extended_type_info & type_
419     )
420 ){
421     m_moveable_objects.is_pointer = true;
422     serialization::state_saver<bool> w(m_moveable_objects.is_pointer);
423 
424     class_id_type cid;
425     load(ar, cid);
426 
427 #if BOOST_VERSION < 107400
428     if(NULL_POINTER_TAG == cid){
429         t = NULL;
430         return bpis_ptr;
431     }
432 #else // this case is taken from https://github.com/boostorg/serialization/blob/develop/src/basic_iarchive.cpp#L430-L433
433     if(BOOST_SERIALIZATION_NULL_POINTER_TAG == cid){
434         t = NULL;
435         return bpis_ptr;
436     }
437 #endif
438 
439     // if its a new class type - i.e. never been registered
440     if(class_id_type(cobject_info_set.size()) <= cid){
441         // if its either abstract
442         if(NULL == bpis_ptr
443         // or polymorphic
444         || bpis_ptr->get_basic_serializer().is_polymorphic()){
445             // is must have been exported
446             char key[BOOST_SERIALIZATION_MAX_KEY_SIZE];
447             class_name_type class_name(key);
448             load(ar, class_name);
449             // if it has a class name
450             const serialization::extended_type_info *eti = NULL;
451             if(0 != key[0])
452                 eti = serialization::extended_type_info::find(key);
453             if(NULL == eti)
454                 boost::serialization::throw_exception(
455                     archive_exception(archive_exception::unregistered_class)
456                 );
457             bpis_ptr = (*finder)(*eti);
458         }
459         BOOST_ASSERT(NULL != bpis_ptr);
460         // class_id_type new_cid = register_type(bpis_ptr->get_basic_serializer());
461         BOOST_VERIFY(register_type(bpis_ptr->get_basic_serializer()) == cid);
462         int i = cid;
463         cobject_id_vector[i].bpis_ptr = bpis_ptr;
464     }
465     int i = cid;
466     cobject_id & co = cobject_id_vector[i];
467     bpis_ptr = co.bpis_ptr;
468 
469     load_preamble(ar, co);
470 
471     // extra line to evade borland issue
472     const bool tracking = co.tracking_level;
473     // if we're tracking and the pointer has already been read
474     if(tracking && ! track(ar, t))
475         // we're done
476         return bpis_ptr;
477 
478     // save state
479     serialization::state_saver<object_id_type> w_start(m_moveable_objects.start);
480 
481     // allocate space on the heap for the object - to be constructed later
482     t = bpis_ptr->heap_allocation();
483     BOOST_ASSERT(NULL != t);
484 
485     if(! tracking){
486         bpis_ptr->load_object_ptr(ar, t, co.file_version);
487     }
488     else{
489         serialization::state_saver<void *> x(m_pending.object);
490         serialization::state_saver<const basic_iserializer *> y(m_pending.bis);
491         serialization::state_saver<version_type> z(m_pending.version);
492 
493         m_pending.bis = & bpis_ptr->get_basic_serializer();
494         m_pending.version = co.file_version;
495 
496         // predict next object id to be created
497         const unsigned int ui = object_id_vector.size();
498 
499         serialization::state_saver<object_id_type> w_end(m_moveable_objects.end);
500 
501 
502         // add to list of serialized objects so that we can properly handle
503         // cyclic strucures
504         object_id_vector.push_back(aobject(t, cid));
505 
506         // remember that that the address of these elements could change
507         // when we make another call so don't use the address
508         bpis_ptr->load_object_ptr(
509             ar,
510             t,
511             m_pending.version
512         );
513         object_id_vector[ui].loaded_as_pointer = true;
514     }
515 
516     return bpis_ptr;
517 }
518 
519 } // namespace detail
520 } // namespace archive
521 } // namespace boost
522 
523 //////////////////////////////////////////////////////////////////////
524 // implementation of basic_iarchive functions
525 namespace boost {
526 namespace archive {
527 namespace detail {
528 
529 BOOST_ARCHIVE_DECL void
next_object_pointer(void * t)530 basic_iarchive::next_object_pointer(void *t){
531     pimpl->next_object_pointer(t);
532 }
533 
534 BOOST_ARCHIVE_DECL
basic_iarchive(unsigned int flags)535 basic_iarchive::basic_iarchive(unsigned int flags) :
536     pimpl(new basic_iarchive_impl(flags))
537 {}
538 
539 BOOST_ARCHIVE_DECL
~basic_iarchive()540 basic_iarchive::~basic_iarchive()
541 {}
542 
543 BOOST_ARCHIVE_DECL void
set_library_version(library_version_type archive_library_version)544 basic_iarchive::set_library_version(library_version_type archive_library_version){
545     pimpl->set_library_version(archive_library_version);
546 }
547 
548 BOOST_ARCHIVE_DECL void
reset_object_address(const void * new_address,const void * old_address)549 basic_iarchive::reset_object_address(
550     const void * new_address,
551     const void * old_address
552 ){
553     pimpl->reset_object_address(new_address, old_address);
554 }
555 
556 BOOST_ARCHIVE_DECL void
load_object(void * t,const basic_iserializer & bis)557 basic_iarchive::load_object(
558     void *t,
559     const basic_iserializer & bis
560 ){
561     pimpl->load_object(*this, t, bis);
562 }
563 
564 // load a pointer object
565 BOOST_ARCHIVE_DECL const basic_pointer_iserializer *
load_pointer(void * & t,const basic_pointer_iserializer * bpis_ptr,const basic_pointer_iserializer * (* finder)(const boost::serialization::extended_type_info & type_))566 basic_iarchive::load_pointer(
567     void * &t,
568     const basic_pointer_iserializer * bpis_ptr,
569     const basic_pointer_iserializer * (*finder)(
570         const boost::serialization::extended_type_info & type_
571     )
572 
573 ){
574     return pimpl->load_pointer(*this, t, bpis_ptr, finder);
575 }
576 
577 BOOST_ARCHIVE_DECL void
register_basic_serializer(const basic_iserializer & bis)578 basic_iarchive::register_basic_serializer(const basic_iserializer & bis){
579     pimpl->register_type(bis);
580 }
581 
582 BOOST_ARCHIVE_DECL void
delete_created_pointers()583 basic_iarchive::delete_created_pointers()
584 {
585     pimpl->delete_created_pointers();
586 }
587 
588 BOOST_ARCHIVE_DECL boost::archive::library_version_type
get_library_version() const589 basic_iarchive::get_library_version() const{
590     return pimpl->m_archive_library_version;
591 }
592 
593 BOOST_ARCHIVE_DECL unsigned int
get_flags() const594 basic_iarchive::get_flags() const{
595     return pimpl->m_flags;
596 }
597 
598 } // namespace detail
599 } // namespace archive
600 } // namespace boost
601