Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Platform:Microsoft Visual C++ ABI/C++ exception handling

From ReverseEngineering.wiki

How MSVC implements C++ exceptions: throw calls _CxxThrowException, which raises SEH exception 0xE06D7363 with the object and a ThrowInfo record (plus the image base on x64); the ThrowInfo's CatchableTypeArray lists the types it can be caught as; per-function FuncInfo tables (magic 0x19930522, __CxxFrameHandler3) or compressed FuncInfo4 records (__CxxFrameHandler4, the x64 default) describe try blocks, catch handlers and unwind actions.

Throwing

throw x; copies x to a temporary and calls _CxxThrowException(void *object, ThrowInfo *info) (__stdcall on x86), which calls RaiseException(0xE06D7363, EXCEPTION_NONCONTINUABLE, n, params):

Param x86 (n = 3) x64 (n = 4)
[0] 0x19930520 (magic) 0x19930520
[1] address of the exception object same
[2] address of the ThrowInfo same (an address, not an RVA)
[3] none image base of the module holding the ThrowInfo; the RVAs inside it are relative to this

0xE06D7363 is 0xE0000000 | 'msc' (little-endian bytes 63 73 6D E0). Runtime test (MSVC 19.44): on x64 the record had code E06D7363, flags 0x81, 4 parameters, param[2] = image base + RVA of _TI1?AUErr@@, param[3] = image base; on x86 flags 0x1 and 3 parameters. Unverified: what flag 0x80 on x64 means.

ThrowInfo, CatchableTypeArray, CatchableType

ref = absolute VA on x86, RVA on x64.

ThrowInfo (_TI<n><type>, 16 bytes)

Offset Size Type Name Notes
0x00 4 uint32_t attributes 1 const, 2 volatile, 4 __unaligned, 0x10 WinRT (Wine)
0x04 4 ref pmfnUnwind destructor of the thrown object, 0 if trivial
0x08 4 ref pForwardCompat 0
0x0C 4 ref pCatchableTypeArray

CatchableTypeArray (_CTA<n><type>): int32_t nCatchableTypes followed by that many refs to CatchableTypes: the thrown type first, then its public bases (observed: MyError, std::runtime_error, std::exception).

CatchableType (_CT??_R0..., 28 bytes)

Offset Size Type Name Notes
0x00 4 uint32_t properties 1 simple (scalar) type, 4 has virtual bases, 8 WinRT (Wine), 0x10 std::bad_alloc
0x04 4 ref pType TypeDescriptor (Platform:Microsoft Visual C++ ABI/RTTI)
0x08 4 int32_t mdisp this-adjustment to this base
0x0C 4 int32_t pdisp vbptr offset or -1
0x10 4 int32_t vdisp offset in the vbtable
0x14 4 int32_t sizeOrOffset size of the type (the object is copied when caught by value)
0x18 4 ref copyFunction copy constructor, 0 for scalars

Observed: throw 42 gives _TI1H with one CatchableType of type int, properties 1, size 4, no copy function; std::bad_alloc has properties 0x10.

FuncInfo (__CxxFrameHandler3)

The per-function table of the older scheme, used on x86 and on x64 with /d2FH4- (and by compilers before FH4). x86 functions with C++ EH register an SEH frame whose handler is an __ehhandler$<function> thunk: /GS cookie checks, then mov eax, offset FuncInfo; jmp __CxxFrameHandler3; the bytes 33 C8 E8 ?? ?? ?? ?? B8 ?? ?? ?? ?? E9 matched all 54 thunks of an x86 test build and all 286 of the x86 msvcp140.dll, with no other hits. On x64 the UNWIND_INFO handler data is the FuncInfo RVA.

Offset Size Type Name Notes
0x00 4 uint32_t magicNumber 0x19930520 (VC6), 0x19930521 (adds pESTypeList), 0x19930522 (adds EHFlags); current compilers write 0x19930522
0x04 4 int32_t maxState entries in the unwind map
0x08 4 ref pUnwindMap UnwindMapEntry[maxState]
0x0C 4 uint32_t nTryBlocks
0x10 4 ref pTryBlockMap TryBlockMapEntry[nTryBlocks]
0x14 4 uint32_t nIPMapEntries 0 on x86
0x18 4 ref pIPtoStateMap x64: {RVA ip, int32 state} pairs; 0 on x86
0x1C 4 int32_t dispUnwindHelp x64 only: frame offset of the state variable; on x86 the next two fields move up by 4
0x20 (x86 0x1C) 4 ref pESTypeList 0
0x24 (x86 0x20) 4 int32_t EHFlags 1 = /EHs (synchronous exceptions only), 4 = noexcept function

