Skip to content

Commit b69be4d

Browse files
gdamsdagood
andauthored
initial implementation of macOS crypto backend (#1453)
* initial implementation of macOS crypto backend * fixup patches * Also unassign GOROOT in run-builder * fix patches * try to fix test * rebase * skip internal linking * add arm64 testing * skip arm64 macOS (for now) * move supports functions * fixup --------- Co-authored-by: Davis Goodin <[email protected]>
1 parent d52119f commit b69be4d

21 files changed

+4539
-155
lines changed

eng/_util/buildutil/buildutil.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func AppendExperimentEnv(experiment string) {
9191
if strings.Contains(experiment, "opensslcrypto") ||
9292
strings.Contains(experiment, "cngcrypto") ||
9393
strings.Contains(experiment, "boringcrypto") ||
94+
strings.Contains(experiment, "darwincrypto") ||
9495
strings.Contains(experiment, "systemcrypto") {
9596

9697
experiment += ",allowcryptofallback"
@@ -103,3 +104,19 @@ func AppendExperimentEnv(experiment string) {
103104
panic(err)
104105
}
105106
}
107+
108+
// UnassignGOROOT unsets the GOROOT env var if it is set.
109+
//
110+
// Setting GOROOT explicitly in the environment has not been necessary since Go
111+
// 1.9 (https://go.dev/doc/go1.9#goroot), but a dev or build machine may still
112+
// have it set. It interferes with attempts to run the built Go (such as when
113+
// building the race runtime), so remove the explicit GOROOT if set.
114+
func UnassignGOROOT() error {
115+
if explicitRoot, ok := os.LookupEnv("GOROOT"); ok {
116+
fmt.Printf("---- Removing explicit GOROOT from environment: %v\n", explicitRoot)
117+
if err := os.Unsetenv("GOROOT"); err != nil {
118+
return err
119+
}
120+
}
121+
return nil
122+
}

eng/_util/cmd/build/build.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,8 @@ func build(o *options) error {
138138
}
139139
fmt.Printf("---- Target platform: %v_%v\n", targetOS, targetArch)
140140

141-
// Setting GOROOT explicitly in the environment has not been necessary since Go 1.9
142-
// (https://go.dev/doc/go1.9#goroot), but a dev or build machine may still have it set. It
143-
// interferes with attempts to run the built Go (such as when building the race runtime), so
144-
// remove the explicit GOROOT if set.
145-
if explicitRoot, ok := os.LookupEnv("GOROOT"); ok {
146-
fmt.Printf("---- Removing explicit GOROOT from environment: %v\n", explicitRoot)
147-
if err := os.Unsetenv("GOROOT"); err != nil {
148-
return err
149-
}
141+
if err := buildutil.UnassignGOROOT(); err != nil {
142+
return err
150143
}
151144

152145
// The upstream build scripts in {repo-root}/src require your working directory to be src, or

eng/_util/cmd/run-builder/run-builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ func main() {
105105
env("GO_TEST_TIMEOUT_SCALE", strconv.Itoa(timeoutScale))
106106
}
107107

108+
if err := buildutil.UnassignGOROOT(); err != nil {
109+
log.Fatal(err)
110+
}
111+
108112
buildCmdline := []string{"pwsh", "eng/run.ps1", "build"}
109113

110114
// run.ps1 compiles Go code, so we can't use the experiment yet. We must pass the experiment

eng/pipeline/stages/go-builder-matrix-stages.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ stages:
8181
- { os: linux, arch: arm64, config: buildandpack }
8282
- ${{ if parameters.innerloop }}:
8383
- { os: darwin, arch: amd64, config: devscript }
84+
- { os: darwin, arch: amd64, config: test }
85+
- { experiment: darwincrypto, os: darwin, arch: amd64, config: test }
86+
- { experiment: darwincrypto, os: darwin, arch: amd64, config: test, fips: true }
87+
# - { os: darwin, arch: arm64, config: devscript }
88+
# - { os: darwin, arch: arm64, config: test }
89+
# - { experiment: darwincrypto, os: darwin, arch: arm64, config: test }
90+
# - { experiment: darwincrypto, os: darwin, arch: arm64, config: test, fips: true }
8491
- { os: linux, arch: amd64, config: devscript }
8592
- { os: linux, arch: amd64, config: test }
8693
- { os: linux, arch: amd64, config: test, distro: ubuntu }

eng/pipeline/stages/pool-2.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,9 @@ stages:
5353

5454
${{ elseif eq(parameters.os, 'darwin') }}:
5555
# https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml#software
56-
vmImage: 'macos-14'
57-
os: macOs
56+
${{ if eq(parameters.hostArch, 'amd64') }}:
57+
vmImage: 'macos-14'
58+
os: macOS
59+
${{ else }}:
60+
vmImage: 'macos-latest-internal'
61+
os: macOS

patches/0001-Add-crypto-backend-GOEXPERIMENTs.patch

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@ information about the behavior.
1111
Includes new tests in "build_test.go" and "buildbackend_test.go" to help
1212
maintain this feature. For more information, see the test files.
1313
---
14-
src/cmd/go/internal/modindex/build.go | 54 ++++++++++++++
15-
src/cmd/go/internal/modindex/build_test.go | 73 +++++++++++++++++++
16-
src/go/build/build.go | 54 ++++++++++++++
17-
src/go/build/buildbackend_test.go | 66 +++++++++++++++++
14+
src/cmd/go/internal/modindex/build.go | 57 +++++++++++++
15+
src/cmd/go/internal/modindex/build_test.go | 73 ++++++++++++++++
16+
src/go/build/build.go | 57 +++++++++++++
17+
src/go/build/buildbackend_test.go | 84 +++++++++++++++++++
1818
.../testdata/backendtags_openssl/main.go | 3 +
1919
.../testdata/backendtags_openssl/openssl.go | 3 +
2020
.../build/testdata/backendtags_system/main.go | 3 +
2121
.../backendtags_system/systemcrypto.go | 3 +
2222
.../goexperiment/exp_cngcrypto_off.go | 8 ++
2323
src/internal/goexperiment/exp_cngcrypto_on.go | 8 ++
24+
.../goexperiment/exp_darwincrypto_off.go | 8 ++
25+
.../goexperiment/exp_darwincrypto_on.go | 8 ++
2426
.../goexperiment/exp_opensslcrypto_off.go | 8 ++
2527
.../goexperiment/exp_opensslcrypto_on.go | 8 ++
2628
.../goexperiment/exp_systemcrypto_off.go | 8 ++
2729
.../goexperiment/exp_systemcrypto_on.go | 8 ++
28-
src/internal/goexperiment/flags.go | 17 +++++
29-
15 files changed, 324 insertions(+)
30+
src/internal/goexperiment/flags.go | 18 ++++
31+
17 files changed, 365 insertions(+)
3032
create mode 100644 src/cmd/go/internal/modindex/build_test.go
3133
create mode 100644 src/go/build/buildbackend_test.go
3234
create mode 100644 src/go/build/testdata/backendtags_openssl/main.go
@@ -35,22 +37,25 @@ maintain this feature. For more information, see the test files.
3537
create mode 100644 src/go/build/testdata/backendtags_system/systemcrypto.go
3638
create mode 100644 src/internal/goexperiment/exp_cngcrypto_off.go
3739
create mode 100644 src/internal/goexperiment/exp_cngcrypto_on.go
40+
create mode 100644 src/internal/goexperiment/exp_darwincrypto_off.go
41+
create mode 100644 src/internal/goexperiment/exp_darwincrypto_on.go
3842
create mode 100644 src/internal/goexperiment/exp_opensslcrypto_off.go
3943
create mode 100644 src/internal/goexperiment/exp_opensslcrypto_on.go
4044
create mode 100644 src/internal/goexperiment/exp_systemcrypto_off.go
4145
create mode 100644 src/internal/goexperiment/exp_systemcrypto_on.go
4246

4347
diff --git a/src/cmd/go/internal/modindex/build.go b/src/cmd/go/internal/modindex/build.go
44-
index b4dacb0f523a8d..615ae461eb8cdc 100644
48+
index b4dacb0f523a8d..4315c288d10cb3 100644
4549
--- a/src/cmd/go/internal/modindex/build.go
4650
+++ b/src/cmd/go/internal/modindex/build.go
47-
@@ -886,13 +886,67 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
51+
@@ -886,13 +886,70 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
4852
name = "goexperiment.boringcrypto" // boringcrypto is an old name for goexperiment.boringcrypto
4953
}
5054

5155
+ const system = "goexperiment.systemcrypto"
5256
+ const openssl = "goexperiment.opensslcrypto"
5357
+ const cng = "goexperiment.cngcrypto"
58+
+ const darwin = "goexperiment.darwincrypto"
5459
+ const boring = "goexperiment.boringcrypto"
5560
+ // Implement the SystemCrypto GOEXPERIMENT logic. This is done here rather
5661
+ // than during GOEXPERIMENT parsing so "-tags goexperiment.systemcrypto"
@@ -71,11 +76,12 @@ index b4dacb0f523a8d..615ae461eb8cdc 100644
7176
+ satisfiedByAnyBackend := name == system
7277
+ satisfiedBySystemCrypto :=
7378
+ (ctxt.GOOS == "linux" && name == openssl) ||
74-
+ (ctxt.GOOS == "windows" && name == cng)
79+
+ (ctxt.GOOS == "windows" && name == cng) ||
80+
+ (ctxt.GOOS == "darwin" && name == darwin)
7581
+ satisfiedBy := func(tag string) bool {
7682
+ if satisfiedByAnyBackend {
7783
+ switch tag {
78-
+ case openssl, cng, boring:
84+
+ case openssl, cng, darwin, boring:
7985
+ return true
8086
+ }
8187
+ }
@@ -89,6 +95,7 @@ index b4dacb0f523a8d..615ae461eb8cdc 100644
8995
+ if satisfiedByAnyBackend {
9096
+ allTags[openssl] = true
9197
+ allTags[cng] = true
98+
+ allTags[darwin] = true
9299
+ allTags[boring] = true
93100
+ }
94101
+ if satisfiedBySystemCrypto {
@@ -192,16 +199,17 @@ index 00000000000000..1756c5d027fee0
192199
+ }
193200
+}
194201
diff --git a/src/go/build/build.go b/src/go/build/build.go
195-
index 9ffffda08a99b1..570937cba3cb85 100644
202+
index 9ffffda08a99b1..78fd536fa6a6d1 100644
196203
--- a/src/go/build/build.go
197204
+++ b/src/go/build/build.go
198-
@@ -1984,13 +1984,67 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
205+
@@ -1984,13 +1984,70 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
199206
name = "goexperiment.boringcrypto" // boringcrypto is an old name for goexperiment.boringcrypto
200207
}
201208

202209
+ const system = "goexperiment.systemcrypto"
203210
+ const openssl = "goexperiment.opensslcrypto"
204211
+ const cng = "goexperiment.cngcrypto"
212+
+ const darwin = "goexperiment.darwincrypto"
205213
+ const boring = "goexperiment.boringcrypto"
206214
+ // Implement the SystemCrypto GOEXPERIMENT logic. This is done here rather
207215
+ // than during GOEXPERIMENT parsing so "-tags goexperiment.systemcrypto"
@@ -222,11 +230,12 @@ index 9ffffda08a99b1..570937cba3cb85 100644
222230
+ satisfiedByAnyBackend := name == system
223231
+ satisfiedBySystemCrypto :=
224232
+ (ctxt.GOOS == "linux" && name == openssl) ||
225-
+ (ctxt.GOOS == "windows" && name == cng)
233+
+ (ctxt.GOOS == "windows" && name == cng) ||
234+
+ (ctxt.GOOS == "darwin" && name == darwin)
226235
+ satisfiedBy := func(tag string) bool {
227236
+ if satisfiedByAnyBackend {
228237
+ switch tag {
229-
+ case openssl, cng, boring:
238+
+ case openssl, cng, darwin, boring:
230239
+ return true
231240
+ }
232241
+ }
@@ -240,6 +249,7 @@ index 9ffffda08a99b1..570937cba3cb85 100644
240249
+ if satisfiedByAnyBackend {
241250
+ allTags[openssl] = true
242251
+ allTags[cng] = true
252+
+ allTags[darwin] = true
243253
+ allTags[boring] = true
244254
+ }
245255
+ if satisfiedBySystemCrypto {
@@ -265,10 +275,10 @@ index 9ffffda08a99b1..570937cba3cb85 100644
265275
}
266276
diff --git a/src/go/build/buildbackend_test.go b/src/go/build/buildbackend_test.go
267277
new file mode 100644
268-
index 00000000000000..a22abbb42e37c0
278+
index 00000000000000..aa3c5f1007ed79
269279
--- /dev/null
270280
+++ b/src/go/build/buildbackend_test.go
271-
@@ -0,0 +1,66 @@
281+
@@ -0,0 +1,84 @@
272282
+// Copyright 2023 The Go Authors. All rights reserved.
273283
+// Use of this source code is governed by a BSD-style
274284
+// license that can be found in the LICENSE file.
@@ -326,14 +336,32 @@ index 00000000000000..a22abbb42e37c0
326336
+ if err != nil {
327337
+ t.Fatal(err)
328338
+ }
329-
+ want = []string{"goexperiment.boringcrypto", "goexperiment.cngcrypto", "goexperiment.opensslcrypto", "goexperiment.systemcrypto"}
339+
+ want = []string{"goexperiment.boringcrypto", "goexperiment.cngcrypto", "goexperiment.darwincrypto", "goexperiment.opensslcrypto", "goexperiment.systemcrypto"}
330340
+ if !reflect.DeepEqual(p.AllTags, want) {
331341
+ t.Errorf("AllTags = %v, want %v", p.AllTags, want)
332342
+ }
333343
+ wantFiles = []string{"main.go", "systemcrypto.go"}
334344
+ if !reflect.DeepEqual(p.GoFiles, wantFiles) {
335345
+ t.Errorf("GoFiles = %v, want %v", p.GoFiles, wantFiles)
336346
+ }
347+
+
348+
+ ctxt.GOARCH = "amd64"
349+
+ ctxt.GOOS = "darwin"
350+
+ ctxt.BuildTags = []string{"goexperiment.darwincrypto"}
351+
+ p, err = ctxt.ImportDir("testdata/backendtags_openssl", 0)
352+
+ if err != nil {
353+
+ t.Fatal(err)
354+
+ }
355+
+ // Given the current GOOS (darwin), systemcrypto would not affect the
356+
+ // decision, so we don't want it to be included in AllTags.
357+
+ want = []string{"goexperiment.opensslcrypto"}
358+
+ if !reflect.DeepEqual(p.AllTags, want) {
359+
+ t.Errorf("AllTags = %v, want %v", p.AllTags, want)
360+
+ }
361+
+ wantFiles = []string{"main.go"}
362+
+ if !reflect.DeepEqual(p.GoFiles, wantFiles) {
363+
+ t.Errorf("GoFiles = %v, want %v", p.GoFiles, wantFiles)
364+
+ }
337365
+}
338366
diff --git a/src/go/build/testdata/backendtags_openssl/main.go b/src/go/build/testdata/backendtags_openssl/main.go
339367
new file mode 100644
@@ -399,6 +427,34 @@ index 00000000000000..5b0a55d6c5772e
399427
+
400428
+const CNGCrypto = true
401429
+const CNGCryptoInt = 1
430+
diff --git a/src/internal/goexperiment/exp_darwincrypto_off.go b/src/internal/goexperiment/exp_darwincrypto_off.go
431+
new file mode 100644
432+
index 00000000000000..331111ce4759f7
433+
--- /dev/null
434+
+++ b/src/internal/goexperiment/exp_darwincrypto_off.go
435+
@@ -0,0 +1,8 @@
436+
+// Code generated by mkconsts.go. DO NOT EDIT.
437+
+
438+
+//go:build !goexperiment.darwincrypto
439+
+
440+
+package goexperiment
441+
+
442+
+const DarwinCrypto = false
443+
+const DarwinCryptoInt = 0
444+
diff --git a/src/internal/goexperiment/exp_darwincrypto_on.go b/src/internal/goexperiment/exp_darwincrypto_on.go
445+
new file mode 100644
446+
index 00000000000000..4bf785b999ecce
447+
--- /dev/null
448+
+++ b/src/internal/goexperiment/exp_darwincrypto_on.go
449+
@@ -0,0 +1,8 @@
450+
+// Code generated by mkconsts.go. DO NOT EDIT.
451+
+
452+
+//go:build goexperiment.darwincrypto
453+
+
454+
+package goexperiment
455+
+
456+
+const DarwinCrypto = true
457+
+const DarwinCryptoInt = 1
402458
diff --git a/src/internal/goexperiment/exp_opensslcrypto_off.go b/src/internal/goexperiment/exp_opensslcrypto_off.go
403459
new file mode 100644
404460
index 00000000000000..b28c0976a94cb0
@@ -456,17 +512,18 @@ index 00000000000000..fcd4cb9da0d162
456512
+const SystemCrypto = true
457513
+const SystemCryptoInt = 1
458514
diff --git a/src/internal/goexperiment/flags.go b/src/internal/goexperiment/flags.go
459-
index 31b3d0315b64f8..8c140f0dbed134 100644
515+
index 31b3d0315b64f8..e6c9b7d5e62dc0 100644
460516
--- a/src/internal/goexperiment/flags.go
461517
+++ b/src/internal/goexperiment/flags.go
462-
@@ -59,6 +59,23 @@ type Flags struct {
518+
@@ -59,6 +59,24 @@ type Flags struct {
463519
PreemptibleLoops bool
464520
StaticLockRanking bool
465521
BoringCrypto bool
466522
+ OpenSSLCrypto bool
467523
+ CNGCrypto bool
524+
+ DarwinCrypto bool
468525
+
469-
+ // SystemCrypto enables the OpenSSL or CNG crypto experiment depending on
526+
+ // SystemCrypto enables the OpenSSL, CNG or Darwin crypto experiment depending on
470527
+ // which one is appropriate on the target GOOS.
471528
+ //
472529
+ // If SystemCrypto is enabled but no crypto experiment is appropriate on the

0 commit comments

Comments
 (0)