Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: _cgoCheckPointer call occurs too early for deferred function calls. #15921

Closed
bcmills opened this issue Jun 1, 2016 · 2 comments
Closed

Comments

@bcmills
Copy link
Contributor

bcmills commented Jun 1, 2016

I found a related-but-different problem when investigating issue #13830.

Making the _cgoCheckPointer call explicitly in the function parameters of a deferred call causes the pointer check to occur at the wrong time. We end up checking whether the argument had a Go pointer at the point where the call was deferred, not whether the argument had a Go pointer at the point where the C call was actually made.

Consider this program:

package main

/*
#include <stdint.h>

struct sneaky {
  int *p;
};

void sneak(struct sneaky *s, uintptr_t *u) {
  *u = (uintptr_t)(s->p);
}
*/
import "C"

import "fmt"

func main() {
    var s C.struct_sneaky
    var u C.uintptr_t

    defer func() { fmt.Println(u) }()
    defer C.sneak(&s, &u)

    var escapee C.int
    s.p = &escapee
}

With go version go1.6.1 linux/amd64, the program (incorrectly) prints a leaked address and exits with code 0.

Changing the call to C.sneak to an explicit call at the end of main causes the program to (correctly) panic with a cgo pointer violation.

The problem is that the _cgoCheckPointer calls wrap the function's arguments, not the function call itself:

  defer _Cfunc_sneak(_cgoCheckPointer((*_Ctype_struct_sneaky)(&s), true).(*_Ctype_struct_sneaky), &u)
@bcmills bcmills changed the title cmd/cgo: _cgoCheckPointer call occurs at wrong time for deferred function calls. cmd/cgo: _cgoCheckPointer call occurs to early for deferred function calls. Jun 1, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7Maybe milestone Jun 1, 2016
@ianlancetaylor ianlancetaylor self-assigned this Jun 1, 2016
@ianlancetaylor
Copy link
Contributor

Nice example.

@bcmills bcmills changed the title cmd/cgo: _cgoCheckPointer call occurs to early for deferred function calls. cmd/cgo: _cgoCheckPointer call occurs too early for deferred function calls. Jun 1, 2016
@gopherbot
Copy link

CL https://golang.org/cl/23650 mentions this issue.

@golang golang locked and limited conversation to collaborators Jun 3, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants