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: problem with forward-declared enum #1293

Closed
gopherbot opened this issue Nov 22, 2010 · 20 comments
Closed

cmd/cgo: problem with forward-declared enum #1293

gopherbot opened this issue Nov 22, 2010 · 20 comments

Comments

@gopherbot
Copy link

by aotto1968zwei:

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull -u" and retry what you did to
reproduce the problem.  Thanks.

hg pull -u -> done, same error

> make
CGOPKGPATH= cgo -- -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN MqS.go MqSException.go
MqBufferS.go MqBinary.go send.go read.go link.go config.go service.go
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
unrecognized Go type &{- 0x2aadded25af0 false}
6g -o _go_.6  MqS.cgo1.go MqSException.cgo1.go MqBufferS.cgo1.go MqBinary.cgo1.go
send.cgo1.go read.cgo1.go link.cgo1.go config.cgo1.go service.cgo1.go _cgo_gotypes.go
6c -FVw -I"/home/dev1usr/src/go/src/pkg/runtime" _cgo_defun.c
rm -f _obj/gomsgque.a
gopack grc _obj/gomsgque.a _go_.6  _cgo_defun.6
cp _obj/gomsgque.a "/home/dev1usr/src/go/pkg/linux_amd64/gomsgque.a"
gcc -m64 -fPIC -O2 -o MqS.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
MqS.cgo2.c
gcc -m64 -fPIC -O2 -o MqSException.cgo2.o -c -I . -I ./../../libmsgque
-DMQ_IGNORE_EXTERN MqSException.cgo2.c
gcc -m64 -fPIC -O2 -o MqBufferS.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
MqBufferS.cgo2.c
gcc -m64 -fPIC -O2 -o MqBinary.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
MqBinary.cgo2.c
gcc -m64 -fPIC -O2 -o send.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
send.cgo2.c
gcc -m64 -fPIC -O2 -o read.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
read.cgo2.c
gcc -m64 -fPIC -O2 -o link.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
link.cgo2.c
gcc -m64 -fPIC -O2 -o config.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
config.cgo2.c
gcc -m64 -fPIC -O2 -o service.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
service.cgo2.c
service.cgo2.c: In function '_cgo_Cfunc_MqServiceGetFilter':
service.cgo2.c:31:3: error: expected specifier-qualifier-list before '*' token
service.cgo2.c:36:3: error: 'struct <anonymous>' has no member named 'r'
service.cgo2.c:36:43: error: 'struct <anonymous>' has no member named 'p2'
make: *** [service.cgo2.o] Fehler 1


What steps will reproduce the problem?
compile a file with cgo:
msgque.h
MQ_EXTERN enum MqErrorE MQ_DECL MqServiceGetFilter (
  struct MqS  * const ctx,
  MQ_SIZE const id,
  struct MqS ** const filter
) __attribute__((nonnull(1)));

service.go:
func (this *MqS) ServiceGetFilter(i int32) *MqS {
  var ret *_Ctype_struct_MqS
  this.iErrorMqToGoWithCheck(C.MqServiceGetFilter((*_Ctype_struct_MqS)(this), C.MQ_SIZE(i), &ret))
  return (*MqS)(ret)
}

What is the expected output?
service.cgo2.c:
void
_cgo_Cfunc_MqServiceGetFilter(void *v)
{
        struct {
                struct MqS* p0;
                MQ_SIZE p1;
                char __pad12[4];
                struct MqS** p2;
                enum MqErrorE r;
                char __pad28[4];
        }
 *a = v;
        a->r = MqServiceGetFilter(a->p0, a->p1, a->p2);
}



What do you see instead?
service.cgo2.c:
void
_cgo_Cfunc_MqServiceGetFilter(void *v)
{
        struct {
                struct MqS* p0;
                MQ_SIZE p1;
                char __pad12[4];
                * p2;
                enum MqErrorE r;
                char __pad28[4];
        }
 *a = v;
        a->r = MqServiceGetFilter(a->p0, a->p1, a->p2);
}
RESULT:
gcc -m64 -fPIC -O2 -o service.cgo2.o -c -I . -I ./../../libmsgque -DMQ_IGNORE_EXTERN
service.cgo2.c
service.cgo2.c: In function '_cgo_Cfunc_MqServiceGetFilter':
service.cgo2.c:31:3: error: expected specifier-qualifier-list before '*' token
service.cgo2.c:36:3: error: 'struct <anonymous>' has no member named 'r'
service.cgo2.c:36:43: error: 'struct <anonymous>' has no member named 'p2'



Which compiler are you using (5g, 6g, 8g, gccgo)?
cgo


Which operating system are you using?
linux> uname -a
Linux linux-w0uy 2.6.34.7-0.5-desktop #1 SMP PREEMPT 2010-10-25 08:40:12 +0200 x86_64
x86_64 x86_64 GNU/Linux


Which revision are you using?  (hg identify)
> hg identify
f85e929d53c7 tip

Please provide any additional information below.
compiling my file service.go with cgo create wrong wrapper code

steps to solve

1: delete "const" from "struct MqS ** const filter" 
-> error still available
2. change service.cgo2.c by hand from "* p2" to "struct MqS** p2" 
-> compile ok -> test ok -> works


mfg, Andreas Otto
@robpike
Copy link
Contributor

robpike commented Dec 2, 2010

Comment 1:

Labels changed: added cgo.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Dec 13, 2010

Comment 2:

This is not enough information to reproduce the problem.
None of the files you've supplied are complete.
The real problem is the unrecognized Go type prints from cgo.
Could you please change src/cmd/cgo/out.go:605 from
    error(e.Pos(), "unrecognized Go type %v", e)
to
    error(e.Pos(), "unrecognized Go type %T", e)
(%v -> %T) and then reinstall cgo and let us know what
the output says?

Status changed to WaitingForReply.

@gopherbot
Copy link
Author

Comment 3 by aotto1968zwei:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I updaten to the last release
1. I change in the file "src/cmd/cgo/out.go" the line "559" to
"error(e.Pos(), "unrecognized Go type %T", e)"
2. I create a header file with the following text entry
MQ_EXTERN enum MqErrorE MQ_DECL MqDummy (
  struct MqS  * const ctx,
  MQ_SIZE const id,
  struct MqS ** const filter
) __attribute__((nonnull(1)));
3. I create a "Go" file with the following text entry
func (this *MqS) Dummy(i int32) *MqS {
  var ret *_Ctype_struct_MqS
  this.iErrorMqToGoWithCheck(C.MqDummy((*_Ctype_struct_MqS)(this),
C.MQ_SIZE(i), &ret))
  return (*MqS)(ret)
}
4. I get the following results
CGOPKGPATH= cgo -- -I . -I /home/dev1usr/Project/NHI1/theLink/libmsgque
- -DMQ_IGNORE_EXTERN MqS.go MqSException.go MqBufferS.go MqBinary.go
send.go read.go link.go config.go service.go slave.go
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
unrecognized Go type *ast.StructType
6g -o _go_.6  MqS.cgo1.go MqSException.cgo1.go MqBufferS.cgo1.go
MqBinary.cgo1.go send.cgo1.go read.cgo1.go link.cgo1.go config.cgo1.go
service.cgo1.go slave.cgo1.go _cgo_gotypes.go
6c -FVw -I"/home/dev1usr/src/go/src/pkg/runtime" _cgo_defun.c
rm -f _obj/gomsgque.a
gopack grc _obj/gomsgque.a _go_.6  _cgo_defun.6
cp _obj/gomsgque.a "/home/dev1usr/src/go/pkg/linux_amd64/gomsgque.a"
gcc -m64 -fPIC -O2 -o MqS.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN MqS.cgo2.c
gcc -m64 -fPIC -O2 -o MqSException.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN
MqSException.cgo2.c
gcc -m64 -fPIC -O2 -o MqBufferS.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN
MqBufferS.cgo2.c
gcc -m64 -fPIC -O2 -o MqBinary.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN
MqBinary.cgo2.c
gcc -m64 -fPIC -O2 -o send.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN send.cgo2.c
gcc -m64 -fPIC -O2 -o read.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN read.cgo2.c
gcc -m64 -fPIC -O2 -o link.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN link.cgo2.c
gcc -m64 -fPIC -O2 -o config.cgo2.o -c -I . -I
/home/dev1usr/Project/NHI1/theLink/libmsgque -DMQ_IGNORE_EXTERN
config.cgo2.c
config.cgo2.c: In function '_cgo_Cfunc_MqDummy':
config.cgo2.c:287:3: error: expected specifier-qualifier-list before '*'
token
config.cgo2.c:292:3: error: 'struct <anonymous>' has no member named 'r'
config.cgo2.c:292:32: error: 'struct <anonymous>' has no member named 'p2'
5. the "unrecognized Go type ..." is not the problem. every "callback"
create such a line of code and this is !not! related to the
compile-error at the end of the file
6. the problem is the compile error, the following code can not be compiled
void
_cgo_Cfunc_MqDummy(void *v)
{
        struct {
                struct MqS* p0;
                MQ_SIZE p1;
                char __pad12[4];
                * p2;
                enum MqErrorE r;
                char __pad28[4];
        }
 *a = v;
        a->r = MqDummy(a->p0, a->p1, a->p2);
}
mfg, andreas otto
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJNBkBjAAoJEGTcPijNG3/AiDgH/iki8GzJVx9cBv8cZZy7Wj84
VFh8fbqlCeLbAxTiavRX8tSoZXY+YmFRoS3fi+LCxhLvxT9XrKJE138HTGwTZgEk
0+vumdMdzPFbbUIsiT4AX5+sJr/wSZ7c3KWoCybG0coYm/YXeYH0F4AsuSOTzeKZ
qsbX+j/lp22bmHHlCVL8p8vkUvWADJNt5/GuCbRPboHtORN52ahpEVHnftzRueIT
dlN9rMh+HKEXsz0TxVB/Ycb1RvOwxjGWVFNfXHBGA1oM2AfnK7vZgiN3Yl2ufvnR
sd86AXYp/oFgbJ1NrY8YzjPnOLn+zDxuXtqHI3AFR0uIwFLRYZWBxx3SEWIMN5M=
=NAtb
-----END PGP SIGNATURE-----

