Skip to content

py/obj: Add new type flag to indicate subscr accepts slice-on-stack. #17736

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

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

Conversation

dpgeorge
Copy link
Member

Summary

The recently merged PR #12518 allowed temporary slices to be allocated on the C stack, which is much better than allocating them on the GC heap.

Unfortunately there are cases where the C-allocated slice can escape and be retained as an object, which leads to crashes (because that object points to the C stack which now has other values on it).

See #17723 and #17733.

The fix here is to add a new MP_TYPE_FLAG_SUBSCR_ALLOWS_STACK_SLICE. Native types should set this flag if their subscr method is guaranteed not to hold on to a reference of the slice object.

Testing

The existing tests pass. A new test is added to test the bugs reported in #17723 and #17733.

Trade-offs and Alternatives

The function mp_obj_is_native_type() actually tests a type flag, so we replace the test of one flag with another flag. Thus the fix here should be no change in code size and no change in performance.

We do use up another MP_TYPE_FLAG_xxx bit, but there are still 5 left.

Eventually more native types (eg tuple, list) can have this flag set, but I'll leave that for a follow-up PR.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 22, 2025
Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@dpgeorge dpgeorge force-pushed the py-obj-add-type-flag-for-subscr-slice branch from 8b65d69 to d54de76 Compare July 22, 2025 03:34
Copy link

codecov bot commented Jul 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.41%. Comparing base (e993f53) to head (49f5f42).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17736   +/-   ##
=======================================
  Coverage   98.41%   98.41%           
=======================================
  Files         171      171           
  Lines       22210    22211    +1     
=======================================
+ Hits        21857    21858    +1     
  Misses        353      353           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jepler
Copy link
Contributor

jepler commented Jul 22, 2025

This makes sense and seems worth spending a flag bit on.

Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

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

Looks like a good fix to me, agree with jeff that it's worth spending a flag bit on this given the optimisation potential.

One minor thing, the test name has a typo (optimse instead of optimise).

The recently merged 5e9189d now allows
temporary slices to be allocated on the C stack, which is much better than
allocating them on the GC heap.

Unfortunately there are cases where the C-allocated slice can escape and be
retained as an object, which leads to crashes (because that object points
to the C stack which now has other values on it).

The fix here is to add a new `MP_TYPE_FLAG_SUBSCR_ALLOWS_STACK_SLICE`.
Native types should set this flag if their subscr method is guaranteed not
to hold on to a reference of the slice object.

Fixes issue micropython#17733 (see also micropython#17723).

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the py-obj-add-type-flag-for-subscr-slice branch from d54de76 to 49f5f42 Compare July 23, 2025 00:55
@dpgeorge
Copy link
Member Author

One minor thing, the test name has a typo (optimse instead of optimise).

Ah, thanks! Now fixed.

@dpgeorge dpgeorge added this to the release-1.26.0 milestone Jul 23, 2025
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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy