Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ lib dll_polymorphic_derived2
<link>shared
;

lib dll_tracking
:
dll_tracking.cpp
../build//boost_serialization
:
<link>shared
;

test-suite "serialization" :
[ test-bsl-run_files test_array : A : : [ requires cxx11_hdr_array ] ] # BOOST_NO_CXX11_HDR_ARRAY
[ test-bsl-run_files test_boost_array : A ]
Expand Down Expand Up @@ -111,6 +119,8 @@ test-suite "serialization" :
[ test-bsl-run_files test_slist : A : : [ requires slist ] ] # BOOST_HAS_SLIST ]
[ test-bsl-run_files test_slist_ptrs : A : : [ requires slist ] ] # BOOST_HAS_SLIST ] ]
[ test-bsl-run_files test_split ]
[ test-bsl-run_files test_stored_tracking_save ]
[ test-bsl-run_files test_stored_tracking_load ]
[ test-bsl-run_files test_stack : A ]
[ test-bsl-run_files test_string_allocator ]
[ test-bsl-run_files test_tracking ]
Expand All @@ -134,6 +144,7 @@ test-suite "serialization" :
# so we'll just run the shared version of the test, which is the one that we're most interested
# in anyway.
[ test-bsl-run_polymorphic_files test_dll_exported : polymorphic_derived1 : dll_polymorphic_base dll_polymorphic_derived2 : <link>static:<build>no ]
[ test-bsl-run_polymorphic_files test_dll_tracking : : dll_tracking : <link>static:<build>no ]

[ test-bsl-run_polymorphic_files test_no_rtti : polymorphic_base polymorphic_derived1 polymorphic_derived2 ]
[ test-bsl-run_polymorphic_files test_exported : polymorphic_base polymorphic_derived1 polymorphic_derived2 ]
Expand Down
52 changes: 52 additions & 0 deletions test/dll_tracking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// dll_tracking.cpp

// Copyright 2026 Gennaro Prota.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#define DLL_TRACKING_EXPORT

#include <boost/archive/polymorphic_iarchive.hpp>
#include <boost/archive/polymorphic_oarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/nvp.hpp>

#include "dll_tracking.hpp"

template<class Archive>
void dll_tracked_base::serialize(Archive & ar, const unsigned int){
ar & BOOST_SERIALIZATION_NVP(i);
}

template<class Archive>
void dll_tracked_derived::serialize(Archive & ar, const unsigned int){
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(dll_tracked_base);
ar & BOOST_SERIALIZATION_NVP(j);
}

template<class Archive>
void dll_untracked_base::serialize(Archive & ar, const unsigned int){
ar & BOOST_SERIALIZATION_NVP(i);
}

template<class Archive>
void dll_untracked_derived::serialize(Archive & ar, const unsigned int){
ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(dll_untracked_base);
ar & BOOST_SERIALIZATION_NVP(j);
}

#define DLL_TRACKING_INSTANTIATE(T) \
template DLL_TRACKING_DECL void T::serialize( \
boost::archive::polymorphic_oarchive &, const unsigned int); \
template DLL_TRACKING_DECL void T::serialize( \
boost::archive::polymorphic_iarchive &, const unsigned int);

DLL_TRACKING_INSTANTIATE(dll_tracked_base)
DLL_TRACKING_INSTANTIATE(dll_tracked_derived)
DLL_TRACKING_INSTANTIATE(dll_untracked_base)
DLL_TRACKING_INSTANTIATE(dll_untracked_derived)

BOOST_CLASS_EXPORT_IMPLEMENT(dll_tracked_base)
BOOST_CLASS_EXPORT_IMPLEMENT(dll_untracked_derived)
64 changes: 64 additions & 0 deletions test/dll_tracking.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef BOOST_SERIALIZATION_TEST_DLL_TRACKING_HPP
#define BOOST_SERIALIZATION_TEST_DLL_TRACKING_HPP

/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// dll_tracking.hpp

// Copyright 2026 Gennaro Prota.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// Two hierarchies whose serialization lives in the shared library
// dll_tracking. The library exports dll_tracked_base, and so serializes it
// through a pointer: its serializer for that type tracks objects. It never
// serializes dll_untracked_base through a pointer.

#include <boost/config.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/export.hpp>

#if defined(DLL_TRACKING_EXPORT)
#define DLL_TRACKING_DECL BOOST_SYMBOL_EXPORT
#else
#define DLL_TRACKING_DECL BOOST_SYMBOL_IMPORT
#endif

class BOOST_SYMBOL_VISIBLE dll_tracked_base {
friend class boost::serialization::access;
template<class Archive>
DLL_TRACKING_DECL void serialize(Archive & ar, const unsigned int version);
public:
int i = 0;
virtual ~dll_tracked_base() {}
};

class BOOST_SYMBOL_VISIBLE dll_tracked_derived : public dll_tracked_base {
friend class boost::serialization::access;
template<class Archive>
DLL_TRACKING_DECL void serialize(Archive & ar, const unsigned int version);
public:
int j = 0;
};

class BOOST_SYMBOL_VISIBLE dll_untracked_base {
friend class boost::serialization::access;
template<class Archive>
DLL_TRACKING_DECL void serialize(Archive & ar, const unsigned int version);
public:
int i = 0;
virtual ~dll_untracked_base() {}
};

class BOOST_SYMBOL_VISIBLE dll_untracked_derived : public dll_untracked_base {
friend class boost::serialization::access;
template<class Archive>
DLL_TRACKING_DECL void serialize(Archive & ar, const unsigned int version);
public:
int j = 0;
};

BOOST_CLASS_EXPORT_KEY(dll_tracked_base)
BOOST_CLASS_EXPORT_KEY(dll_untracked_derived)

#endif // BOOST_SERIALIZATION_TEST_DLL_TRACKING_HPP
92 changes: 92 additions & 0 deletions test/test_dll_tracking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_dll_tracking.cpp

// Copyright 2026 Gennaro Prota.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// Whether a type is tracked is settled per module: the serializer of a module
// which serializes the type through a pointer tracks it, the others don't.
// When an archive gets the same type from the serializers of two modules, the
// tracking level stored for it must still match whether object ids follow.
// For dll_tracked_base, the test's untracked serializer comes first, and the
// library's tracked one follows; for dll_untracked_base, the test's tracked
// serializer is registered first, by the save through a base pointer, and the
// library's untracked one saves the base. The first order failed up to Boost
// 1.83, the second one from 1.84 to 1.92.

// Reported by gri38 in https://github.com/boostorg/serialization/issues/183
// and by ckreatsoMW in https://github.com/boostorg/serialization/issues/329.
// Thanks!

#include <cstddef>
#include <cstdio>
#include <fstream>
#include <memory>

#include <boost/config.hpp>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
using ::remove;
}
#endif

#include "test_tools.hpp"

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/shared_ptr.hpp>

#include "dll_tracking.hpp"

int test_main(int /* argc */, char * /* argv */ []){
const char * testfile = boost::archive::tmpnam(NULL);
BOOST_REQUIRE(NULL != testfile);

dll_tracked_base base;
base.i = 1;
std::shared_ptr<dll_tracked_derived> first =
std::make_shared<dll_tracked_derived>();
std::shared_ptr<dll_tracked_derived> second =
std::make_shared<dll_tracked_derived>();
second->i = 2;
second->j = 3;
std::shared_ptr<dll_untracked_derived> other =
std::make_shared<dll_untracked_derived>();
other->i = 4;
other->j = 5;
std::shared_ptr<dll_untracked_base> through_base = other;
{
test_ostream os(testfile, TEST_STREAM_FLAGS);
test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
oa << BOOST_SERIALIZATION_NVP(base);
oa << BOOST_SERIALIZATION_NVP(first);
oa << BOOST_SERIALIZATION_NVP(second);
oa << boost::serialization::make_nvp("again", second);
oa << BOOST_SERIALIZATION_NVP(through_base);
}

dll_tracked_base base_in;
std::shared_ptr<dll_tracked_derived> first_in;
std::shared_ptr<dll_tracked_derived> second_in;
std::shared_ptr<dll_tracked_derived> again_in;
std::shared_ptr<dll_untracked_base> through_base_in;
{
test_istream is(testfile, TEST_STREAM_FLAGS);
test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
ia >> boost::serialization::make_nvp("base", base_in);
ia >> boost::serialization::make_nvp("first", first_in);
ia >> boost::serialization::make_nvp("second", second_in);
ia >> boost::serialization::make_nvp("again", again_in);
ia >> boost::serialization::make_nvp("through_base", through_base_in);
}
BOOST_CHECK(1 == base_in.i);
BOOST_CHECK(second_in && 2 == second_in->i && 3 == second_in->j);
BOOST_CHECK(again_in == second_in);
const std::shared_ptr<dll_untracked_derived> other_in =
std::dynamic_pointer_cast<dll_untracked_derived>(through_base_in);
BOOST_CHECK(other_in && 4 == other_in->i && 5 == other_in->j);

std::remove(testfile);
return EXIT_SUCCESS;
}
61 changes: 61 additions & 0 deletions test/test_stored_tracking_load.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_stored_tracking_load.cpp

