Skip to content

Array out-of-bounds checking problem caused by compilation optimization level - O2 #469

@XiaoSenLuo

Description

@XiaoSenLuo

Describe the bug
I compile the demo_threadx code with -O2 flag, but, tx_thread_system_suspend.c is occur a error about array bounds. I try to change the level of optimize to -O1, and then there are no thing happened, anything look like good.

The reason is that _tx_thread_lowest_set_bit_calculate function can return a value beyond the value of TX_THREAD_SMP_MAX_CORES - 1, and its parameter "map" should be limited.

my method is to modify the macro definition TX_LOWEST_SET_BIT_CALCULATE,
the original definition:

#define TX_LOWEST_SET_BIT_CALCULATE (m, b) (b) = _tx_thread_lowest_set_bit_calculate((m)); 

New definition:

#define TX_LOWEST_SET_BIT_CALCULATE(m, b) (b) = _tx_thread_lowest_set_bit_calculate((m) &TX_THREAD_SMP_CORE_MASK);

I don't know what the impact of such a change will be, and I hope someone can tell me, thanks.

Please also mention any information which could help others to understand
the problem you're facing:

  • Cortex-A9, zynq 7000?
    -Threadx V6.4.3.202503?
  • xilinx arm-none-eabi-gcc 12.2?

This is an error message from the terminal, I hope it helps you

C:\UserProgram\Xilinx\Vitis\2023.2\gnu\aarch32\nt\gcc-arm-none-eabi\bin\arm-none-eabi-gcc.exe -DNDEBUG -DSHAREABLE_DDR=1 -DTARGET_FPU_VFP=1 -DTX_ENABLE_VFP_SUPPORT=1 -DTX_INCLUDE_USER_DEFINE_FILE -DTX_THREAD_SMP_CORE_MASK=0x03 -DTX_THREAD_SMP_DEFAULT_WAKEUP_LOGIC -DTX_THREAD_SMP_MAX_CORES=2 -DTX_THREAD_SMP_ONLY_CORE_0_DEFAULT -DXDEBUG_WARNING -D_GNU_SOURCE -IC:/UserTemp/test_7020/ps/common/os/threadx_zynq/ports_smp/cortex_a9_smp/gnu/inc -IC:/UserTemp/test_7020/ps/common/os/threadx_zynq -IC:/UserTemp/test_7020/ps/cmake-build-release-xilinx-vitis/common/os/threadx/custom_inc -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/coresightps_dcc_v1_9/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/cpu_cortexa9_v2_12/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/ddrps_v1_2/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/dmaps_v2_9/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/devcfg_v3_8/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/gpiops_v3_12/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/emacps_v3_19/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/scugic_v5_2/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/scutimer_v2_5/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/scuwdt_v2_5/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/sdps_v4_2/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/ttcps_v3_18/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/uartps_v3_13/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/xadcps_v2_7/src -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/standalone_v9_0/src/common -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/standalone_v9_0/src/arm/common -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/libsrc/standalone_v9_0/src/arm/cortexa9 -IC:/UserTemp/test_7020/ps/s19_platform/ps7_cortexa9_0/standalone_domain/bsp/ps7_cortexa9_0/include -isystem C:/UserTemp/test_7020/ps/common/os/threadx/common_smp/inc -O3 -DNDEBUG -std=gnu11 -fdiagnostics-color=always -mcpu=cortex-a9 -mfloat-abi=hard -marm -mfpu=neon-vfpv4 -Wall -Wextra -fno-tree-loop-distribute-patterns -fdata-sections -ffunction-sections -O2 -Werror -pedantic -fmessage-length=0 -fsigned-char -Wunused -Wuninitialized -Wmissing-declarations -Wconversion -Wpointer-arith -Wshadow -Wlogical-op -Waggregate-return -Wfloat-equal -MD -MT common/os/threadx/CMakeFiles/threadx.dir/common_smp/src/tx_thread_system_suspend.c.obj -MF common\os\threadx\CMakeFiles\threadx.dir\common_smp\src\tx_thread_system_suspend.c.obj.d -o common/os/threadx/CMakeFiles/threadx.dir/common_smp/src/tx_thread_system_suspend.c.obj -c C:/UserTemp/test_7020/ps/common/os/threadx/common_smp/src/tx_thread_system_suspend.c
In file included from C:/UserTemp/test_7020/ps/common/os/threadx/common_smp/src/tx_thread_system_suspend.c:31:
In function '_tx_thread_smp_remap_solution_find',
    inlined from '_tx_thread_system_suspend' at C:/UserTemp/test_7020/ps/common/os/threadx/common_smp/src/tx_thread_system_suspend.c:777:57:
C:/UserTemp/test_7020/ps/common/os/threadx/common_smp/inc/tx_thread.h:1336:37: error: array subscript 2 is above array bounds of 'TX_THREAD *[2]' {aka 'struct TX_THREAD_STRUCT *[2]'} [-Werror=array-bounds]
 1336 |         _tx_thread_smp_schedule_list[core] =     last_thread;
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
C:/UserTemp/test_7020/ps/common/os/threadx/common_smp/inc/tx_thread.h: In function '_tx_thread_system_suspend':
C:/UserTemp/test_7020/ps/common/os/threadx/common_smp/inc/tx_thread.h:187:45: note: while referencing '_tx_thread_smp_schedule_list'
  187 | THREAD_DECLARE  TX_THREAD *                 _tx_thread_smp_schedule_list[TX_THREAD_SMP_MAX_CORES];
      |                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1.exe: all warnings being treated as errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghardwareNew hardware or architecture support request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions