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

x/tools/cmd/gomvpkg: incorrect handling of cgo packages #24532

Open
mewmew opened this issue Mar 26, 2018 · 2 comments
Open

x/tools/cmd/gomvpkg: incorrect handling of cgo packages #24532

mewmew opened this issue Mar 26, 2018 · 2 comments

Comments

@mewmew
Copy link
Contributor

@mewmew mewmew commented Mar 26, 2018

What version of Go are you using (go version)?

go version devel +438a757d73 Wed Feb 21 18:10:00 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes, on the latest version of gomvpkg (rev golang/tools@77106db).

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/u/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/u/Desktop/foo"
GORACE=""
GOROOT="/home/u/go"
GOTMPDIR=""
GOTOOLDIR="/home/u/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build382385690=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Tried to move a package containing cgo code. In this example, we try to move package a to b.

gomvpkg -from a -to b

Source code of package a. Files:

a.go

package a

import "C"

var _ = C.CString("foo")

Output source code of package b. Files:

a.go:

// Created by cgo - DO NOT EDIT

//line a.go:1
package b

import _ "unsafe"

var _ = (_Cfunc_CString)("foo")

C:

// Created by cgo - DO NOT EDIT

package b

import "unsafe"

import _ "runtime/cgo"

import "syscall"

var _ syscall.Errno

func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }

//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse
var _Cgo_always_false bool

//go:linkname _Cgo_use runtime.cgoUse
func _Cgo_use(interface{})

type _Ctype_char int8

type _Ctype_void [0]byte

//go:linkname _cgo_runtime_cgocall runtime.cgocall
func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32

//go:linkname _cgo_runtime_cgocallback runtime.cgocallback
func _cgo_runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr, uintptr)

//go:linkname _cgoCheckPointer runtime.cgoCheckPointer
func _cgoCheckPointer(interface{}, ...interface{})

//go:linkname _cgoCheckResult runtime.cgoCheckResult
func _cgoCheckResult(interface{})

func _Cfunc_CString(s string) *_Ctype_char {
	p := _cgo_cmalloc(uint64(len(s) + 1))
	pp := (*[1 << 30]byte)(p)
	copy(pp[:], s)
	pp[len(s)] = 0
	return (*_Ctype_char)(p)
}

//go:cgo_import_static _cgo_dbcbf9d0883b_Cfunc__Cmalloc
//go:linkname __cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc _cgo_dbcbf9d0883b_Cfunc__Cmalloc
var __cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc byte
var _cgo_dbcbf9d0883b_Cfunc__Cmalloc = unsafe.Pointer(&__cgofn__cgo_dbcbf9d0883b_Cfunc__Cmalloc)

//go:linkname runtime_throw runtime.throw
func runtime_throw(string)

//go:cgo_unsafe_args
func _cgo_cmalloc(p0 uint64) (r1 unsafe.Pointer) {
	_cgo_runtime_cgocall(_cgo_dbcbf9d0883b_Cfunc__Cmalloc, uintptr(unsafe.Pointer(&p0)))
	if r1 == nil {
		runtime_throw("runtime: C malloc failed")
	}
	return
}

What did you expect to see?

I expected to see the package moved from $GOPATH/src/a to $GOPATH/src/b, without affecting cgo code. That is the output should be b/b.go:

package b

import "C"

var _ = C.CString("foo")

What did you see instead?

A mysterious C file and a rewrite of the code using cgo.

Note, the output (i.e. package b) does not compile, while the input did.

[u@x220 b]$ go install
# b
a.go:5: undefined: _Cfunc_CString
@gopherbot gopherbot added this to the Unreleased milestone Mar 26, 2018
@andybons

This comment has been minimized.

Copy link
Member

@andybons andybons commented Mar 26, 2018

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 26, 2018

@gopherbot gopherbot added the Tools label Sep 12, 2019
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
4 participants
You can’t perform that action at this time.