Size 36 bytes on x86, 40 on x64. Observed for main of the test program: x86 22 05 93 19 05 00 00 00 <VA> 02 00 00 00 <VA> 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00; x64 (/d2FH4-) maxState 5, 2 try blocks, 5 IP-to-state entries, dispUnwindHelp 0x70, EHFlags 1.

UnwindMapEntry (8 bytes): int32_t toState; ref action (cleanup funclet or 0).

TryBlockMapEntry (20 bytes): int32_t tryLow, tryHigh, catchHigh, nCatches; ref pHandlerArray.

HandlerType (16 bytes on x86, 20 on x64): uint32_t adjectives (1 const, 2 volatile, 8 reference), ref pType (TypeDescriptor; 0 = catch(...)), int32_t dispCatchObj (frame offset of the catch variable, 0 if none), ref addressOfHandler (the catch funclet), and on x64 uint32_t dispFrame. Observed: catch (const std::exception&) has adjectives 9 (const + reference), catch (int v) 0.

FuncInfo4 (__CxxFrameHandler4)

x64 builds of MSVC 19.44 use __CxxFrameHandler4 by default (observed: the UNWIND_INFO handler of main jumps to __CxxFrameHandler4). The handler data is the RVA of a variable-length, unaligned record:

  • 1 header byte: 0x01 isCatch, 0x02 isSeparated, 0x04 BBT, 0x08 has unwind map, 0x10 has try-block map, 0x20 /EHs, 0x40 noexcept.
  • then, when the bits are set: BBT flags (compressed uint), RVA of the unwind map, RVA of the try-block map; always the RVA of the IP-to-state map (for separated code a list of {function RVA, map RVA} pairs instead).
  • Each map starts with a compressed count. A compressed unsigned int is tagged by the low bits of its first byte: xxxxxxx0 1 byte (value = byte >> 1), xxxxxx01 2 bytes, xxxxx011 3 bytes, xxxx0111 4 bytes (little-endian, value = all bits >> tag length). Unverified: the 5-byte form xxxx1111 (Wine does not decode it).

Observed: main's record is 38 04 64 09 00 11 64 09 00 3E 64 09 00: header 0x38 (unwind map, try-block map, /EHs), then RVAs 0x96404 ($stateUnwindMap$main), 0x96411 ($tryMap$main) and 0x9643E ($ip2state$main) as in the MAP file; the unwind map begins with 0A = 5 entries, the same maxState as the FH3 build.

Evidence & references

  • clang MicrosoftCXXABI.cpp (llvm-project 8cdfc2de): CatchableType/CTA/ThrowInfo types 737-784, _CxxThrowException 786-800, CatchableType flags 4296-4306, ThrowInfo flags and .xdata 4470-4505.
  • LLVM WinException.cpp: FuncInfo, UnwindMapEntry, TryBlockMap and HandlerType layouts and the 0x19930522 magic, lines 717-870.
  • Wine (7298a818): include/winternl.h 4115-4122 (0xE06D7363, 0x19930520, parameter list); dlls/msvcrt/cpp.c 902-921 (_CxxThrowException, 4th parameter = RVA base); dlls/msvcrt/cxx.h 24-31 (type flags), 333-381 (cxx_type_info, cxx_exception_type); dlls/msvcrt/except.c 127-131 (null type = catch(...)); dlls/msvcrt/handler4.c 37-56 (FH4 header bits), 119-153 (compressed uint), 696-740 (record parsing order).
  • Runtime tests with MSVC 19.44 x86/x64 builds: a __try/__except filter printing the EXCEPTION_RECORD of a C++ throw; FuncInfo and HandlerType bytes compared with the MAP symbols $cppxdata$main, $tryMap$main, $handlerMap$0$main, __ehfuncinfo$_main, __catchsym$_main$5.