golang: backport an upstream fix for non-retpoline-compatible error

This fixes the following build error:
```
Building targets
runtime
<autogenerated>:1: non-retpoline-compatible: 00200 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/time.go:915)       JMP     (R15)(R12*8)
<autogenerated>:1: non-retpoline-compatible: 00115 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/type.go:614)       JMP     (AX)(SI*8)
<autogenerated>:1: non-retpoline-compatible: 00028 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/time.go:452)       JMP     (R11)(R10*8)
<autogenerated>:1: non-retpoline-compatible: 00021 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/error.go:261)      JMP     (DX)(CX*8)
<autogenerated>:1: non-retpoline-compatible: 00050 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/time.go:691)       JMP     (CX)(R12*8)
<autogenerated>:1: non-retpoline-compatible: 00024 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/debuglog.go:616)   JMP     (CX)(SI*8)
<autogenerated>:1: non-retpoline-compatible: 00079 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/time.go:617)       JMP     (R9)(R8*8)
<autogenerated>:1: non-retpoline-compatible: 00025 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/cgocall.go:453)    JMP     (R9)(DX*8)
<autogenerated>:1: non-retpoline-compatible: 00018 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/type.go:66)        JMP     (DX)(CX*8)
<autogenerated>:1: non-retpoline-compatible: 00020 (/home/username/works/openwrt/staging_dir/hostpkg/lib/go-cross/src/runtime/alg.go:156)        JMP     (SI)(DX*8)
<autogenerated>:1: too many errors
```

Fixes: #20026

Signed-off-by: Tianling Shen <cnsztl@immortalwrt.org>
This commit is contained in:
Tianling Shen 2022-12-27 13:18:29 +08:00 committed by Tianling Shen
parent ea969a117a
commit 35f33c99b1
1 changed files with 67 additions and 0 deletions

View File

@ -0,0 +1,67 @@
From 156578067111742b55718066c91b8ec66d35e03d Mon Sep 17 00:00:00 2001
From: Keith Randall <khr@golang.org>
Date: Mon, 5 Dec 2022 16:26:26 -0800
Subject: [PATCH] [release-branch.go1.19] cmd/compile: turn off jump tables
when spectre retpolines are on
Fixes #57100
Change-Id: I6ab659abbca1ae0ac8710674d39aec116fab0baa
Reviewed-on: https://go-review.googlesource.com/c/go/+/455336
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
(cherry picked from commit 1eb0465fa596a2d6e9c1a632499989544f0d7e68)
Reviewed-on: https://go-review.googlesource.com/c/go/+/455416
Reviewed-by: Michael Pratt <mpratt@google.com>
---
src/cmd/compile/internal/walk/switch.go | 2 +-
test/codegen/retpoline.go | 28 +++++++++++++++++++++++++
2 files changed, 29 insertions(+), 1 deletion(-)
--- a/src/cmd/compile/internal/walk/switch.go
+++ b/src/cmd/compile/internal/walk/switch.go
@@ -289,7 +289,7 @@ func (s *exprSwitch) tryJumpTable(cc []e
const minCases = 8 // have at least minCases cases in the switch
const minDensity = 4 // use at least 1 out of every minDensity entries
- if !go119UseJumpTables || base.Flag.N != 0 || !ssagen.Arch.LinkArch.CanJumpTable {
+ if !go119UseJumpTables || base.Flag.N != 0 || !ssagen.Arch.LinkArch.CanJumpTable || base.Ctxt.Retpoline {
return false
}
if len(cc) < minCases {
--- a/test/codegen/retpoline.go
+++ b/test/codegen/retpoline.go
@@ -12,3 +12,31 @@ func CallInterface(x interface{ M() }) {
// amd64:`CALL\truntime.retpoline`
x.M()
}
+
+// Check to make sure that jump tables are disabled
+// when retpoline is on. See issue 57097.
+func noJumpTables(x int) int {
+ switch x {
+ case 0:
+ return 0
+ case 1:
+ return 1
+ case 2:
+ return 2
+ case 3:
+ return 3
+ case 4:
+ return 4
+ case 5:
+ return 5
+ case 6:
+ return 6
+ case 7:
+ return 7
+ case 8:
+ return 8
+ case 9:
+ return 9
+ }
+ return 10
+}