diff options
| author | Cherry Mui <cherryyz@google.com> | 2026-03-17 21:59:52 -0400 |
|---|---|---|
| committer | Cherry Mui <cherryyz@google.com> | 2026-03-24 12:43:58 -0700 |
| commit | e20a1b603579fc36148530048c76de6938347019 (patch) | |
| tree | e20fc02f9420f4860a689ca97d3b9ef50a9c6d03 /src/cmd/compile | |
| parent | 3390ec59af33d3c9ab431ff88a245b710b7c9e40 (diff) | |
| download | go-e20a1b603579fc36148530048c76de6938347019.tar.xz | |
cmd/compile: handle string literals in FIPS mode consistently
There are different code paths for compiling a composite literal,
e.g. small vs. large, fully static vs. partially static. Following
CL 755600, we need to apply the condition for string literals in
FIPS mode consistently in all places.
Enhance the test to check that not only does the code compile, the
same code inside and outside of FIPS mode produce the same result.
If the condition is not consistent in the compiler, it may compile
the code, but not all the fields are actually assigned.
Fixes #78173.
Change-Id: Icaf673bd4798d4312d86c39b147d7fd33b9dae2c
Reviewed-on: https://go-review.googlesource.com/c/go/+/756260
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/cmd/compile')
| -rw-r--r-- | src/cmd/compile/internal/walk/complit.go | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/src/cmd/compile/internal/walk/complit.go b/src/cmd/compile/internal/walk/complit.go index 053b124a91..e0ffca86ce 100644 --- a/src/cmd/compile/internal/walk/complit.go +++ b/src/cmd/compile/internal/walk/complit.go @@ -85,9 +85,7 @@ const ( func getdyn(n ir.Node, top bool) initGenType { switch n.Op() { default: - // Handle constants in linker, except that linker cannot do - // the relocations necessary for string constants in FIPS packages. - if ir.IsConstNode(n) && (!n.Type().IsString() || !base.Ctxt.IsFIPS()) { + if isStaticLiteral(n) { return initConst } return initDynamic @@ -127,8 +125,17 @@ func getdyn(n ir.Node, top bool) initGenType { return mode } -// isStaticCompositeLiteral reports whether n is a compile-time constant, -// which can be represented in the read-only data section. +// isStaticLiteral reports whether n is a compile-time (non-composite) +// constant, which can be represented in the read-only data section. +func isStaticLiteral(n ir.Node) bool { + // A string reference requires a relocation, not allowed + // in static data in FIPS mode. + return ir.IsConstNode(n) && !(base.Ctxt.IsFIPS() && n.Type().IsString()) +} + +// isStaticCompositeLiteral reports whether a composite literal n +// is a compile-time constant, which can be represented in the +// read-only data section. func isStaticCompositeLiteral(n ir.Node) bool { switch n.Op() { case ir.OSLICELIT: @@ -153,13 +160,10 @@ func isStaticCompositeLiteral(n ir.Node) bool { } } return true - case ir.OLITERAL, ir.ONIL: - if base.Ctxt.IsFIPS() && n.Type().IsString() { - // A string reference requires a relocation, not allowed - // in static data in FIPS mode. - return false - } + case ir.ONIL: return true + case ir.OLITERAL: + return isStaticLiteral(n) case ir.OCONVIFACE: // See staticinit.Schedule.StaticAssign's OCONVIFACE case for comments. if base.Ctxt.IsFIPS() && base.Ctxt.Flag_shared { @@ -264,7 +268,7 @@ func fixedlit(ctxt initContext, kind initKind, n *ir.CompLitExpr, var_ ir.Node, continue } - islit := ir.IsConstNode(value) + islit := isStaticLiteral(value) if (kind == initKindStatic && !islit) || (kind == initKindDynamic && islit) { continue } @@ -404,7 +408,7 @@ func slicelit(ctxt initContext, n *ir.CompLitExpr, var_ ir.Node, init *ir.Nodes) continue } - if vstat != nil && ir.IsConstNode(value) { // already set by copy from static value + if vstat != nil && isStaticLiteral(value) { // already set by copy from static value continue } |
