Pin the whole SharedBlock layout with static_asserts (ABI tripwire)

The cross-process block's layout is a wire protocol shared by the x64 host and the
x86 hook, but only three front offsets were asserted, and hook_selftest's
dump_layout merely printed the rest. A field reordered/resized inside HookStatus
(which precedes control/video/mkb) would silently shift everything with no
compile-time tripwire and, if the developer forgot to bump kProtocolVersion, ship
a silent host<->DLL mismatch.

- protocol.hpp now static_asserts sizeof(SharedBlock) and every sub-channel offset
  (status/control/video/mkb) plus each sub-struct size (HookStatus/HookControl/
  VideoShare/AudioStreamInfo/HookEntry/MkbRing). protocol.hpp is compiled for both
  arches, so a cross-bitness divergence fails to compile on the one that disagrees.
- hook_selftest's dump_layout now ASSERTS the same numbers instead of only
  printing, so hook_selftest_x86 confirms the x86 layout at runtime too.

Verified: x64 and x86 builds both compile (identical layout) and both selftests
pass; sizeof(SharedBlock)=3936 on both.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-24 01:47:19 +02:00
parent 1a93fcf196
commit 5e9b1cde4c
3 changed files with 31 additions and 7 deletions

View File

@@ -97,6 +97,19 @@ void dump_layout()
offsetof(HookStatus, audio_streams), offsetof(HookStatus, hook_entries));
std::printf("LAYOUT VideoShare sizeof=%zu present_calls=%zu HookControl sizeof=%zu\n", sizeof(VideoShare),
offsetof(VideoShare, present_calls), sizeof(HookControl));
std::printf("LAYOUT off mkb=%zu MkbRing sizeof=%zu AudioStreamInfo sizeof=%zu HookEntry sizeof=%zu\n",
offsetof(SharedBlock, mkb), sizeof(MkbRing), sizeof(AudioStreamInfo), sizeof(HookEntry));
// Assert the layout, not just print it -- and this runs in the x86 build too (hook_selftest_x86),
// so the same numbers the static_asserts pin at compile time are confirmed at runtime on both
// arches. The reference values match the static_asserts in protocol.hpp.
check(sizeof(SharedBlock) == 3936, "layout: sizeof(SharedBlock)");
check(offsetof(SharedBlock, sequence) == 12 && offsetof(SharedBlock, pads) == 16, "layout: seqlock front");
check(offsetof(SharedBlock, status) == 96 && offsetof(SharedBlock, control) == 1816, "layout: status/control");
check(offsetof(SharedBlock, video) == 1840 && offsetof(SharedBlock, mkb) == 1880, "layout: video/mkb");
check(sizeof(HookStatus) == 1720 && sizeof(HookControl) == 24 && sizeof(VideoShare) == 40,
"layout: sub-struct sizes");
check(sizeof(AudioStreamInfo) == 32 && sizeof(HookEntry) == 56 && sizeof(MkbRing) == 2056,
"layout: more sub-struct sizes");
}
int main()