Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TDBStore: Must use work buffer with size of programming unit. #13731

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions storage/kvstore/source/TDBStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ typedef struct {
uint32_t crc;
} reserved_trailer_t;

static const uint32_t work_buf_size = 64;
static const uint32_t initial_crc = 0xFFFFFFFF;
static const uint32_t initial_max_keys = 16;

Expand Down Expand Up @@ -327,7 +326,7 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
user_key_ptr[key_size] = '\0';
} else {
dest_buf = _work_buf;
chunk_size = std::min(key_size, work_buf_size);
chunk_size = std::min(key_size, _prog_size);
}
} else {
// This means that we're on the data part
Expand All @@ -337,13 +336,13 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
// 3. After actual part is finished - read to work buffer
// 4. Copy data flag not set - read to work buffer
if (curr_data_offset < data_offset) {
chunk_size = std::min((size_t)work_buf_size, (size_t)(data_offset - curr_data_offset));
chunk_size = std::min((size_t)_prog_size, (size_t)(data_offset - curr_data_offset));
dest_buf = _work_buf;
} else if (copy_data && (curr_data_offset < data_offset + actual_data_size)) {
chunk_size = actual_data_size;
dest_buf = static_cast<uint8_t *>(data_buf);
} else {
chunk_size = std::min(work_buf_size, total_size);
chunk_size = std::min(_prog_size, total_size);
dest_buf = _work_buf;
}
}
Expand Down Expand Up @@ -824,17 +823,19 @@ int TDBStore::copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_o
uint32_t &to_next_offset)
{
int ret;
record_header_t header;
record_header_t *header = (record_header_t *) _work_buf;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no guarantee _work_buf is large enough

uint32_t total_size;
uint16_t chunk_size;

ret = read_area(from_area, from_offset, sizeof(header), &header);
memset(_work_buf, 0, _prog_size);

ret = read_area(from_area, from_offset, sizeof(record_header_t), header);
if (ret) {
return ret;
}

total_size = align_up(sizeof(record_header_t), _prog_size) +
align_up(header.key_size + header.data_size, _prog_size);;
align_up(header->key_size + header->data_size, _prog_size);;


if (to_offset + total_size > _size) {
Expand All @@ -847,7 +848,7 @@ int TDBStore::copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_o
}

chunk_size = align_up(sizeof(record_header_t), _prog_size);
ret = write_area(1 - from_area, to_offset, chunk_size, &header);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I get that this is what the PR intended to fix

ret = write_area(1 - from_area, to_offset, chunk_size, header);
if (ret) {
return ret;
}
Expand All @@ -857,7 +858,7 @@ int TDBStore::copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_o
total_size -= chunk_size;

while (total_size) {
chunk_size = std::min(total_size, work_buf_size);
chunk_size = std::min(total_size, _prog_size);
ret = read_area(from_area, from_offset, chunk_size, _work_buf);
if (ret) {
return ret;
Expand Down Expand Up @@ -1041,7 +1042,7 @@ int TDBStore::init()
}

_prog_size = _bd->get_program_size();
_work_buf = new uint8_t[work_buf_size];
_work_buf = new uint8_t[_prog_size];
Copy link
Contributor

@LDong-Arm LDong-Arm Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, we read record_header_t (a 32-bit aligned struct of size 24 bytes) into _work_buff, but

  • here it's only 8-bit aligned
  • the program size can be less than that - some flashes can be programmed to 1-byte, in which case the buffer is insufficient

This could be why the Greentea test failed and hard faulted.

_key_buf = new char[MAX_KEY_SIZE];
_inc_set_handle = new inc_set_handle_t;
memset(_inc_set_handle, 0, sizeof(inc_set_handle_t));
Expand Down
14 changes: 8 additions & 6 deletions storage/kvstore/tests/UNITTESTS/TDBStore/moduletest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include "kvstore/TDBStore.h"
#include <stdlib.h>

#define BLOCK_SIZE (512)
#define BLOCK_SIZE (256)
#define DEVICE_SIZE (BLOCK_SIZE*200)

using namespace mbed;

class TDBStoreModuleTest : public testing::Test {
protected:
HeapBlockDevice heap{DEVICE_SIZE};
HeapBlockDevice heap{DEVICE_SIZE, BLOCK_SIZE};
FlashSimBlockDevice flash{&heap};
TDBStore tdb{&flash};

Expand Down Expand Up @@ -77,15 +77,17 @@ TEST_F(TDBStoreModuleTest, erased_set_get)

TEST_F(TDBStoreModuleTest, set_deinit_init_get)
{
char buf[100];
size_t size;
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
char str[11] = {0};
char buf[100] = {0};
int len = snprintf(str, 11, "data%d", i) + 1;
EXPECT_EQ(tdb.set("key", str, len, 0), MBED_SUCCESS);
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
EXPECT_EQ(size, 5);
EXPECT_STREQ("data", buf);
EXPECT_EQ(size, len);
EXPECT_STREQ(str, buf);
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
}
}
Expand Down