cmd/compile, runtime: add checkptr instrumentation for unsafe.Add

Fixes #74431

Change-Id: Id651ea0b82599ccaff8816af0a56ddbb149b6f89
Reviewed-on: https://go-review.googlesource.com/c/go/+/692015
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: t hepudds <thepudds1460@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Cuong Manh Le 2025-07-29 18:45:23 +07:00 committed by Gopher Robot
parent fcc036f03b
commit 5ab9f23977
3 changed files with 15 additions and 0 deletions

View file

@ -131,6 +131,14 @@ func walkExpr1(n ir.Node, init *ir.Nodes) ir.Node {
n := n.(*ir.BinaryExpr) n := n.(*ir.BinaryExpr)
n.X = walkExpr(n.X, init) n.X = walkExpr(n.X, init)
n.Y = walkExpr(n.Y, init) n.Y = walkExpr(n.Y, init)
if n.Op() == ir.OUNSAFEADD && ir.ShouldCheckPtr(ir.CurFunc, 1) {
// For unsafe.Add(p, n), just walk "unsafe.Pointer(uintptr(p)+uintptr(n))"
// for the side effects of validating unsafe.Pointer rules.
x := typecheck.ConvNop(n.X, types.Types[types.TUINTPTR])
y := typecheck.Conv(n.Y, types.Types[types.TUINTPTR])
conv := typecheck.ConvNop(ir.NewBinaryExpr(n.Pos(), ir.OADD, x, y), types.Types[types.TUNSAFEPTR])
walkExpr(conv, init)
}
return n return n
case ir.OUNSAFESLICE: case ir.OUNSAFESLICE:

View file

@ -35,6 +35,7 @@ func TestCheckPtr(t *testing.T) {
{"CheckPtrAlignmentNilPtr", ""}, {"CheckPtrAlignmentNilPtr", ""},
{"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"}, {"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
{"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"}, {"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
{"CheckPtrArithmeticUnsafeAdd", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
{"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"}, {"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
{"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"}, {"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"},
{"CheckPtrSliceOK", ""}, {"CheckPtrSliceOK", ""},

View file

@ -16,6 +16,7 @@ func init() {
register("CheckPtrAlignmentNilPtr", CheckPtrAlignmentNilPtr) register("CheckPtrAlignmentNilPtr", CheckPtrAlignmentNilPtr)
register("CheckPtrArithmetic", CheckPtrArithmetic) register("CheckPtrArithmetic", CheckPtrArithmetic)
register("CheckPtrArithmetic2", CheckPtrArithmetic2) register("CheckPtrArithmetic2", CheckPtrArithmetic2)
register("CheckPtrArithmeticUnsafeAdd", CheckPtrArithmeticUnsafeAdd)
register("CheckPtrSize", CheckPtrSize) register("CheckPtrSize", CheckPtrSize)
register("CheckPtrSmall", CheckPtrSmall) register("CheckPtrSmall", CheckPtrSmall)
register("CheckPtrSliceOK", CheckPtrSliceOK) register("CheckPtrSliceOK", CheckPtrSliceOK)
@ -79,6 +80,11 @@ func CheckPtrArithmetic2() {
sink2 = unsafe.Pointer(uintptr(p) & ^one) sink2 = unsafe.Pointer(uintptr(p) & ^one)
} }
func CheckPtrArithmeticUnsafeAdd() {
data := make([]byte, 128)
sink2 = (*byte)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(data)), len(data)))
}
func CheckPtrSize() { func CheckPtrSize() {
p := new(int64) p := new(int64)
sink2 = p sink2 = p