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

drivers: flash: simulator: fix address resolution #79084

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fgrandel
Copy link
Contributor

The flash simulator assumes that flash is being addressed with absolute addresses by clients. But this is not the case. Given addresses are relative to the flash base address.

The existing code only worked because the base address was always zero. Testing with non-zero base addresses caused a mem fault/page fault.

(Reproducible e.g. when using the NVS settings subsystem on QEMU x86 with a non-zero base address in the soc-nv-flash node.)

Fixes #79082

FLASH_SIMULATOR_BASE_OFFSET) ||
(offset < FLASH_SIMULATOR_BASE_OFFSET)) {

if ((offset + len > FLASH_SIMULATOR_FLASH_SIZE) || (offset < 0)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since you are fixing this, can we fix the somehow absurd check for correctness of offset after doing calculations with it? I know that it was here before...
Also the more correct approach would be to do:

(offset < 0 || offset >= FLASH_SIMULATOR_FLASH_SIZE || (FLASH_SIMULATOR_FLASH_SIZE - offset) < size)

this should let us avoid all overflows the addition could cause.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Fixed. BTW: The reason, why I'm fixing this is #79130.

The flash simulator assumes that flash is being addressed with absolute
addresses by clients. But this is not the case. Given addresses are
relative to the flash base address.

The existing code only worked because the base address was always zero.
Testing with non-zero base addresses caused a mem fault/page fault.

(Reproducible e.g. when using the NVS settings subsystem on QEMU x86
with a non-zero base address in the soc-nv-flash node.)

Fixes zephyrproject-rtos#79082

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
@fgrandel fgrandel force-pushed the fix/79082-flash-simulator-memfault branch from a6b9282 to 3af7da8 Compare September 27, 2024 19:51
@fgrandel fgrandel added bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug and removed area: Build System labels Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Flash bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

drivers: flash: simulator: fix address resolution
4 participants