Skip to content

fix: use unsigned bitfields for portable bytecode cache struct layout - #141

Merged
dylan-conway merged 2 commits into
mainfrom
dylan/fix-cross-compile-bytecode-bitfields
Jan 21, 2026
Merged

dylan-conway merged 2 commits into
mainfrom
dylan/fix-cross-compile-bytecode-bitfields

Conversation

@dylan-conway

@dylan-conway dylan-conway commented Jan 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes cross-platform bytecode serialization crash when bytecode generated on macOS/Linux is loaded on Windows (e.g., bun build --compile --bytecode --target=bun-windows-x64).

Root cause

The CachedFunctionExecutable and CachedCodeBlock structs contain fields with different types mixed together:

  • SourceParseMode (enum : uint8_t)
  • OptionSet<CodeGenerationMode> (class wrapping uint8_t)
  • CodeFeatures (typedef uint16_t)
  • LexicallyScopedFeatures (typedef uint8_t)
  • unsigned bitfields

MSVC and GCC have different struct packing rules when mixing different-sized types. This causes layout mismatches:

Test struct macOS Windows
Mixed types (uint8_t + unsigned bitfields) 8 bytes 12 bytes ❌
All unsigned bitfields 8 bytes 8 bytes ✅

Fix

Replace all non-unsigned bitfield types with unsigned : N bitfields:

  • SourceParseMode m_sourceParseMode → unsigned m_sourceParseMode : 8
  • OptionSet<CodeGenerationMode> m_codeGenerationMode → unsigned m_codeGenerationMode : 8
  • CodeFeatures m_features : 14 → unsigned m_features : 14
  • LexicallyScopedFeatures m_lexicallyScopedFeatures : 2 → unsigned m_lexicallyScopedFeatures : 2

Accessors cast back to original types. This ensures consistent layout across all platforms.

Why not __attribute__((gcc_struct))?

The initial approach tried using gcc_struct to force GCC-compatible layout, but clang-cl (LLVM 19) doesn't support this attribute and silently ignores it.

Test plan

  • Build WebKit on macOS and Windows
  • Test cross-compilation: generate bytecode on macOS, run on Windows
    # On macOS
    bun build --compile --bytecode --target=bun-windows-x64 ./index.js
    # Copy to Windows and run

🤖 Generated with Claude Code

@dylan-conway

Copy link
Copy Markdown
Member Author

/build-preview

@github-actions

Copy link
Copy Markdown

🔄 Preview build started

Release tag: autobuild-preview-pr-141-31b7cc20
Workflow: View progress

@coderabbitai

coderabbitai Bot commented Jan 21, 2026 •

Copy link
Copy Markdown

Walkthrough

Replaced enum/OptionSet-typed members with raw byte storage and added casts/wrappers when exposing or serializing those values in CachedFunctionExecutable and CachedCodeBlock within Source/JavaScriptCore/runtime/CachedTypes.cpp. Encoding/decoding paths updated accordingly. Lines changed: +9/-9.

Changes

Cohort / File(s) Summary
Cached types: storage & accessors
Source/JavaScriptCore/runtime/CachedTypes.cpp
Changed enum/OptionSet fields to raw bytes: SourceParseMode m_sourceParseMode → uint8_t m_sourceParseMode, SourceParseMode m_parseMode → uint8_t m_parseMode, OptionSet<CodeGenerationMode> m_codeGenerationMode → uint8_t m_codeGenerationMode. Updated accessors to return static_cast<SourceParseMode>(...) and OptionSet<CodeGenerationMode>::fromRaw(...).
Encoding / decoding
Source/JavaScriptCore/runtime/CachedTypes.cpp
Updated encode/decode paths to read/write the raw uint8_t storage and convert via toRaw/fromRaw or static_cast when serializing/deserializing the parse and code-generation mode values.
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: converting to unsigned bitfields for consistent struct layout across platforms in the bytecode cache.
Description check ✅ Passed The description provides comprehensive context, root cause analysis, the fix, and a test plan, though it lacks explicit bug tracker reference and formal review signoff as required by WebKit's template.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Comment @coderabbitai help to get the list of available commands and usage tips.

