Binary operations on undefined variables crash the native emitter.
Port, board and/or hardware
unix
MicroPython version
MicroPython v1.24.0-preview.206.ge9814e987.dirty on 2024-08-16; linux [GCC 14.2.1] version
Reproduction
Run the following code:
@micropython.native
def f():
a += 0 # Or anything else, really
f()
Expected behaviour
A NameError exception should be raised. (CPython raises NameError: name 'a' is not defined)
Observed behaviour
This is the backtrace of the crash when running the interpreter under GDB:
Program received signal SIGSEGV, Segmentation fault.
mp_obj_get_type (o_in=o_in@entry=0x0) at ../../py/obj.c:61
61 return o->type;
(gdb) bt
#0 mp_obj_get_type (o_in=o_in@entry=0x0) at ../../py/obj.c:61
#1 0x0000555555591333 in mp_binary_op (op=MP_BINARY_OP_INPLACE_ADD, lhs=0x0, rhs=0x1) at ../../py/runtime.c:630
#2 0x00007ffff7fc0053 in ?? ()
#3 0x00007ffff7a06440 in ?? ()
#4 0x00007ffff7fc006d in ?? ()
#5 0x00007fffffff9610 in ?? ()
#6 0x0000000000000003 in ?? ()
#7 0x00007fffffff9760 in ?? ()
#8 0x0000000000000000 in ?? ()
Additional Information
No, I've provided everything above.
Code of Conduct
Yes, I agree
Native & viper failures with setjmp exception handling
Port, board and/or hardware
unix
MicroPython version
MicroPython v1.26.0-preview.169.g429478dcfc.dirty on 2025-06-02; linux [GCC 12.2.0] version
(modified from v1.26.0-preview-166-gc0111e63b3)
Reproduction
- grab my branch at https://github.com/jepler/circuitpython/tree/coverage-setjmp
- in ports/unix
make VARIANT=coverage_setjmp test
Expected behaviour
Expected all tests to pass
Observed behaviour
The native_gen.py test fails with a segfault. This is the minimized version of the crashing code:
# catching an exception from .throw()
@micropython.native
def gen3():
try:
yield 1
except Exception as er:
print("caught", repr(er))
g = gen3()
print(g.throw(ValueError(42)))
Additional Information
The branch above has 3 changes:
- introduce a unix variant that uses setjmp for exception handling
- fix a build error in
mp_native_yield_from - turns on N_NLR_SETJMP in emitnative whenever MICROPY_NLR_SETJMP is enabled and N_NLR_SETJMP is not otherwise used (note: this fixes other errors in
make testthat occur with MICROPY_NLR_SETJMP enabled)
It is vaguely possible that my change in mp_native_yield_from is wrong and I considered whether it might contribute to the crash, but in my reduced testcase a gdb breakpoint on that function isn't hit so I don't think that's it.
Code of Conduct
Yes, I agree