The Thread Information Block (TIB) or Thread Environment Block (TEB) is a data structure in Win32 on x86 that stores information about the currently running thread. It descended from, and is backward-compatible on 32-bit systems with, a similar structure in OS/2.[1]
The TIB is officially undocumented for Windows 9x. The Windows NT series DDK (as well as the MinGW/ReactOS implementation) includes a struct NT_TIB in winnt.h that documents the subsystem independent part. Even before TIB was effectively documented, many applications have already started using its fields that they are effectively a part of the API. The first field containing the SEH frame, in particular, is referenced by the code produced by Microsoft's own compiler.[1] The Win32 subsystem-specific part of the TEB is undocumented, but Wine includes a TEB definition in winternl.h.[2]
The TIB can be used to get a lot of information on the process without calling Win32 API. Examples include emulating GetLastError(), GetVersion(). Through the pointer to the PEB one can obtain access to the import tables (IAT), process startup arguments, image name, etc. It is accessed from the FS segment register on 32-bit Windows and GS on 64-bit Windows.
This table is based on Wine's work on Microsoft Windows internals.[2]
FS(32ビットの場合)またはGS(64ビットの場合)は、TDB(スレッドデータベース)と呼ばれるデータブロックに埋め込まれたTIBにマッピングされます。TIBには、スレッド固有の例外処理チェーンとTLS(スレッドローカルストレージ)へのポインタが含まれています。スレッドローカルストレージは、C言語のローカルストレージとは異なります。
プロセスは、TIB に格納されている情報を適切に更新する限り、スレッドのスタックを自由に移動できる必要があります。この問題には、スタックベース、スタック制限、解放スタック、保証スタックバイトなど、いくつかのフィールドが重要です。これらはそれぞれ、オフセット0x8、0x10、0x1478に0x174864 ビットで格納されます。さまざまな Windowsカーネル関数は、これらの値を読み書きします。特に、スタックオーバーフローを他の読み書きページフォルトと区別するためです (保証スタックバイトのスタック制限内で保護されているページへの読み書きは、アクセス違反ではなくスタックオーバーフロー例外を生成します)。解放スタックは、Windows API で保護されているページの数を変更できるため重要です。関数は、SetThreadStackGuarantee現在のスペースを読み取り、それを拡張することの両方を可能にします。それを読み取るには、GuaranteedStackBytesフィールドを読み取り、拡張するには、スタックページをアンコミットする必要があります。 を設定せずにスタック制限を設定すると、 でDeallocationStack奇妙な動作が発生する可能性がありますSetThreadStackGuarantee。たとえば、スタック制限が間違った値に上書きされます。さまざまなライブラリが を呼び出します。SetThreadStackGuaranteeたとえば、.NET CLR は、スレッドのスタックを設定するためにこれを使用します。
現在のスレッドのTIBには、セグメントレジスタFS(x86)またはGS(x64)のオフセットとしてアクセスできます。
TIB フィールドにアクセスするには、オフセットを使用するのが一般的ではなくFS:[0]、まず に格納されている線形自己参照ポインタを取得します。このポインタはポインタ演算で使用したり、構造体ポインタFS:[18h]にキャストしたりできます。
Microsoft Windows SDKまたは同様のものを使用すると、プログラマーは、現在のスレッド情報ブロックのアドレスをとして返す、というwinnt.h名前のインライン関数を使用できます。[ 5 ]NtCurrentTebNT_TIB *
IA-32アーキテクチャにおける代替アクセス方法は以下のとおりです。
// gcc (AT&T スタイルのインラインアセンブリ)。void * getTIB ( void ) { register void * pTIB ; #if defined(__x86_64__) || defined(__amd64__) __asm__ ( "movq %%gs:0x30, %0" : "=r" ( pTIB )); #elif defined(__i386__) __asm__ ( "movl %%fs:0x18, %0" : "=r" ( pTIB )); #else #error unsupported architecture #endif return pTIB ; }// gcc (名前付きアドレス空間、-O1 または -ftree-ter のインラインアセンブリバージョンと同じ)。void * getTIB ( void ) { #if defined(__x86_64__) || defined(__amd64__) #ifndef __SEG_GS #error サポートされていない GCC バージョン#endif return * ( void * __seg_gs * ) 0x30 ; #elif defined(__i386__) #ifndef __SEG_FS #error サポートされていない GCC バージョン#endif return * ( void * __seg_fs * ) 0x18 ; #else #error サポートされていないアーキテクチャ#endif }// Microsoft C __declspec ( naked ) void * getTIB () { __asm mov EAX , FS : [ 18 h ] __asm ret }// インラインアセンブリの代わりに Microsoft の組み込み関数を使用 (X86 および X64 アーキテクチャの両方で動作します) void * getTIB () { #ifdef _M_IX86 return ( void * ) __readfsdword ( 0x18 ); #elif _M_AMD64 return ( void * ) __readgsqword ( 0x30 ); #else #error サポートされていないアーキテクチャ#endif }