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

esp_himem_map() calculates wrong pointer if range_offset is nonzero (IDFGH-3713) #5639

Closed
devanlai opened this issue Jul 25, 2020 · 10 comments
Closed
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally

Comments

@devanlai
Copy link
Contributor

Environment

  • Development Kit: none
  • Module or chip used: ESP32-WROVER-E
  • IDF version: v4.3-dev-472-gcf056a7d0
  • Build System: idf.py
  • Compiler version: xtensa-esp32-elf-gcc (crosstool-NG esp-2020r2) 8.2.0
  • Operating System: Windows
  • (Windows only) environment type: Plain Command Prompt.
  • Using an IDE?: VSCode with official Espressif IDF extension
  • Power Supply: USB

Problem Description

The esp_himem_map() function accepts a range_offset parameter that selects the virtual memory range within an existing range handle to use. For any value of range_offset other than zero, the resulting pointer returned via *out_ptr is calculated incorrectly.

By simple inspection of line 338 in esp_himem.c:

//Set out pointer
*out_ptr = (void *)(VIRT_HIMEM_RANGE_START + (range->block_start + range_offset) * CACHE_BLOCKSIZE);

range_offset is in bytes, but it's being added to range->block_start which is counting cache blocks and then being multiplied by CACHE_BLOCKSIZE.

Instead, it should be calculated one of two possible more-or-less equivalent ways:

// Using range_block, which is calculated earlier as range_block = range_offset / CACHE_BLOCKSIZE
*out_ptr = (void *)(VIRT_HIMEM_RANGE_START + (range->block_start + range_block) * CACHE_BLOCKSIZE);

or:

 // Using range_offset directly
 *out_ptr = (void *)(VIRT_HIMEM_RANGE_START + (range->block_start * CACHE_BLOCKSIZE) + range_offset);
@github-actions github-actions bot changed the title esp_himem_map() calculates wrong pointer if range_offset is nonzero esp_himem_map() calculates wrong pointer if range_offset is nonzero (IDFGH-3713) Jul 25, 2020
@Alvin1Zhang
Copy link
Collaborator

Thanks for reporting, we will look into.

@AxelLin
Copy link
Contributor

AxelLin commented Jul 15, 2021

Thanks for reporting, we will look into.

I think this bug report is valid.
@devanlai already provided the fix in the bug report, I'm wondering why nobody fix it?

@AxelLin
Copy link
Contributor

AxelLin commented Nov 3, 2021

Thanks for reporting, we will look into.

Any progress?

@AxelLin
Copy link
Contributor

AxelLin commented Jan 20, 2022

Thanks for reporting, we will look into.

@Alvin1Zhang
This was reported by 2020 and it's 2022 now, I'm wondering if anyone will take a look.

@alyf80
Copy link

alyf80 commented Mar 29, 2023

Just hit this. Any chance it will ever be fixed?

@AxelLin
Copy link
Contributor

AxelLin commented Mar 29, 2023

Just hit this. Any chance it will ever be fixed?

Does the change mentioned in #5639 (comment) work for you?

@alyf80
Copy link

alyf80 commented Mar 29, 2023

@AxelLin It does.

FWIW, current release/v4.4, release/v5.0, release/v5.1 and master are also affected.

@espressif-bot espressif-bot added the Status: In Progress Work is in progress label Mar 30, 2023
@alyf80
Copy link

alyf80 commented Mar 30, 2023

@ginkgm

It's not a matter of API vs. implementation: the actual mapping works fine and just in the way described in the documentation.

The problem is a bug in the computation of out_ptr, which instead of being a valid pointer to the beginning of the mapped block inside the range ends up with a totally incorrect value whenever the function is called with a non-zero range_offset.

On the positive side, the bug cannot go unnoticed because the incorrect pointer is always beyond the end of the valid ESP32 memory address range, so attempting to dereference it immediately causes an abort.

@ginkgm
Copy link
Collaborator

ginkgm commented Apr 4, 2023

@alyf80 ,

I see. I have reconsidered this and think your proposal make sense.

No one can call the function with range_offset by block num but work well. So it shouldn't be treated as breaking change.

The fix is on the way.

@espressif-bot espressif-bot added Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: In Progress Work is in progress Resolution: NA Issue resolution is unavailable labels Apr 5, 2023
@AxelLin
Copy link
Contributor

AxelLin commented May 9, 2023

@ginkgm v5.0 branch still needs this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

6 participants