Skip to content

Commit 97597bd

Browse files
committed
runtime (gc): fix scanning of memory for which the type is unknown
1 parent 61da7aa commit 97597bd

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/runtime/gc_list.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ func markRoot(addr, root uintptr) {
6262
}
6363

6464
func markRoots(start, end uintptr) {
65-
ptrSize, ptrAlign := unsafe.Sizeof(unsafe.Pointer(nil)), unsafe.Alignof(unsafe.Pointer(nil))
66-
67-
// Align the start and end.
68-
start = align(start)
69-
end &^= ptrAlign - 1
70-
71-
// Shift the end down so that we do not read past it.
72-
end -= ptrSize - ptrAlign
73-
7465
// Scan the data as a []unsafe.Pointer, using a temporary gcType value.
7566
// In the future, we can do precise scanning of the globals instead.
7667
t := struct {
@@ -120,7 +111,14 @@ func (t *gcType) scan(start, end uintptr) {
120111
return
121112
}
122113

123-
ptrAlign := unsafe.Alignof(unsafe.Pointer(nil))
114+
ptrSize, ptrAlign := unsafe.Sizeof(unsafe.Pointer(nil)), unsafe.Alignof(unsafe.Pointer(nil))
115+
116+
// Align the start and end.
117+
start = align(start)
118+
end &^= ptrAlign - 1
119+
120+
// Shift the end down so that we do not read past it.
121+
end -= ptrSize - ptrAlign
124122

125123
width := t.size
126124

@@ -274,13 +272,22 @@ searchLoop:
274272
return mem
275273
}
276274

275+
// anyPtrType is a special fake type that is used when the type of an allocation is not known.
276+
var anyPtrType = struct {
277+
t gcType
278+
data [1]byte
279+
}{
280+
t: gcType{
281+
size: 1, // 1 pointer-width
282+
},
283+
data: [1]byte{1}, // scan the pointer-width data
284+
}
285+
277286
// alloc a chunk of untyped memory.
278287
//go:inline
279288
func alloc(size uintptr) unsafe.Pointer {
280-
ptrSize := unsafe.Sizeof(unsafe.Pointer(nil))
281-
size += ptrSize - 1
282-
buf := make([]unsafe.Pointer, size/ptrSize)
283-
return *(*unsafe.Pointer)(unsafe.Pointer(&buf))
289+
// Use a placeholder type to scan the entire thing.
290+
return allocTyped(size, &anyPtrType.t.size)
284291
}
285292

286293
// allocTyped tries to find some free space on the heap, possibly doing a garbage

0 commit comments

Comments
 (0)