From f962f6639c18e9f5e23416ec7d145534f2683701 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 13:05:35 -0500 Subject: [PATCH 1/5] objint_longlong: Avoid undefined behavior in left shift. This fixes the following two flavors of diagnostics: ``` ../../py/objint_longlong.c:215:30: runtime error: left shift of negative value -10000000000000000 ../../py/objint_longlong.c:215:30: runtime error: left shift of 72623859790382856 by 8 places cannot be represented in type 'long long int' ``` This formulation compiles to identical code (gcc 15 -m32 -O3). In fact, the compiler is so sure the original and modified versions are identical that building the following: ``` long long f1(long long lhs_val, long long rhs_val, bool *overflow) { long long result = lhs_val * (1ll << (int)rhs_val); *overflow = rhs_val > 0 && ((lhs_val >= 0 && result < lhs_val) || (lhs_val < 0 && result > lhs_val)); return result; } long long f2(long long lhs_val, long long rhs_val, bool *overflow) { long long result = lhs_val << ((int)rhs_val); *overflow = rhs_val > 0 && ((lhs_val >= 0 && result < lhs_val) || (lhs_val < 0 && result > lhs_val)); return result; } ``` under `-Os` makes the body of f2 just say `jmp f1`! Signed-off-by: Jeff Epler --- py/objint_longlong.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/py/objint_longlong.c b/py/objint_longlong.c index 22ac0ba12efa..ddbb9160cb1e 100644 --- a/py/objint_longlong.c +++ b/py/objint_longlong.c @@ -212,9 +212,7 @@ mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i // negative shift not allowed mp_raise_ValueError(MP_ERROR_TEXT("negative shift count")); } - result = lhs_val << (int)rhs_val; - // Left-shifting of negative values is implementation defined in C, but assume compiler - // will give us typical 2s complement behaviour unless the value overflows + result = lhs_val * (1ll << (int)rhs_val); overflow = rhs_val > 0 && ((lhs_val >= 0 && result < lhs_val) || (lhs_val < 0 && result > lhs_val)); break; case MP_BINARY_OP_RSHIFT: From f8f714d22fd24f0f0d364dabcdbff5928c33e3ec Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 13:33:51 -0500 Subject: [PATCH 2/5] nativeglue: Fix -Werror=clobbered diagnostic. This diagnostic occurs when `nlr_push` is marked with the gcc/clang attribute "returns_twice". Signed-off-by: Jeff Epler --- py/nativeglue.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/py/nativeglue.c b/py/nativeglue.c index 6bf16f1acc29..dbe241d2eae5 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -199,10 +199,11 @@ static bool mp_native_yield_from(mp_obj_t gen, mp_obj_t send_value, mp_obj_t *re nlr_buf_t nlr_buf; mp_obj_t throw_value = *ret_value; if (nlr_push(&nlr_buf) == 0) { + mp_obj_t to_send = send_value; if (throw_value != MP_OBJ_NULL) { - send_value = MP_OBJ_NULL; + to_send = MP_OBJ_NULL; } - ret_kind = mp_resume(gen, send_value, throw_value, ret_value); + ret_kind = mp_resume(gen, to_send, throw_value, ret_value); nlr_pop(); } else { ret_kind = MP_VM_RETURN_EXCEPTION; From 8d52ec7a7ee88323d4a372b663e0e6c7682f93c0 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 13:35:04 -0500 Subject: [PATCH 3/5] nlr: Sanitizer must know that nlr_push returns twice. This might affect codegen adversely in other cases, so the attribute is only enabled in the case where the address sanitizer is enabled. Availabilty of the standard gcc/clang attribute syntax is assumed when __SANITIZE_ADDRESS__ is defined. Signed-off-by: Jeff Epler --- py/nlr.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/py/nlr.h b/py/nlr.h index 47447c5d174f..045c9b2fd533 100644 --- a/py/nlr.h +++ b/py/nlr.h @@ -172,6 +172,9 @@ struct _nlr_jump_callback_node_t { // For this case it is safe to call nlr_push_tail() first. #define nlr_push(buf) (nlr_push_tail(buf), setjmp((buf)->jmpbuf)) #else +#if defined(__SANITIZE_ADDRESS__) +__attribute((returns_twice)) +#endif unsigned int nlr_push(nlr_buf_t *); #endif From 47c1d047ff6e331792a7c2d9ef6b673fd77579d1 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 13:35:43 -0500 Subject: [PATCH 4/5] ci: Add commands for sanitizing longlong builds. Signed-off-by: Jeff Epler --- tools/ci.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tools/ci.sh b/tools/ci.sh index 564b7810f57b..7845743979ed 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -509,13 +509,11 @@ CI_UNIX_OPTS_QEMU_RISCV64=( ) CI_UNIX_OPTS_SANITIZE_ADDRESS=( - VARIANT=coverage CFLAGS_EXTRA="-fsanitize=address --param asan-use-after-return=0" LDFLAGS_EXTRA="-fsanitize=address --param asan-use-after-return=0" ) CI_UNIX_OPTS_SANITIZE_UNDEFINED=( - VARIANT=coverage CFLAGS_EXTRA="-fsanitize=undefined -fno-sanitize=nonnull-attribute" LDFLAGS_EXTRA="-fsanitize=undefined -fno-sanitize=nonnull-attribute" ) @@ -695,6 +693,22 @@ function ci_unix_nanbox_run_tests { ci_unix_run_tests_full_no_native_helper nanbox PYTHON=python2.7 } +function ci_unix_longlong_sanitize_address_build { + ci_unix_build_helper VARIANT=longlong "${CI_UNIX_OPTS_SANITIZE_ADDRESS[@]}" +} + +function ci_unix_longlong_sanitize_address_run_tests { + ci_unix_run_tests_full_helper longlong "${CI_UNIX_OPTS_SANITIZE_ADDRESS[@]}" +} + +function ci_unix_longlong_sanitize_undefined_build { + ci_unix_build_helper VARIANT=longlong "${CI_UNIX_OPTS_SANITIZE_UNDEFINED[@]}" +} + +function ci_unix_longlong_sanitize_undefined_run_tests { + ci_unix_run_tests_full_helper longlong "${CI_UNIX_OPTS_SANITIZE_UNDEFINED[@]}" +} + function ci_unix_longlong_build { ci_unix_build_helper VARIANT=longlong } @@ -752,7 +766,7 @@ function ci_unix_settrace_stackless_run_tests { function ci_unix_sanitize_undefined_build { make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/unix submodules - make ${MAKEOPTS} -C ports/unix "${CI_UNIX_OPTS_SANITIZE_UNDEFINED[@]}" + make ${MAKEOPTS} -C ports/unix VARIANT=coverage "${CI_UNIX_OPTS_SANITIZE_UNDEFINED[@]}" ci_unix_build_ffi_lib_helper gcc } @@ -763,7 +777,7 @@ function ci_unix_sanitize_undefined_run_tests { function ci_unix_sanitize_address_build { make ${MAKEOPTS} -C mpy-cross make ${MAKEOPTS} -C ports/unix submodules - make ${MAKEOPTS} -C ports/unix "${CI_UNIX_OPTS_SANITIZE_ADDRESS[@]}" + make ${MAKEOPTS} -C ports/unix VARIANT=coverage "${CI_UNIX_OPTS_SANITIZE_ADDRESS[@]}" ci_unix_build_ffi_lib_helper gcc } From ba3f9cacf075a0b1d1a118640fc07005e0a35b67 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 18 Jul 2025 13:38:47 -0500 Subject: [PATCH 5/5] workflows: Add unix longlong sanitize builds. Signed-off-by: Jeff Epler --- .github/workflows/ports_unix.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/.github/workflows/ports_unix.yml b/.github/workflows/ports_unix.yml index 60c0244a8f9e..a5ea7462a310 100644 --- a/.github/workflows/ports_unix.yml +++ b/.github/workflows/ports_unix.yml @@ -148,6 +148,34 @@ jobs: if: failure() run: tests/run-tests.py --print-failures + longlong_sanitize_address: + runs-on: ubuntu-22.04 # use 22.04 to get python2, and libffi-dev:i386 + steps: + - uses: actions/checkout@v4 + - name: Install packages + run: source tools/ci.sh && ci_unix_32bit_setup + - name: Build + run: source tools/ci.sh && ci_unix_longlong_sanitize_address_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_longlong_sanitize_address_run_tests + - name: Print failures + if: failure() + run: tests/run-tests.py --print-failures + + longlong_sanitize_undefined: + runs-on: ubuntu-22.04 # use 22.04 to get python2, and libffi-dev:i386 + steps: + - uses: actions/checkout@v4 + - name: Install packages + run: source tools/ci.sh && ci_unix_32bit_setup + - name: Build + run: source tools/ci.sh && ci_unix_longlong_sanitize_undefined_build + - name: Run main test suite + run: source tools/ci.sh && ci_unix_longlong_sanitize_undefined_run_tests + - name: Print failures + if: failure() + run: tests/run-tests.py --print-failures + float: runs-on: ubuntu-latest steps: 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