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: treat Windows HANDLE as non-pointer type #42018

Open
eliasnaur opened this issue Oct 16, 2020 · 0 comments
Open

cmd/cgo: treat Windows HANDLE as non-pointer type #42018

eliasnaur opened this issue Oct 16, 2020 · 0 comments

Comments

@eliasnaur
Copy link
Contributor

@eliasnaur eliasnaur commented Oct 16, 2020

The Windows HANDLE type is translated to an unsafe.Pointer in by cgo, but can contain non-pointer values; it should be treated accordingly. Incomplete pointer types such as the JNI types are covered by a rule so that they're not treated as pointers. However, that rule does not match HANDLE, which is a void *.

From golang-dev:

package main

import (
    "fmt"
    "os"
    "path/filepath"
    "runtime"

    "golang.org/x/sys/windows"
)

/*
#include <windows.h>
*/
import "C"

func main() {
    name := filepath.Join(os.TempDir(), "handletest.tmp")
    name16, err := windows.UTF16PtrFromString(name)
    if err != nil {
        panic(err)
    }
    fmt.Println("Using filepath", name)

    handle := C.CreateFileW((*C.wchar_t)(name16), C.GENERIC_READ, C.FILE_SHARE_READ|C.FILE_SHARE_WRITE, nil, C.CREATE_ALWAYS, 0, nil)
    if handle == C.HANDLE(C.INVALID_HANDLE_VALUE) {
        panic("failed to open")
    }

    fmt.Println("handle addr", handle)

    runtime.GC()

    rtn := C.CloseHandle(handle)
    if rtn == C.FALSE {
        panic("failed to close handle")
    }
}
go build && handletest.exe
Using filepath C:\Users\Alex\AppData\Local\Temp\handletest.tmp
handle addr 0x154
runtime: bad pointer in frame main.main at 0xc000075f10: 0x154
fatal error: invalid pointer found on stack

Duplicate of #8921.

CC @randall77

@toothrot toothrot added the OS-Windows label Oct 16, 2020
@toothrot toothrot added this to the Backlog milestone Oct 16, 2020
@ianlancetaylor ianlancetaylor changed the title runtime/cgo: treat Windows HANDLE as non-pointer type cmd/cgo: treat Windows HANDLE as non-pointer type Oct 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.