Visible to Intel only — GUID: GUID-F0B0D34B-6691-4033-931F-4BBBD1A1F791
Visible to Intel only — GUID: GUID-F0B0D34B-6691-4033-931F-4BBBD1A1F791
Cross-thread Stack Access
Occurs when a thread accesses a different thread's stack.
ID |
Code Location |
Description |
---|---|---|
1 |
Allocation site |
If present, represents the location and associated call stack where the accessed stack memory was allocated. |
2 |
Stack owned |
Represents the location and associated call stack where the thread owning the stack was created. |
3 |
Stack cross access |
Represents the location and associated call stack where the stack memory was accessed by a different thread. |
It is unsafe if the two threads do not coordinate the access properly. This can happen even if the accesses do not have race conditions. Failure to coordinate safe accesses can potentially result in unexpected application behavior or even an application crash. However, the Intel Inspector does not distinguish between safe accesses and unsafe accesses. It is the user's responsibility to determine if a cross-thread stack access is safe or not.
Preparation |
int *p; CreateThread(..., thread #1, ...); CreateThread(..., thread #2, ...); |
Thread #1 |
int q[1024]; // Allocated on Thread #1's stack p = q; q[0] = 1; |
Thread #2 |
*p = 2; // Thread #1's stack accessed |
If thread #1 and thread #2 are concurrent and there is no other synchronization between them, the Intel Inspector detects a Cross-thread stack access problem if p = q; in thread #1 executes before *p = 2; executes in thread #2.
The example shows the case where thread #1 publishes the address of a local stack variable q to the global pointer p, and later thread #2 accesses the stack of thread #1 by dereferencing the global pointer p, in effect writing to the variable q on thread #1.
Keep stack variables private to each thread.
Avoid publishing addresses of stack variables and storing shared data on thread's stack. Instead, store shared data on the heap or in static variables.
If you cannot avoid accessing a different thread's stack, establish a handshaking protocol between the owning thread and accessing thread to prevent accidental corruption or the reading of unexpected or garbage stack data.