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