@rsc
Copy link
Contributor

rsc commented Dec 16, 2010

Comment 4:

I believe you about #5.
This is still not enough information to reproduce the problem.
None of the files you've supplied are complete.
You've only shown fragments.
Please include a recipe with complete files so that I can
run cgo myself on the files and see it generate the
problematic output.

@gopherbot
Copy link
Author

Comment 5 by aotto1968zwei:

Hi,
sorry for my wrong answer. I prepare an example ...
this example create a core during compile ...
it's not exactly the same problem but we get closer

Attachments:

  1. error.zip (2443 bytes)

@rsc
Copy link
Contributor

rsc commented Dec 17, 2010

Comment 6:

Owner changed to r...@golang.org.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Dec 9, 2011

Comment 7:

Labels changed: added priority-later.

@rsc
Copy link
Contributor

rsc commented Dec 12, 2011

Comment 8:

Labels changed: added priority-go1.

@robpike
Copy link
Contributor

robpike commented Jan 13, 2012

Comment 9:

Owner changed to builder@golang.org.

@rsc
Copy link
Contributor

rsc commented Feb 17, 2012

Comment 11:

Standalone test:
package p
/*
typedef signed int MQ_INT;
enum ErrorE {
  OK = 1,
  WAR = 2,
  ERR =3,
};
struct DummyS {
  MQ_INT  a;
  float   b;
};
enum MqErrorE MqDummy (
  struct DummyS *ctx,
  MQ_INT const id,
  struct DummyS ** const filter
);
*/
import "C"
type DummyS _Ctype_struct_DummyS
func (this *DummyS) Dummy(i int32) *DummyS {
  var ret *_Ctype_struct_DummyS
  C.MqDummy((*_Ctype_struct_DummyS)(this),C.MQ_INT(i),&ret)
  return (*DummyS)(ret)
}
On Linux, go build:
# test
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x18 pc=0x40c5c6]
goroutine 2 [running]:
main.(*Package).rewriteRef(0xf84007a000, 0xf84004eaf0, 0xf840071a20, 0x200000002)
    /home/rsc/go/src/cmd/cgo/gcc.go:658 +0xa7f
main.(*Package).Translate(0xf84007a000, 0xf84004eaf0, 0x29, 0x588d24)
    /home/rsc/go/src/cmd/cgo/gcc.go:271 +0x129
main.main()
    /home/rsc/go/src/cmd/cgo/main.go:225 +0x905
created by _rt0_amd64
    /home/rsc/go/src/pkg/runtime/asm_amd64.s:69 +0xd1
goroutine 1 [syscall]:
On Mac, go build:
# test
test.go:30:3: unexpected: 0-byte enum type - enum MqErrorE {}
Leaving for after Go 1.

Labels changed: added priority-later, removed priority-go1.

Status changed to HelpWanted.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 12:

Labels changed: added go1.1.

@rsc
Copy link
Contributor

rsc commented Dec 10, 2012

Comment 14:

Labels changed: added size-l.

@rsc
Copy link
Contributor

rsc commented Mar 12, 2013

Comment 15:

Labels changed: added go1.1maybe, removed go1.1.

@robpike
Copy link
Contributor

robpike commented May 18, 2013

Comment 16:

Labels changed: added go1.2maybe, removed go1.1maybe.

@robpike
Copy link
Contributor

robpike commented Aug 16, 2013

Comment 17:

Today on Linux:
% go build z.go
# command-line-arguments
z.go: In function ‘_cgo_f33557c6eda4_Cfunc_MqDummy’:
z.go:53:8: error: variable or field ‘r’ declared void
z.go:55:2: error: invalid use of undefined type ‘enum MqErrorE’
It doesn't crash any more. On the Mac, it's unchanged.
Where does 'r' come from?
Delaying to Go 1.3.

Labels changed: added go1.3maybe, removed go1.2maybe.

@robpike
Copy link
Contributor

robpike commented Aug 20, 2013

Comment 18:

Labels changed: removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 19:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 20:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 21:

Labels changed: added repo-main.

@ianlancetaylor
Copy link
Contributor

Comment 22:

The program calls a function that returns an undefined enum type.  That is invalid C,
and there is no way that cgo can support it.  cgo now gives a sensible error rather than
crashing.  I think this bug is as fixed as it can be.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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

4 participants