You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(kvdb): reject invalid cached records after read failures - #435
With the KV address cache enabled, find_kv() returned success after read_kv()
failed, so a cached record with a damaged value could still be returned by fdb_kv_get_obj(). This branch keeps only the cached-record validation part of
the original change and rebuilds the tests on top of it.
find_kv() validates a cache hit: the read must succeed, the CRC must match,
the record must be committed, and its name length must be the requested one.
An invalid hint is dropped and the lookup falls back to the validated scan.
The cache lookup honours a failed read of the stored name instead of using the
stale hint.
read_kv() reports backend failures of the header, CRC data and record name
reads, and it resets the record metadata before every read, so an iterator
cannot reuse the length, name length or value address of the previous record
after a failed read.
A maximum-length key stays valid on a cache hit: get_kv_from_cache() already
compares the raw on-media name, so the lookup no longer compares the copy that read_kv() NUL-truncates for such a key.
The successful cache path is unchanged, and neither the public API nor the
storage layout changes. The separate unchecked final value read in get_kv() is
outside this PR. This does not establish the cause of any board-level corruption.
Related issue
Refs #59. This addresses the cached-record lookup portion of the issue, not every
unchecked read in the library.
Test closure
Added or updated cases in tests/fdb_kvdb_tc.c or tests/fdb_tsdb_tc.c
New cases are registered in the UTEST_UNIT_RUN(...) list of testcase()
New cases cover a maximum-length key, a value crossing a write unit, stale
cached entries with and without a replacement, a committed empty blob,
backend read failures of the cached name, the record header, the CRC data and
the record name, the name read of the CRC failure branch, and an iterator
failure after a successful read. Init check failure is outside the changed
behaviour.
Bug fix: the case failed before the fix and passes after, and its name carries the issue number
test_fdb_github_issue_59_max_key fails on the initial commit of this PR
(7 assertions) and passes with the fix.
test_fdb_github_issue_59_cached_name_read_error, test_fdb_github_issue_59_crc_data_read_error and test_fdb_github_issue_59_iterator_read_error fail on the base tree and pass
with this change.
No new case needed, reason: N/A; this changes behaviour and includes new cases.
Seven issue-59 cases run in the normal cached-KVDB test build. No fault injection
hook and no extra configuration macro is needed: the suite runs the KVDB on the
file backend, and shrinking a sector file makes that backend report a short read,
so the first access at or past the new end becomes FDB_READ_ERR. The cases use
that to damage the persisted data, which also covers the file backend read path
itself. The stale-cache case deliberately restores an obsolete cached address;
normal API calls need not create one.
Write granularity covered
CI passed for FDB_WRITE_GRAN = 1 / 8 / 32 / 64 / 128 / 256
Reproduced the CI flow locally with RT-Thread ENV: Arm GNU 10.3, SCons 4.8, FlashDBAutoTestBSP, scons --global-macros="FDB_WRITE_GRAN=<n>", then the
UtestRunner qemu_runner.py under QEMU. KVDB and TSDB pass at all six values,
and with FDB_USING_READ_CACHE enabled as well.
Red runs: with the base tree, the cached-name, CRC-data and iterator cases
fail; with the initial commit of this PR, the maximum-length-key case fails.
Not verified, reason and risk: N/A
Data layout and compatibility
KV/TSL node format unchanged
Node format changed: version bumped, old data migrates by: N/A
Public struct or API in inc/flashdb.h changed, compatibility impact: N/A
API or config macro changed, and docs/ plus docs/zh-cn/ are updated: N/A, no runtime API or configuration macro change
Power-loss semantics
Not on a write path
No write sequence is changed; the existing malformed-header status handling
remains. The new early exits only stop a read, and read_kv() still writes the FDB_KV_ERR_HDR status for a length that cannot be trusted.
On a write path, covered by the fdb_xxx_deinit -> fdb_xxx_init reboot simulation: N/A
On a write path, not coverable by utest, verification method: N/A
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Backend read failures can cause garbage collection to erase valid committed records, and the lookup changes reject otherwise valid maximum-length keys.
This PR addresses the cached-record lookup portion of issue #59 by validating KV cache hits and falling back to a scan when cached records are invalid. Public APIs and storage layout remain unchanged.
Changes:
Validate cached records against read success, key, CRC, and committed status.
Check backend read failures and reset record metadata before reuse.
Add registered regression cases for CRC damage, stale cache entries, empty blobs, and lookup after garbage collection.
Official BSP/QEMU execution remains not verified for write granularities 1/8/32/64/128/256.
File
Description
tests/fdb_kvdb_tc.c
Adds and registers issue-59 cache-validation regression cases.
src/fdb_kvdb.c
Validates cache hits, evicts invalid entries, and handles record-read failures.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Maximum-length keys remain unfindable after transient read failures because fallback lookup compares a NUL-truncated name, requiring a complete on-media comparison with read-success checks and a recovery regression test.
Preserve maximum-length keys after transient read failures
src/fdb_kvdb.c:652
After a transient read failure evicts a valid FDB_KV_NAME_MAX-byte key, lookups still fail once the backend recovers: find_kv_cb() compares against kv->name, whose last byte read_kv() replaces with NUL. The cached-name eviction at lines 262–264 has the same recovery problem. Make the fallback scan compare the complete on-media name while checking read success, and add a maximum-length-key fault case that verifies recovery after injection is disabled.
read_kv() NUL-truncates kv->name when the stored name is exactly
FDB_KV_NAME_MAX bytes, so comparing that copy again after a cache hit rejected
a valid key. get_kv_from_cache() already compared the raw name on the flash, so
reuse that comparison and only validate the record behind the cached address:
read success, CRC, committed status and the exact name length.
Refs armink#59
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The suite runs the KVDB on the file backend, and shrinking a sector file makes
that backend report a short read: the first access at or past the new end turns
into FDB_READ_ERR. The new cases use that to damage the persisted data and cover
the cached name, record header, CRC data and record name read failures, the
name read of the CRC failure branch, and the iterator metadata reset after a
failed read. No fault injection hook is needed, and no configuration macro or
storage layout changes.
Refs armink#59
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
With the KV address cache enabled,
find_kv()returned success afterread_kv()failed, so a cached record with a damaged value could still be returned by
fdb_kv_get_obj(). This branch keeps only the cached-record validation part ofthe original change and rebuilds the tests on top of it.
find_kv()validates a cache hit: the read must succeed, the CRC must match,the record must be committed, and its name length must be the requested one.
An invalid hint is dropped and the lookup falls back to the validated scan.
stale hint.
read_kv()reports backend failures of the header, CRC data and record namereads, and it resets the record metadata before every read, so an iterator
cannot reuse the length, name length or value address of the previous record
after a failed read.
get_kv_from_cache()alreadycompares the raw on-media name, so the lookup no longer compares the copy that
read_kv()NUL-truncates for such a key.The successful cache path is unchanged, and neither the public API nor the
storage layout changes. The separate unchecked final value read in
get_kv()isoutside this PR. This does not establish the cause of any board-level corruption.
Related issue
Refs #59. This addresses the cached-record lookup portion of the issue, not every
unchecked read in the library.
Test closure
tests/fdb_kvdb_tc.cortests/fdb_tsdb_tc.cUTEST_UNIT_RUN(...)list oftestcase()cached entries with and without a replacement, a committed empty blob,
backend read failures of the cached name, the record header, the CRC data and
the record name, the name read of the CRC failure branch, and an iterator
failure after a successful read. Init check failure is outside the changed
behaviour.
test_fdb_github_issue_59_max_keyfails on the initial commit of this PR(7 assertions) and passes with the fix.
test_fdb_github_issue_59_cached_name_read_error,test_fdb_github_issue_59_crc_data_read_errorandtest_fdb_github_issue_59_iterator_read_errorfail on the base tree and passwith this change.
Seven issue-59 cases run in the normal cached-KVDB test build. No fault injection
hook and no extra configuration macro is needed: the suite runs the KVDB on the
file backend, and shrinking a sector file makes that backend report a short read,
so the first access at or past the new end becomes
FDB_READ_ERR. The cases usethat to damage the persisted data, which also covers the file backend read path
itself. The stale-cache case deliberately restores an obsolete cached address;
normal API calls need not create one.
Write granularity covered
FDB_WRITE_GRAN= 1 / 8 / 32 / 64 / 128 / 256FlashDBAutoTestBSP,scons --global-macros="FDB_WRITE_GRAN=<n>", then theUtestRunner
qemu_runner.pyunder QEMU. KVDB and TSDB pass at all six values,and with
FDB_USING_READ_CACHEenabled as well.fail; with the initial commit of this PR, the maximum-length-key case fails.
Data layout and compatibility
inc/flashdb.hchanged, compatibility impact: N/Adocs/plusdocs/zh-cn/are updated: N/A, no runtime API or configuration macro changePower-loss semantics
remains. The new early exits only stop a read, and
read_kv()still writes theFDB_KV_ERR_HDRstatus for a length that cannot be trusted.fdb_xxx_deinit->fdb_xxx_initreboot simulation: N/A