delve/_fixtures/testunsafepointers.go
Andrei Matei aee401b69a
pkg/proc: populate pointer values (#3229)
* proc: add a test for dangling unsafe pointers

This new tests checks the behavior when dereferencing dangling pointers.
The behavior does not fully make sense; the test checks the current
behavior for now, which will be improved in subsequent commits.

* proc: populate pointer values

This patch changes how Value and Unreadable are populated for pointer
Variables. Before this patch, variables of kind reflect.Ptr did not have
their Value field populated. This patch populates it in
Variable.loadValue(), which seems natural and consistent with other
types of variables. The Value is the address that the pointer points to.
The Unreadable field was populated inconsistently for pointer variables:
it was never populated for an outer pointer, but it could be populated
for an inner pointer in pointer-to-pointer types. Before this patch,
in pointer whose value could not be read was not easily distinguishable
from a pointer with a value that could be read, but that cannot be
dereferenced (i.e. a dangling pointer): neither of these would be marked
as Unreadable, and both would have a child marked as Unreadable. This
patch makes it so that a pointer variable whose pointer value cannot be
read is marked as Unreadable.

Using this new distinction, this patch fixes a bug around dereferencing
dangling pointers: before, attempting such a dereference produced a
"nil pointer dereference" error. This was bogus, since the pointer was
not nil. Now, we are more discerning and generate a different error.
2023-01-04 09:07:23 -08:00

18 lines
412 B
Go

package main
import (
"runtime"
"unsafe"
)
func main() {
// We're going to produce a pointer with a bad address.
badAddr := uintptr(0x42)
unsafeDanglingPtrPtr := unsafe.Pointer(badAddr)
// We produce a **int, instead of more simply a *int, in order for the test
// program to test more complex Delve behavior.
danglingPtrPtr := (**int)(unsafeDanglingPtrPtr)
_ = danglingPtrPtr
runtime.Breakpoint()
}