go: runtime: canonical 48-bit address bug in lfstack

What version of Go are you using (go version)?

go version go1.16.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ryanh/.cache/go-build"
GOENV="/home/ryanh/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ryanh/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ryanh/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.16"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.16/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1471403295=/tmp/go-build -gno-record-gcc-switches"

What did you do?

If lfstack gets any pointer on amd64 platforms where the top bit is set then an assert is thrown. In most cases this won’t occur since the kernel will consume the top bit of VA, leaving the userspace with 47bits of VA. This is a logic bug around address canonicalization.

Check is here: https://github.com/golang/go/blob/master/src/runtime/lfstack.go#L28 Pointer sign extension is here: https://github.com/golang/go/blob/master/src/runtime/lfstack_64bit.go#L52

I don’t have a basic application or environment for reproducing this. Easy to see the logic bug.

What did you expect to see?

While it is correct that you must canonicalize the pointer by sign extending it on 48bit systems (I’m not even going to bring up LA57). The check inside of lfstack is incorrect since it assumes the sign extension will never occur. Thus assuming it will never receive a true 48-bit pointer. This assumption is broken in environments where you get 48-bit pointers with the high bit set. This is purely a simple logic bug, it shouldn’t assert in this case.

What did you see instead?

An assert on 48-bit address with high bit set.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 44 (21 by maintainers)

Commits related to this issue

Most upvoted comments

Not backporting the CL is really all the justification I’d need to drop the old versions, so I wouldn’t do any difficult backporting work if it were just for FreeBSD’s benefit.

A backport would be available earlier than the 1.25 release. (In a few weeks, say, versus a few months for 1.25.) But point taken, thanks.

Ahh ok, well I was pretty optimistic there. It would of course be nice to get this into the ports tree sooner rather than later so we can stop universally limiting pointer width on the builders (with all of its downstream effects).

So, if it’s possible to backport to 1.24, it’d make a difference for us. But we also understand that sometimes dicey is dicey.

Just to clarify: on Linux, I believe mmap will only return an address above 1<<47 if you pass a hint address above 1<<47 to mmap. I assume this is not true on FreeBSD? That mmap with hint address of 0 can return a higher address unless the elfctl -e +la48 is set?

Correct, if LA57 is enabled, then mmap(NULL, …) can return addresses above 1 << 47. The main thread stack is also likely to be above 1 << 47 as well since main thread stacks are typically allocated at the “top” of the user address space.

Hi @randall77 On FreeBSD (developer branch), we are noticing this issue on newer CPUs that have the LA57 feature.

Building Go cmd/dist using /home/dbaio/ports/lang/go121/work/go-freebsd-amd64-bootstrap. (go1.20 freebsd/amd64)
cmd/dist
runtime: lfstack.push invalid packing: node=0x6d66afc44d8d40 cnt=0x1 packed=0x66afc44d8d400001 -> node=0x66afc44d8d40
fatal error: lfstack.push

runtime stack:
runtime.throw({0xa3a2b1?, 0xaffb50e700000000?})
	/usr/home/dg/go/go-bootstrap/go-freebsd-amd64-bootstrap/src/runtime/panic.go:1047 +0x5d fp=0x84c48df00 sp=0x84c48ded0 pc=0x43457d
runtime.(*lfstack).push(0x2?, 0x84c48dfb0?)
	/usr/home/dg/go/go-bootstrap/go-freebsd-amd64-bootstrap/src/runtime/lfstack.go:29 +0x125 fp=0x84c48df40 sp=0x84c48df00 pc=0x40bbc5
runtime.(*spanSetBlockAlloc).free(...)
	/usr/home/dg/go/go-bootstrap/go-freebsd-amd64-bootstrap/src/runtime/mspanset.go:322
runtime.(*spanSet).reset(0xe828b0)
	/usr/home/dg/go/go-bootstrap/go-freebsd-amd64-bootstrap/src/runtime/mspanset.go:264 +0x87 fp=0x84c48df70 sp=0x84c48df40 pc=0x42f727
runtime.finishsweep_m()
	/usr/home/dg/go/go-bootstrap/go-freebsd-amd64-bootstrap/src/runtime/mgcsweep.go:260 +0x9b fp=0x84c48dfb0 sp=0x84c48df70 pc=0x423b1b
runtime.gcStart.func1()
	/usr/home/dg/go/go-bootstrap/go-freebsd-amd64-bootstrap/src/runtime/mgc.go:668 +0x17 fp=0x84c48dfc0 sp=0x84c48dfb0 pc=0x45ec17
runtime.systemstack()
	/usr/home/dg/go/go-bootstrap/go-freebsd-amd64-bootstrap/src/runtime/asm_amd64.s:496 +0x49 fp=0x84c48dfc8 sp=0x84c48dfc0 pc=0x463d69

Full build log is here: https://people.freebsd.org/~dbaio/go/go121-gohan06-logs.txt

I can run some tests. Just guide me, please.

Details about the CPU:

CPU: Intel(R) Xeon(R) Silver 4314 CPU @ 2.40GHz (2400.00-MHz K8-class CPU)
  Origin="GenuineIntel"  Id=0x606a6  Family=0x6  Model=0x6a  Stepping=6
  Features=0xbfebfbff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CLFLUSH,DTS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE>
  Features2=0x7ffefbff<SSE3,PCLMULQDQ,DTES64,MON,DS_CPL,VMX,SMX,EST,TM2,SSSE3,SDBG,FMA,CX16,xTPR,PDCM,PCID,DCA,SSE4.1,SSE4.2,x2APIC,MOVBE,POPCNT,TSCDLT,AESNI,XSAVE,OSXSAVE,AVX,F16C,RDRAND>
  AMD Features=0x2c100800<SYSCALL,NX,Page1GB,RDTSCP,LM>
  AMD Features2=0x121<LAHF,ABM,Prefetch>
  Structured Extended Features=0xf3bfbfff<FSGSBASE,TSCADJ,SGX,BMI1,HLE,AVX2,FDPEXC,SMEP,BMI2,ERMS,INVPCID,RTM,PQM,NFPUSG,PQE,AVX512F,AVX512DQ,RDSEED,ADX,SMAP,AVX512IFMA,CLFLUSHOPT,CLWB,PROCTRACE,AVX512CD,SHA,AVX512BW,AVX512VL>
  Structured Extended Features2=0x40417f5e<AVX512VBMI,UMIP,PKU,OSPKE,AVX512VBMI2,GFNI,VAES,VPCLMULQDQ,AVX512VNNI,AVX512BITALG,TME,AVX512VPOPCNTDQ,LA57,RDPID,SGXLC>
  Structured Extended Features3=0xbc040412<FSRM,MD_CLEAR,PCONFIG,IBPB,STIBP,L1DFL,ARCH_CAP,SSBD>
  XSAVE Features=0xf<XSAVEOPT,XSAVEC,XINUSE,XSAVES>
  IA32_ARCH_CAPS=0x6bdeb<RDCL_NO,IBRS_ALL,SKIP_L1DFL_VME,MDS_NO,TSX_CTRL,TAA_NO>
  AMD Extended Feature Extensions ID EBX=0x200<WBNOINVD>
  VT-x: PAT,HLT,MTF,PAUSE,EPT,UG,VPID,VID,PostIntr
  TSC: P-state invariant, performance statistics

When building on another box (same FreeBSD version) that doesn’t have LA57, it works just fine.