mirror of
https://github.com/go-delve/delve.git
synced 2025-10-28 12:47:22 +08:00
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.
This commit is contained in:
@ -1610,3 +1610,66 @@ func TestEvalExpressionGenerics(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Test the behavior when reading dangling pointers produced by unsafe code.
|
||||
func TestBadUnsafePtr(t *testing.T) {
|
||||
withTestProcess("testunsafepointers", t, func(p *proc.Target, fixture protest.Fixture) {
|
||||
assertNoError(p.Continue(), t, "Continue()")
|
||||
|
||||
// danglingPtrPtr is a pointer with value 0x42, which is an unreadable
|
||||
// address.
|
||||
danglingPtrPtr, err := evalVariableWithCfg(p, "danglingPtrPtr", pnormalLoadConfig)
|
||||
assertNoError(err, t, "eval returned an error")
|
||||
t.Logf("danglingPtrPtr (%s): unreadable: %v. addr: 0x%x, value: %v",
|
||||
danglingPtrPtr.TypeString(), danglingPtrPtr.Unreadable, danglingPtrPtr.Addr, danglingPtrPtr.Value)
|
||||
assertNoError(danglingPtrPtr.Unreadable, t, "danglingPtrPtr is unreadable")
|
||||
if val := danglingPtrPtr.Value; val == nil {
|
||||
t.Fatal("Value not set danglingPtrPtr")
|
||||
}
|
||||
val, ok := constant.Uint64Val(danglingPtrPtr.Value)
|
||||
if !ok {
|
||||
t.Fatalf("Value not uint64: %v", danglingPtrPtr.Value)
|
||||
}
|
||||
if val != 0x42 {
|
||||
t.Fatalf("expected value to be 0x42, got 0x%x", val)
|
||||
}
|
||||
if len(danglingPtrPtr.Children) != 1 {
|
||||
t.Fatalf("expected 1 child, got: %d", len(danglingPtrPtr.Children))
|
||||
}
|
||||
|
||||
badPtr, err := evalVariableWithCfg(p, "*danglingPtrPtr", pnormalLoadConfig)
|
||||
assertNoError(err, t, "error evaluating *danglingPtrPtr")
|
||||
t.Logf("badPtr: (%s): unreadable: %v. addr: 0x%x, value: %v",
|
||||
badPtr.TypeString(), badPtr.Unreadable, badPtr.Addr, badPtr.Value)
|
||||
if badPtr.Unreadable == nil {
|
||||
t.Fatalf("badPtr should be unreadable")
|
||||
}
|
||||
if badPtr.Addr != 0x42 {
|
||||
t.Fatalf("expected danglingPtr to point to 0x42, got 0x%x", badPtr.Addr)
|
||||
}
|
||||
if len(badPtr.Children) != 1 {
|
||||
t.Fatalf("expected 1 child, got: %d", len(badPtr.Children))
|
||||
}
|
||||
badPtrChild := badPtr.Children[0]
|
||||
t.Logf("badPtr.Child (%s): unreadable: %v. addr: 0x%x, value: %v",
|
||||
badPtrChild.TypeString(), badPtrChild.Unreadable, badPtrChild.Addr, badPtrChild.Value)
|
||||
// We expect the dummy child variable to be marked as unreadable.
|
||||
if badPtrChild.Unreadable == nil {
|
||||
t.Fatalf("expected x to be unreadable, but got value: %v", badPtrChild.Value)
|
||||
}
|
||||
|
||||
// Evaluating **danglingPtrPtr fails.
|
||||
_, err = evalVariableWithCfg(p, "**danglingPtrPtr", pnormalLoadConfig)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error doing **danglingPtrPtr")
|
||||
}
|
||||
expErr := "couldn't read pointer"
|
||||
if !strings.Contains(err.Error(), expErr) {
|
||||
t.Fatalf("expected \"%s\", got: \"%s\"", expErr, err)
|
||||
}
|
||||
nexpErr := "nil pointer dereference"
|
||||
if strings.Contains(err.Error(), nexpErr) {
|
||||
t.Fatalf("shouldn't have gotten \"%s\", but got: \"%s\"", nexpErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user