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.
13package main
14
15import (
16	"bytes"
17	"fmt"
18	"go/format"
19	"io/ioutil"
20	"log"
21	"os"
22	"regexp"
23)
24
25func main() {
26	// Get the OS and architecture (using GOARCH_TARGET if it exists)
27	goos := os.Getenv("GOOS")
28	goarch := os.Getenv("GOARCH_TARGET")
29	if goarch == "" {
30		goarch = os.Getenv("GOARCH")
31	}
32	// Check that we are using the Docker-based build system if we should be.
33	if goos == "linux" {
34		if os.Getenv("GOLANG_SYS_BUILD") != "docker" {
35			os.Stderr.WriteString("In the Docker-based build system, mkpost should not be called directly.\n")
36			os.Stderr.WriteString("See README.md\n")
37			os.Exit(1)
38		}
39	}
40
41	b, err := ioutil.ReadAll(os.Stdin)
42	if err != nil {
43		log.Fatal(err)
44	}
45
46	if goos == "aix" {
47		// Replace type of Atim, Mtim and Ctim by Timespec in Stat_t
48		// to avoid having both StTimespec and Timespec.
49		sttimespec := regexp.MustCompile(`_Ctype_struct_st_timespec`)
50		b = sttimespec.ReplaceAll(b, []byte("Timespec"))
51	}
52
53	if goos == "darwin" {
54		// KinfoProc contains various pointers to objects stored
55		// in kernel space. Replace these by uintptr to prevent
56		// accidental dereferencing.
57		kinfoProcPointerRegex := regexp.MustCompile(`\*_Ctype_struct_(pgrp|proc|session|sigacts|ucred|user|vnode)`)
58		b = kinfoProcPointerRegex.ReplaceAll(b, []byte("uintptr"))
59
60		// ExternProc contains a p_un member that in kernel
61		// space stores a pair of pointers and in user space
62		// stores the process creation time. We only care about
63		// the process creation time.
64		externProcStarttimeRegex := regexp.MustCompile(`P_un\s*\[\d+\]byte`)
65		b = externProcStarttimeRegex.ReplaceAll(b, []byte("P_starttime Timeval"))
66	}
67
68	// Intentionally export __val fields in Fsid and Sigset_t
69	valRegex := regexp.MustCompile(`type (Fsid|Sigset_t) struct {(\s+)X__(bits|val)(\s+\S+\s+)}`)
70	b = valRegex.ReplaceAll(b, []byte("type $1 struct {${2}Val$4}"))
71
72	// Intentionally export __fds_bits field in FdSet
73	fdSetRegex := regexp.MustCompile(`type (FdSet) struct {(\s+)X__fds_bits(\s+\S+\s+)}`)
74	b = fdSetRegex.ReplaceAll(b, []byte("type $1 struct {${2}Bits$3}"))
75
76	// Intentionally export __icmp6_filt field in icmpv6_filter
77	icmpV6Regex := regexp.MustCompile(`type (ICMPv6Filter) struct {(\s+)X__icmp6_filt(\s+\S+\s+)}`)
78	b = icmpV6Regex.ReplaceAll(b, []byte("type $1 struct {${2}Filt$3}"))
79
80	// If we have empty Ptrace structs, we should delete them. Only s390x emits
81	// nonempty Ptrace structs.
82	ptraceRexexp := regexp.MustCompile(`type Ptrace((Psw|Fpregs|Per) struct {\s*})`)
83	b = ptraceRexexp.ReplaceAll(b, nil)
84
85	// Replace the control_regs union with a blank identifier for now.
86	controlRegsRegex := regexp.MustCompile(`(Control_regs)\s+\[0\]uint64`)
87	b = controlRegsRegex.ReplaceAll(b, []byte("_ [0]uint64"))
88
89	// Remove fields that are added by glibc
90	// Note that this is unstable as the identifers are private.
91	removeFieldsRegex := regexp.MustCompile(`X__glibc\S*`)
92	b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
93
94	// Convert [65]int8 to [65]byte in Utsname members to simplify
95	// conversion to string; see golang.org/issue/20753
96	convertUtsnameRegex := regexp.MustCompile(`((Sys|Node|Domain)name|Release|Version|Machine)(\s+)\[(\d+)\]u?int8`)
97	b = convertUtsnameRegex.ReplaceAll(b, []byte("$1$3[$4]byte"))
98
99	// Convert [n]int8 to [n]byte in Statvfs_t members to simplify
100	// conversion to string.
101	convertStatvfsRegex := regexp.MustCompile(`((Fstype|Mnton|Mntfrom)name)(\s+)\[(\d+)\]int8`)
102	b = convertStatvfsRegex.ReplaceAll(b, []byte("$1$3[$4]byte"))
103
104	// Convert []int8 to []byte in device mapper ioctl interface
105	convertDmIoctlNames := regexp.MustCompile(`(Name|Uuid|Target_type|Data)(\s+)\[(\d+)\]u?int8`)
106	dmIoctlTypes := regexp.MustCompile(`type Dm(\S+) struct {[^}]*}`)
107	dmStructs := dmIoctlTypes.FindAll(b, -1)
108	for _, s := range dmStructs {
109		newNames := convertDmIoctlNames.ReplaceAll(s, []byte("$1$2[$3]byte"))
110		b = bytes.Replace(b, s, newNames, 1)
111	}
112
113	// Convert []int8 to []byte in EthtoolDrvinfo
114	convertEthtoolDrvinfoNames := regexp.MustCompile(`(Driver|Version|Fw_version|Bus_info|Erom_version|Reserved2)(\s+)\[(\d+)\]u?int8`)
115	ethtoolDrvinfoTypes := regexp.MustCompile(`type EthtoolDrvinfo struct {[^}]*}`)
116	ethtoolDrvinfoStructs := ethtoolDrvinfoTypes.FindAll(b, -1)
117	for _, s := range ethtoolDrvinfoStructs {
118		newNames := convertEthtoolDrvinfoNames.ReplaceAll(s, []byte("$1$2[$3]byte"))
119		b = bytes.Replace(b, s, newNames, 1)
120	}
121
122	// Convert []int8 to []byte in ctl_info ioctl interface
123	convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`)
124	ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`)
125	ctlInfoStructs := ctlInfoType.FindAll(b, -1)
126	for _, s := range ctlInfoStructs {
127		newNames := convertCtlInfoName.ReplaceAll(s, []byte("$1$2[$3]byte"))
128		b = bytes.Replace(b, s, newNames, 1)
129	}
130
131	// Convert [1024]int8 to [1024]byte in Ptmget members
132	convertPtmget := regexp.MustCompile(`([SC]n)(\s+)\[(\d+)\]u?int8`)
133	b = convertPtmget.ReplaceAll(b, []byte("$1[$3]byte"))
134
135	// Remove spare fields (e.g. in Statx_t)
136	spareFieldsRegex := regexp.MustCompile(`X__spare\S*`)
137	b = spareFieldsRegex.ReplaceAll(b, []byte("_"))
138
139	// Remove cgo padding fields
140	removePaddingFieldsRegex := regexp.MustCompile(`Pad_cgo_\d+`)
141	b = removePaddingFieldsRegex.ReplaceAll(b, []byte("_"))
142
143	// Remove padding, hidden, or unused fields
144	removeFieldsRegex = regexp.MustCompile(`\b(X_\S+|Padding)`)
145	b = removeFieldsRegex.ReplaceAll(b, []byte("_"))
146
147	// Remove the first line of warning from cgo
148	b = b[bytes.IndexByte(b, '\n')+1:]
149	// Modify the command in the header to include:
150	//  mkpost, our own warning, and a build tag.
151	replacement := fmt.Sprintf(`$1 | go run mkpost.go
152// Code generated by the command above; see README.md. DO NOT EDIT.
153
154//go:build %s && %s
155// +build %s,%s`, goarch, goos, goarch, goos)
156	cgoCommandRegex := regexp.MustCompile(`(cgo -godefs .*)`)
157	b = cgoCommandRegex.ReplaceAll(b, []byte(replacement))
158
159	// Rename Stat_t time fields
160	if goos == "freebsd" && goarch == "386" {
161		// Hide Stat_t.[AMCB]tim_ext fields
162		renameStatTimeExtFieldsRegex := regexp.MustCompile(`[AMCB]tim_ext`)
163		b = renameStatTimeExtFieldsRegex.ReplaceAll(b, []byte("_"))
164	}
165	renameStatTimeFieldsRegex := regexp.MustCompile(`([AMCB])(?:irth)?time?(?:spec)?\s+(Timespec|StTimespec)`)
166	b = renameStatTimeFieldsRegex.ReplaceAll(b, []byte("${1}tim ${2}"))
167
168	// gofmt
169	b, err = format.Source(b)
170	if err != nil {
171		log.Fatal(err)
172	}
173
174	os.Stdout.Write(b)
175}
176