]> Cypherpunks repositories - gostls13.git/commitdiff
gc: treat uintptr as potentially containing a pointer
authorDmitriy Vyukov <dvyukov@google.com>
Mon, 17 Oct 2011 19:14:07 +0000 (15:14 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 17 Oct 2011 19:14:07 +0000 (15:14 -0400)
Fixes #2376

R=golang-dev, lvd, rsc
CC=golang-dev
https://golang.org/cl/5278048

src/cmd/gc/reflect.c
src/pkg/runtime/gc_test.go

index ca7d08e511a756bc9c633435759247d4b936f8f4..4ce16957929aadf31008a6dac53ac7d97a1bd067 100644 (file)
@@ -516,7 +516,6 @@ haspointers(Type *t)
        case TUINT32:
        case TINT64:
        case TUINT64:
-       case TUINTPTR:
        case TFLOAT32:
        case TFLOAT64:
        case TBOOL:
@@ -534,6 +533,7 @@ haspointers(Type *t)
        case TPTR32:
        case TPTR64:
        case TUNSAFEPTR:
+       case TUINTPTR:
        case TINTER:
        case TCHAN:
        case TMAP:
index fad60a368041c7b457791f68b7345a595326e928..c299ba20e7aaf1ea4b294a87aa1476546909a3ea 100644 (file)
@@ -3,6 +3,7 @@ package runtime_test
 import (
        "runtime"
        "testing"
+       "unsafe"
 )
 
 func TestGcSys(t *testing.T) {
@@ -22,3 +23,73 @@ func TestGcSys(t *testing.T) {
 func workthegc() []byte {
        return make([]byte, 1029)
 }
+
+func TestGcUintptr(t *testing.T) {
+       p1 := unsafe.Pointer(new(int))
+       *(*int)(unsafe.Pointer(p1)) = 42
+       p2 := uintptr(unsafe.Pointer(new(int)))
+       *(*int)(unsafe.Pointer(p2)) = 42
+       var a1 [1]unsafe.Pointer
+       a1[0] = unsafe.Pointer(new(int))
+       *(*int)(unsafe.Pointer(a1[0])) = 42
+       var a2 [1]uintptr
+       a2[0] = uintptr(unsafe.Pointer(new(int)))
+       *(*int)(unsafe.Pointer(a2[0])) = 42
+       s1 := make([]unsafe.Pointer, 1)
+       s1[0] = unsafe.Pointer(new(int))
+       *(*int)(unsafe.Pointer(s1[0])) = 42
+       s2 := make([]uintptr, 1)
+       s2[0] = uintptr(unsafe.Pointer(new(int)))
+       *(*int)(unsafe.Pointer(s2[0])) = 42
+       m1 := make(map[int]unsafe.Pointer)
+       m1[0] = unsafe.Pointer(new(int))
+       *(*int)(unsafe.Pointer(m1[0])) = 42
+       m2 := make(map[int]uintptr)
+       m2[0] = uintptr(unsafe.Pointer(new(int)))
+       *(*int)(unsafe.Pointer(m2[0])) = 42
+       c1 := make(chan unsafe.Pointer, 1)
+       func() {
+               p := new(int)
+               *p = 42
+               c1 <- unsafe.Pointer(p)
+       }()
+       c2 := make(chan uintptr, 1)
+       func() {
+               p := new(int)
+               *p = 42
+               c2 <- uintptr(unsafe.Pointer(p))
+       }()
+
+       runtime.GC()
+
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(p1))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("p1 is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(p2))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("p2 is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(a1[0]))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("a1[0] is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(a2[0]))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("a2[0] is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(s1[0]))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("s1[0] is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(s2[0]))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("s2[0] is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(m1[0]))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("m1[0] is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(m2[0]))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("m2[0] is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(<-c1))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("<-c1 is freed")
+       }
+       if p, _ := runtime.Lookup((*byte)(unsafe.Pointer(<-c2))); p == nil || *(*int)(unsafe.Pointer(p)) != 42 {
+               t.Fatalf("<-c2 is freed")
+       }
+}