Skip to content

Consider leveraging structs.HostLayout #1876

@braydonk

Description

@braydonk

Describe the bug
Go does not guarantee structs will be laid out in memory as they are defined. As a result, a large number of spots in this codebase are relying on undefined behaviour by directly casting bytes from unsafe.Pointer on C byte buffers directly to Go structs. See example in reproduction section.

Starting in Go 1.23, the intended way to handle this is: https://pkg.go.dev/structs#HostLayout

This field can be used as an "annotation" of sorts to force Go to pack the struct as the platform's C ABI dictates. This ensures that if Go decides to alter how they lay out structs in future versions, this codebase won't break in undefined ways.

To Reproduce

peb := (*processEnvironmentBlock64)(unsafe.Pointer(&buf[0]))

Expected behavior
Everything works as expected now, but it is mostly by luck and could change with any future Go version.

Environment (please complete the following information):
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions