Skip to content

Add 32-bit Windows (windows/386) support - #27

Merged
aegistudio merged 5 commits into
winfsp:masterfrom
nscheer:add-32bit-support
Oct 3, 2026
Merged

aegistudio merged 5 commits into
winfsp:masterfrom
nscheer:add-32bit-support

Conversation

@nscheer

@nscheer nscheer commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Disclaimer: The code and this PR's description was written with support from Claude Opus 5.5 - I reviewed it and tested it to the best of my knowledge.

Fixes #26.

Importing go-winfsp in a 32-bit Windows program (GOARCH=386) panics during package initialization, before main() runs and even if nothing is mounted:

panic: compileCallback: argument size is larger than uintptr

dll_windows.go already picks winfsp-x86.dll on 386, so 32-bit was meant to be supported. Two problems stood in the way.

1. Callbacks with 64-bit arguments

On 386, syscall.NewCallbackCDecl refuses arguments wider than uintptr. Seven callbacks take a UINT64 by value:

Callback 64-bit argument
Create, CreateEx, Overwrite AllocationSize
Read, Write Offset
SetFileSize NewSize
SetBasicInfo 4 timestamps

Because the callbacks are package-level variables, the panic happens at init.

x86 cdecl passes a 64-bit argument as two consecutive 32-bit stack slots, low word first. The seven callback definitions move unchanged into filesystem_windows_64bits.go (//go:build windows && (amd64 || arm64)). A new filesystem_windows_386.go receives each 64-bit argument as lo, hi uint32 and recombines them before calling the existing delegateX handlers, the same approach cgofuse takes in host_nocgo_windows.go. The build tag picks the right file at compile time, so 64-bit builds compile the same code as before.

2. FSP_FSCTL_DIR_INFO layout

Go aligns 64-bit fields to 4 bytes on 386, but MSVC aligns them to 8. The 64-bit fields in FSP_FSCTL_DIR_INFO inherit that difference:

FileInfo offset NextOffset offset size
C (fsctl.h, all archs) 8 80 104
Go 386, before 4 76 100

This garbled every directory entry. TestMount on 386 listed g-size-123 instead of reg-size-123, because WinFsp read the name 4 bytes (2 UTF-16 chars) past where Go wrote it.

The fix adds explicit _ [6]byte padding after Size. amd64/arm64 already had this implicit padding, so their layout is unchanged. FileSystemAddDirInfo took its alignment from unsafe.Alignof(FSP_FSCTL_DIR_INFO{}), which is 4 on 386. It now uses FSP_FSCTL_DEFAULT_ALIGNMENT (8), as the C FspFileSystemAddDirInfo does.

The other mirrored structs either contain no 64-bit fields or place them at 8-byte-aligned offsets, so they already match on x86. This covers FILE_INFO, VOLUME_PARAMS_V0/V1, OPEN_FILE_INFO, NOTIFY_INFO, FSP_FILE_SYSTEM and FSP_FILE_SYSTEM_INTERFACE.

Tests and CI

  • New TestStructLayout checks sizes and key offsets of the mirrored structs against the static asserts in fsctl.h. With the old DIR_INFO definition it fails on 386 (sizeof = 100, want 104).
  • The workflow gets one extra step that runs go test -v ./... with GOARCH=386. The WinFsp installer ships winfsp-x86.dll, and 32-bit test binaries run under WOW64 on windows-latest.

Testing done

On Windows 11 x64 with Go 1.27.1 and WinFsp installed:

  • go test ./... passes on amd64 and on 386, including TestMount, which mounts T:.

  • I mounted the 32-bit passthrough example and tested:

    • resizing a file to 5 GiB + 7 bytes
    • writing and reading 16 bytes at offset 5 GiB − 16

    Size and data matched the backing file, which covers the high word of the SetFileSize, Write and Read arguments.

  • A small probe file system that logs SetBasicInfo arguments received identical creation, access and write times on 386 and amd64.

  • Create, append, rename and directory listing behave the same on the 32-bit and 64-bit builds.

Not included

  • FSP_FSCTL_STREAM_INFO's Go mirror doesn't match C on any arch: it has a pointer where C has a flexible array. It's unused, so I left it alone.
  • FSP_FSCTL_VOLUME_INFO is 84 bytes on 386 versus 88 in C, but only in trailing padding. Its size isn't used on the Go side.

No public API changes.

nscheer and others added 4 commits September 27, 2026 17:15
syscall.NewCallbackCDecl panics on 386 when a callback argument is
wider than uintptr. Since the callbacks are created in package-level
variables, any 32-bit program importing go-winfsp crashed at init.

Move the seven callbacks taking UINT64 by value (Create, CreateEx,
Overwrite, Read, Write, SetBasicInfo, SetFileSize) into a !386 file
unchanged, and add 386 variants that receive each 64-bit argument as
its two 32-bit cdecl stack slots and recombine them.

Refs winfsp#26

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Go aligns 64-bit fields to 4 bytes on 386, while MSVC aligns them to
8, so FileInfo landed at offset 4 instead of 8 and the struct was 100
instead of 104 bytes, garbling every directory entry. Add explicit
padding after Size; the layout on 64-bit targets is unchanged.

FileSystemAddDirInfo also derived its alignment from Go's Alignof,
which is 4 on 386; use FSP_FSCTL_DEFAULT_ALIGNMENT (8) like the C
FspFileSystemAddDirInfo.

Refs winfsp#26

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Check the sizes and key offsets of the mirrored structs against the
static asserts in winfsp/fsctl.h, so a layout mismatch on any
architecture is caught without mounting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The WinFsp installer ships winfsp-x86.dll, and 32-bit test binaries
run under WOW64 on windows-latest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aegistudio

Copy link
Copy Markdown
Collaborator

Thank you for putting up this PR, I will review when I have spare time.

I would also like to see if the examples run on 386 (WOW on Windows 64-bit is okay), it would be better if you have tested them.

@nscheer

nscheer commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

The passthrough example was already tested, see above description.
Happy to make more tests explicitly.

@nscheer

nscheer commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

Here are two additional manual test cases for the examples:

Setup

PS C:\Users\Nico\AppData\Local\Temp> cd D:\work\mount\go-winfsp
PS D:\work\mount\go-winfsp> git switch add-32bit-support
Already on 'add-32bit-support'
Your branch is up to date with 'origin/add-32bit-support'.
PS D:\work\mount\go-winfsp> $env:GOARCH = "386"
PS D:\work\mount\go-winfsp> go build -o "$env:TEMP\memfs386.exe" ./examples/memfs/cmd
PS D:\work\mount\go-winfsp> go build -o "$env:TEMP\passthrough386.exe" ./examples/passthrough/cmd
PS D:\work\mount\go-winfsp> Remove-Item Env:GOARCH
PS D:\work\mount\go-winfsp> go version -m "$env:TEMP\memfs386.exe" | Select-String GOARCH   # expect GOARCH=386

        build   GOARCH=386
PS C:\Users\Nico> go version -m "$env:TEMP\passthrough386.exe" | Select-String GOARCH   # expect GOARCH=386

        build   GOARCH=386

Running examples

memfs

First terminal:

PS D:\work\mount\go-winfsp> & "$env:TEMP\memfs386.exe" -m M:

Second terminal:

PS C:\Users\Nico> mkdir M:\dir

    Directory: M:\


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        27.09.2026     18:22                dir


PS C:\Users\Nico> [IO.File]::WriteAllText('M:\dir\hello.txt', 'Hello from 32-bit')
PS C:\Users\Nico> [IO.File]::AppendAllText('M:\dir\hello.txt', ' - appended')
PS C:\Users\Nico> [IO.File]::ReadAllText('M:\dir\hello.txt')
Hello from 32-bit - appended
PS C:\Users\Nico> Rename-Item M:\dir\hello.txt greeting.txt
PS C:\Users\Nico> Get-ChildItem M:\dir | Format-Table Name, Length, LastWriteTime

Name         Length LastWriteTime
----         ------ -------------
greeting.txt     28 27.09.2026 18:22:01

passthrough

First terminal:

PS D:\work\mount\go-winfsp> mkdir $env:TEMP\pt-src

    Directory: C:\Users\Nico\AppData\Local\Temp

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          27.09.2026    18:27                pt-src

PS D:\work\mount\go-winfsp> & "$env:TEMP\passthrough386.exe" -m P: $env:TEMP\pt-sr

Second terminal:

PS C:\Users\Nico> $fs = [IO.File]::Create('P:\big.bin'); $fs.Close()
PS C:\Users\Nico> fsutil sparse setflag $env:TEMP\pt-src\big.bin        # set on the backing file, so no 5 GB gets written
PS C:\Users\Nico> $fs = [IO.File]::Open('P:\big.bin', 'Open', 'ReadWrite')
PS C:\Users\Nico> $fs.SetLength(5GB + 7)
PS C:\Users\Nico> $fs.Position = 5GB - 16; $b = [Text.Encoding]::ASCII.GetBytes('HIGH-OFFSET-DATA'); $fs.Write($b, 0, 16)
PS C:\Users\Nico> $fs.Close()
PS C:\Users\Nico> (Get-Item P:\big.bin).Length                           # 5368709127
5368709127
PS C:\Users\Nico> $fs = [IO.File]::OpenRead('P:\big.bin'); $fs.Position = 5GB - 16
PS C:\Users\Nico> $r = New-Object byte[] 16; [void]$fs.Read($r, 0, 16); $fs.Close()
PS C:\Users\Nico> [Text.Encoding]::ASCII.GetString($r)                   # HIGH-OFFSET-DATA
HIGH-OFFSET-DATA

@aegistudio

aegistudio commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Okay, given that the whole PR is ready, and most of the PR looks fine to me, it just need some minor changes:

  1. Some file names like filesystem_callback_windows_386.go look verbose to me. A cleaner way to split filesystem_windows.go is like:
    • filesystem_windows.go: The file that would compile on every windows platform, regardless of architecture.
    • filesystem_windows_386.go: The file that would only compile on windows / 386.
    • filesystem_windows_64bits.go: The file that would compile on windows / amd64 and windows / arm64.
    • You can see the similar way of splitting so in https://github.com/golang/sys/blob/master/unix/, where they split into syscall_linux.go, syscall_linux_386.go and so on.
  2. Avoid overly detailed comments and explanation. LLMs have a quirk to do so, but this may make human developers also write verbose comment in order to keep the same coding style, which is not I want. (The opening comments in filesystem_callbacks_windows_386.go and filesystem_callbacks_windows.go are overly detailed to me.).

@nscheer

nscheer commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Fair points - I’ll adjust the PR.

@nscheer

nscheer commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Adjusted the structure as per your comment. Also ran the manual tests again - everything worked as expected.

@aegistudio

Copy link
Copy Markdown
Collaborator

LGTM

@aegistudio
aegistudio merged commit 1014788 into winfsp:master Oct 3, 2026
1 check passed
@aegistudio

Copy link
Copy Markdown
Collaborator

Merged and tagged v1.0.5.

@nscheer
nscheer deleted the add-32bit-support branch October 3, 2026 14:55
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.

32-bit (windows/386) programs crash at startup when importing go-winfsp

2 participants