…rtable serialization

CachedFunctionExecutable and CachedCodeBlock in CachedTypes.cpp are
serialized as raw memory bytes for the JSC bytecode cache. These structs
contain bitfields of mixed types (unsigned, CodeFeatures, etc.) which are
packed differently by MSVC-style (clang-cl) vs GCC-style (Clang/GCC)
compilers. This makes bytecode generated on macOS/Linux crash when loaded
on Windows.

Adding __attribute__((gcc_struct)) to just these two structs forces
GCC-compatible layout regardless of compiler, making the bytecode cache
format portable across all platforms. This is more targeted than a global
-mno-ms-bitfields flag which would break MSVC ABI compatibility for all
of WebKit.
@github-actions

Copy link
Copy Markdown

⚠️ Preview build was cancelled

This may have been caused by a newer build being triggered for the same PR.

@dylan-conway
dylan-conway force-pushed the dylan/fix-cross-compile-bytecode-bitfields branch from 31b7cc2 to 481ba51 Compare January 21, 2026 05:15
@dylan-conway

Copy link
Copy Markdown
Member Author

/build-preview

@github-actions

Copy link
Copy Markdown

🔄 Preview build started

Release tag: autobuild-preview-pr-141-481ba51a
Workflow: View progress

@github-actions

Copy link
Copy Markdown

✅ Preview build completed

Release: autobuild-preview-pr-141-481ba51a

@sosukesuzuki

Copy link
Copy Markdown
Member

nice

@dylan-conway dylan-conway changed the title fix: add -mno-ms-bitfields for clang-cl to fix cross-platform bytecode cache fix: use __attribute__((gcc_struct)) on bytecode cache structs for portable serialization Jan 21, 2026
@dylan-conway

Copy link
Copy Markdown
Member Author

So it seems clang-cl does not support gcc_structs, yet this change fixed the cross compiling bytecode crash. Need to investigate more

@dylan-conway dylan-conway changed the title fix: use __attribute__((gcc_struct)) on bytecode cache structs for portable serialization fix: use uint8_t for portable bytecode cache struct layout Jan 21, 2026
@dylan-conway

Copy link
Copy Markdown
Member Author

/build-preview

@github-actions

Copy link
Copy Markdown

🔄 Preview build started

Release tag: autobuild-preview-pr-141-16a168cf
Workflow: View progress

Replace enum/OptionSet/typedef fields with unsigned bitfields to ensure
consistent struct layout across MSVC and GCC packing rules.

The issue: mixing different-sized types (uint8_t, uint16_t, enums) with
unsigned bitfields causes different packing on Windows vs macOS/Linux.
This breaks cross-platform bytecode serialization.

The fix: use `unsigned m_field : N` for all bitfields. This keeps all
fields in the same unsigned bitfield sequence, ensuring consistent layout.

Test results (test struct with mixed types):
- OriginalLayout: macOS=8, Windows=12 (DIFFERENT)
- FixedLayout:    macOS=8, Windows=8  (SAME)

Co-Authored-By: Claude <[email protected]>
@dylan-conway
dylan-conway force-pushed the dylan/fix-cross-compile-bytecode-bitfields branch from 16a168c to 9899e38 Compare January 21, 2026 18:54
@dylan-conway dylan-conway changed the title fix: use uint8_t for portable bytecode cache struct layout fix: use unsigned bitfields for portable bytecode cache struct layout Jan 21, 2026
@dylan-conway

Copy link
Copy Markdown
Member Author

/build-preview

@github-actions

Copy link
Copy Markdown

⚠️ Preview build was cancelled

This may have been caused by a newer build being triggered for the same PR.

@github-actions

Copy link
Copy Markdown

🔄 Preview build started

Release tag: autobuild-preview-pr-141-9899e383
Workflow: View progress

@github-actions

Copy link
Copy Markdown

✅ Preview build completed

Release: autobuild-preview-pr-141-9899e383

@dylan-conway
dylan-conway merged commit 87c6cde into main Jan 21, 2026
1 check passed
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.

2 participants