Skip to content

Fix mpprintf argument type errors #17704

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 16 commits into
base: master
Choose a base branch
from

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Jul 17, 2025

Summary

I have developed a gcc plugin for checking mp_printf format strings vs argument types at compile time. #17556 is the fixes plus the checker, while this PR is only the fixes.

Testing

I added tests to the coverage build for stuff I felt was not yet adequately covered.

Copy link

codecov bot commented Jul 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.44%. Comparing base (17fbc5a) to head (66f30bf).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17704   +/-   ##
=======================================
  Coverage   98.44%   98.44%           
=======================================
  Files         171      171           
  Lines       22208    22209    +1     
=======================================
+ Hits        21863    21864    +1     
  Misses        345      345           

☔ 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.

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
Copy link
Member

If HEX_FMT is really needed, maybe a simpler change would be to delete as many defines of INT_FMT and UINT_FMT as possible and let the defaults from py/mpconfig.h be used instead?

@jepler
Copy link
Contributor Author

jepler commented Jul 18, 2025

Introducing and using HEX_FMT allows the nanbox variant to print cells correctly:

<closure <function <lambda> at 0xf79e7300> at f79e7320, n_closed=1 <cell f79e72a0 []> >
<closure <function <lambda> at 0xf79e7360> at f79e7380, n_closed=1 <cell 1000000000003 1> >
<closure <function <lambda> at 0xf79e73c0> at f79e73e0, n_closed=1 <cell c00d1eb851eb851f 3.14> >
<closure <function <lambda> at 0xf79e7440> at f79e7460, n_closed=1 <cell 200000000002b '__main__'> >

If the argument is cast to a pointer type and the %p format is used, then only the lower 32 bits are printed:

<closure <function <lambda> at 0xf79d8300> at f79d8320, n_closed=1 <cell f79d82a0 []> >
<closure <function <lambda> at 0xf79d8360> at f79d8380, n_closed=1 <cell 3 1> >
<closure <function <lambda> at 0xf79d83c0> at f79d83e0, n_closed=1 <cell 51eb851f 3.14> >
<closure <function <lambda> at 0xf79d8440> at f79d8460, n_closed=1 <cell 2b '__main__'> >

@jepler
Copy link
Contributor Author

jepler commented Jul 18, 2025

I noticed I introduced a bug with printing pointers in #17618. I put a fix for it here but can break it out to its own PR if desired: 333665b

@dpgeorge
Copy link
Member

I noticed I introduced a bug with printing pointers in #17618. I put a fix for it here but can break it out to its own PR if desired: 333665b

It's OK to keep it here.

@dpgeorge
Copy link
Member

Introducing and using HEX_FMT allows the nanbox variant to print cells correctly:

Oh! In that case how about my suggestion to let the defaults in py/mpconfig.h be used?

jepler added 16 commits July 18, 2025 06:33
The default definition in py/mpconfig.h is %u/%d, so
these can be removed.

Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Jeff Epler <jepler@gmail.com>
The name field of type objects is of type uint16_t for efficiency,
but when the type is passed to mp_printf it must be cast explicitly
to type qstr.

These locations were found using an experimental gcc plugin
for mp_printf error checking, cross-building for x64 windows
on Linux.

Signed-off-by: Jeff Epler <jepler@gmail.com>
The type of the argument must match the format string. Add
casts to ensure that they do.

It's possible that casting from `size_t` to `unsigned` loses
the correct values by masking off upper bits, but it seems likely
that the quantities involved in practice are small enough that
the %u formatter (32 bits on most platforms, 16 on pic16bit) will
in fact hold the correct value.

The alternative, casting to a wider type, adds code size.

These locations were found using an experimental gcc plugin
for mp_printf error checking, cross-building for x64 windows
on Linux.

In one case there was already a cast, but it was written
incorrectly and did not have the intended effect.

Signed-off-by: Jeff Epler <jepler@gmail.com>
we still want this not to crash a runtime but the
new static checker wouldn't like it.

Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Jeff Epler <jepler@gmail.com>
This fixes the following diagnostic produced by the plugin:

```
error: argument 3: Format ‘%x’ requires a ‘int’ or
    ‘unsigned int’ (32 bits), not ‘long unsigned int’ [size 64]
    [-Werror=format=]
```

Signed-off-by: Jeff Epler <jepler@gmail.com>
During the coverage test, all the values encountered are within the
range of %d.

These locations were found using an experimental gcc plugin
for mp_printf error checking.

Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Jeff Epler <jepler@gmail.com>
These locations were found using an experimental gcc plugin
for mp_printf error checking.

Signed-off-by: Jeff Epler <jepler@gmail.com>
As timeout is of type `mp_int_t`, it must be printed with INT_FMT.

Before, the compiler plugin produced an error in the PYBD_SF6
build, which is a nanboxing build with 64-bit ints.

Signed-off-by: Jeff Epler <jepler@gmail.com>
Before, the compiler plugin produced an error in the PYBD_SF6
build, which is a nanboxing build with 64-bit ints.

I made the decision here to cast the value even though some
significant bits might be lost after 49.7 days. However, the
format used is "% 8d", which produces a consistent width
output for small ticks values (up to about 1.1 days). I judged
that it was more valuable to preserve the fixed width display
than to accurately represent long time periods.

Signed-off-by: Jeff Epler <jepler@gmail.com>
On the nanbox build, `o->obj` is a 64-bit type but `%p` formats
a 32-bit type, leading to undefined behavior.

Print the cell's ID as a hex integer instead.

This location was found using an experimental gcc plugin for mp_printf
error checking.

Signed-off-by: Jeff Epler <jepler@gmail.com>
All these arguments are of type `mp_{u,}int_t`, but the actual
value is always a small integer. Cast it so that it can format
with the %d/%u formatter.

Before, the compiler plugin produced an error in the PYBD_SF6
build, which is a nanboxing build with 64-bit ints.

Signed-off-by: Jeff Epler <jepler@gmail.com>
On a build like nanbox, mp_uint_t is wider than u/intptr_t.
Using a signed type for fetching pointer values resulted in
erroneous results: like `<function f at 0xfffffffff7a60bc0>`
instead of `<function f at 0xf7a60bc0>`.

Signed-off-by: Jeff Epler <jepler@gmail.com>
@jepler jepler force-pushed the mpprintf-argtype-fixes branch from 333665b to 66f30bf Compare July 18, 2025 11:38
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 18, 2025
@dpgeorge dpgeorge added this to the release-1.26.0 milestone Jul 18, 2025
@jepler
Copy link
Contributor Author

jepler commented Jul 18, 2025

The one remaining failure is 1 tests failed: extmod/select_poll_eintr.py

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.

2 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