The littlefs library is an open-source filesystem for microcontrollers, designed to use a small amount of RAM and be robust against unexpected power loss. We used Super CLI to verify several requirements about how littlefs stores and updates data. While checking that closing and reopening a file preserves its contents, we uncovered a bug that can silently discard writes after a file rename. Both the write and the close report success, but the file is empty when reopened.

What do we want to verify?

For a filesystem such as littlefs, we want guarantees about how it stores and updates data: appending should preserve existing contents, writes to separate files should remain independent, and closing and reopening a file should preserve its contents.

We can verify requirements like these using supertests. A supertest expresses a requirement as code, allowing Super to check mathematically whether it holds for every possible input the test allows. They’re written much like ordinary unit tests, and your favorite coding agent can write them for you.

Let’s look more closely at the last requirement: closing and reopening a file should preserve its contents. We can express this as a supertest to verify that it holds for every allowed sequence of file operations, including sequences we may not have thought to test ourselves.

We mark the function as a supertest with SUPERTEST and give it the input parameter file_operations_t operations, representing a sequence of file operations. SUPERTEST_ASSUME restricts this input to sequences accepted by our valid_operations helper, so the requirement must hold for every such sequence:

C
SUPERTEST
void close_preserves_contents(file_operations_t operations) {
    SUPERTEST_ASSUME(valid_operations(operations));
    // ...
}

We set up a test filesystem, fs, and open an empty file through the handle file. We can then apply the sequence of operations to the file:

C
const char *file_path = apply_operations(fs, &file, "file0", operations);

Next comes the operation we’re checking: closing and reopening the file. We read its contents into before_close and after_reopen on either side:

C
file_contents_t before_close = read_file_contents(fs, &file);
assert(lfs_file_close(fs, &file) == 0);
assert(lfs_file_open(fs, &file, file_path, LFS_O_RDWR) == 0);
file_contents_t after_reopen = read_file_contents(fs, &file);

Finally, we use assertions, just as in an ordinary unit test, to check that the file’s length and bytes match. Here’s the complete supertest:

