On Mon, Jul 07, 2025 at 11:59:48AM -0400, Jason Merrill wrote: > > Another case are targets with implicit -fno-delete-null-pointer-checks or > > when users use that explicitly. In that case one variable or function can > > actually end up being at address 0, so am not sure if it is correct to > > pretend at constant evaluation time something that is not true at runtime. > > Though on the other side, pointers with NULL value in the standard mean that > > there is no object at that location, so any kind of placing variables or > > functions at that location will cause problems everywhere anyway. > > A simple thing might be to adjust Patrick's patch to only set the flag > > if (!targetm.addr_space.zero_address_valid (ADDR_SPACE_GENERIC))
That could work too, we'd probably just need to update msp430 so that it overrides the zero_address_valid hook: grep flag_delete_null_pointer_checks config/*/*.cc config/avr/avr.cc: /* Disable flag_delete_null_pointer_checks if zero is a valid address. */ config/avr/avr.cc: flag_delete_null_pointer_checks = 0; config/msp430/msp430.cc: flag_delete_null_pointer_checks = 0; grep zero_address_valid config/*/*.cc config/avr/avr.cc: if (targetm.addr_space.zero_address_valid (ADDR_SPACE_GENERIC)) config/avr/avr.cc:avr_addr_space_zero_address_valid (addr_space_t) config/avr/avr.cc:#define TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID avr_addr_space_zero_address_valid config/i386/i386.cc:ix86_addr_space_zero_address_valid (addr_space_t as) config/i386/i386.cc:#define TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID ix86_addr_space_zero_address_valid (i386 has it for non-generic as). Though, that would only help avr and msp430, when one uses say arm-elf or some other bare metal target and explicit -fno-delete-null-pointer-checks with vars at zero address. The other possibility is to add some flag (Optimization like flag_delete_null_pointer_checks) that would simply always be false unless /* When instrumenting the pointers, we don't want to remove the null pointer checks. */ if (opts->x_flag_sanitize & (SANITIZE_NULL | SANITIZE_NONNULL_ATTRIBUTE | SANITIZE_RETURNS_NONNULL_ATTRIBUTE)) opts->x_flag_delete_null_pointer_checks = 0; when it was previously non-zero. And use that flag in the decision whether to temporarilz change flag_delete_null_pointer_checks or not. Jakub