Skip to content

Commit

Permalink
SafePtr: safe destruct ut
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Jun 5, 2024
1 parent 2872cfb commit 3642981
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/domino/DataDomino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#pragma once

#include <unordered_map>
#include <memory> // make_shared

#include "UniLog.hpp"
#include "UniPtr.hpp"
Expand Down
3 changes: 1 addition & 2 deletions src/msg_self/MsgSelf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#pragma once

#include <functional>
#include <memory> // shared_ptr
#include <deque>

#include "UniLog.hpp"
Expand Down Expand Up @@ -73,7 +72,7 @@ using SharedMsgCB = shared_ptr<MsgCB>;
class MsgSelf : public UniLog
{
public:
MsgSelf(const LogName& aUniLogName = ULN_DEFAULT) : UniLog(aUniLogName) {}
explicit MsgSelf(const LogName& aUniLogName = ULN_DEFAULT) : UniLog(aUniLogName) {}
~MsgSelf() { if (nMsg_) WRN("discard nMsg=" << nMsg_); }

void newMsg(const MsgCB&, const EMsgPriority = EMsgPri_NORM); // can't withdraw CB but easier usage
Expand Down
6 changes: 3 additions & 3 deletions src/safe_mem/SafePtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
// . ms & google: ~70% safety defects caused by mem safe issue
// - REQ: this class is to enhance safety of shared_ptr:
// . safe create : null or make_safe only (not allow unsafe create eg via raw ptr); minor in mem-bug
// . safe cast : only among self, base & void; compile-err is safer than ret-null; major in mem-bug
// * safe cast : only among self, base & void; compile-err is safer than ret-null; major in mem-bug
// . safe lifecycle: by shared_ptr (auto mem-mgmt, no use-after-free); major in mem-bug
// . safe ptr array: no need since std::array
// . safe del : not support self-deletor that maybe unsafe
// . safe del : not support self-deletor that maybe unsafe; call correct destructor
// . loop-ref : ???
// - DUTY-BOUND:
// . ensure ptr address is safe: legal created, not freed, not wild, etc
// . ensure ptr type is valid: origin*, or base*, or void*
// . not SafePtr but T to ensure T's inner safety (eg no exception within T's constructor)
// . hope cooperate with tool to ensure/track SafePtr, all T, all code's mem safe
//
// - VALUE:
// - How to solve safety issue:
// . way#1: Rust is language-based mem ctrl (heavy)
// . way#2: tool (dynamic eg valdrind, or static eg coverity)
// . keep legacy code/invest
Expand Down
37 changes: 37 additions & 0 deletions ut/safe_mem/SafePtrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,43 @@ TEST(SafePtrTest, safe_assign) // operator=() is auto-gen, just simple test 1 c
EXPECT_EQ(42, *(one.get())) << "REQ: valid get after assigner is reset";
}

#define DESTRUCT
// ***********************************************************************************************
struct TestBase
{
bool& isBaseOver_;
explicit TestBase(bool& aExtFlag) : isBaseOver_(aExtFlag) { isBaseOver_ = false; }
virtual ~TestBase() { isBaseOver_ = true; }
};
TEST(SafePtrTest, GOLD_destructByVoid)
{
bool isBaseOver;
SafePtr<void> test = make_safe<TestBase>(isBaseOver);
EXPECT_FALSE(isBaseOver) << "correctly constructed";

test = nullptr;
EXPECT_TRUE(isBaseOver) << "REQ: correctly destructed by SafePtr<void>";
}
struct TestDerive : public TestBase
{
bool& isDeriveOver_;
explicit TestDerive(bool& aBaseFlag, bool& aDeriveFlag)
: TestBase(aBaseFlag)
, isDeriveOver_(aDeriveFlag)
{ isDeriveOver_ = false; }
~TestDerive() { isDeriveOver_ = true; }
};
TEST(SafePtrTest, GOLD_destructByBase)
{
bool isBaseOver;
bool isDeriveOver;
SafePtr<TestBase> test = make_safe<TestDerive>(isBaseOver, isDeriveOver);
EXPECT_FALSE(isDeriveOver) << "correctly constructed";

test = nullptr;
EXPECT_TRUE(isDeriveOver) << "REQ: correctly destructed by SafePtr<TestBase>";
}

#define LIKE_SHARED_PTR
// ***********************************************************************************************
TEST(SafePtrTest, get_isMemSafe_afterDelOrigin)
Expand Down

0 comments on commit 3642981

Please sign in to comment.