diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 0982af3df..ec76185fe 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -57,6 +57,14 @@ lib dll_polymorphic_derived2 shared ; +lib dll_tracking + : + dll_tracking.cpp + ../build//boost_serialization + : + 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 ] @@ -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 ] @@ -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 : static:no ] + [ test-bsl-run_polymorphic_files test_dll_tracking : : dll_tracking : static: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 ] diff --git a/test/dll_tracking.cpp b/test/dll_tracking.cpp new file mode 100644 index 000000000..f5b2bd4cc --- /dev/null +++ b/test/dll_tracking.cpp @@ -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 +#include +#include +#include + +#include "dll_tracking.hpp" + +template +void dll_tracked_base::serialize(Archive & ar, const unsigned int){ + ar & BOOST_SERIALIZATION_NVP(i); +} + +template +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 +void dll_untracked_base::serialize(Archive & ar, const unsigned int){ + ar & BOOST_SERIALIZATION_NVP(i); +} + +template +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) diff --git a/test/dll_tracking.hpp b/test/dll_tracking.hpp new file mode 100644 index 000000000..ddc8df08f --- /dev/null +++ b/test/dll_tracking.hpp @@ -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 +#include +#include + +#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 + 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 + 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 + 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 + 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 diff --git a/test/test_dll_tracking.cpp b/test/test_dll_tracking.cpp new file mode 100644 index 000000000..af041932f --- /dev/null +++ b/test/test_dll_tracking.cpp @@ -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 +#include +#include +#include + +#include +#if defined(BOOST_NO_STDC_NAMESPACE) +namespace std{ + using ::remove; +} +#endif + +#include "test_tools.hpp" + +#include +#include + +#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 first = + std::make_shared(); + std::shared_ptr second = + std::make_shared(); + second->i = 2; + second->j = 3; + std::shared_ptr other = + std::make_shared(); + other->i = 4; + other->j = 5; + std::shared_ptr 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 first_in; + std::shared_ptr second_in; + std::shared_ptr again_in; + std::shared_ptr 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 other_in = + std::dynamic_pointer_cast(through_base_in); + BOOST_CHECK(other_in && 4 == other_in->i && 5 == other_in->j); + + std::remove(testfile); + return EXIT_SUCCESS; +} diff --git a/test/test_stored_tracking_load.cpp b/test/test_stored_tracking_load.cpp new file mode 100644 index 000000000..5ffadb66d --- /dev/null +++ b/test/test_stored_tracking_load.cpp @@ -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 +#include +#include + +#include +#include + +#include "test_tools.hpp" + +#include +#include + +struct A { + int i; + template + 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 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; +} diff --git a/test/test_stored_tracking_save.cpp b/test/test_stored_tracking_save.cpp new file mode 100644 index 000000000..15c37f5ca --- /dev/null +++ b/test/test_stored_tracking_save.cpp @@ -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 +#include + +#include +#include + +#include "test_tools.hpp" + +#include + +struct A { + int i; + template + 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; +}