Contents of the TIB on Windows
This table is based onStack information stored in the TIB
A process should be free to move the stack of its threads as long as it updates the information stored in the TIB accordingly. A few fields are key to this matter: stack base, stack limit, deallocation stack, and guaranteed stack bytes, respectively stored at offsets 0x8, 0x10, 0x1478 and 0x1748 in 64 bits. Different Windows kernel functions read and write these values, specially to distinguish stack overflows from other read/write page faults (a read or write to a page guarded among the stack limits in guaranteed stack bytes will generate a stack-overflow exception instead of an access violation). The deallocation stack is important because Windows API allows to change the amount of guarded pages: the function SetThreadStackGuarantee allows both read the current space and to grow it. In order to read it, it reads the GuaranteedStackBytes field, and to grow it, it uses has to uncommit stack pages. Setting stack limits without setting DeallocationStack will probably cause odd behavior in SetThreadStackGuarantee. For example, it will overwrite the stack limits to wrong values. Different libraries call SetThreadStackGuarantee, for example the .NET CLR uses it for setting up the stack of their threads.Accessing the TIB
The TIB of the current thread can be accessed as an offset of segmentFS: /code>, but rather first getting a linear self-referencing pointer to it stored at FS: 8h/code>. That pointer can be used with pointer arithmetic or be cast to a struct
In computer science, a record (also called a structure, struct, or compound data) is a basic data structure. Records in a database or spreadsheet are usually called "rows".
A record is a collection of '' fields'', possibly of different data typ ...
pointer.
Using Microsoft Windows SDK
Microsoft Windows SDK, and its predecessors Platform SDK, and .NET Framework SDK, are software development kits (SDKs) from Microsoft that contain documentation, header files, libraries, samples and tools required to develop applications for Micr ...
or similar, a programmer could use an inline function defined in winnt.h
named NtCurrentTeb
which returns the address of the current Thread Information Block as NT_TIB *
.
Alternative methods of access for IA-32
IA-32 (short for "Intel Architecture, 32-bit", commonly called i386) is the 32-bit version of the x86 instruction set architecture, designed by Intel and first implemented in the 80386 microprocessor in 1985. IA-32 is the first incarnation of ...
architectures are as follows:
// gcc (AT&T-style inline assembly).
void *getTIB(void)
// gcc (named address spaces, same as the inline assembly version on -O1 or -ftree-ter).
void *getTIB(void)
// Microsoft C
__declspec(naked)
void *getTIB()
// Using Microsoft's intrinsics instead of inline assembly (works for both X86 and X64 architectures)
void *getTIB()
See also
* Structured Exception Handling The Microsoft Windows family of operating systems employ some specific exception handling mechanisms.
Structured Exception Handling
Microsoft Structured Exception Handling is the native exception handling mechanism for Windows and a forerunner te ...
References
Further reading
* {{cite book, url=https://archive.org/details/windows95systemp00matt/page/136, title=Windows 95 Programming Secrets, author-last=Pietrek, author-first=Matt, author-link=Matt Pietrek, date=March 1996, publisher=IDG, isbn=978-1-56884-318-6, page
136–138
format=pdf, accessdate=2010-07-17, url-status=dead, url-access=registration
External links
Structured Exception Handling and the TIB
TEB definitions for various Windows versions
Windows NT architecture
Microsoft application programming interfaces
Threads (computing)