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

runtime: issue confused about the use of pin #64735

Closed
bobozhengsir opened this issue Dec 15, 2023 · 1 comment
Closed

runtime: issue confused about the use of pin #64735

bobozhengsir opened this issue Dec 15, 2023 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@bobozhengsir
Copy link

Go version

go version go1.21.0 linux/amd64

What operating system and processor architecture are you using (go env)?

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/data00/home/zhengjianbo/.cache/go-build'
GOENV='/data00/home/zhengjianbo/.config/go/env'
GOEXE=''
GOEXPERIMENT='cgocheck2'
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/data00/home/zhengjianbo/.gvm/pkgsets/go1.21/global/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/data00/home/zhengjianbo/.gvm/pkgsets/go1.21/global'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/data00/home/zhengjianbo/.gvm/gos/go1.21'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/data00/home/zhengjianbo/.gvm/gos/go1.21/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/data00/home/zhengjianbo/code/github/mytest/gopin/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build953265995=/tmp/go-build -gno-record-gcc-switches'

What did you do?

package main

/*
#include <stdint.h>

typedef struct Str {
	const char *ptr;
	size_t len;
} Str;

typedef struct Vec {
  const void *ptr;
  size_t len;
} Vec;

void print_vec(Vec v) {
	for(size_t i =0; i < v.len; i++) {
		for(size_t j=0; j < ((Str*)(v.ptr))[i].len; j++) {
			//todo: (v.ptr[i])->ptr
			i+j;
		}
	}
}
*/
import "C"

import (
	"runtime"
	"unsafe"
)

// export test
func test() {
	pin := new(runtime.Pinner)
	_ = pin

	str1 := "def"
	// Pin1. why StringData can't pin?
	s1 := C.Str{(*C.char)(unsafe.Pointer(unsafe.StringData(str1))), C.size_t(len(str1))}

	str2 := []byte("abc")
	// Pin2. why this SliceData need pin?
	//pin.Pin(unsafe.SliceData(str2))
	//defer pin.Unpin()
	s2 := C.Str{(*C.char)(unsafe.Pointer(unsafe.SliceData(str2))), C.size_t(len(str2))}

	str3 := []byte("ghi")
	// Pin3
	pin.Pin(&str3[0])
	defer pin.Unpin()
	s3 := C.Str{(*C.char)(unsafe.Pointer(&str3[0])), C.size_t(len(str3))}

	str_slice := []C.Str{s1, s2, s3}

	// Pin4. why this SliceData not need pin?
	C.print_vec(C.Vec{unsafe.Pointer(unsafe.SliceData(str_slice)), C.size_t(len(str_slice))})
}

func main() {
	test()
}

I write some test code about runtime.Pinner, I’m confused about the use of pin:

  1. why unsafe.StringData don't need or can't pin?
  2. why Pin2 and Pin3 in my code just need one? It don't panic if I just pin one of them. Why not pin all of them?
  3. why Pin4 in my code don't need pin?

What did you expect to see?

Nothing

What did you see instead?

panic: runtime error: cgo argument has Go pointer to unpinned Go pointer

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Dec 15, 2023
@seankhliao
Copy link
Member

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants