Skip to content

cgo crashes with simple argument passing #728

@gopherbot

Description

@gopherbot

by scott+ctt@pakin.org:

> 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.
> 
> What steps will reproduce the problem?

With the attached code, "make testffi" and "./testffi".  For quick 
reference, here's the critical file:

    package badffi

    /*
    #include <stdio.h>

    int somevalue = 100;
    void do_something(int x)
    {
      printf("Hello, %d!\n", x);
    }
    */
    import "C"

    func DoSomething() {
        C.do_something(C.somevalue)
    }

> What is the expected output? What do you see instead?

The program should output "Hello, 100!".  Instead it crashes with a bad 
pointer deference:

    panic: runtime error: invalid memory address or nil pointer dereference

    panic PC=0x7f35d9945ff8
    runtime.panic+0x7c /home/pakin/goroot/src/pkg/runtime/proc.c:1011
        runtime.panic(0x7f3500000000, 0x404d59)
    panicstring+0x60 /home/pakin/goroot/src/pkg/runtime/runtime.c:83
        panicstring(0x40d7d8, 0x7f35d993d1f0)
    sigpanic+0x84 /home/pakin/goroot/src/pkg/runtime/linux/thread.c:285
        sigpanic()
    badffi.DoSomething+0x16 /home/pakin/badffi/badffi.cgo1.go:17
        badffi.DoSomething()
    main.main+0xf /home/pakin/badffi/testffi.go:6
        main.main()
    mainstart+0xf /home/pakin/goroot/src/pkg/runtime/amd64/asm.s:60
        mainstart()
    goexit /home/pakin/goroot/src/pkg/runtime/proc.c:145
        goexit()

> What is your $GOOS?  $GOARCH?

GOOS=linux
GOARCH=amd64

> Which revision are you using?  (hg identify)

I tried both

    f98f784927ab+ release.2010-04-13/release

and

    9b6baa9163d6+ tip

> Please provide any additional information below.

Sticking a

    fmt.Printf(">>> %v <<<\n", _C_somevalue)

into badffi.cgo1.go outputs ">>> <nil> <<<".  It seems
that _C_somevalue 
is not properly getting initialized to &somevalue.

Attachments:

  1. badffi.tar.gz (449 bytes)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions