eclipse-threadx / threadx

Eclipse ThreadX is an advanced real-time operating system (RTOS) designed specifically for deeply embedded applications.
https://github.com/eclipse-threadx/rtos-docs/blob/main/rtos-docs/threadx/index.md
MIT License
2.8k stars 767 forks source link

ULONG == uint32_t in 64-bit system cause problem #379

Closed zhugengyu closed 3 months ago

zhugengyu commented 3 months ago

Describe the bug A clear and concise description of what the bug is.

Hello

I failed in regression test when do pointer convert in _tx_thread_timeout, input ULONG is 0x802846e8 RAM memory location, convert to random pointer some, later I build the same code in AARCH32 mode, which can run without corrupt

image

I make some walkround here to pass the regression test


#define TX_ULONG_TO_THREAD_POINTER_CONVERT(a)           ((TX_THREAD *) ((VOID *) (a)))

ifndef TX_THREAD_TIMEOUT_POINTER_SETUP

define TX_THREAD_TIMEOUT_POINTER_SETUP(t) (t) = TX_ULONG_TO_THREAD_POINTER_CONVERT(timeout_input);

endif

VOID _tx_thread_timeout(ULONG timeout_input) {

TX_INTERRUPT_SAVE_AREA

TX_THREAD thread_ptr; VOID (suspend_cleanup)(struct TX_THREAD_STRUCT *suspend_thread_ptr, ULONG suspension_sequence); ULONG suspension_sequence;

/* Pickup the thread pointer.  */
//TX_THREAD_TIMEOUT_POINTER_SETUP(thread_ptr)
if (timeout_input != 0)
{
    thread_ptr = (TX_THREAD *)(ULONG64)timeout_input;
}
else
{
    TX_THREAD_TIMEOUT_POINTER_SETUP(thread_ptr)
}

-- disassembly code 00000000801397f8 <_tx_thread_timeout>: 801397f8: a9b97bfd stp x29, x30, [sp, #-112]! 801397fc: 910003fd mov x29, sp 80139800: b9001fe0 str w0, [sp, #28] 80139804: f0000ac0 adrp x0, 80294000 <_tx_queue_created_ptr> 80139808: 910b8000 add x0, x0, #0x2e0 8013980c: f9400000 ldr x0, [x0] 80139810: f9401800 ldr x0, [x0, #48] 80139814: f90037e0 str x0, [sp, #104] 80139818: f94037e2 ldr x2, [sp, #104] 8013981c: b9401fe1 ldr w1, [sp, #28]


![image](https://github.com/eclipse-threadx/threadx/assets/85654684/d1727ebd-cbca-4e87-bc8d-a43859979340)

- I have also noticed that Linux-port which regression test run by default have ULONG == uint64_t

```c
   -- tx_port.h in Linux port
#if defined(__x86_64__) && __x86_64__
typedef int                                     LONG;
typedef unsigned int                            ULONG;
#else /* __x86_64__ */
typedef long                                    LONG;
typedef unsigned long                           ULONG;  --> uint64_t in 64-bit system
#endif /* __x86_64__ */

-- tx_port.h in cortex_a53 port
#define VOID                                    void
typedef char                                    CHAR;
typedef unsigned char                           UCHAR;
typedef int                                     INT;
typedef unsigned int                            UINT;
typedef int                                     LONG;
typedef unsigned int                            ULONG;  --> uint32_t in 64-bit system, but used as pointer value type
typedef unsigned long long                      ULONG64;
typedef short                                   SHORT;
typedef unsigned short                          USHORT;
   tx_thread_sehedule.S
    /* Increment the run count for this thread.  */
    // _tx_thread_current_ptr -> tx_thread_run_count++;

    LDR     w2, [x0, #4]                        // Pickup run counter
    LDR     w3, [x0, #36]                       // Pickup time-slice for this thread
    ADD     w2, w2, #1                          // Increment thread run-counter
    STR     w2, [x0, #4]                        // Store the new run counter

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

ARM64 device

To Reproduce Steps to reproduce the behavior:

  1. Build my project in IAR Workbench.
  2. See error

Expected behavior A clear and concise description of what you expected to happen.

Impact What impact does this issue have on your progress (e.g., annoyance, showstopper)

Failed to pass regression test Unable to use 64-bit memory space

Logs and console output If applicable, add console logs or other types of debug information like Wireshark capture as .zip file.

Additional context Add any other context about the problem here.

zhugengyu commented 3 months ago

i comment below code in tx_port.h,and now it is ok

/* Define the thread timeout setup logic in _tx_thread_create.  */

/* #define TX_THREAD_CREATE_TIMEOUT_SETUP(t)    (t) -> tx_thread_timer.tx_timer_internal_timeout_function =    &(_tx_thread_timeout);            \
                                             (t) -> tx_thread_timer.tx_timer_internal_timeout_param =       0;                                \
                                             (t) -> tx_thread_timer.tx_timer_internal_thread_timeout_ptr =  (VOID *) (t); */

/* Define the thread timeout pointer setup in _tx_thread_timeout.  */

//#define TX_THREAD_TIMEOUT_POINTER_SETUP(t)   (t) =  (TX_THREAD *) _tx_timer_expired_timer_ptr -> tx_timer_internal_thread_timeout_ptr;