-
Notifications
You must be signed in to change notification settings - Fork 15
/
patch.go
345 lines (315 loc) · 9.02 KB
/
patch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/xhd2015/xgo/support/filecopy"
"github.com/xhd2015/xgo/support/goinfo"
"github.com/xhd2015/xgo/support/osinfo"
)
// the _FilePath declared at toplevel
// serves as an item list where
// the runtime and compiler maybe
// affected.
// NOTE: do not remove files, always add files,
// these old files may exists in older version
// so can be cleared by newer xgo
type _FilePath []string
func (c _FilePath) Join(s ...string) string {
return filepath.Join(filepath.Join(s...), filepath.Join(c...))
}
var affectedFiles []_FilePath
func init() {
affectedFiles = append(affectedFiles, compilerFiles...)
affectedFiles = append(affectedFiles, runtimeFiles...)
affectedFiles = append(affectedFiles, reflectFiles...)
}
// assume go 1.20
// the patch should be idempotent
// the origGoroot is used to generate runtime defs, see https://github.com/xhd2015/xgo/issues/4#issuecomment-2017880791
func patchRuntimeAndCompiler(origGoroot string, goroot string, xgoSrc string, goVersion *goinfo.GoVersion, syncWithLink bool, revisionChanged bool) error {
if goroot == "" {
return fmt.Errorf("requires goroot")
}
if isDevelopment && xgoSrc == "" {
return fmt.Errorf("requires xgoSrc")
}
if !isDevelopment && !revisionChanged {
return nil
}
// runtime
err := patchRuntimeAndTesting(goroot, goVersion)
if err != nil {
return err
}
// compiler
err = patchCompiler(origGoroot, goroot, goVersion, xgoSrc, revisionChanged, syncWithLink)
if err != nil {
return err
}
return nil
}
func replaceBuildIgnore(content []byte) ([]byte, error) {
const buildIgnore = "//go:build ignore"
// buggy: content = bytes.Replace(content, []byte("//go:build ignore\n"), nil, 1)
return replaceMarkerNewline(content, []byte(buildIgnore))
}
// content = bytes.Replace(content, []byte("//go:build ignore\n"), nil, 1)
func replaceMarkerNewline(content []byte, marker []byte) ([]byte, error) {
idx := bytes.Index(content, marker)
if idx < 0 {
return nil, fmt.Errorf("missing %s", string(marker))
}
idx += len(marker)
if idx < len(content) && content[idx] == '\r' {
idx++
}
if idx < len(content) && content[idx] == '\n' {
idx++
}
return content[idx:], nil
}
func checkRevisionChanged(revisionFile string, currentRevision string) (bool, error) {
savedRevision, err := readOrEmpty(revisionFile)
if err != nil {
return false, err
}
logDebug("current revision: %s, last revision: %s from file %s", currentRevision, savedRevision, revisionFile)
if savedRevision == "" || savedRevision != currentRevision {
return true, nil
}
return false, nil
}
func readOrEmpty(file string) (string, error) {
version, err := os.ReadFile(file)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", nil
}
return "", err
}
s := string(version)
s = strings.TrimSuffix(s, "\n")
s = strings.TrimSuffix(s, "\r")
return s, nil
}
// NOTE: flagA never cause goroot to reset
func syncGoroot(goroot string, instrumentGoroot string, fullSyncRecordFile string) error {
// check if src goroot has src/runtime
srcRuntimeDir := filepath.Join(goroot, "src", "runtime")
err := assertDir(srcRuntimeDir)
if err != nil {
return err
}
var goBinaryChanged bool = true
srcGoBin := filepath.Join(goroot, "bin", "go"+osinfo.EXE_SUFFIX)
dstGoBin := filepath.Join(instrumentGoroot, "bin", "go"+osinfo.EXE_SUFFIX)
srcFile, err := os.Stat(srcGoBin)
if err != nil {
return err
}
if srcFile.IsDir() {
return fmt.Errorf("bad goroot: %s", goroot)
}
dstFile, statErr := os.Stat(dstGoBin)
if statErr != nil {
if !os.IsNotExist(statErr) {
return statErr
}
}
if dstFile != nil && !dstFile.IsDir() && dstFile.Size() == srcFile.Size() {
goBinaryChanged = false
}
var doPartialCopy bool
if !goBinaryChanged && statNoErr(fullSyncRecordFile) {
// full sync record does not yet exist
doPartialCopy = true
}
if doPartialCopy {
// do partial copy
err := partialCopy(goroot, instrumentGoroot)
if err != nil {
return err
}
} else {
rmErr := os.Remove(fullSyncRecordFile)
if rmErr != nil {
if !errors.Is(rmErr, os.ErrNotExist) {
return rmErr
}
}
// need copy, delete target dst dir first
// TODO: use git worktree add if .git exists
err = filecopy.NewOptions().
Concurrent(2). // 10 is too much
CopyReplaceDir(goroot, instrumentGoroot)
if err != nil {
return err
}
// record this full sync
copyTime := time.Now().Format("2006-01-02T15:04:05Z07:00")
err = os.WriteFile(fullSyncRecordFile, []byte(copyTime), 0755)
if err != nil {
return err
}
}
// change binary executable
return nil
}
func partialCopy(goroot string, instrumentGoroot string) error {
err := os.RemoveAll(xgoRewriteInternal.Join(instrumentGoroot))
if err != nil {
return err
}
for _, affectedFile := range affectedFiles {
srcFile := affectedFile.Join(goroot)
dstFile := affectedFile.Join(instrumentGoroot)
err := filecopy.CopyFileAll(srcFile, dstFile)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
// delete dstFile
err := os.RemoveAll(dstFile)
if err != nil {
return err
}
continue
}
}
return nil
}
func statNoErr(f string) bool {
_, err := os.Stat(f)
return err == nil
}
func buildInstrumentTool(goroot string, xgoSrc string, compilerBin string, compilerBuildIDFile string, execToolBin string, xgoBin string, debugPkg string, logCompile bool, noSetup bool, debugWithDlv bool) (compilerChanged bool, toolExecFlag string, err error) {
var execToolCmd []string
if !noSetup {
// build the instrumented compiler
err = buildCompiler(goroot, compilerBin)
if err != nil {
return false, "", err
}
compilerChanged, err = compareAndUpdateCompilerID(compilerBin, compilerBuildIDFile)
if err != nil {
return false, "", err
}
if false {
actualExecToolBin := execToolBin
if isDevelopment {
// build exec tool
buildExecToolCmd := exec.Command("go", "build", "-o", execToolBin, "./exec_tool")
buildExecToolCmd.Dir = filepath.Join(xgoSrc, "cmd")
buildExecToolCmd.Stdout = os.Stdout
buildExecToolCmd.Stderr = os.Stderr
err = buildExecToolCmd.Run()
if err != nil {
return false, "", err
}
} else {
actualExecToolBin, err = findBuiltExecTool()
if err != nil {
return false, "", err
}
}
// unused any more
execToolCmd = []string{actualExecToolBin, "--enable"}
_ = execToolCmd
}
}
execToolCmd = []string{xgoBin, "exec_tool", "--enable"}
if logCompile {
execToolCmd = append(execToolCmd, "--log-compile")
}
if debugPkg != "" {
execToolCmd = append(execToolCmd, "--debug="+debugPkg)
}
if debugWithDlv {
execToolCmd = append(execToolCmd, "--debug-with-dlv")
}
// always add trailing '--' to mark exec tool flags end
execToolCmd = append(execToolCmd, "--")
toolExecFlag = "-toolexec=" + strings.Join(execToolCmd, " ")
return compilerChanged, toolExecFlag, nil
}
// find exec_tool, first try the same dir with xgo,
// but if that is not found, we can fallback to ~/.xgo/bin/exec_tool
// because exec_tool changes rarely, so it is safe to use
// an older version.
// we may add version to check if exec_tool is compatible
// Deprecated: xgo itself embed exec tool
func findBuiltExecTool() (string, error) {
dirName := filepath.Dir(os.Args[0])
absDirName, err := filepath.Abs(dirName)
if err != nil {
return "", err
}
exeSuffix := osinfo.EXE_SUFFIX
execToolBin := filepath.Join(absDirName, "exec_tool"+exeSuffix)
_, statErr := os.Stat(execToolBin)
if statErr == nil {
return execToolBin, nil
}
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("exec_tool not found in %s", dirName)
}
execToolBin = filepath.Join(homeDir, ".xgo", "bin", "exec_tool"+exeSuffix)
_, statErr = os.Stat(execToolBin)
if statErr == nil {
return execToolBin, nil
}
return "", fmt.Errorf("exec_tool not found in %s and ~/.xgo/bin", dirName)
}
func buildCompiler(goroot string, output string) error {
args := []string{"build"}
if isDevelopment {
args = append(args, "-gcflags=all=-N -l")
}
args = append(args, "-o", output, "./")
cmd := exec.Command(filepath.Join(goroot, "bin", "go"), args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
env, err := patchEnvWithGoroot(os.Environ(), goroot)
if err != nil {
return err
}
cmd.Env = env
cmd.Dir = filepath.Join(goroot, "src", "cmd", "compile")
return cmd.Run()
}
func compareAndUpdateCompilerID(compilerFile string, compilerIDFile string) (changed bool, err error) {
prevData, statErr := ioutil.ReadFile(compilerIDFile)
if statErr != nil {
if !errors.Is(statErr, os.ErrNotExist) {
return false, statErr
}
}
prevID := string(prevData)
curID, err := getBuildID(compilerFile)
if err != nil {
return false, err
}
if prevID != "" && prevID == curID {
return false, nil
}
err = ioutil.WriteFile(compilerIDFile, []byte(curID), 0755)
if err != nil {
return false, err
}
return true, nil
}
func getBuildID(file string) (string, error) {
data, err := exec.Command("go", "tool", "buildid", file).Output()
if err != nil {
return "", err
}
return strings.TrimSuffix(string(data), "\n"), nil
}