wear_levelling: wl_read()/wl_write() access the wrong physical sector when the range crosses a 4 KB page (512 B sector modes) (IDFGH-18356)
Answers checklist.
- I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
- I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
- I have searched the issue tracker for a similar issue and not found a similar issue.
IDF version.
v6.1. WL_Flash::read() / WL_Flash::write() are unchanged on master (checked 2026-09-27, after 2039a586f).
Espressif SoC revision.
ESP32-S3 (revision v0.2). Also reproduced on the linux target, so it is not hardware related.
Operating System used.
macOS
How did you build your project?
Command line with idf.py
Development Kit.
Custom board (ESP32-S3, 8 MB flash, 2 MB quad PSRAM); the reproducer runs on the linux target.
Power Supply used.
USB
What is the expected behavior?
wl_read() / wl_write() return and store the requested data for any range inside the volume. The API documents no alignment requirement, and esp_tinyusb explicitly allows CONFIG_TINYUSB_MSC_BUFSIZE larger than CONFIG_WL_SECTOR_SIZE (see espressif/esp-usb#159), so multi-sector ranges at sector-aligned addresses are a supported use.
What is the actual behavior?
With CONFIG_WL_SECTOR_SIZE=512 (Performance and Safe mode), a request that is not aligned to the WL page (4 KB) and spans more than one page is partly served from, or written to, the wrong physical flash sector:
- reads return data from 4 KB earlier in the volume (the stale copy left in the dummy sector);
- writes land on a sector that was not erased: on real flash the data is silently lost (AND-ed into the dummy sector), on the linux target the emulator reports
invalid flash operation; in a longer random test other, unrelated sectors of the volume were damaged too.
Root cause. WL_Flash::read() and WL_Flash::write() walk the request in wl_page_size steps counted from the request's own start address and call calcAddr() only for the first byte of each step. When the start address is not page-aligned, every step straddles two logical pages, and the second part is read/written as if it were physically contiguous with the first. Across the dummy sector (and at the wrap-around) it is not: calcAddr() would map it one page further.
Who is affected. Any caller issuing multi-sector wl_read() / wl_write() at addresses not aligned to 4 KB on a 512 B sector volume, e.g.:
- esp_tinyusb MSC on SPI flash with
CONFIG_TINYUSB_MSC_BUFSIZE>CONFIG_WL_SECTOR_SIZE: the whole buffer goes towl_read()/wl_write()atlba * 512. This is how we found it: with an 8 KB buffer, a 108 KB HTML file written by the firmware was read by the PC with 2 KB (bytes 26624-28671) replaced by the data 4 KB earlier, and files copied from the PC can be damaged. - FATFS on WL with 512 B sectors and
allocation_unit_size> 512:f_mkfs()does not align the data area to the flash page (ff_wl_ioctl(GET_BLOCK_SIZE)returnsRES_ERROR, sosz_blk = 1), and cluster-sizeddisk_read()/disk_write()cross pages. With 4 KB clusters (data area at sector 97), 2 of 8 files of 256 KB written and read back with plainf_write()/f_read()were corrupted, one of them without any error reported. With the defaultallocation_unit_size = 0(cluster = sector, as in all IDF examples) FATFS only issues single-sector requests and is not affected.
CONFIG_WL_SECTOR_SIZE=4096 is not affected in practice (requests are page-aligned).
Steps to reproduce.
Standalone project, linux target, only wear_levelling and esp_partition:
- Create the files below.
idf.py --preview set-target linux && idf.py build./build/wl_unaligned_repro.elf
It fills the volume one sector at a time (single-sector accesses never cross a page, so this is known good), then reads 8 KB at every sector-aligned address and writes 8 KB at page + 2 KB addresses, verifying with single-sector reads.
main/main.c/*
* wl_read()/wl_write() with a range that crosses a 4 KB page, at a
* sector-aligned (512 B) but not page-aligned address.
*
* 1. Fill the whole WL volume one sector at a time, each sector tagged with
* its own index (single-sector accesses never cross a page: known good).
* 2. READ: wl_read() 8 KB at every sector-aligned address and compare.
* 3. WRITE: wl_erase_range() + wl_write() 8 KB at page + 2 KB addresses,
* then read back one sector at a time and compare.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_partition.h"
#include "wear_levelling.h"
#define XFER 8192
static uint8_t expect[XFER], got[XFER];
/* Every 8 bytes of a sector: its index and a generation number. */
static void tag_sector(uint8_t *dst, uint32_t sector, uint32_t gen, size_t ss)
{
for (size_t i = 0; i < ss; i += 8) {
memcpy(dst + i, §or, 4);
memcpy(dst + i + 4, &gen, 4);
}
}
void app_main(void)
{
const esp_partition_t *part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
ESP_PARTITION_SUBTYPE_DATA_FAT, "storage");
wl_handle_t wl;
ESP_ERROR_CHECK(wl_mount(part, &wl));
const size_t ss = wl_sector_size(wl);
const size_t size = wl_size(wl);
printf("WL sector %zu B, volume %zu B, transfer %d B\n", ss, size, XFER);
for (size_t a = 0; a < size; a += ss) {
tag_sector(expect, a / ss, 0, ss);
ESP_ERROR_CHECK(wl_erase_range(wl, a, ss));
ESP_ERROR_CHECK(wl_write(wl, a, expect, ss));
}
int reads = 0, bad_reads = 0;
for (size_t a = 0; a + XFER <= size; a += ss) {
for (size_t o = 0; o < XFER; o += ss) {
tag_sector(expect + o, (a + o) / ss, 0, ss);
}
ESP_ERROR_CHECK(wl_read(wl, a, got, XFER));
++reads;
if (memcmp(got, expect, XFER) != 0) {
if (bad_reads++ < 3) {
size_t o = 0;
while (!memcmp(got + o, expect + o, ss)) o += ss;
uint32_t holds;
memcpy(&holds, got + o, 4);
printf(" read 0x%06zx: sector %zu holds data of sector %" PRIu32 "\n",
a, (a + o) / ss, holds);
}
}
}
printf("READ : %d of %d unaligned %d B reads returned wrong data\n", bad_reads, reads, XFER);
int writes = 0, bad_writes = 0, failed_writes = 0;
/* Half a page in with 512 B sectors; page-aligned with 4 KB sectors (the
* only alignment such a volume's users produce). */
for (size_t a = (ss < 4096) ? 2048 : 0; a + XFER <= size; a += 4096) {
for (size_t o = 0; o < XFER; o += ss) {
tag_sector(expect + o, (a + o) / ss, 1, ss);
}
ESP_ERROR_CHECK(wl_erase_range(wl, a, XFER));
const esp_err_t err = wl_write(wl, a, expect, XFER);
++writes;
if (err != ESP_OK) {
++failed_writes;
}
for (size_t o = 0; o < XFER; o += ss) {
ESP_ERROR_CHECK(wl_read(wl, a + o, got + o, ss));
}
if (memcmp(got, expect, XFER) != 0) {
++bad_writes;
}
}
printf("WRITE: %d of %d unaligned %d B writes did not read back (%d returned an error)\n",
bad_writes, writes, XFER, failed_writes);
wl_unmount(wl);
exit(0); /* linux target: returning would leave the scheduler running */
}# CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main)
project(wl_unaligned_repro)# main/CMakeLists.txt
idf_component_register(SRCS "main.c" REQUIRES wear_levelling esp_partition)# sdkconfig.defaults
CONFIG_IDF_TARGET="linux"
CONFIG_WL_SECTOR_SIZE_512=y
CONFIG_WL_SECTOR_MODE_PERF=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_OFFSET=0x8000
CONFIG_MMU_PAGE_SIZE=0X10000# partitions.csv
# Name, Type, SubType, Offset, Size
nvs, data, nvs, 0x9000, 0x6000,
factory, app, factory, 0x10000, 1M,
storage, data, fat, 0x110000, 1M,Results (deterministic):
| Configuration | Wrong 8 KB reads | Failed 8 KB writes |
|---|---|---|
| 512 B, Performance | 14 / 1985 | 2 / 248 |
| 512 B, Safe | 21 / 1969 | 1 / 246 |
| 4096 B | 0 / 249 | 0 / 249 (page-aligned) |
| 512 B, Performance, with the patch below | 0 / 1985 | 0 / 248 |
Debug Logs.
WL sector 512 B, volume 1024000 B, transfer 8192 B
read 0x07b200: sector 1000 holds data of sector 992
read 0x07b400: sector 1000 holds data of sector 992
read 0x07b600: sector 1000 holds data of sector 992
READ : 14 of 1985 unaligned 8192 B reads returned wrong data
W (303831126) linux_spiflash: invalid flash operation detected
E (303831126) wl_flash: write(593): result = 0x00006001
W (303831126) linux_spiflash: invalid flash operation detected
E (303831126) wl_flash: write(589): result = 0x00006001
WRITE: 2 of 248 unaligned 8192 B writes did not read back (2 returned an error)More Information.
Proposed fix: make every piece end at the next page boundary, so each piece stays inside one page and calcAddr() maps it correctly. Applies cleanly to v6.1 and master; the size==0 guards are kept. With it the reproducer above reports no errors.
--- a/components/wear_levelling/WL_Flash.cpp
+++ b/components/wear_levelling/WL_Flash.cpp
@@ -582,15 +582,22 @@
return ESP_OK;
}
ESP_LOGD(TAG, "%s - dest_addr= 0x%08" PRIx32 ", size= 0x%08" PRIx32 , __func__, (uint32_t) dest_addr, (uint32_t) size);
- uint32_t count = (size - 1) / this->cfg.wl_page_size;
- for (size_t i = 0; i < count; i++) {
- size_t virt_addr = this->calcAddr(dest_addr + i * this->cfg.wl_page_size);
- result = this->partition->write(this->cfg.wl_partition_start_addr + virt_addr, &((uint8_t *)src)[i * this->cfg.wl_page_size], this->cfg.wl_page_size);
+ const uint8_t *in = (const uint8_t *)src;
+ while (size > 0) {
+ // calcAddr() maps one page at a time, and logically adjacent pages are
+ // not physically adjacent across the dummy sector: never let a piece
+ // cross a page boundary, whatever the alignment of dest_addr.
+ size_t piece = this->cfg.wl_page_size - (dest_addr % this->cfg.wl_page_size);
+ if (piece > size) {
+ piece = size;
+ }
+ size_t virt_addr = this->calcAddr(dest_addr);
+ result = this->partition->write(this->cfg.wl_partition_start_addr + virt_addr, in, piece);
WL_RESULT_CHECK(result);
+ dest_addr += piece;
+ in += piece;
+ size -= piece;
}
- size_t virt_addr_last = this->calcAddr(dest_addr + count * this->cfg.wl_page_size);
- result = this->partition->write(this->cfg.wl_partition_start_addr + virt_addr_last, &((uint8_t *)src)[count * this->cfg.wl_page_size], size - count * this->cfg.wl_page_size);
- WL_RESULT_CHECK(result);
return result;
}
@@ -605,16 +612,21 @@
return ESP_OK;
}
ESP_LOGD(TAG, "%s - src_addr= 0x%08" PRIx32 ", size= 0x%08" PRIx32 , __func__, (uint32_t) src_addr, (uint32_t) size);
- uint32_t count = (size - 1) / this->cfg.wl_page_size;
- for (size_t i = 0; i < count; i++) {
- size_t virt_addr = this->calcAddr(src_addr + i * this->cfg.wl_page_size);
- ESP_LOGV(TAG, "%s - real_addr= 0x%08" PRIx32 ", size= 0x%08" PRIx32 , __func__, (uint32_t) (this->cfg.wl_partition_start_addr + virt_addr), (uint32_t) size);
- result = this->partition->read(this->cfg.wl_partition_start_addr + virt_addr, &((uint8_t *)dest)[i * this->cfg.wl_page_size], this->cfg.wl_page_size);
+ uint8_t *out = (uint8_t *)dest;
+ while (size > 0) {
+ // See write(): a piece must stay within one page.
+ size_t piece = this->cfg.wl_page_size - (src_addr % this->cfg.wl_page_size);
+ if (piece > size) {
+ piece = size;
+ }
+ size_t virt_addr = this->calcAddr(src_addr);
+ ESP_LOGV(TAG, "%s - real_addr= 0x%08" PRIx32 ", size= 0x%08" PRIx32 , __func__, (uint32_t) (this->cfg.wl_partition_start_addr + virt_addr), (uint32_t) piece);
+ result = this->partition->read(this->cfg.wl_partition_start_addr + virt_addr, out, piece);
WL_RESULT_CHECK(result);
+ src_addr += piece;
+ out += piece;
+ size -= piece;
}
- size_t virt_addr_last = this->calcAddr(src_addr + count * this->cfg.wl_page_size);
- result = this->partition->read(this->cfg.wl_partition_start_addr + virt_addr_last, &((uint8_t *)dest)[count * this->cfg.wl_page_size], size - count * this->cfg.wl_page_size);
- WL_RESULT_CHECK(result);
return result;
}
Workaround until a fix is released, without patching IDF: wrap wl_read/wl_write at link time (-Wl,--wrap=wl_read -Wl,--wrap=wl_write) with functions that split the request at 4 KB boundaries and call __real_wl_read/__real_wl_write; or keep every request within one page (e.g. CONFIG_TINYUSB_MSC_BUFSIZE = CONFIG_WL_SECTOR_SIZE, cluster size = sector size).
Source: espressif/esp-idf