supertests/close_preserves_contents.c
SUPERTEST
void close_preserves_contents(file_operations_t operations) {
    SUPERTEST_ASSUME(valid_operations(operations));

// Create a fresh filesystem, open an empty file, and apply the operations. lfs_t *fs = create_test_filesystem(); lfs_file_t file; assert(lfs_file_open(fs, &file, "file0", LFS_O_CREAT | LFS_O_RDWR) == 0); const char *file_path = apply_operations(fs, &file, "file0", operations);
// Capture the contents before and after closing and reopening. file_contents_t before_close = read_file_contents(fs, &file); assert(lfs_file_close(fs, &file) == 0); assert(lfs_file_open(fs, &file, file_path, LFS_O_RDWR) == 0); file_contents_t after_reopen = read_file_contents(fs, &file);
// Assert that the file's length and contents are unchanged. assert(after_reopen.size == before_close.size); assert(memcmp(after_reopen.bytes, before_close.bytes, before_close.size) == 0); }

We can now ask Super to verify the requirement.

Checking the requirement

With our supertest in place, we can run super check from the root of the littlefs repository:

Bash
$ super check supertests/close_preserves_contents.c

The super check command asks Super to determine whether littlefs’s implementation satisfies the requirement expressed by our supertest. Using mathematical reasoning, Super can prove that the assertions hold for every allowed input or identify an input that violates them.

When we asked Super to check this requirement, it found a counterexample: if you rename an open file, write to it, then close and reopen it, the new data is missing.

Output
  × fail · close_preserves_contents
  supertests/close_preserves_contents.c:3 · ead3def · #1

Rename can discard dirty file contents
Renaming an open file can detach its handle, so closing the file succeeds without saving its pending writes.
Review and apply: super fix --check 1
Enter for more details · Esc/Ctrl+C close

This goes against littlefs’s intended behavior: an open handle should still refer to the same file after a rename, as confirmed by a maintainer in a separate issue discussion, and the API documentation for lfs_file_close says a successful close writes pending changes to storage.

To see a minimal reproduction of the bug based on the counterexample Super found, expand the example below.

Reproduction
// Open an empty file and rename it while keeping it open.
lfs_t *fs = create_test_filesystem();
lfs_file_t file;
assert(lfs_file_open(fs, &file, "record", LFS_O_CREAT | LFS_O_RDWR) == 0);
assert(lfs_rename(fs, "record", "renamed") == 0);

// Write through the same handle and close; both calls report success. assert(lfs_file_write(fs, &file, "A", 1) == 1); assert(lfs_file_close(fs, &file) == 0);
// Reopen the file and look for 'A'. assert(lfs_file_open(fs, &file, "renamed", LFS_O_RDONLY) == 0); char byte; lfs_ssize_t bytes_read = lfs_file_read(fs, &file, &byte, 1); assert(bytes_read == 1 && byte == 'A'); // Fails: 0 bytes read.

What does Super propose changing?

When a supertest fails, Super can propose a change to the code to meet the requirement. Here’s what super fix shows for littlefs:

Bash
$ super fix --check 1
  littlefs · ead3def
  Fix proposal · close_preserves_contents · #1
  Keep open source handles attached to the destination during rename metadata updates.

lfs.c +15 -2
  old new   @@ -2363,10 +2363,23 @@ fixmlist:;
  2363 2363           // our mdir.
  2364 2364           lfs_block_t oldpair[2] = {pair[0], pair[1]};
  2365 2365           for (struct lfs_mlist *d = lfs->mlist; d; d = d->next) {
  2366      -             if (lfs_pair_cmp(d->m.pair, oldpair) == 0) {
       2366 +             int begin = 0;
       2367 +             for (int i = 0; i < attrcount; i++) {
       2368 +                 if (lfs_tag_type3(attrs[i].tag) == LFS_FROM_MOVE &&
       2369 +                         d->type == LFS_TYPE_REG &&
       2370 +                         d->id == lfs_tag_size(attrs[i].tag) &&
       2371 +                         lfs_pair_cmp(d->m.pair,
       2372 +                             ((const lfs_mdir_t*)attrs[i].buffer)->pair) == 0) {
       2373 +                     d->id = lfs_tag_id(attrs[i].tag);
       2374 +                     begin = i + 1;
       2375 +                     break;
       2376 +                 }
       2377 +             }
       2378 +
       2379 +             if (begin > 0 || lfs_pair_cmp(d->m.pair, oldpair) == 0) {
  2367 2380                   d->m = *dir;
  2368 2381                   if (d->m.pair != pair) {
  2369      -                     for (int i = 0; i < attrcount; i++) {
       2382 +                     for (int i = begin; i < attrcount; i++) {
  2370 2383                           if (lfs_tag_type3(attrs[i].tag) == LFS_TYPE_DELETE &&
  2371 2384                                   d->id == lfs_tag_id(attrs[i].tag) &&
  2372 2385                                   d->type != LFS_TYPE_DIR) {

? Apply this fix to your working tree? [Y/n] ›

That is, Super proposes updating the open file handle to point to the new directory entry before littlefs removes the old one. Currently, removing the old entry leaves the handle detached. A write through that handle can still update the file’s contents in memory, but closing a detached handle reports success without saving those changes. With the handle pointing to the new entry, littlefs can save the pending write when the file is closed.

Verifying the fix

We can apply Super’s proposed fix, then run super check again on the same supertest. This time it passes:

Bash
$ super check supertests/close_preserves_contents.c
  ✓ pass · close_preserves_contents

This means Super has mathematically proved that closing and reopening the file preserves its length and every byte of its contents for every sequence of file operations the supertest allows. It goes beyond what we might hope from reading the code or running a sample of test cases: it’s a guarantee.

That requirement stays useful as littlefs evolves. Even if the way littlefs handles renames changes again, we can use the same supertest to verify the behavior callers rely on.