github.com/tristanisham/sys@v0.0.0-20240326010300-a16cbabb7555/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  
     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 identifers 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 ctl_info ioctl interface
   189  	convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`)
   190  	ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`)
   191  	ctlInfoStructs := ctlInfoType.FindAll(b, -1)
   192  	for _, s := range ctlInfoStructs {
   193  		newNames := convertCtlInfoName.ReplaceAll(s, []byte("$1$2[$3]byte"))
   194  		b = bytes.Replace(b, s, newNames, 1)
   195  	}
   196  
   197  	// Convert [1024]int8 to [1024]byte in Ptmget members
   198  	convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`)
   199  	b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte"))
   200  
   201  	// Remove spare fields (e.g. in Statx_t)
   202  	spareFieldsRegex := regexp.MustCompile(`X__spare\S*`)
   203  	b = spareFieldsRegex.ReplaceAll(b, []byte("_"))
   204  
   205  	// Remove cgo padding fields
   206  	removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
   207  	b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_"))
   208  
   209  	// Remove padding, hidden, or unused fields
   210  	removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`)
   211  	b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
   212  
   213  	// Remove the first line of warning from cgo
   214  	b = b[bytes.IndexByte(b, '\n')+1:]
   215  	// Modify the command in the header to include:
   216  	//  mkpost, our own warning, and a build tag.
   217  	replacement := fmt.Sprintf(`$1 | go run mkpost.go
   218  // Code generated by the command above; see README.md. DO NOT EDIT.
   219  
   220  //go:build %s && %s`, goarch, goos)
   221  	cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
   222  	b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
   223  
   224  	// Rename Stat_t time fields
   225  	if goos == "freebsd" && goarch == "386" {
   226  		// Hide Stat_t.[AMCB]tim_ext fields
   227  		renameStatTimeExtFieldsRegex := regexp.MustCompile(`[AMCB]tim_ext`)
   228  		b = renameStatTimeExtFieldsRegex.ReplaceAll(b, []byte("_"))
   229  	}
   230  	renameStatTimeFieldsRegex := regexp.MustCompile(`([AMCB])(?:irth)?time?(?:spec)?\s+(Timespec|StTimespec)`)
   231  	b = renameStatTimeFieldsRegex.ReplaceAll(b, []byte("${1}tim ${2}"))
   232  
   233  	// gofmt
   234  	b, err = format.Source(b)
   235  	if err != nil {
   236  		log.Fatal(err)
   237  	}
   238  
   239  	os.Stdout.Write(b)
   240  }