Microsoft KB Archive/262515

From BetaArchive Wiki
Knowledge Base


The /GT compiler switch may cause an access violation when a fiber is switched between two threads

Article ID: 262515

Article Last Modified on 8/7/2007



APPLIES TO

  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q262515

SYMPTOMS

During the switching of threads, an access violation may occur, with the following message at the top of the call stack:

KERNEL32! IsBadReadPtr@8 + 82 bytes

In general, this problem can occur whenever a fiber is switched between two threads; for instance:

  • when switching between preemptive mode and nonpreemptive mode in User Mode Schedulers (UMS)
  • when accessing a variable that is stored in thread-local storage (TLS) before and after a switch.


CAUSE

A fiber is a lightweight object that consists of a stack and a register context, and which can be scheduled on various threads. A fiber can run on any thread. Because a fiber may be swapped out and then restarted on a different thread, it must not be cached or optimized as a common subexpression across a function call.

The /GT compiler switch prevents such optimizations. The /GT option supports fiber safety for data that is allocated by using static thread-local storage; in other words, data that is allocated with the __declspec( thread ) keyword.

The /GT compiler switch leaves the TLSArray references as constant. When accessing a TLS variable before and after a switch, the address is computed incorrectly. The pointer to the TLS variable is stored in the esi register, the offset is stored in the eax register, and the address is calculated by adding these two values. After a thread switch, eax is recalculated but esi is not. The address is still evaluated by adding esi and eax, and when the fiber switch changes the value in esi, an access violation occurs.

RESOLUTION

Obtain the latest service pack for Visual Studio 6.0.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in the next service pack for Visual Studio 6.0.

For additional information about Visual Studio service packs, click the following article numbers to view the articles in the Microsoft Knowledge Base:

194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why

194295 HOWTO: Tell That a Visual Studio Service Pack Is Installed


To download the latest Visual Studio service pack, visit the following Microsoft Web site:


Additional query words: sp4

Keywords: kbbug kbcompiler kbfix kbvs600sp4fix kbvs600sp5fix KB262515