test_vptr_manager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 
  2. #include "catch.hpp"
  3. #include "vptr_manager.h"
  4. #include "shm.h"
  5. extern bg::detail::ShmContext g_shm_ctx;
  6. class TestBase3
  7. {
  8. public:
  9. virtual int Do1()
  10. {
  11. return a;
  12. }
  13. virtual ~TestBase3() = default;
  14. int a = 2;
  15. };
  16. class TestBase4 : virtual public TestBase3
  17. {
  18. public:
  19. virtual int Do1() override
  20. {
  21. return 4;
  22. }
  23. };
  24. class TestDrvied1 :virtual public TestBase3, public TestBase4
  25. {
  26. public:
  27. virtual ~TestDrvied1() = default;
  28. };
  29. class TestBase
  30. {
  31. public:
  32. virtual int Do1()
  33. {
  34. return a;
  35. }
  36. virtual ~TestBase() = default;
  37. int a = 2;
  38. };
  39. class TestBase2
  40. {
  41. public:
  42. virtual int Do1()
  43. {
  44. return b;
  45. }
  46. virtual ~TestBase2() = default;
  47. int b = 3;
  48. };
  49. class TestDrvied :public TestBase, public TestBase2
  50. {
  51. public:
  52. virtual ~TestDrvied() = default;
  53. };
  54. TEST_CASE("VptrObjectContainer<TestDrvied>, ShmNew Fix ShmDelete", "[TestDrvied][base]")
  55. {
  56. bg::ShmOptions opts(false, "Test", nullptr);
  57. opts.fix_vptr_on_init = false;
  58. REQUIRE(bg::ShmInit(opts) == true);
  59. REQUIRE(g_shm_ctx.mgr);
  60. TestDrvied* drv = bg::ShmNew<TestDrvied>();
  61. uintptr_t drv_ptr = *reinterpret_cast<uintptr_t*>(drv);
  62. memset(drv, 0, sizeof(TestDrvied));
  63. TestBase* base = static_cast<TestBase*>(drv);
  64. base->a = 2;
  65. TestBase2* base2 = static_cast<TestBase2*>(drv);
  66. base2->b = 3;
  67. REQUIRE(*reinterpret_cast<uintptr_t*>(base) == 0);
  68. *(uint64_t*)(0x600000006080) = 2;
  69. bg::ShmFixVptr();
  70. REQUIRE(*reinterpret_cast<uintptr_t*>(base) == drv_ptr);
  71. REQUIRE(*reinterpret_cast<uintptr_t*>(base2) != 0);
  72. REQUIRE(base->Do1() == 2);
  73. REQUIRE(base2->Do1() == 3);
  74. bg::ShmDelete(base);
  75. bg::ShmFini();
  76. }
  77. TEST_CASE("VptrObjectContainer<TestDrvied1>, ShmNew Fix ShmDelete")
  78. {
  79. bg::ShmOptions opts(false, "Test", nullptr);
  80. opts.fix_vptr_on_init = false;
  81. REQUIRE(bg::ShmInit(opts) == true);
  82. REQUIRE(g_shm_ctx.mgr);
  83. TestDrvied1* drv = bg::ShmNew<TestDrvied1>();
  84. uintptr_t drv_ptr = *reinterpret_cast<uintptr_t*>(drv);
  85. TestBase4* base2 = static_cast<TestBase4*>(drv);
  86. TestBase3* base = static_cast<TestBase3*>(drv);
  87. memset(drv, 0, sizeof(TestDrvied));
  88. base->a = 2;
  89. REQUIRE(*reinterpret_cast<uintptr_t*>(base) == 0);
  90. *(uint64_t*)(0x600000006080) = 2;
  91. bg::ShmFixVptr();
  92. REQUIRE(*reinterpret_cast<uintptr_t*>(base2) == drv_ptr);
  93. REQUIRE(base->Do1() == 4);
  94. REQUIRE(base2->Do1() == 4);
  95. bg::ShmDelete(base);
  96. bg::ShmFini();
  97. }
  98. class TestDrvied3
  99. {
  100. public:
  101. virtual ~TestDrvied3() = default;
  102. };
  103. TEST_CASE("VptrObjectContainer<TestDrvied3>, ShmNew Fix ShmDelete")
  104. {
  105. bg::ShmOptions opts(false, "Test", nullptr);
  106. opts.fix_vptr_on_init = false;
  107. REQUIRE(bg::ShmInit(opts) == true);
  108. REQUIRE(g_shm_ctx.mgr);
  109. TestDrvied3* drv = bg::ShmNew<TestDrvied3>();
  110. uintptr_t drv_ptr = *reinterpret_cast<uintptr_t*>(drv);
  111. memset(drv, 0, sizeof(TestDrvied3));
  112. REQUIRE(*reinterpret_cast<uintptr_t*>(drv) == 0);
  113. *(uint64_t*)(0x600000006080) = 2;
  114. bg::ShmFixVptr();
  115. REQUIRE(*reinterpret_cast<uintptr_t*>(drv) == drv_ptr);
  116. bg::ShmDelete(drv);
  117. bg::ShmFini();
  118. }