Commit graph

457 commits

Author SHA1 Message Date
13039bb403 unix/modsocket: Don't crash on invalid argument type.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-08-04 10:12:09 -05:00
Yoctopuce dev
dbbaa959c8 py/formatfloat: Improve accuracy of float formatting code.
Following discussions in PR #16666, this commit updates the float
formatting code to improve the `repr` reversibility, i.e. the percentage of
valid floating point numbers that do parse back to the same number when
formatted by `repr` (in CPython it's 100%).

This new code offers a choice of 3 float conversion methods, depending on
the desired tradeoff between code size and conversion precision:

- BASIC method is the smallest code footprint

- APPROX method uses an iterative method to approximate the exact
  representation, which is a bit slower but but does not have a big impact
  on code size.  It provides `repr` reversibility on >99.8% of the cases in
  double precision, and on >98.5% in single precision (except with REPR_C,
  where reversibility is 100% as the last two bits are not taken into
  account).

- EXACT method uses higher-precision floats during conversion, which
  provides perfect results but has a higher impact on code size.  It is
  faster than APPROX method, and faster than the CPython equivalent
  implementation.  It is however not available on all compilers when using
  FLOAT_IMPL_DOUBLE.

Here is the table comparing the impact of the three conversion methods on
code footprint on PYBV10 (using single-precision floats) and reversibility
rate for both single-precision and double-precision floats.  The table
includes current situation as a baseline for the comparison:

              PYBV10  REPR_C   FLOAT  DOUBLE
    current = 364688   12.9%   27.6%   37.9%
    basic   = 364812   85.6%   60.5%   85.7%
    approx  = 365080  100.0%   98.5%   99.8%
    exact   = 366408  100.0%  100.0%  100.0%

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-08-01 00:47:33 +10:00
338ca3b68f unix/coverage: Remove unused printf arguments.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:59:17 +10:00
aa9152ae0c unix/coverage: Provide argmuents of expected integer types.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:59:04 +10:00
db7e935917 unix/coverage: Cast values to int for format printing.
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>
2025-07-25 10:58:24 +10:00
61006d8016 unix/coverage: Cast values to fit %x formatting code.
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>
2025-07-25 10:57:59 +10:00
a06857a11a unix/coverage: Cast type names to qstr explicitly.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:57:54 +10:00
a1a8eacdce unix/coverage: Avoid type checking an invalid string.
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>
2025-07-25 10:57:39 +10:00
7493275918 py/mpconfig,ports: Define new HEX_FMT formatting macro.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-25 10:55:28 +10:00
Andrew Leech
bc77b27bad unix/mpthreadport: Ensure consistent type of PTHREAD_STACK_MIN.
It seems GCC 14 got stricter with warn/errs like -Wsign-compare and types a
"bare number" as a long int that can't be compared to a (unsigned) size_t.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
2025-07-24 13:29:36 +10:00
Damien George
6a4306a0df unix/mpconfigport: Include time.h to get definition of time_t.
Without this there's a build error on macOS (at least).  This was likely
due to a combination of 9b7d85227e and
df05caea6c.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-24 11:42:53 +10:00
Yoctopuce dev
8b3439e26c unix/variants/longlong: Use REPR_C on this variant.
There is currently no build using REPR_C in the unix CI tests.  As
discussed in PR #16953, this is something that combines well with the
longlong build.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-07-24 11:07:30 +10:00
Damien George
b6460df721 unix: Allow the GIL to be enabled.
The unix port can now be built with the GIL enabled, by passing
MICROPY_PY_THREAD_GIL=1 on the make command line.

Signed-off-by: Damien George <damien@micropython.org>
2025-07-23 11:35:38 +10:00
Damien George
167c888df9 tests/run-tests.py: Detect threading and automatically run thread tests.
When detecting the target platform, also check if it has threading and
whether the GIL is enabled or not (using the new attribute
`sys.implementation._thread`).  If threading is available, add the thread
tests to the set of tests to run (unless the set of tests is explicitly
given).

With this change, the unix port no longer needs to explicitly run the set
of thread tests, so that line has been removed from the Makefile.

This change will make sure thread tests are run with other testing
combinations.  In particular, thread tests are now run:
- on the unix port with the native emitter
- on macOS builds
- on unix qemu, the architectures MIPS, ARM and RISCV-64

Signed-off-by: Damien George <damien@micropython.org>
2025-07-23 11:06:09 +10:00
Angus Gratton
a54b5d9aed unix/variants: Add a 'longlong' variant to test 64-bit bigints in CI.
Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-07-18 00:11:08 +10:00
628d53d23c unix/coverage: Expand mp_printf coverage tests.
Test 'l' and 'll' sized objects.  When the platform's `mp_int_t` is not 64
bits, dummy values are printed instead so the test result can match across
all platforms.

Ensure hex test values have a letter so 'x' vs 'X' is tested.

And test 'p' and 'P' pointer printing.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-16 11:13:08 +10:00
Yanfeng Liu
aa2362d4de unix/Makefile: Drop include path of "i686-linux-gnu".
This drops use of non-existing path `/usr/include/i686-linux-gnu` as
default include paths shall suffice.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2025-07-15 14:03:06 +10:00
Yoctopuce dev
df05caea6c shared/timeutils: Standardize supported date range on all platforms.
This is code makes sure that time functions work properly on a
reasonable date range, on all platforms, regardless of the epoch.
The suggested minimum range is 1970 to 2099.

In order to reduce code footprint, code to support far away dates
is only enabled specified by the port.

New types are defined to identify timestamps.

The implementation with the smallest code footprint is when
support timerange is limited to 1970-2099 and Epoch is 1970.
This makes it possible to use 32 bit unsigned integers for
all timestamps.

On ARM4F, adding support for dates up to year 3000 adds
460 bytes of code. Supporting dates back to 1600 adds
another 44 bytes of code.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-07-09 11:54:21 +10:00
Yoctopuce dev
c4a88f2ce7 py/obj: Add functions to retrieve large integers from mp_obj_t.
This commit provides helpers to retrieve integer values from
mp_obj_t when the content does not fit in a 32 bits integer,
without risking an implicit wrap due to an int overflow.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-07-09 11:54:21 +10:00
da3709a738 unix/coverage: Add missing MP_OBJ_FROM_PTR casts.
An attempt to build the coverage module into the nanbox binary failed, but
pointed out that these sites needed explicit conversion from pointer to
object.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-08 21:50:27 +10:00
f33f1aa9d3 unix/coverage: Initialize more code_state fields.
When `MICROPY_PY_SYS_SETTRACE` was enabled, a crash was seen in the
qemu_mips build.  It seems likely that this was due to these added fields
not being initialized.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-06 00:06:14 +10:00
fcfed6a0ea unix/variants/coverage: Enable sys.settrace.
The unix coverage variant should have all features enabled, so they can be
tested for coverage.  Therefore, enabled `MICROPY_PY_SYS_SETTRACE`.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-06 00:04:18 +10:00
1eb27e11f3 unix/README: Add some small documentation about sanitizers.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-07-01 15:19:07 +10:00
fc6205c4f0 unix/coverage: Add coverage test for left adjusted print.
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-06-16 13:59:57 +10:00
0ef5ede382 py/mpz: Avoid undefined behavior decrementing NULL.
In the case where an mpz number is zero, its `len` is 0 and its `dig` is
NULL.  In that case, decrementing NULL via `d--` is undefined behavior
according to the C specification.

Restructuring the loops in this way avoids undefined behavior.

Also, ensure that these cases are tested in the coverage test.  This
doesn't make much difference now, but would otherwise cause errors later
when the undefined behavior sanitizer is employed in CI.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-06-16 12:11:07 +10:00
Alessandro Gatti
0da22b2c30 tools/ci.sh: Fix nanbox CI test runs.
This commit fixes CI test runs for the `nanbox` target, which were
broken by the unconditional native emitter code output changes in the
test runner.

The `nanbox` configuration does not enable native emitters of any kind,
and with a full test run that includes executing emitted native code
things would break when doing CI runs.

This is worked around by introducing a common subset of tests that do
not involve the native emitter, and a more comprehensive set of tests
that include both non-emitter and emitter tests.  The `nanbox` CI test
run will stop at the first subset, whilst other configurations will run
that and execute further tests.

Function names have been kept the same for steps that involve native
code, with the `nanbox` subset having another one.  This should not
trigger any breakage in existing CI configurations or external scripts.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-06-10 11:29:02 +10:00
Andrew Leech
820485358d unix/coverage: Add coverage test for mp_sched_schedule_node.
Test modified to reschedule itself based on a flag setting. Without the
change in the parent commit, this test executes the callback indefinitely
and hangs but with the change it runs only once each time
mp_handle_pending() is called.

Modified-by: Angus Gratton <angus@redyak.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
2025-06-04 11:31:12 +10:00
Andrew Leech
d5f2fc239a extmod/modbluetooth: Add timeout to deinit.
If the BLE radio stops responding before deinit is called the function can
get stuck waiting for an event that is never received, particularly if the
radio is external or on a separate core.

This commit adds a timeout, similar to the timeout already used in the init
function.  Updated for nimble, btstack, esp32 and zephyr bindings.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
2025-06-04 02:21:32 +10:00
Angus Gratton
0b224048ef unix/modsocket: Expose MSG_PEEK flag for recv & recvfrom.
This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-06-03 12:52:06 +10:00
stijn
9a377801dc unix/coveragecpp: Verify struct-initializing macros' C++-compatibility.
Add code using all relevant macros to make sure they initialize
structs correctly.

Signed-off-by: stijn <stijn@ignitron.net>
2025-04-24 15:55:06 +02:00
Alessandro Gatti
38a3873310 unix/mpthreadport: Work around lack of thread cancellation on Android.
This commit fixes thread-related compilation issues under Android using
Termux as its runtime environment.

On Android's libc (Bionic) thread cancellation is not implemented, but
the Unix port uses that mechanism to provide asynchronous thread
termination.  In this commit there is a workaround for that, by adding a
new signal handler to each newly created thread, whose callback simply
exits the thread.  Threads are then sent the new signal rather than
being explicitly cancelled, which in turn trigger the signal handler to
stop the thread execution at the next possible occasion.

This makes the cancellation behaviour differ slightly on Android, as
threads are probably going to linger a little bit more since the method
introduced in this commit is equivalent to setting
PTHREAD_CANCEL_DEFERRED as the thread cancellation type.  On the other
hand there are no guarantees of immediate cancellation using
PTHREAD_CANCEL_ASYNCHRONOUS either.

This fixes the pthread-related issues reported in #16259.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-04-22 10:19:20 +10:00
David Yang
6025b78d01 unix/main: Remove PATH_MAX from realpath.
POSIX.1-2008 ensures realpath() give a dynamically allocated buffer if
NULL is passed (which is also true for ports/windows/realpath.c),
avoiding an explicit call to malloc() and use of PATH_MAX, which may be
undefined on some systems.

Signed-off-by: David Yang <mmyangfl@gmail.com>
2025-04-22 00:20:47 +08:00
Yoctopuce dev
52ca826880 unix/variants: Enable os.uname() in coverage build for tests.
In order to provide test coverage for the previous commit, `os.uname()`
support is added to the unix coverage build.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-04-21 17:38:42 +10:00
Damien George
be0fce9429 unix/main: Add coverage test for mounting ROMFS filesystem at startup.
Signed-off-by: Damien George <damien@micropython.org>
2025-03-06 12:52:35 +11:00
Damien George
89e6c58c80 extmod/modvfs: Add vfs.rom_ioctl function and its ioctl constants.
This is a generic interface to allow querying and modifying the read-only
memory area of a device, if it has such an area.

Signed-off-by: Damien George <damien@micropython.org>
2025-03-06 12:52:35 +11:00
Angus Gratton
fd0e529a47 unix: Add recursive mutex support.
Allows refactoring the existing thread_mutex atomic section support to use
the new recursive mutex type.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-02-03 15:02:02 +11:00
Angus Gratton
d642cce27a unix: Use the bare metal mbedTLS config in the coverage buiid.
This allows coverage to test MicroPython-specific features such as
the tracked alloc cleanups added in the parent commit.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-02-03 15:02:02 +11:00
Damien George
d9378c9287 unix: Enable VfsRom on standard and coverage variants.
Signed-off-by: Damien George <damien@micropython.org>
2024-12-23 13:04:55 +11:00
Damien George
8e4c809694 unix,windows: Force _FILE_OFFSET_BITS=64 to fix 32-bit file ABI.
On 64-bit systems this should have no effect.

On 32-bit systems it will force 64-bit file sizes and fixes directory
listing on certain 32-bit ports.  This option should work on pretty much
all 32-bit systems these days.

Signed-off-by: Damien George <damien@micropython.org>
2024-12-16 11:56:03 +11:00
Damien George
838f21298a unix/main: Add --version command-line option.
CPython also has this option.

Signed-off-by: Damien George <damien@micropython.org>
2024-10-15 14:44:10 +11:00
Damien George
70a01550c9 unix/alloc: Remove unused MICROPY_FORCE_PLAT_ALLOC_EXEC option.
The `MICROPY_FORCE_PLAT_ALLOC_EXEC` config option was made obsolete by
commit 9796625457, so remove it.

Signed-off-by: Damien George <damien@micropython.org>
2024-10-03 11:09:42 +10:00
Damien George
36fe9bbf00 unix/alloc: Don't trace executable memory for GC pointers.
This tracing was originally needed for three reasons:

1. To trace object/raw-code pointers embedded in native code, eg from
   @micropython.native functions.

2. To trace pointers to BSS/rodata from relocated viper code.

3. For libffi tracing.

The tracing in point 1 was made unnecessary long ago in commit
7d4b6cc868 (see also commit
bbccb0f630 which removed scanning of native
code on the esp8266 port).

Since the previous commit, point 2 is no longer needed.

For point 3, this was made unnecessary in the recent commit
9796625457.

Signed-off-by: Damien George <damien@micropython.org>
2024-09-26 22:36:55 +10:00
Alessandro Gatti
8f465dfd10 unix/modffi: Restrict uint32_t values to 32 bits.
This commit clears the upper 32 bits of returned `uint32_t` values,
which are handled as `unsigned int`s by the MicroPython runtime and
thus could be extended to 64 bits on some platforms.

RV64 holds 32-bit values as signed integers when held in registers, but
the code handling the FFI unsigned int case did not take this into
account.  That introduced test failures when a 32-bit value had its most
significant bit set, as when performing the value extension from 32 to
64 bits, the upper half of the value would be filled with ones.

On 32 bit platforms this change should be converted to a no-op, and
on other 64 bit platforms that aren't RISC-V it shouldn't hurt as the
value being manipulated is expected to only hold valid bits in its lower
half.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-09-23 11:47:18 +10:00
Alessandro Gatti
9796625457 unix/modffi: Clean up FFI closures memory management.
This commit removes custom FFI closures alloc/free functions, in favour
of using the tracked allocation facility to allocate memory for FFI
callback objects.

This stems from linking issues in the Arm port when updating LibFFI to
the latest stable version, as the overridden alloc/free functions didn't
replace LibFFI's (unlike in other ports).  The original code did no
effective cleanup for allocated callback objects, so there is no real
impact when switching allocation strategy.

The tracked allocation feature used to be enabled only if the Bluetooth
stack integration was enabled.  This commit also enables tracked
allocation support if FFI support is enabled.

Co-authored-by: Damien George <damien@micropython.org>
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-09-23 11:47:18 +10:00
Alessandro Gatti
2b5feb9121 lib/libffi: Update libffi to 3.4.6.
Libffi is updated to the latest stable version (v3.4.6) as the version
of libffi that was linked by the repository predates the introduction of
RISC-V processors (both 32 and 64 bits).

This is necessary as the packaged version of libffi used by the CI for
RISC-V 64 turned out to simply not work correctly, whilst a source-built
version of libffi worked just fine.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2024-09-23 11:47:18 +10:00
Andrew Leech
7e14680a83 py/objringio: Add micropython.RingIO() interface for general use.
This commit adds a new `RingIO` type which exposes the internal ring-buffer
code for general use in Python programs.  It has the stream interface
making it similar to `StringIO` and `BytesIO`, except `RingIO` has a fixed
buffer size and is automatically safe when reads and writes are in
different threads or an IRQ.

This new type is enabled at the "extra features" ROM level.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
2024-09-19 18:00:44 +10:00
Michael Sawyer
b05983ff6d unix/modffi: Fix signed integer cast in return_ffi_value.
Casting an ffi_arg to a signed int may truncate the value. E.g., when the
ffi_arg is 64-bit and the signed int is 32-bit. Also, casting an ffi_arg
to a larger signed type will not sign extend the value. E.g., when the
ffi_arg is 32-bit and the larger signed type is int64_t. If the value is
signed, it should be cast to ffi_sarg, which is the same size as ffi_arg.

Signed-off-by: Michael Sawyer <mjfsawyer@gmail.com>
2024-09-19 15:00:06 +10:00
Amirreza Hamzavi
f1bdac3752 unix/README: Fix typo in build dependencies.
Signed-off-by: Amirreza Hamzavi <amirrezahamzavi2000@gmail.com>
2024-09-10 11:30:22 +10:00
Angus Gratton
e370999e37 unix: Add a description of COPT in the README.
This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-09-06 17:07:48 +10:00
Angus Gratton
d04974d8d0 unix: Expand the build steps in the README.
- Present the default build dependencies in one place at the top, and make
  a separate section about building standalone.

- Add steps for the "minimal" variant as well.

- Document that building standalone requires autoconf and libtool.

- Allow MICROPY_STANDALONE to be set as an environment variable.

Fixes issue #11313.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-09-06 17:07:35 +10:00