Skip to content

Fix the Vulkan shim's size assertion on 32-bit ABIs - #143

Open
YeautyYE wants to merge 1 commit into
zmwangx:masterfrom
YeautyYE:fix-vulkan-shim-32bit-size
Open

YeautyYE wants to merge 1 commit into
zmwangx:masterfrom
YeautyYE:fix-vulkan-shim-32bit-size

Conversation

@YeautyYE

Copy link
Copy Markdown

Fixes #137.

Summary

This is the bug reported in #137; I hit the same assertion independently on armhf
CI and had already worked out the details below, so I am sending the fix rather
than another report.

hwcontext_stubs/vulkan/vulkan.h pins sizeof(VkPhysicalDeviceFeatures2) to a
hard-coded 240 bytes, which is the 64-bit layout. On a 32-bit ABI the same struct
is 228 bytes, so the _Static_assert fires and bindgen aborts. Every 32-bit
target whose linked FFmpeg was built with Vulkan therefore fails to build.

This replaces the constant with a pointer-width branch. On a 64-bit ABI the
expression still evaluates to 240, so nothing changes there.

Reproduced with 9.1.1 and 9.0.1, whose shims are byte-identical. 8.1.0 does not
ship the shim.

Reproduction

The shim is self-contained, so this needs neither FFmpeg, nor a 32-bit sysroot,
nor a Vulkan SDK. From the crate root, before this change:

printf '#include <vulkan/vulkan.h>\nint main(void){return 0;}\n' > /tmp/probe.c
clang -target armv7-unknown-linux-gnueabihf -ffreestanding -fsyntax-only \
  -Ihwcontext_stubs /tmp/probe.c
hwcontext_stubs/vulkan/vulkan.h:64:16: error: static assertion failed due to requirement 'sizeof(struct VkPhysicalDeviceFeatures2) == 240': VkPhysicalDeviceFeatures2 must be 240 bytes (Vulkan 1.1 frozen ABI)
   64 | _Static_assert(sizeof(VkPhysicalDeviceFeatures2) == 240,
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hwcontext_stubs/vulkan/vulkan.h:64:50: note: expression evaluates to '228 == 240'

-target i386-unknown-linux-gnu fails identically. x86_64-unknown-linux-gnu
and aarch64-unknown-linux-gnu pass. With this change all four pass.

In a real build the same assertion surfaces as a bindgen failure in the build
script, on an otherwise working armhf toolchain.

Why 228 is the correct size there

member 32-bit 64-bit
VkStructureType sType 4 4
padding before pNext 0 4
void *pNext 4 8
VkBool32 features[55] 220 220
tail padding to struct alignment 0 4
total 228 240

Real Vulkan computes 228 on a 32-bit ABI too: VkPhysicalDeviceFeatures2 is a
VkStructureType, a void *, and VkPhysicalDeviceFeatures (55 VkBool32).
What Vulkan 1.1 froze is the member layout, not a byte count that survives a
change of pointer width, so the old assertion's premise held only on 64-bit. The
struct definition was already correct; only the constant was wrong.

Why not the form suggested in #137

#137 floats sizeof(VkPhysicalDeviceFeatures2) - sizeof(void *) == 240 - sizeof(void *).
That one does not actually unbreak the build: it subtracts a single pointer
width, but 240 → 228 is a 12-byte difference, because the 4 bytes of padding
before pNext and the 4 bytes of tail padding disappear along with the 4 bytes
of pointer. On a 32-bit ABI it reduces to 224 == 236. Checked with clang on
armv7 and i386, where it fails exactly as the current assertion does.

When it triggers

hwcontext_wrapper.h gates the Vulkan block on
__has_include(<libavutil/hwcontext_vulkan.h>), and build.rs puts
hwcontext_stubs first on the include path, so the shim is parsed whenever the
linked FFmpeg installs that header. Debian builds FFmpeg with Vulkan, so
apt install libavutil-dev on armhf or i386 is enough to reach it; no Vulkan SDK
has to be present.

What I verified, and what I did not

Verified with clang on armv7, i386, x86_64 and aarch64: the assertion accepts the
struct as the shim defines it on all four, and rejects a changed prefix (dropping
pNext) on all four. On the two 32-bit targets it also rejects a changed member
count.

One limit worth stating plainly: on a 64-bit ABI a single added member is
invisible to any == 240 check, because 16 + 55 × 4 = 236 pads up to 240, while
16 + 56 × 4 = 240 exactly. The assertion as it stands today has that same blind
spot; this change neither widens nor closes it. It only stops the assertion from
rejecting a correct 32-bit layout.

This is also the only _Static_assert, and the only hard-coded size, in
hwcontext_stubs.

Not verified: a complete 32-bit build, end to end. The CI I hit this in sidesteps
the Vulkan block by hiding hwcontext_vulkan.h, so I have no evidence about what
happens after bindgen on a 32-bit target; there may be further issues behind this
one. This change is scoped to the assertion, and I did not want to imply more
than that.

Workaround for anyone hitting this before a release

A 32-bit build can make the __has_include gate skip the Vulkan block by hiding
the header, which reproduces a libavutil configured without Vulkan:

dpkg -L libavutil-dev | grep '/libavutil/hwcontext_vulkan\.h$' \
  | while IFS= read -r h; do mv "$h" "$h.disabled"; done

Two details that cost me time: on Debian the header lives under the multiarch
include directory (/usr/include/arm-linux-gnueabihf/libavutil/), not
/usr/include/libavutil/. And a probe written as
#include <libavutil/hwcontext_vulkan.h> cannot confirm the header is gone,
because that header pulls in <vulkan/vulkan.h>, which is absent unless the
Vulkan SDK is installed — such a probe fails either way. __has_include is what
distinguishes the two cases.

The assertion pinned sizeof(VkPhysicalDeviceFeatures2) to 240 bytes, which
is the 64-bit layout. On a 32-bit ABI the struct is 228 bytes: the pointer
is 4 bytes and neither the padding before it nor the tail padding applies.
The assertion therefore fired and bindgen aborted on every 32-bit target
whose linked FFmpeg was built with Vulkan.

Select the expected size by pointer width. On a 64-bit ABI the expression
still evaluates to 240, so nothing changes there. What Vulkan 1.1 froze is
the member layout, not a byte count that survives a change of pointer
width, so the struct definition itself needed no change.

Checked with clang for armv7, i386, x86_64 and aarch64.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

static assert assumes 64bit (32bit builds fail)

1 participant