Skip to content

Commit 5ab9f23

Browse files
cuonglmgopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: t hepudds <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent fcc036f commit 5ab9f23

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/cmd/compile/internal/walk/expr.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ func walkExpr1(n ir.Node, init *ir.Nodes) ir.Node {
131131
n := n.(*ir.BinaryExpr)
132132
n.X = walkExpr(n.X, init)
133133
n.Y = walkExpr(n.Y, init)
134+
if n.Op() == ir.OUNSAFEADD && ir.ShouldCheckPtr(ir.CurFunc, 1) {
135+
// For unsafe.Add(p, n), just walk "unsafe.Pointer(uintptr(p)+uintptr(n))"
136+
// for the side effects of validating unsafe.Pointer rules.
137+
x := typecheck.ConvNop(n.X, types.Types[types.TUINTPTR])
138+
y := typecheck.Conv(n.Y, types.Types[types.TUINTPTR])
139+
conv := typecheck.ConvNop(ir.NewBinaryExpr(n.Pos(), ir.OADD, x, y), types.Types[types.TUNSAFEPTR])
140+
walkExpr(conv, init)
141+
}
134142
return n
135143

136144
case ir.OUNSAFESLICE:

src/runtime/checkptr_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestCheckPtr(t *testing.T) {
3535
{"CheckPtrAlignmentNilPtr", ""},
3636
{"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
3737
{"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
38+
{"CheckPtrArithmeticUnsafeAdd", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
3839
{"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
3940
{"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"},
4041
{"CheckPtrSliceOK", ""},

src/runtime/testdata/testprog/checkptr.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func init() {
1616
register("CheckPtrAlignmentNilPtr", CheckPtrAlignmentNilPtr)
1717
register("CheckPtrArithmetic", CheckPtrArithmetic)
1818
register("CheckPtrArithmetic2", CheckPtrArithmetic2)
19+
register("CheckPtrArithmeticUnsafeAdd", CheckPtrArithmeticUnsafeAdd)
1920
register("CheckPtrSize", CheckPtrSize)
2021
register("CheckPtrSmall", CheckPtrSmall)
2122
register("CheckPtrSliceOK", CheckPtrSliceOK)
@@ -79,6 +80,11 @@ func CheckPtrArithmetic2() {
7980
sink2 = unsafe.Pointer(uintptr(p) & ^one)
8081
}
8182

83+
func CheckPtrArithmeticUnsafeAdd() {
84+
data := make([]byte, 128)
85+
sink2 = (*byte)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(data)), len(data)))
86+
}
87+
8288
func CheckPtrSize() {
8389
p := new(int64)
8490
sink2 = p

0 commit comments

Comments
 (0)