wear_leveling_4byte.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <numeric>
  4. #include "gtest/gtest.h"
  5. #include "gmock/gmock.h"
  6. #include "backing_mocks.hpp"
  7. class WearLeveling4Byte : public ::testing::Test {
  8. protected:
  9. void SetUp() override {
  10. MockBackingStore::Instance().reset_instance();
  11. wear_leveling_init();
  12. }
  13. };
  14. static std::array<std::uint8_t, WEAR_LEVELING_LOGICAL_SIZE> verify_data;
  15. static wear_leveling_status_t test_write(const uint32_t address, const void* value, size_t length) {
  16. memcpy(&verify_data[address], value, length);
  17. return wear_leveling_write(address, value, length);
  18. }
  19. /**
  20. * This test verifies that the first write after initialisation occurs after the FNV1a_64 hash location.
  21. */
  22. TEST_F(WearLeveling4Byte, FirstWriteOccursAfterHash) {
  23. auto& inst = MockBackingStore::Instance();
  24. uint8_t test_value = 0x15;
  25. test_write(0x02, &test_value, sizeof(test_value));
  26. EXPECT_EQ(inst.log_begin()->address, WEAR_LEVELING_LOGICAL_SIZE + 8) << "Invalid first write address.";
  27. }
  28. /**
  29. * This test verifies that the first write after initialisation occurs after the FNV1a_64 hash location, after an erase has occurred.
  30. */
  31. TEST_F(WearLeveling4Byte, FirstWriteOccursAfterHash_AfterErase) {
  32. auto& inst = MockBackingStore::Instance();
  33. uint8_t test_value = 0x15;
  34. wear_leveling_erase();
  35. test_write(0x02, &test_value, sizeof(test_value));
  36. EXPECT_EQ((inst.log_begin() + 1)->address, WEAR_LEVELING_LOGICAL_SIZE + 8) << "Invalid first write address.";
  37. }
  38. /**
  39. * This test ensures the correct number of backing store writes occurs with a multibyte write, given the input buffer size.
  40. */
  41. TEST_F(WearLeveling4Byte, MultibyteBackingStoreWriteCounts) {
  42. auto& inst = MockBackingStore::Instance();
  43. for (std::size_t length = 1; length <= 5; ++length) {
  44. // Clear things out
  45. std::fill(verify_data.begin(), verify_data.end(), 0);
  46. inst.reset_instance();
  47. wear_leveling_init();
  48. // Generate a test block of data
  49. std::vector<std::uint8_t> testvalue(length);
  50. std::iota(testvalue.begin(), testvalue.end(), 0x20);
  51. // Write the data
  52. EXPECT_EQ(test_write(0, testvalue.data(), testvalue.size()), WEAR_LEVELING_SUCCESS) << "Write failed with incorrect status";
  53. std::size_t expected;
  54. if (length > 1) {
  55. expected = 2;
  56. } else {
  57. expected = 1;
  58. }
  59. // Check that we got the expected number of write log entries
  60. EXPECT_EQ(std::distance(inst.log_begin(), inst.log_end()), expected);
  61. }
  62. }
  63. /**
  64. * This test forces consolidation by writing enough to the write log that it overflows, consolidating the data into the
  65. * base logical area.
  66. */
  67. TEST_F(WearLeveling4Byte, ConsolidationOverflow) {
  68. auto& inst = MockBackingStore::Instance();
  69. // Generate a test block of data
  70. std::array<std::uint8_t, WEAR_LEVELING_LOGICAL_SIZE> testvalue;
  71. // Write the data
  72. std::iota(testvalue.begin(), testvalue.end(), 0x20);
  73. EXPECT_EQ(test_write(0, testvalue.data(), testvalue.size()), WEAR_LEVELING_CONSOLIDATED) << "Write returned incorrect status";
  74. uint8_t dummy = 0x40;
  75. EXPECT_EQ(test_write(0x04, &dummy, sizeof(dummy)), WEAR_LEVELING_SUCCESS) << "Write returned incorrect status";
  76. // Expected log:
  77. // [0,1]: multibyte, 5 bytes, backing address 0x18, logical address 0x00
  78. // [2,3]: multibyte, 5 bytes, backing address 0x20, logical address 0x05
  79. // [4,5]: multibyte, 5 bytes, backing address 0x28, logical address 0x0A, triggers consolidation
  80. // [6]: erase
  81. // [7,8]: consolidated data, backing address 0x00, logical address 0x00
  82. // [9,10]: consolidated data, backing address 0x08, logical address 0x08
  83. // [11,12]: FNV1a_64 result, backing address 0x10
  84. // [13]: multibyte, 1 byte, backing address 0x18, logical address 0x04
  85. EXPECT_EQ(std::distance(inst.log_begin(), inst.log_end()), 14);
  86. // Verify the backing store writes for the write log
  87. std::size_t index;
  88. write_log_entry_t e;
  89. for (index = 0; index < 6; ++index) {
  90. auto write_iter = inst.log_begin() + index;
  91. EXPECT_EQ(write_iter->address, WEAR_LEVELING_LOGICAL_SIZE + 8 + (index * BACKING_STORE_WRITE_SIZE)) << "Invalid write log address";
  92. // If this is the backing store write that contains the metadata, verify it
  93. if (index % 2 == 0) {
  94. write_log_entry_t e;
  95. e.raw64 = write_iter->value;
  96. EXPECT_EQ(LOG_ENTRY_GET_TYPE(e), LOG_ENTRY_TYPE_MULTIBYTE) << "Invalid write log entry type";
  97. }
  98. }
  99. // Verify the backing store erase
  100. {
  101. index = 6;
  102. auto write_iter = inst.log_begin() + index;
  103. e.raw64 = write_iter->value;
  104. EXPECT_TRUE(write_iter->erased) << "Backing store erase did not occur as required";
  105. }
  106. // Verify the backing store writes for consolidation
  107. for (index = 7; index < 11; ++index) {
  108. auto write_iter = inst.log_begin() + index;
  109. EXPECT_EQ(write_iter->address, (index - 7) * BACKING_STORE_WRITE_SIZE) << "Invalid write log entry address";
  110. }
  111. // Verify the FNV1a_64 write
  112. {
  113. EXPECT_EQ((inst.log_begin() + 11)->address, WEAR_LEVELING_LOGICAL_SIZE) << "Invalid write log address";
  114. e.raw32[0] = (inst.log_begin() + 11)->value;
  115. e.raw32[1] = (inst.log_begin() + 12)->value;
  116. EXPECT_EQ(e.raw64, fnv_64a_buf(testvalue.data(), testvalue.size(), FNV1A_64_INIT)) << "Invalid checksum"; // Note that checksum is based on testvalue, as we overwrote one byte and need to consult the consolidated data, not the current
  117. }
  118. // Verify the final write
  119. EXPECT_EQ((inst.log_begin() + 13)->address, WEAR_LEVELING_LOGICAL_SIZE + 8) << "Invalid write log address";
  120. // Verify the data is what we expected
  121. std::array<std::uint8_t, WEAR_LEVELING_LOGICAL_SIZE> readback;
  122. EXPECT_EQ(wear_leveling_read(0, readback.data(), WEAR_LEVELING_LOGICAL_SIZE), WEAR_LEVELING_SUCCESS) << "Failed to read back the saved data";
  123. EXPECT_TRUE(memcmp(readback.data(), verify_data.data(), WEAR_LEVELING_LOGICAL_SIZE) == 0) << "Readback did not match";
  124. // Re-init and re-read, verifying the reload capability
  125. EXPECT_NE(wear_leveling_init(), WEAR_LEVELING_FAILED) << "Re-initialisation failed";
  126. EXPECT_EQ(wear_leveling_read(0, readback.data(), WEAR_LEVELING_LOGICAL_SIZE), WEAR_LEVELING_SUCCESS) << "Failed to read back the saved data";
  127. EXPECT_TRUE(memcmp(readback.data(), verify_data.data(), WEAR_LEVELING_LOGICAL_SIZE) == 0) << "Readback did not match";
  128. }
  129. /**
  130. * This test verifies multibyte readback gets canceled with an out-of-bounds address.
  131. */
  132. TEST_F(WearLeveling4Byte, PlaybackReadbackMultibyte_OOB) {
  133. auto& inst = MockBackingStore::Instance();
  134. auto logstart = inst.storage_begin() + (WEAR_LEVELING_LOGICAL_SIZE / sizeof(backing_store_int_t));
  135. // Invalid FNV1a_64 hash
  136. (logstart + 0)->set(0);
  137. (logstart + 1)->set(0);
  138. // Set up a 2-byte logical write of [0x11,0x12] at logical offset 0x01
  139. auto entry0 = LOG_ENTRY_MAKE_MULTIBYTE(0x01, 2);
  140. entry0.raw8[3] = 0x11;
  141. entry0.raw8[4] = 0x12;
  142. (logstart + 2)->set(~entry0.raw32[0]);
  143. (logstart + 3)->set(~entry0.raw32[1]);
  144. // Set up a 2-byte logical write of [0x13,0x14] at logical offset 0x1000 (out of bounds)
  145. auto entry1 = LOG_ENTRY_MAKE_MULTIBYTE(0x1000, 2);
  146. entry1.raw8[3] = 0x13;
  147. entry1.raw8[4] = 0x14;
  148. (logstart + 4)->set(~entry1.raw32[0]);
  149. (logstart + 5)->set(~entry1.raw32[1]);
  150. // Set up a 2-byte logical write of [0x15,0x16] at logical offset 0x10
  151. auto entry2 = LOG_ENTRY_MAKE_MULTIBYTE(0x01, 2);
  152. entry2.raw8[3] = 0x15;
  153. entry2.raw8[4] = 0x16;
  154. (logstart + 6)->set(~entry2.raw32[0]);
  155. (logstart + 7)->set(~entry2.raw32[1]);
  156. EXPECT_EQ(inst.erasure_count(), 0) << "Invalid initial erase count";
  157. EXPECT_EQ(wear_leveling_init(), WEAR_LEVELING_CONSOLIDATED) << "Readback should have failed and triggered consolidation";
  158. EXPECT_EQ(inst.erasure_count(), 1) << "Invalid final erase count";
  159. uint8_t buf[2];
  160. wear_leveling_read(0x01, buf, sizeof(buf));
  161. EXPECT_EQ(buf[0], 0x11) << "Readback should have maintained the previous pre-failure value from the write log";
  162. EXPECT_EQ(buf[1], 0x12) << "Readback should have maintained the previous pre-failure value from the write log";
  163. }