From fadad851a3222867b374e901ede9c4919594837f Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 21 Apr 2021 02:11:15 -0700 Subject: cmd/compile: implement unsafe.Add and unsafe.Slice Updates #19367. Updates #40481. Change-Id: Iabd2afdd0d520e5d68fd9e6dedd013335a4b3886 Reviewed-on: https://go-review.googlesource.com/c/go/+/312214 Run-TryBot: Matthew Dempsky Trust: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le Reviewed-by: Keith Randall --- src/cmd/compile/internal/typecheck/typecheck.go | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/cmd/compile/internal/typecheck/typecheck.go') diff --git a/src/cmd/compile/internal/typecheck/typecheck.go b/src/cmd/compile/internal/typecheck/typecheck.go index 00dd44b96b..1650144375 100644 --- a/src/cmd/compile/internal/typecheck/typecheck.go +++ b/src/cmd/compile/internal/typecheck/typecheck.go @@ -775,6 +775,14 @@ func typecheck1(n ir.Node, top int) ir.Node { n := n.(*ir.CallExpr) return tcRecover(n) + case ir.OUNSAFEADD: + n := n.(*ir.BinaryExpr) + return tcUnsafeAdd(n) + + case ir.OUNSAFESLICE: + n := n.(*ir.BinaryExpr) + return tcUnsafeSlice(n) + case ir.OCLOSURE: n := n.(*ir.ClosureExpr) tcClosure(n, top) @@ -1934,6 +1942,35 @@ func checkmake(t *types.Type, arg string, np *ir.Node) bool { return true } +// checkunsafeslice is like checkmake but for unsafe.Slice. +func checkunsafeslice(np *ir.Node) bool { + n := *np + if !n.Type().IsInteger() && n.Type().Kind() != types.TIDEAL { + base.Errorf("non-integer len argument in unsafe.Slice - %v", n.Type()) + return false + } + + // Do range checks for constants before DefaultLit + // to avoid redundant "constant NNN overflows int" errors. + if n.Op() == ir.OLITERAL { + v := toint(n.Val()) + if constant.Sign(v) < 0 { + base.Errorf("negative len argument in unsafe.Slice") + return false + } + if ir.ConstOverflow(v, types.Types[types.TINT]) { + base.Errorf("len argument too large in unsafe.Slice") + return false + } + } + + // DefaultLit is necessary for non-constants too: n might be 1.1<