test_vptr.cpp 3.2 KB

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