Skip to content

py/persistentcode: Maintain root ptr list of imported native .mpy code.#6290

Merged
dpgeorge merged 1 commit into
micropython:masterfrom
dpgeorge:py-persistentcode-track-reloc-code
Aug 2, 2020
Merged

py/persistentcode: Maintain root ptr list of imported native .mpy code.#6290
dpgeorge merged 1 commit into
micropython:masterfrom
dpgeorge:py-persistentcode-track-reloc-code

Conversation

@dpgeorge
Copy link
Copy Markdown
Member

(This is an alternative to #6061 that is more comprehensive and robust.)

On ports where normal heap memory can contain executable code (eg ARM-based ports such as stm32), native code loaded from an .mpy file may be reclaimed by the GC because there's no reference to the very start of the native machine code block that is reachable from root pointers (only pointers to internal parts of the machine code block are reachable, but that doesn't help the GC find the memory).

This commit fixes this issue by maintaining an explicit list of root pointers pointing to native code that is loaded from an .mpy file. This is not needed for all ports so is selectable by the new configuration option MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE. It's enabled by default if a port does not specify any special functions to allocate or commit executable memory.

A test is included to test that native code loaded from an .mpy file does not get reclaimed by the GC.

Fixes #6045.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 27, 2020
@dpgeorge dpgeorge mentioned this pull request Jul 27, 2020
2 tasks
Comment thread py/mpstate.h Outdated
On ports where normal heap memory can contain executable code (eg ARM-based
ports such as stm32), native code loaded from an .mpy file may be reclaimed
by the GC because there's no reference to the very start of the native
machine code block that is reachable from root pointers (only pointers to
internal parts of the machine code block are reachable, but that doesn't
help the GC find the memory).

This commit fixes this issue by maintaining an explicit list of root
pointers pointing to native code that is loaded from an .mpy file.  This
is not needed for all ports so is selectable by the new configuration
option MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE.  It's enabled by default
if a port does not specify any special functions to allocate or commit
executable memory.

A test is included to test that native code loaded from an .mpy file does
not get reclaimed by the GC.

Fixes micropython#6045.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the py-persistentcode-track-reloc-code branch from 77b8e8e to 9883d8e Compare August 2, 2020 12:34
@dpgeorge dpgeorge merged commit 9883d8e into micropython:master Aug 2, 2020
@dpgeorge dpgeorge deleted the py-persistentcode-track-reloc-code branch August 2, 2020 13:00
tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Feb 4, 2026
The CoreS3 SE is a variant of the CoreS3 with the camera, IMU,
magnetometer, proximity sensor, and audio codec removed, paired with
the M5GO Battery Bottom3 which adds 10 NeoPixels (GPIO5) and an IR
transmitter (GPIO7).

Based on the CoreS3 board definition with the following changes:
- Board name set to "M5Stack CoreS3 SE"
- NEOPIXEL pin defined (GPIO5) for M5GO Bottom3 LEDs
- Camera pin definitions and data tuple removed from pins.c
- NEOPIXEL and IR named pin aliases added to pins.c
- ESPCAMERA support disabled in mpconfigboard.mk
- Camera-related AXP2101 power rails (ALDO2/ALDO3) disabled in board.c
- Camera config removed from sdkconfig
- SD card CS (GPIO4) driven high before display init to mitigate
  shared SPI bus contention (see micropython#10536)

Note: SD card does not work on any Core S3 variant in CircuitPython
due to GPIO35 being shared between SPI MISO and display DC pin
(upstream issues micropython#10536, micropython#6290).

Tested on hardware: display, I2C (AXP2101, AW9523B, FT6336, BM8563),
NeoPixels, IR pin, I2S speaker pins, Port B/C GPIOs all verified.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak for native machine code in mpy files

2 participants