Skip to content

Commit

Permalink
#98: sanitization: fix some bugs, make rt/enabled plugable
Browse files Browse the repository at this point in the history
  • Loading branch information
lifflander committed Dec 8, 2020
1 parent 4b5529d commit 69e04d2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/checkpoint/dispatch/dispatch.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ T* Traverse::reconstruct(SerialByteType* mem) {

template <typename T, typename SizerT, typename...Args>
SerialSizeType Standard::size(T& target, Args&&... args) {
Traverse::with<T, serializers::Sanitizer>(target);

auto sizer = Traverse::with<T, SizerT>(target, std::forward<Args>(args)...);
return sizer.getSize();
}
Expand Down
14 changes: 13 additions & 1 deletion src/checkpoint/serializers/sanitizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,21 @@

#include "checkpoint/serializers/sanitizer.h"

#include <memory>

namespace checkpoint { namespace sanitizer {

std::unique_ptr<Runtime> rt_ = nullptr;
Runtime* rt() {
static std::unique_ptr<sanitizer::Runtime> base_rt;
if (base_rt == nullptr) {
base_rt = std::make_unique<sanitizer::Runtime>();
}
return base_rt.get();
}

bool enabled() {
return false;
}

}} /* end namespace checkpoint::sanitizer */

Expand Down
34 changes: 22 additions & 12 deletions src/checkpoint/serializers/sanitizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace checkpoint { namespace sanitizer {
*/
struct Runtime {

virtual ~Runtime() = default;

/**
* \brief Check that a member is serialized
*
Expand Down Expand Up @@ -104,7 +106,10 @@ struct Runtime {
};

/// pimpl to runtime that contains runtime sanitizer logic
extern std::unique_ptr<Runtime> rt_;
extern Runtime* rt();

/// function that informs sanitizer if its enabled
extern bool enabled();

}} /* end namespace checkpoint::sanitizer*/

Expand Down Expand Up @@ -172,6 +177,14 @@ struct BaseSanitizer : checkpoint::Serializer {
: checkpoint::Serializer(in_mode)
{ }

/**
* \internal \brief Construct from other sanitizer-like class
*/
template <typename U>
explicit BaseSanitizer(U& cl)
: checkpoint::Serializer(cl.getMode())
{ }

/**
* \brief Check that member is actually serialized
*
Expand Down Expand Up @@ -206,16 +219,6 @@ struct BaseSanitizer : checkpoint::Serializer {
* \brief Do nothing on contiguous bytes
*/
void contiguousBytes(void*, std::size_t, std::size_t) { }

/**
* \internal \brief Clean \c t and cast to \c void*
*
* \param[in] t the element
*
* \return untyped pointer to the element
*/
template <typename T>
static void* cleanTypeToVoid(T&& t);
};

/**
Expand All @@ -234,7 +237,14 @@ struct Sanitizer : BaseSanitizer { };
* specialization to purposely invoke the regular serialization method on a
* class.
*/
struct NonSanitizingSanitizer : BaseSanitizer { };
struct NonSanitizingSanitizer : BaseSanitizer {
/**
* \internal \brief Construct from normal sanitizer
*/
explicit NonSanitizingSanitizer(Sanitizer& s)
: BaseSanitizer(s)
{ }
};

}} /* end namespace checkpoint::serializers */

Expand Down
34 changes: 23 additions & 11 deletions src/checkpoint/serializers/sanitizer.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@
namespace checkpoint { namespace serializers {

template <typename T>
/*static*/ void* BaseSanitizer::cleanTypeToVoid(T&& t) {
void* cleanTypeToVoid(T&& t) {
return reinterpret_cast<void*>(dispatch::cleanType(&t));
}

template <typename SerT, typename T>
void SanitizerDispatch<SerT,T>::serializeIntrusive(SerT& s, T& t) {
if (not sanitizer::enabled()) {
return;
}

// Declare this member as serialized
sanitizer::rt_->isSerialized(cleanTypeToVoid(t), 1, typeid(T).name());
sanitizer::rt()->isSerialized(cleanTypeToVoid(t), 1, typeid(T).name());

// Push this type on the stack since we are about to traverse it
sanitizer::rt_->push(typeid(T).name());
sanitizer::rt()->push(typeid(T).name());

// Recurse with the current serializer (a sanitizing pass)
t.serialize(s);
Expand All @@ -75,22 +79,30 @@ void SanitizerDispatch<SerT,T>::serializeIntrusive(SerT& s, T& t) {
t.serialize(nss);

// Pop off of stack
sanitizer::rt_->pop(typeid(T).name());
sanitizer::rt()->pop(typeid(T).name());
}

template <typename SerT, typename T>
void SanitizerDispatch<SerT,T>::serializeNonIntrusiveEnum(SerT& s, T& t) {
if (not sanitizer::enabled()) {
return;
}

// Declare this enum as serialized
sanitizer::rt_->isSerialized(cleanTypeToVoid(t), 1, typeid(T).name());
sanitizer::rt()->isSerialized(cleanTypeToVoid(t), 1, typeid(T).name());
}

template <typename SerT, typename T>
void SanitizerDispatch<SerT,T>::serializeNonIntrusive(SerT& s, T& t) {
if (not sanitizer::enabled()) {
return;
}

// Declare this enum as serialized
sanitizer::rt_->isSerialized(cleanTypeToVoid(t), 1, typeid(T).name());
sanitizer::rt()->isSerialized(cleanTypeToVoid(t), 1, typeid(T).name());

// Push this type on the stack since we are about to traverse it
sanitizer::rt_->push(typeid(T).name());
sanitizer::rt()->push(typeid(T).name());

// Recurse with the current serializer (a sanitizing pass)
serialize(s, t);
Expand All @@ -102,22 +114,22 @@ void SanitizerDispatch<SerT,T>::serializeNonIntrusive(SerT& s, T& t) {
serialize(nss, t);

// Pop off of stack
sanitizer::rt_->pop(typeid(T).name());
sanitizer::rt()->pop(typeid(T).name());
}

template <typename T>
void BaseSanitizer::check(T& t, std::string t_name) {
sanitizer::rt_->checkMember(cleanTypeToVoid(t), t_name, typeid(T).name());
sanitizer::rt()->checkMember(cleanTypeToVoid(t), t_name, typeid(T).name());
}

template <typename T>
void BaseSanitizer::skip(T& t, std::string t_name) {
sanitizer::rt_->skipMember(cleanTypeToVoid(t), t_name, typeid(T).name());
sanitizer::rt()->skipMember(cleanTypeToVoid(t), t_name, typeid(T).name());
}

template <typename SerializerT, typename T>
void BaseSanitizer::contiguousTyped(SerializerT&, T* t, std::size_t num_elms) {
sanitizer::rt_->isSerialized(
sanitizer::rt()->isSerialized(
reinterpret_cast<void*>(t), num_elms, typeid(t).name()
);
}
Expand Down

0 comments on commit 69e04d2

Please sign in to comment.