micropython-ulab: [BUG] Memory Corruption Cases by ULAB subscript access.

Describe the bug In ulab 6.7.0, accessing a large numpy array using the [] operator causes memory corruption, leading to a system crash. This has been verified using vanilla micropython 1.24. with ulab added as a user_c_module. A board with 256KB of MP heap will be needed to run the error cases.

To Reproduce Please see the attached zip file. It contains an array.bin file to write to the uPY flash. Then it has three test scripts. The passing script loads the array.bin file, converts it to an ndarray, and then flattens the array and turns it into a list before processing it. This works.

I do the same thing in the error_case_2 script, but, don’t turn the flattened numpy array into a list. Memory corruption then happens which leads to objects on the stack not being marked by gc.collect() before it sweeps causing them to be deallocated while still in use.

Finally, error_case shows off that the error happens even without a flattened numpy array. Pretty much any numpy [] access results in memory corruption happening.

Expected behavior All 3 of the scripts should be able to run without the system crashing.

Additional context Some type of stack corruption is happening, leading to objects currently in scope in MicroPython being marked for deletion by gc.collect() while they are still in use. We debugged that gc.collect() does not mark objects currently in scope before it sweeps, which is what causes the crash.

The question is why are there objects currently in scope that are not marked when gc.collect() runs. Given the difference between scripts is the use of [] access it must be something related to that. Specifically, some stack corruption would be the most likely candidate to cause this.

ulab_issue.zip

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

Sure, it’s on a private repo right now. When it makes it upstream I will link to it.

@iabdalkader Oh, don’t worry at all, it’s completely fine.

Is this bug reproducible using stock stm32 hardware (eg a pyboard or Nucleo) and stock MicroPython? If so can you please specify the hardware and the make command used to build (including options to enable ulab) and then I could see if I can reproduce it.