From b1439209b2b1df8bc42606ac686d053896b160e2 Mon Sep 17 00:00:00 2001 From: Nicolai Scheer Date: Sun, 27 Sep 2026 17:15:55 +0200 Subject: [PATCH 1/5] fix(winfsp): pass 64-bit callback arguments as two words on 386 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 #26 Co-Authored-By: Claude Opus 5.5 --- filesystem_callbacks_windows.go | 99 +++++++++++++++++++++++++ filesystem_callbacks_windows_386.go | 110 ++++++++++++++++++++++++++++ filesystem_windows.go | 91 ----------------------- 3 files changed, 209 insertions(+), 91 deletions(-) create mode 100644 filesystem_callbacks_windows.go create mode 100644 filesystem_callbacks_windows_386.go diff --git a/filesystem_callbacks_windows.go b/filesystem_callbacks_windows.go new file mode 100644 index 0000000..01b49ef --- /dev/null +++ b/filesystem_callbacks_windows.go @@ -0,0 +1,99 @@ +//go:build !386 + +package winfsp + +import "syscall" + +// Callbacks taking 64-bit arguments by value. See +// filesystem_callbacks_windows_386.go for the 32-bit variants. + +var go_delegateCreate = syscall.NewCallbackCDecl(func( + fileSystem, fileName uintptr, + createOptions, grantedAccess, fileAttributes uint32, + securityDescriptor uintptr, allocationSize uint64, + file *uintptr, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateCreate( + fileSystem, fileName, + createOptions, grantedAccess, fileAttributes, + securityDescriptor, allocationSize, + file, fileInfoAddr, + )) +}) + +var go_delegateOverwrite = syscall.NewCallbackCDecl(func( + fileSystem, file uintptr, + attributes uint32, replaceAttributes uint8, + allocationSize uint64, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateOverwrite( + fileSystem, file, + attributes, replaceAttributes, + allocationSize, fileInfoAddr, + )) +}) + +var go_delegateRead = syscall.NewCallbackCDecl(func( + fileSystem, fileContext, buffer uintptr, + offset uint64, length uint32, bytesRead *uint32, +) uintptr { + return uintptr(delegateRead( + fileSystem, fileContext, buffer, + offset, length, bytesRead, + )) +}) + +var go_delegateWrite = syscall.NewCallbackCDecl(func( + fileSystem, fileContext, buffer uintptr, + offset uint64, length uint32, + writeToEndOfFile, constrainedIo uint8, + bytesWritten *uint32, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateWrite( + fileSystem, fileContext, buffer, + offset, length, + writeToEndOfFile, constrainedIo, + bytesWritten, fileInfoAddr, + )) +}) + +var go_delegateSetBasicInfo = syscall.NewCallbackCDecl(func( + fileSystem, fileContext uintptr, + attributes uint32, + creationTime, lastAccessTime, lastWriteTime, changeTime uint64, + fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateSetBasicInfo( + fileSystem, fileContext, attributes, + creationTime, lastAccessTime, lastWriteTime, changeTime, + fileInfoAddr, + )) +}) + +var go_delegateSetFileSize = syscall.NewCallbackCDecl(func( + fileSystem, fileContext uintptr, + newSize uint64, setAllocationSize uint8, + fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateSetFileSize( + fileSystem, fileContext, + newSize, setAllocationSize, + fileInfoAddr, + )) +}) + +var go_delegateCreateEx = syscall.NewCallbackCDecl(func( + fileSystem, fileName uintptr, + createOptions, grantedAccess, fileAttributes uint32, + securityDescriptor uintptr, allocationSize uint64, + extraBuffer uintptr, extraLength uint32, isReparse uint8, + file *uintptr, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateCreateEx( + fileSystem, fileName, + createOptions, grantedAccess, fileAttributes, + securityDescriptor, allocationSize, + extraBuffer, extraLength, isReparse, + file, fileInfoAddr, + )) +}) diff --git a/filesystem_callbacks_windows_386.go b/filesystem_callbacks_windows_386.go new file mode 100644 index 0000000..63e8e01 --- /dev/null +++ b/filesystem_callbacks_windows_386.go @@ -0,0 +1,110 @@ +package winfsp + +import "syscall" + +// Callbacks taking 64-bit arguments by value. The x86 cdecl +// calling convention passes such an argument in two consecutive +// 32-bit stack slots, low word first, while syscall.NewCallbackCDecl +// only accepts arguments up to the size of uintptr. So each 64-bit +// argument is received as two uint32 halves and recombined here. + +func makeUint64(lo, hi uint32) uint64 { + return uint64(hi)<<32 | uint64(lo) +} + +var go_delegateCreate = syscall.NewCallbackCDecl(func( + fileSystem, fileName uintptr, + createOptions, grantedAccess, fileAttributes uint32, + securityDescriptor uintptr, allocationSizeLo, allocationSizeHi uint32, + file *uintptr, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateCreate( + fileSystem, fileName, + createOptions, grantedAccess, fileAttributes, + securityDescriptor, makeUint64(allocationSizeLo, allocationSizeHi), + file, fileInfoAddr, + )) +}) + +var go_delegateOverwrite = syscall.NewCallbackCDecl(func( + fileSystem, file uintptr, + attributes uint32, replaceAttributes uint8, + allocationSizeLo, allocationSizeHi uint32, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateOverwrite( + fileSystem, file, + attributes, replaceAttributes, + makeUint64(allocationSizeLo, allocationSizeHi), fileInfoAddr, + )) +}) + +var go_delegateRead = syscall.NewCallbackCDecl(func( + fileSystem, fileContext, buffer uintptr, + offsetLo, offsetHi uint32, length uint32, bytesRead *uint32, +) uintptr { + return uintptr(delegateRead( + fileSystem, fileContext, buffer, + makeUint64(offsetLo, offsetHi), length, bytesRead, + )) +}) + +var go_delegateWrite = syscall.NewCallbackCDecl(func( + fileSystem, fileContext, buffer uintptr, + offsetLo, offsetHi uint32, length uint32, + writeToEndOfFile, constrainedIo uint8, + bytesWritten *uint32, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateWrite( + fileSystem, fileContext, buffer, + makeUint64(offsetLo, offsetHi), length, + writeToEndOfFile, constrainedIo, + bytesWritten, fileInfoAddr, + )) +}) + +var go_delegateSetBasicInfo = syscall.NewCallbackCDecl(func( + fileSystem, fileContext uintptr, + attributes uint32, + creationTimeLo, creationTimeHi uint32, + lastAccessTimeLo, lastAccessTimeHi uint32, + lastWriteTimeLo, lastWriteTimeHi uint32, + changeTimeLo, changeTimeHi uint32, + fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateSetBasicInfo( + fileSystem, fileContext, attributes, + makeUint64(creationTimeLo, creationTimeHi), + makeUint64(lastAccessTimeLo, lastAccessTimeHi), + makeUint64(lastWriteTimeLo, lastWriteTimeHi), + makeUint64(changeTimeLo, changeTimeHi), + fileInfoAddr, + )) +}) + +var go_delegateSetFileSize = syscall.NewCallbackCDecl(func( + fileSystem, fileContext uintptr, + newSizeLo, newSizeHi uint32, setAllocationSize uint8, + fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateSetFileSize( + fileSystem, fileContext, + makeUint64(newSizeLo, newSizeHi), setAllocationSize, + fileInfoAddr, + )) +}) + +var go_delegateCreateEx = syscall.NewCallbackCDecl(func( + fileSystem, fileName uintptr, + createOptions, grantedAccess, fileAttributes uint32, + securityDescriptor uintptr, allocationSizeLo, allocationSizeHi uint32, + extraBuffer uintptr, extraLength uint32, isReparse uint8, + file *uintptr, fileInfoAddr uintptr, +) uintptr { + return uintptr(delegateCreateEx( + fileSystem, fileName, + createOptions, grantedAccess, fileAttributes, + securityDescriptor, makeUint64(allocationSizeLo, allocationSizeHi), + extraBuffer, extraLength, isReparse, + file, fileInfoAddr, + )) +}) diff --git a/filesystem_windows.go b/filesystem_windows.go index 5e771ca..84bffd2 100644 --- a/filesystem_windows.go +++ b/filesystem_windows.go @@ -366,20 +366,6 @@ func delegateCreate( return windows.STATUS_SUCCESS } -var go_delegateCreate = syscall.NewCallbackCDecl(func( - fileSystem, fileName uintptr, - createOptions, grantedAccess, fileAttributes uint32, - securityDescriptor uintptr, allocationSize uint64, - file *uintptr, fileInfoAddr uintptr, -) uintptr { - return uintptr(delegateCreate( - fileSystem, fileName, - createOptions, grantedAccess, fileAttributes, - securityDescriptor, allocationSize, - file, fileInfoAddr, - )) -}) - // BehaviourOverwrite overwrites a file's attribute. type BehaviourOverwrite interface { Overwrite( @@ -406,18 +392,6 @@ func delegateOverwrite( )) } -var go_delegateOverwrite = syscall.NewCallbackCDecl(func( - fileSystem, file uintptr, - attributes uint32, replaceAttributes uint8, - allocationSize uint64, fileInfoAddr uintptr, -) uintptr { - return uintptr(delegateOverwrite( - fileSystem, file, - attributes, replaceAttributes, - allocationSize, fileInfoAddr, - )) -}) - // BehaviourCleanup performs the cleanup behaviour. type BehaviourCleanup interface { Cleanup( @@ -479,16 +453,6 @@ func delegateRead( return convertNTStatus(err) } -var go_delegateRead = syscall.NewCallbackCDecl(func( - fileSystem, fileContext, buffer uintptr, - offset uint64, length uint32, bytesRead *uint32, -) uintptr { - return uintptr(delegateRead( - fileSystem, fileContext, buffer, - offset, length, bytesRead, - )) -}) - // BehaviourWrite writes an open file. type BehaviourWrite interface { Write( @@ -520,20 +484,6 @@ func delegateWrite( return convertNTStatus(err) } -var go_delegateWrite = syscall.NewCallbackCDecl(func( - fileSystem, fileContext, buffer uintptr, - offset uint64, length uint32, - writeToEndOfFile, constrainedIo uint8, - bytesWritten *uint32, fileInfoAddr uintptr, -) uintptr { - return uintptr(delegateWrite( - fileSystem, fileContext, buffer, - offset, length, - writeToEndOfFile, constrainedIo, - bytesWritten, fileInfoAddr, - )) -}) - // BehaviourFlush flushes a file or volume. // // When file is not NULL, the specific file will be flushed, @@ -650,19 +600,6 @@ func delegateSetBasicInfo( )) } -var go_delegateSetBasicInfo = syscall.NewCallbackCDecl(func( - fileSystem, fileContext uintptr, - attributes uint32, - creationTime, lastAccessTime, lastWriteTime, changeTime uint64, - fileInfoAddr uintptr, -) uintptr { - return uintptr(delegateSetBasicInfo( - fileSystem, fileContext, attributes, - creationTime, lastAccessTime, lastWriteTime, changeTime, - fileInfoAddr, - )) -}) - // BehaviourSetFileSize sets file's size or allocation size. type BehaviourSetFileSize interface { SetFileSize( @@ -687,18 +624,6 @@ func delegateSetFileSize( )) } -var go_delegateSetFileSize = syscall.NewCallbackCDecl(func( - fileSystem, fileContext uintptr, - newSize uint64, setAllocationSize uint8, - fileInfoAddr uintptr, -) uintptr { - return uintptr(delegateSetFileSize( - fileSystem, fileContext, - newSize, setAllocationSize, - fileInfoAddr, - )) -}) - // BehaviourCanDelete detects whether the file can be deleted. type BehaviourCanDelete interface { CanDelete( @@ -1236,22 +1161,6 @@ func delegateCreateEx( return windows.STATUS_SUCCESS } -var go_delegateCreateEx = syscall.NewCallbackCDecl(func( - fileSystem, fileName uintptr, - createOptions, grantedAccess, fileAttributes uint32, - securityDescriptor uintptr, allocationSize uint64, - extraBuffer uintptr, extraLength uint32, isReparse uint8, - file *uintptr, fileInfoAddr uintptr, -) uintptr { - return uintptr(delegateCreateEx( - fileSystem, fileName, - createOptions, grantedAccess, fileAttributes, - securityDescriptor, allocationSize, - extraBuffer, extraLength, isReparse, - file, fileInfoAddr, - )) -}) - var ( fileSystemResolveReparsePoints dllProc ) From b69f3dc388f7735aec31195e1169f1abb3065876 Mon Sep 17 00:00:00 2001 From: Nicolai Scheer Date: Sun, 27 Sep 2026 17:16:17 +0200 Subject: [PATCH 2/5] fix(fsctl): align FSP_FSCTL_DIR_INFO like MSVC on 386 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 #26 Co-Authored-By: Claude Opus 5.5 --- fsctl_windows.go | 1 + gohelper_windows.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/fsctl_windows.go b/fsctl_windows.go index ca32502..7bb197b 100644 --- a/fsctl_windows.go +++ b/fsctl_windows.go @@ -159,6 +159,7 @@ type FSP_FSCTL_OPEN_FILE_INFO struct { type FSP_FSCTL_DIR_INFO struct { Size uint16 + _ [6]byte // FileInfo is 8-byte aligned in C, also on 386 FileInfo FSP_FSCTL_FILE_INFO NextOffset uint64 Padding0 uint64 diff --git a/gohelper_windows.go b/gohelper_windows.go index d963a72..a88e1db 100644 --- a/gohelper_windows.go +++ b/gohelper_windows.go @@ -12,7 +12,7 @@ import ( const () const ( - dirInfoAlignment uint16 = uint16(unsafe.Alignof(FSP_FSCTL_DIR_INFO{})) + dirInfoAlignment uint16 = 8 // FSP_FSCTL_DEFAULT_ALIGNMENT replacementChar = '\uFFFD' // Unicode replacement character ) From 183f08af88a2ad70f654733d9725cd9e67f3bf6e Mon Sep 17 00:00:00 2001 From: Nicolai Scheer Date: Sun, 27 Sep 2026 17:16:53 +0200 Subject: [PATCH 3/5] test(fsctl): assert struct layouts match fsctl.h 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 --- fsctl_windows_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 fsctl_windows_test.go diff --git a/fsctl_windows_test.go b/fsctl_windows_test.go new file mode 100644 index 0000000..710e63d --- /dev/null +++ b/fsctl_windows_test.go @@ -0,0 +1,39 @@ +//go:build windows + +package winfsp_test + +import ( + "testing" + "unsafe" + + "github.com/winfsp/go-winfsp" +) + +// TestStructLayout checks the mirrored structs against the +// layout asserted in winfsp/fsctl.h, which must hold on every +// architecture, including 386. +func TestStructLayout(t *testing.T) { + var ( + fileInfo winfsp.FSP_FSCTL_FILE_INFO + dirInfo winfsp.FSP_FSCTL_DIR_INFO + paramsV0 winfsp.FSP_FSCTL_VOLUME_PARAMS_V0 + paramsV1 winfsp.FSP_FSCTL_VOLUME_PARAMS_V1 + ) + for _, tc := range []struct { + name string + got, want uintptr + }{ + {"sizeof(FSP_FSCTL_FILE_INFO)", unsafe.Sizeof(fileInfo), 72}, + {"sizeof(FSP_FSCTL_DIR_INFO)", unsafe.Sizeof(dirInfo), 104}, + {"offsetof(FSP_FSCTL_DIR_INFO.FileInfo)", unsafe.Offsetof(dirInfo.FileInfo), 8}, + {"offsetof(FSP_FSCTL_DIR_INFO.NextOffset)", unsafe.Offsetof(dirInfo.NextOffset), 80}, + {"sizeof(FSP_FSCTL_VOLUME_PARAMS_V0)", unsafe.Sizeof(paramsV0), 456}, + {"sizeof(FSP_FSCTL_VOLUME_PARAMS_V1)", unsafe.Sizeof(paramsV1), 504}, + {"offsetof(FSP_FSCTL_VOLUME_PARAMS_V1.VolumeCreationTime)", unsafe.Offsetof(paramsV1.VolumeCreationTime), 8}, + {"offsetof(FSP_FSCTL_VOLUME_PARAMS_V1.Reserved64)", unsafe.Offsetof(paramsV1.Reserved64), 488}, + } { + if tc.got != tc.want { + t.Errorf("%s = %d, want %d", tc.name, tc.got, tc.want) + } + } +} From 812e26d59e018bc3ecd67ba569f20baad3a09e63 Mon Sep 17 00:00:00 2001 From: Nicolai Scheer Date: Sun, 27 Sep 2026 17:16:53 +0200 Subject: [PATCH 4/5] ci: run tests as windows/386 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 --- .github/workflows/test.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 404c3b9..8b10962 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,12 @@ jobs: run: | go test -coverprofile="coverage.out" -v ./... + - name: "run tests (386)" + env: + GOARCH: "386" + run: | + go test -v ./... + - name: "generate coverage report" run: | go tool cover -html="coverage.out" -o "coverage.html" From 32b594c9e0d4ec0ef75d588c530f5e4033bfdaff Mon Sep 17 00:00:00 2001 From: Nicolai Scheer Date: Fri, 2 Oct 2026 19:04:48 +0200 Subject: [PATCH 5/5] refactor(winfsp): rename per-arch callback files and trim comments --- ...m_callbacks_windows_386.go => filesystem_windows_386.go | 7 +------ ...em_callbacks_windows.go => filesystem_windows_64bits.go | 5 +---- fsctl_windows_test.go | 4 +--- 3 files changed, 3 insertions(+), 13 deletions(-) rename filesystem_callbacks_windows_386.go => filesystem_windows_386.go (90%) rename filesystem_callbacks_windows.go => filesystem_windows_64bits.go (95%) diff --git a/filesystem_callbacks_windows_386.go b/filesystem_windows_386.go similarity index 90% rename from filesystem_callbacks_windows_386.go rename to filesystem_windows_386.go index 63e8e01..3af2651 100644 --- a/filesystem_callbacks_windows_386.go +++ b/filesystem_windows_386.go @@ -2,12 +2,7 @@ package winfsp import "syscall" -// Callbacks taking 64-bit arguments by value. The x86 cdecl -// calling convention passes such an argument in two consecutive -// 32-bit stack slots, low word first, while syscall.NewCallbackCDecl -// only accepts arguments up to the size of uintptr. So each 64-bit -// argument is received as two uint32 halves and recombined here. - +// makeUint64 joins a 64-bit argument passed as two 32-bit halves. func makeUint64(lo, hi uint32) uint64 { return uint64(hi)<<32 | uint64(lo) } diff --git a/filesystem_callbacks_windows.go b/filesystem_windows_64bits.go similarity index 95% rename from filesystem_callbacks_windows.go rename to filesystem_windows_64bits.go index 01b49ef..db64fe3 100644 --- a/filesystem_callbacks_windows.go +++ b/filesystem_windows_64bits.go @@ -1,12 +1,9 @@ -//go:build !386 +//go:build windows && (amd64 || arm64) package winfsp import "syscall" -// Callbacks taking 64-bit arguments by value. See -// filesystem_callbacks_windows_386.go for the 32-bit variants. - var go_delegateCreate = syscall.NewCallbackCDecl(func( fileSystem, fileName uintptr, createOptions, grantedAccess, fileAttributes uint32, diff --git a/fsctl_windows_test.go b/fsctl_windows_test.go index 710e63d..21aacd0 100644 --- a/fsctl_windows_test.go +++ b/fsctl_windows_test.go @@ -9,9 +9,7 @@ import ( "github.com/winfsp/go-winfsp" ) -// TestStructLayout checks the mirrored structs against the -// layout asserted in winfsp/fsctl.h, which must hold on every -// architecture, including 386. +// TestStructLayout checks struct layouts against winfsp/fsctl.h. func TestStructLayout(t *testing.T) { var ( fileInfo winfsp.FSP_FSCTL_FILE_INFO