Skip to content

Commit 1ed4f12

Browse files
committed
cmd/link: add a test to test RODATA is indeed read-only
Updates #38830. Change-Id: Ie1f6ccef40a773f038aac587dfc26bf70a1a8536 Reviewed-on: https://go-review.googlesource.com/c/go/+/253921 Run-TryBot: Cherry Zhang <[email protected]> Reviewed-by: Than McIntosh <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent ffd95aa commit 1ed4f12

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/cmd/link/link_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,17 @@ func TestContentAddressableSymbols(t *testing.T) {
800800
t.Errorf("command %s failed: %v\n%s", cmd, err, out)
801801
}
802802
}
803+
804+
func TestReadOnly(t *testing.T) {
805+
// Test that read-only data is indeed read-only.
806+
testenv.MustHaveGoBuild(t)
807+
808+
t.Parallel()
809+
810+
src := filepath.Join("testdata", "testRO", "x.go")
811+
cmd := exec.Command(testenv.GoToolPath(t), "run", src)
812+
out, err := cmd.CombinedOutput()
813+
if err == nil {
814+
t.Errorf("running test program did not fail. output:\n%s", out)
815+
}
816+
}

src/cmd/link/testdata/testRO/x.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Test that read-only data is indeed read-only. This
6+
// program attempts to modify read-only data, and it
7+
// should fail.
8+
9+
package main
10+
11+
import "unsafe"
12+
13+
var s = "hello"
14+
15+
func main() {
16+
println(s)
17+
*(*struct {
18+
p *byte
19+
l int
20+
})(unsafe.Pointer(&s)).p = 'H'
21+
println(s)
22+
}

0 commit comments

Comments
 (0)