]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: add a test to test RODATA is indeed read-only
authorCherry Zhang <cherryyz@google.com>
Thu, 10 Sep 2020 15:14:27 +0000 (11:14 -0400)
committerCherry Zhang <cherryyz@google.com>
Fri, 11 Sep 2020 15:41:20 +0000 (15:41 +0000)
Updates #38830.

Change-Id: Ie1f6ccef40a773f038aac587dfc26bf70a1a8536
Reviewed-on: https://go-review.googlesource.com/c/go/+/253921
Run-TryBot: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/link/link_test.go
src/cmd/link/testdata/testRO/x.go [new file with mode: 0644]

index 98798be4653ad1b0af759c85c8e88e4702c65c85..4e60996d8e65cbe088e1aaee0cea2fa0ce1675e4 100644 (file)
@@ -800,3 +800,17 @@ func TestContentAddressableSymbols(t *testing.T) {
                t.Errorf("command %s failed: %v\n%s", cmd, err, out)
        }
 }
+
+func TestReadOnly(t *testing.T) {
+       // Test that read-only data is indeed read-only.
+       testenv.MustHaveGoBuild(t)
+
+       t.Parallel()
+
+       src := filepath.Join("testdata", "testRO", "x.go")
+       cmd := exec.Command(testenv.GoToolPath(t), "run", src)
+       out, err := cmd.CombinedOutput()
+       if err == nil {
+               t.Errorf("running test program did not fail. output:\n%s", out)
+       }
+}
diff --git a/src/cmd/link/testdata/testRO/x.go b/src/cmd/link/testdata/testRO/x.go
new file mode 100644 (file)
index 0000000..d77db6d
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test that read-only data is indeed read-only. This
+// program attempts to modify read-only data, and it
+// should fail.
+
+package main
+
+import "unsafe"
+
+var s = "hello"
+
+func main() {
+       println(s)
+       *(*struct {
+               p *byte
+               l int
+       })(unsafe.Pointer(&s)).p = 'H'
+       println(s)
+}