| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build ignore |
| 6 |
|
| 7 |
// mkpost processes the output of cgo -godefs to |
| 8 |
// modify the generated types. It is used to clean up |
| 9 |
// the sys API in an architecture specific manner. |
| 10 |
// |
| 11 |
// mkpost is run after cgo -godefs; see README.md. |
| 12 |
package main |
| 13 |
|
| 14 |
import ( |
| 15 |
"bytes" |
| 16 |
"fmt" |
| 17 |
"go/format" |
| 18 |
"io" |
| 19 |
"log" |
| 20 |
"os" |
| 21 |
"regexp" |
| 22 |
) |
| 23 |
|
| 24 |
func main() { |
| 25 |
// Get the OS and architecture (using GOARCH_TARGET if it exists) |
| 26 |
goos := os.Getenv("GOOS_TARGET") |
| 27 |
if goos == "" { |
| 28 |
goos = os.Getenv("GOOS") |
| 29 |
} |
| 30 |
goarch := os.Getenv("GOARCH_TARGET") |
| 31 |
if goarch == "" { |
| 32 |
goarch = os.Getenv("GOARCH") |
| 33 |
} |
| 34 |
// Check that we are using the Docker-based build system if we should be. |
| 35 |
if goos == "linux" { |
| 36 |
if os.Getenv("GOLANG_SYS_BUILD") != "docker" { |
| 37 |
os.Stderr.WriteString("In the Docker-based build system, mkpost should not be called directly.\n") |
| 38 |
os.Stderr.WriteString("See README.md\n") |
| 39 |
os.Exit(1) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
b, err := io.ReadAll(os.Stdin) |
| 44 |
if err != nil { |
| 45 |
log.Fatal(err) |
| 46 |
} |
| 47 |
|
| 48 |
if goos == "aix" { |
| 49 |
// Replace type of Atim, Mtim and Ctim by Timespec in Stat_t |
| 50 |
// to avoid having both StTimespec and Timespec. |
| 51 |
sttimespec := regexp.MustCompile(`_Ctype_struct_st_timespec`) |
| 52 |
b = sttimespec.ReplaceAll(b, []byte("Timespec")) |
| 53 |
} |
| 54 |
|
| 55 |
if goos == "darwin" { |
| 56 |
// KinfoProc contains various pointers to objects stored |
| 57 |
// in kernel space. Replace these by uintptr to prevent |
| 58 |
// accidental dereferencing. |
| 59 |
kinfoProcPointerRegex := regexp.MustCompile(`\*_Ctype_struct_(pgrp|proc|session|sigacts|ucred|user|vnode)`) |
| 60 |
b = kinfoProcPointerRegex.ReplaceAll(b, []byte("uintptr")) |
| 61 |
|
| 62 |
// ExternProc contains a p_un member that in kernel |
| 63 |
// space stores a pair of pointers and in user space |
| 64 |
// stores the process creation time. We only care about |
| 65 |
// the process creation time. |
| 66 |
externProcStarttimeRegex := regexp.MustCompile(`P_un\s*\[\d+\]byte`) |
| 67 |
b = externProcStarttimeRegex.ReplaceAll(b, []byte("P_starttime Timeval")) |
| 68 |
|
| 69 |
// Convert [n]int8 to [n]byte in Eproc and ExternProc members to |
| 70 |
// simplify conversion to string. |
| 71 |
convertEprocRegex := regexp.MustCompile(`(P_comm|Wmesg|Login)(\s+)\[(\d+)\]int8`) |
| 72 |
b = convertEprocRegex.ReplaceAll(b, []byte("$1$2[$3]byte")) |
| 73 |
} |
| 74 |
|
| 75 |
if goos == "freebsd" { |
| 76 |
// Inside PtraceLwpInfoStruct replace __Siginfo with __PtraceSiginfo, |
| 77 |
// Create __PtraceSiginfo as a copy of __Siginfo where every *byte instance is replaced by uintptr |
| 78 |
ptraceLwpInfoStruct := regexp.MustCompile(`(?s:type PtraceLwpInfoStruct struct \{.*?\})`) |
| 79 |
b = ptraceLwpInfoStruct.ReplaceAllFunc(b, func(in []byte) []byte { |
| 80 |
return bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo")) |
| 81 |
}) |
| 82 |
|
| 83 |
siginfoStruct := regexp.MustCompile(`(?s:type __Siginfo struct \{.*?\})`) |
| 84 |
b = siginfoStruct.ReplaceAllFunc(b, func(in []byte) []byte { |
| 85 |
out := append([]byte{}, in...) |
| 86 |
out = append(out, '\n', '\n') |
| 87 |
out = append(out, |
| 88 |
bytes.ReplaceAll( |
| 89 |
bytes.ReplaceAll(in, []byte("__Siginfo"), []byte("__PtraceSiginfo")), |
| 90 |
[]byte("*byte"), []byte("uintptr"))...) |
| 91 |
return out |
| 92 |
}) |
| 93 |
|
| 94 |
// Inside PtraceIoDesc replace the Offs field, which refers to an address |
| 95 |
// in the child process (not the Go parent), with a uintptr. |
| 96 |
ptraceIoDescStruct := regexp.MustCompile(`(?s:type PtraceIoDesc struct \{.*?\})`) |
| 97 |
addrField := regexp.MustCompile(`(\bOffs\s+)\*byte`) |
| 98 |
b = ptraceIoDescStruct.ReplaceAllFunc(b, func(in []byte) []byte { |
| 99 |
return addrField.ReplaceAll(in, []byte(`${1}uintptr`)) |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
if goos == "solaris" { |
| 104 |
// Convert *int8 to *byte in Iovec.Base like on every other platform. |
| 105 |
convertIovecBase := regexp.MustCompile(`Base\s+\*int8`) |
| 106 |
iovecType := regexp.MustCompile(`type Iovec struct {[^}]*}`) |
| 107 |
iovecStructs := iovecType.FindAll(b, -1) |
| 108 |
for _, s := range iovecStructs { |
| 109 |
newNames := convertIovecBase.ReplaceAll(s, []byte("Base *byte")) |
| 110 |
b = bytes.Replace(b, s, newNames, 1) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if goos == "linux" && goarch != "riscv64" { |
| 115 |
// The RISCV_HWPROBE_ constants are only defined on Linux for riscv64 |
| 116 |
hwprobeConstRexexp := regexp.MustCompile(`const\s+\(\s+RISCV_HWPROBE_[^\)]+\)`) |
| 117 |
b = hwprobeConstRexexp.ReplaceAll(b, nil) |
| 118 |
} |
| 119 |
|
| 120 |
// Intentionally export __val fields in Fsid and Sigset_t |
| 121 |
valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__(bits|val)(\s+\S+\s+)}`) |
| 122 |
b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$4}")) |
| 123 |
|
| 124 |
// Intentionally export __fds_bits field in FdSet |
| 125 |
fdSetRegex := regexp.MustCompile(`type (FdSet) struct {(\s+)X__fds_bits(\s+\S+\s+)}`) |
| 126 |
b = fdSetRegex.ReplaceAll(b, []byte("type $1 struct {${2}Bits$3}")) |
| 127 |
|
| 128 |
// Intentionally export __icmp6_filt field in icmpv6_filter |
| 129 |
icmpV6Regex := regexp.MustCompile(`type (ICMPv6Filter) struct {(\s+)X__icmp6_filt(\s+\S+\s+)}`) |
| 130 |
b = icmpV6Regex.ReplaceAll(b, []byte("type $1 struct {${2}Filt$3}")) |
| 131 |
|
| 132 |
// Intentionally export address storage field in SockaddrStorage convert it to [N]byte. |
| 133 |
convertSockaddrStorageData := regexp.MustCompile(`(X__ss_padding)\s+\[(\d+)\]u?int8`) |
| 134 |
sockaddrStorageType := regexp.MustCompile(`type SockaddrStorage struct {[^}]*}`) |
| 135 |
sockaddrStorageStructs := sockaddrStorageType.FindAll(b, -1) |
| 136 |
for _, s := range sockaddrStorageStructs { |
| 137 |
newNames := convertSockaddrStorageData.ReplaceAll(s, []byte("Data [$2]byte")) |
| 138 |
b = bytes.Replace(b, s, newNames, 1) |
| 139 |
} |
| 140 |
|
| 141 |
// If we have empty Ptrace structs, we should delete them. Only s390x emits |
| 142 |
// nonempty Ptrace structs. |
| 143 |
ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`) |
| 144 |
b = ptraceRexexp.ReplaceAll(b, nil) |
| 145 |
|
| 146 |
// If we have an empty RISCVHWProbePairs struct, we should delete it. Only riscv64 emits |
| 147 |
// nonempty RISCVHWProbePairs structs. |
| 148 |
hwprobeRexexp := regexp.MustCompile(`type RISCVHWProbePairs struct {\s*}`) |
| 149 |
b = hwprobeRexexp.ReplaceAll(b, nil) |
| 150 |
|
| 151 |
// Replace the control_regs union with a blank identifier for now. |
| 152 |
controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`) |
| 153 |
b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64")) |
| 154 |
|
| 155 |
// Remove fields that are added by glibc |
| 156 |
// Note that this is unstable as the identifiers are private. |
| 157 |
removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`) |
| 158 |
b = removeFieldsRegex.ReplaceAll(b, []byte("_")) |
| 159 |
|
| 160 |
// Convert [65]int8 to [65]byte in Utsname members to simplify |
| 161 |
// conversion to string; see golang.org/issue/20753 |
| 162 |
convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`) |
| 163 |
b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte")) |
| 164 |
|
| 165 |
// Convert [n]int8 to [n]byte in Statvfs_t and Statfs_t members to simplify |
| 166 |
// conversion to string. |
| 167 |
convertStatvfsRegex := regexp.MustCompile(`(([Ff]stype|[Mm]nton|[Mm]ntfrom)name|mntfromspec)(\s+)\[(\d+)\]int8`) |
| 168 |
b = convertStatvfsRegex.ReplaceAll(b, []byte("$1$3[$4]byte")) |
| 169 |
|
| 170 |
// Convert []int8 to []byte in device mapper ioctl interface |
| 171 |
convertDmIoctlNames := regexp.MustCompile(`(Name|Uuid|Target_type|Data)(\s+)\[(\d+)\]u?int8`) |
| 172 |
dmIoctlTypes := regexp.MustCompile(`type Dm(\S+) struct {[^}]*}`) |
| 173 |
dmStructs := dmIoctlTypes.FindAll(b, -1) |
| 174 |
for _, s := range dmStructs { |
| 175 |
newNames := convertDmIoctlNames.ReplaceAll(s, []byte("$1$2[$3]byte")) |
| 176 |
b = bytes.Replace(b, s, newNames, 1) |
| 177 |
} |
| 178 |
|
| 179 |
// Convert []int8 to []byte in EthtoolDrvinfo |
| 180 |
convertEthtoolDrvinfoNames := regexp.MustCompile(`(Driver|Version|Fw_version|Bus_info|Erom_version|Reserved2)(\s+)\[(\d+)\]u?int8`) |
| 181 |
ethtoolDrvinfoTypes := regexp.MustCompile(`type EthtoolDrvinfo struct {[^}]*}`) |
| 182 |
ethtoolDrvinfoStructs := ethtoolDrvinfoTypes.FindAll(b, -1) |
| 183 |
for _, s := range ethtoolDrvinfoStructs { |
| 184 |
newNames := convertEthtoolDrvinfoNames.ReplaceAll(s, []byte("$1$2[$3]byte")) |
| 185 |
b = bytes.Replace(b, s, newNames, 1) |
| 186 |
} |
| 187 |
|
| 188 |
// Convert []int8 to []byte in PtpPinDesc |
| 189 |
ptpBytesRegex := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]u?int8`) |
| 190 |
ptpIoctlType := regexp.MustCompile(`PtpPinDesc\s+struct {[^}]*}`) |
| 191 |
ptpStructs := ptpIoctlType.FindAll(b, -1) |
| 192 |
for _, s := range ptpStructs { |
| 193 |
newNames := ptpBytesRegex.ReplaceAll(s, []byte("$1$2[$3]byte")) |
| 194 |
b = bytes.Replace(b, s, newNames, 1) |
| 195 |
} |
| 196 |
|
| 197 |
// Convert []int8 to []byte in ctl_info ioctl interface |
| 198 |
convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`) |
| 199 |
ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`) |
| 200 |
ctlInfoStructs := ctlInfoType.FindAll(b, -1) |
| 201 |
for _, s := range ctlInfoStructs { |
| 202 |
newNames := convertCtlInfoName.ReplaceAll(s, []byte("$1$2[$3]byte")) |
| 203 |
b = bytes.Replace(b, s, newNames, 1) |
| 204 |
} |
| 205 |
|
| 206 |
// Convert [1024]int8 to [1024]byte in Ptmget members |
| 207 |
convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`) |
| 208 |
b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte")) |
| 209 |
|
| 210 |
// Remove spare fields (e.g. in Statx_t) |
| 211 |
spareFieldsRegex := regexp.MustCompile(`X__spare\S*`) |
| 212 |
b = spareFieldsRegex.ReplaceAll(b, []byte("_")) |
| 213 |
|
| 214 |
// Rename chunk_size field in XDPUmemReg. |
| 215 |
// When the struct was originally added (CL 136695) the only |
| 216 |
// field with a prefix was chunk_size, so cgo rewrote the |
| 217 |
// field to Size. Later Linux added a tx_metadata_len field, |
| 218 |
// so cgo left chunk_size as Chunk_size (CL 577975). |
| 219 |
// Go back to Size so that packages like gvisor don't have |
| 220 |
// to adjust. |
| 221 |
xdpUmemRegType := regexp.MustCompile(`type XDPUmemReg struct {[^}]*}`) |
| 222 |
xdpUmemRegStructs := xdpUmemRegType.FindAll(b, -1) |
| 223 |
for _, s := range xdpUmemRegStructs { |
| 224 |
newName := bytes.Replace(s, []byte("Chunk_size"), []byte("Size"), 1) |
| 225 |
b = bytes.Replace(b, s, newName, 1) |
| 226 |
} |
| 227 |
|
| 228 |
// Remove cgo padding fields |
| 229 |
removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`) |
| 230 |
b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_")) |
| 231 |
|
| 232 |
// Remove padding, hidden, or unused fields |
| 233 |
removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`) |
| 234 |
b = removeFieldsRegex.ReplaceAll(b, []byte("_")) |
| 235 |
|
| 236 |
// Remove the first line of warning from cgo |
| 237 |
b = b[bytes.IndexByte(b, '\n')+1:] |
| 238 |
// Modify the command in the header to include: |
| 239 |
// mkpost, our own warning, and a build tag. |
| 240 |
replacement := fmt.Sprintf(`$1 | go run mkpost.go |
| 241 |
// Code generated by the command above; see README.md. DO NOT EDIT. |
| 242 |
|
| 243 |
//go:build %s && %s`, goarch, goos) |
| 244 |
cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`) |
| 245 |
b = cgoCommandRegex.ReplaceAll(b, []byte(replacement)) |
| 246 |
|
| 247 |
// Rename Stat_t time fields |
| 248 |
if goos == "freebsd" && goarch == "386" { |
| 249 |
// Hide Stat_t.[AMCB]tim_ext fields |
| 250 |
renameStatTimeExtFieldsRegex := regexp.MustCompile(`[AMCB]tim_ext`) |
| 251 |
b = renameStatTimeExtFieldsRegex.ReplaceAll(b, []byte("_")) |
| 252 |
} |
| 253 |
renameStatTimeFieldsRegex := regexp.MustCompile(`([AMCB])(?:irth)?time?(?:spec)?\s+(Timespec|StTimespec)`) |
| 254 |
b = renameStatTimeFieldsRegex.ReplaceAll(b, []byte("${1}tim ${2}")) |
| 255 |
|
| 256 |
// gofmt |
| 257 |
b, err = format.Source(b) |
| 258 |
if err != nil { |
| 259 |
log.Fatal(err) |
| 260 |
} |
| 261 |
|
| 262 |
os.Stdout.Write(b) |
| 263 |
} |