FAQ
Since go 1.6, there's rule on passing Go pointer to C.
As a result, my following code returned runtime error, that says, "runtime
error: cgo argument has Go pointer to Go pointer".

code:

/*

typedef unsigned long int CK_ULONG;

typedef unsigned char CK_BYTE;

typedef CK_BYTE * CK_BYTE_PTR;


typedef struct CK_DDATA {
   CK_BYTE_PTR pData;
   CK_ULONG ulLen;
} CK_DDATA;

*/


func ConvertData(data []byte) []byte {

   kdsd := new(C.CK_DDATA)
   kdsd.pData = C.CK_BYTE_PTR(unsafe.Pointer(&data[0]))
   kdsd.ulLen = C.CK_ULONG(len(data))
   kdsdBytes := C.GoBytes(unsafe.Pointer(kdsd), C.int(unsafe.Sizeof(*kdsd)))

   return kdsdBytes

}



When runtime, line

   kdsdBytes := C.GoBytes(unsafe.Pointer(kdsd), C.int(unsafe.Sizeof(*kdsd)))


resulted in "runtime error: cgo argument has Go pointer to Go pointer".

1. How could I overcome this runtime exception ?

2. Is there other way to convert the C struct (ie CK_DDATA) into golang
[]byte ?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dan Kortschak at Mar 16, 2016 at 4:52 am
    Why not just do this in pure Go? There doesn't seem to be anything
    C-specific about what you are doing that a few lines of Go would not
    replace.
    On Tue, 2016-03-15 at 20:41 -0700, clashion@gmail.com wrote:
    Since go 1.6, there's rule on passing Go pointer to C.
    As a result, my following code returned runtime error, that says, "runtime
    error: cgo argument has Go pointer to Go pointer".

    code:

    /*

    typedef unsigned long int CK_ULONG;

    typedef unsigned char CK_BYTE;

    typedef CK_BYTE * CK_BYTE_PTR;


    typedef struct CK_DDATA {
    CK_BYTE_PTR pData;
    CK_ULONG ulLen;
    } CK_DDATA;

    */


    func ConvertData(data []byte) []byte {

    kdsd := new(C.CK_DDATA)
    kdsd.pData = C.CK_BYTE_PTR(unsafe.Pointer(&data[0]))
    kdsd.ulLen = C.CK_ULONG(len(data))
    kdsdBytes := C.GoBytes(unsafe.Pointer(kdsd), C.int(unsafe.Sizeof(*kdsd)))

    return kdsdBytes

    }



    When runtime, line

    kdsdBytes := C.GoBytes(unsafe.Pointer(kdsd), C.int(unsafe.Sizeof(*kdsd)))


    resulted in "runtime error: cgo argument has Go pointer to Go pointer".

    1. How could I overcome this runtime exception ?

    2. Is there other way to convert the C struct (ie CK_DDATA) into golang
    []byte ?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Clashion at Mar 16, 2016 at 8:08 am
    Background is,
    1. I'm using the golang pkcs11 lib here:
    https://github.com/miekg/pkcs11

    2. I want to populate the struct "Mechanism".
    refer file: https://github.com/miekg/pkcs11/blob/master/types.go

    type Mechanism struct {
            Mechanism uint
            Parameter []byte
    }

    3. the "Parameter []byte" field I want to populate with: refer file:
    https://github.com/miekg/pkcs11/blob/master/pkcs11t.h

    typedef struct CK_KEY_DERIVATION_STRING_DATA {
       CK_BYTE_PTR pData;
       CK_ULONG ulLen;
    } CK_KEY_DERIVATION_STRING_DATA;

    4. To do so, I instantiate the C.CK_KEY_DERIVATION_STRING_DATA struct, and
    then fill the "CK_BYTE_PTR pData" with the data I want. After that, I
    convert the C.CK_KEY_DERIVATION_STRING_DATA struct into []byte, so that I
    can pass it to the "Mechanism" struct. this is done by the function:

    func ConvertData(data []byte) []byte {

       kdsd := new(C.CK_KEY_DERIVATION_STRING_DATA)
       kdsd.pData = C.CK_BYTE_PTR(unsafe.Pointer(&data[0]))
       kdsd.ulLen = C.CK_ULONG(len(data))
       kdsdBytes := C.GoBytes(unsafe.Pointer(kdsd), C.int(unsafe.Sizeof(*kdsd)))

       return kdsdBytes

    }

    but now with go 1.6 new rule on passing pointer between C and Go, I
    encountered the error "runtime error: cgo argument has Go pointer to Go
    pointer". Therefore, I would like to find out how could I solve this
    problem. Thanks for your further advice.


    On Wednesday, March 16, 2016 at 12:52:20 PM UTC+8, kortschak wrote:

    Why not just do this in pure Go? There doesn't seem to be anything
    C-specific about what you are doing that a few lines of Go would not
    replace.
    On Tue, 2016-03-15 at 20:41 -0700, clas...@gmail.com <javascript:> wrote:
    Since go 1.6, there's rule on passing Go pointer to C.
    As a result, my following code returned runtime error, that says, "runtime
    error: cgo argument has Go pointer to Go pointer".

    code:

    /*

    typedef unsigned long int CK_ULONG;

    typedef unsigned char CK_BYTE;

    typedef CK_BYTE * CK_BYTE_PTR;


    typedef struct CK_DDATA {
    CK_BYTE_PTR pData;
    CK_ULONG ulLen;
    } CK_DDATA;

    */


    func ConvertData(data []byte) []byte {

    kdsd := new(C.CK_DDATA)
    kdsd.pData = C.CK_BYTE_PTR(unsafe.Pointer(&data[0]))
    kdsd.ulLen = C.CK_ULONG(len(data))
    kdsdBytes := C.GoBytes(unsafe.Pointer(kdsd),
    C.int(unsafe.Sizeof(*kdsd)))
    return kdsdBytes

    }



    When runtime, line

    kdsdBytes := C.GoBytes(unsafe.Pointer(kdsd),
    C.int(unsafe.Sizeof(*kdsd)))

    resulted in "runtime error: cgo argument has Go pointer to Go pointer".

    1. How could I overcome this runtime exception ?

    2. Is there other way to convert the C struct (ie CK_DDATA) into golang
    []byte ?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Mar 16, 2016 at 9:50 am
    This is disgusting, but it works.

    http://play.golang.org/p/yCu3FosyZg

    I wouldn't use it.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Mar 16, 2016 at 9:57 am
    Nope. Corrupts the bytes.
    On Wed, 2016-03-16 at 20:20 +1030, Dan Kortschak wrote:
    This is disgusting, but it works.

    http://play.golang.org/p/yCu3FosyZg

    I wouldn't use it.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Mar 16, 2016 at 10:03 am
    Sorry, works, but still disgusting.
    On Wed, 2016-03-16 at 20:27 +1030, Dan Kortschak wrote:
    Nope. Corrupts the bytes.
    On Wed, 2016-03-16 at 20:20 +1030, Dan Kortschak wrote:
    This is disgusting, but it works.

    http://play.golang.org/p/yCu3FosyZg

    I wouldn't use it.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Clashion at Mar 16, 2016 at 3:34 pm
    Thank you . This works.

    On Wednesday, March 16, 2016 at 6:04:01 PM UTC+8, kortschak wrote:

    Sorry, works, but still disgusting.
    On Wed, 2016-03-16 at 20:27 +1030, Dan Kortschak wrote:
    Nope. Corrupts the bytes.
    On Wed, 2016-03-16 at 20:20 +1030, Dan Kortschak wrote:
    This is disgusting, but it works.

    http://play.golang.org/p/yCu3FosyZg

    I wouldn't use it.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Clashion at Mar 18, 2016 at 12:21 am
    In the given code as below, should i invoke C.free to free up the memory ?
    Or is the memory will be auto managed by go GC ?

    return (*(*[C.sizeof_struct_CK_KEY_DERIVATION_STRING_DATA]byte)(unsafe.Pointer(&C.struct_CK_KEY_DERIVATION_STRING_DATA{
            pData: C.CK_BYTE_PTR(unsafe.Pointer(&data[0])),
            ulLen: C.CK_ULONG(len(data)),
    })))[:]






    On Wednesday, March 16, 2016 at 11:35:11 PM UTC+8, clas...@gmail.com wrote:

    Thank you . This works.

    On Wednesday, March 16, 2016 at 6:04:01 PM UTC+8, kortschak wrote:

    Sorry, works, but still disgusting.
    On Wed, 2016-03-16 at 20:27 +1030, Dan Kortschak wrote:
    Nope. Corrupts the bytes.
    On Wed, 2016-03-16 at 20:20 +1030, Dan Kortschak wrote:
    This is disgusting, but it works.

    http://play.golang.org/p/yCu3FosyZg

    I wouldn't use it.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Mar 18, 2016 at 12:37 am
    The values are allocated by the GC, so you can just treat them as normal
    Go memory (they are).

    Note that the behaviour is potentially incredibly unsafe.
    On Thu, 2016-03-17 at 17:21 -0700, clashion@gmail.com wrote:
    In the given code as below, should i invoke C.free to free up the memory ?
    Or is the memory will be auto managed by go GC ?

    return (*(*[C.sizeof_struct_CK_KEY_DERIVATION_STRING_DATA]byte)(unsafe.Pointer(&C.struct_CK_KEY_DERIVATION_STRING_DATA{
    pData: C.CK_BYTE_PTR(unsafe.Pointer(&data[0])),
    ulLen: C.CK_ULONG(len(data)),
    })))[:]

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 16, '16 at 3:41a
activeMar 18, '16 at 12:37a
posts9
users2
websitegolang.org

2 users in discussion

Dan Kortschak: 5 posts Clashion: 4 posts

People

Translate

site design / logo © 2022 Grokbase