-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cycle detection in internal/value.Format
The cycle detection in value.Format uses a map of uintptrs, which is not correct as this assumes that the Go GC never uses a moving collector. Instead, we should be using unsafe.Pointer, which the GC knows how to scan. Also, push pointer checking of slices down to the level of individual elements. We do this because the slice pointer is not sufficient to determine equality. For example, v[:0] and v[:1] both have the same slice pointer, but are clearly different values. Since the purego environment forbids the use of unsafe, create a Pointer type that is an abstraction over an opaque pointer. The Pointer type can only be compared. In the purego, continue to use uintptr, which is the best we can do.
- Loading branch information
Showing
4 changed files
with
82 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2018, 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.md file. | ||
|
||
// +build purego | ||
|
||
package value | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// Pointer is an opaque typed pointer and is guaranteed to be comparable. | ||
type Pointer struct { | ||
p uintptr | ||
t reflect.Type | ||
} | ||
|
||
// PointerOf returns a Pointer from v, which must be a | ||
// reflect.Ptr, reflect.Slice, or reflect.Map. | ||
func PointerOf(v reflect.Value) Pointer { | ||
// NOTE: Storing a pointer as an uintptr is technically incorrect as it | ||
// assumes that the GC implementation does not use a moving collector. | ||
return Pointer{v.Pointer(), v.Type()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2018, 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.md file. | ||
|
||
// +build !purego | ||
|
||
package value | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// Pointer is an opaque typed pointer and is guaranteed to be comparable. | ||
type Pointer struct { | ||
p unsafe.Pointer | ||
t reflect.Type | ||
} | ||
|
||
// PointerOf returns a Pointer from v, which must be a | ||
// reflect.Ptr, reflect.Slice, or reflect.Map. | ||
func PointerOf(v reflect.Value) Pointer { | ||
// The proper representation of a pointer is unsafe.Pointer, | ||
// which is necessary if the GC ever uses a moving collector. | ||
return Pointer{unsafe.Pointer(v.Pointer()), v.Type()} | ||
} |