Intel® Inspector User Guide for Windows* OS

ID 767798
Date 5/15/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

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.

For Microsoft Windows* threading, a thread's stack is allocated by the system when the thread is created, and the allocation site is implicit.

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.

CAUTION:

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.

Example

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.

Possible Correction Strategies

  • 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.