// Copyright 2026 Gennaro Prota.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// Reads the archive test_stored_tracking_save writes. Unlike that program,
// this one serializes an A through a pointer, so here A is tracked. The
// archive must still be read as it was written, following the tracking level
// stored in it rather than the one this program would use.

// Raised by Robert Ramey in
// https://github.com/boostorg/serialization/issues/183. Thanks!

#include <fstream>
#include <memory>
#include <string>

#include <boost/archive/tmpdir.hpp>
#include <boost/preprocessor/stringize.hpp>

#include "test_tools.hpp"

#include <boost/serialization/nvp.hpp>
#include <boost/serialization/unique_ptr.hpp>

struct A {
int i;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* version */){
ar & BOOST_SERIALIZATION_NVP(i);
}
};

// Never called. Compiling it is enough: it gives this program a pointer
// serializer for A, which exists before main() runs.
void load_through_pointer(test_iarchive & ia){
std::unique_ptr<A> p;
ia >> BOOST_SERIALIZATION_NVP(p);
}

int test_main(int /* argc */, char * /* argv */ []){
std::string filename = boost::archive::tmpdir();
filename += "/stored_tracking_";
filename += BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST);

A first = { 0 };
A second = { 0 };
int last = 0;
test_istream is(filename.c_str(), TEST_STREAM_FLAGS);
test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
ia >> BOOST_SERIALIZATION_NVP(first);
ia >> BOOST_SERIALIZATION_NVP(second);
ia >> BOOST_SERIALIZATION_NVP(last);
BOOST_CHECK(1 == first.i);
BOOST_CHECK(2 == second.i);
BOOST_CHECK(3 == last);
return EXIT_SUCCESS;
}
45 changes: 45 additions & 0 deletions test/test_stored_tracking_save.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_stored_tracking_save.cpp

// Copyright 2026 Gennaro Prota.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// Writes the archive test_stored_tracking_load reads. This program never
// serializes an A through a pointer, so A, which is tracked selectively, is
// not tracked here: the archive says so, and carries no object ids for it.

#include <fstream>
#include <string>

#include <boost/archive/tmpdir.hpp>
#include <boost/preprocessor/stringize.hpp>

#include "test_tools.hpp"

#include <boost/serialization/nvp.hpp>

struct A {
int i;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* version */){
ar & BOOST_SERIALIZATION_NVP(i);
}
};

int test_main(int /* argc */, char * /* argv */ []){
std::string filename = boost::archive::tmpdir();
filename += "/stored_tracking_";
filename += BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST);

const A first = { 1 };
const A second = { 2 };
const int last = 3;
test_ostream os(filename.c_str(), TEST_STREAM_FLAGS);
test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
oa << BOOST_SERIALIZATION_NVP(first);
oa << BOOST_SERIALIZATION_NVP(second);
oa << BOOST_SERIALIZATION_NVP(last);
return EXIT_SUCCESS;
}
Loading