Sonia Hamilton
Sonia Hamilton

Reputation: 4409

Golang CGo: converting union field to Go type

I'm working with this C struct on a 64 bit platform, trying to access the ui32v field in the value union:

struct _GNetSnmpVarBind {
  guint32       *oid;       /* name of the variable */
  gsize     oid_len;    /* length of the name */
  GNetSnmpVarBindType   type;       /* variable type / exception */
  union {
    gint32   i32;           /* 32 bit signed   */
    guint32  ui32;          /* 32 bit unsigned */
    gint64   i64;           /* 64 bit signed   */
    guint64  ui64;          /* 64 bit unsigned */
    guint8  *ui8v;          /*  8 bit unsigned vector */
    guint32 *ui32v;         /* 32 bit unsigned vector */
  }         value;      /* value of the variable */
  gsize     value_len;  /* length of a vector in bytes */
};

I could write a C wrapper function for each union element but for didactic purposes I'd rather work in Go. Here's how I'm trying to access the ui32v field:

func union_to_guint32_ptr(cbytes [8]byte) (result *_Ctype_guint32) {
  buf := bytes.NewBuffer(cbytes[:])
  var ptr uint64
  if err := binary.Read(buf, binary.LittleEndian, &ptr); err == nil {
    return (*_Ctype_guint32)(unsafe.Pointer(ptr))
  }
  return nil
}

However this gives an error cannot convert ptr (type uint64) to type unsafe.Pointer

So how do I convert a uint64 to a Go type that points to a C guint32? I've tried various combinations of casting to a uintptr then casting to a *_Ctype_guint32, casting to a uintptr then using unsafe.Pointer, ...

My reasoning is: I'm passed an array of 8 bytes. Convert that to a uint64, that's the memory address. Cast that to a pointer to a guint32 (ie a C array of guint32's), and return that as a result - that is the union field "value" as a guint32 *.


Context

Later I'll want to convert the C array of guint32's to a string utilising the value_len field, using a function I know already works:

guint32_star := union_to_guint32_ptr(data.value)
result += OidArrayToString(guint32_star, data.value_len)

The C code is from gsnmp.

Upvotes: 11

Views: 8650

Answers (4)

Chris Bandy
Chris Bandy

Reputation: 1608

cgo exposes a union as a byte array large enough to hold the largest member of the union. In your case that is 64 bits which are 8 bytes, [8]byte. As you've demonstrated, the contents of this array hold the contents of the union and using it is a matter of pointer conversion.

However, you can use the address of the array to greatly simplify the process. For a C._GNetSnmpVarBind named data,

guint32_star := *(**C.guint32)(unsafe.Pointer(&data.value[0]))

I didn't fully understand this the first time I saw it, but it became more clear when I broke it down:

var data C._GNetSnmpVarBind    // The C struct
var union [8]byte = data.value // The union, as eight contiguous bytes of memory

// The first magic. The address of the first element in that contiguous memory
// is the address of that memory. In other words, the address of that union.
var addr *byte = &union[0]

// The second magic. Instead of pointing to bytes of memory, we can point
// to some useful type, T, by changing the type of the pointer to *T using
// unsafe.Pointer. In this case we want to interpret the union as member
// `guint32 *ui32v`. That is, T = (*C.guint32) and *T = (**C.guint32).
var cast **C.guint32 = (**C.guint32)(unsafe.Pointer(addr))

// The final step. We wanted the contents of the union, not the address
// of the union. Dereference it!
var guint32_star *C.guint32 = *cast

Credit goes to Alan Shen's article which described the cgo representation of a union in a way that finally made sense to me.

Upvotes: 8

user1698814
user1698814

Reputation: 184

Sonia already answered her own question, I just want to provide the reason for why two type conversions are necessary.

From the documentation for unsafe.Pointer:

1) A pointer value of any type can be converted to a Pointer.

2) A Pointer can be converted to a pointer value of any type.

3) A uintptr can be converted to a Pointer.

4) A Pointer can be converted to a uintptr.

Since var ptr uint64 is not a pointer (as type uint64 is not a pointer), ptr cannot be converted directly to unsafe.Pointer using rule 1. Therefore it is necessary to first convert ptr to uintptr, then from uintptr to a Pointer following rule 3.

Upvotes: 3

Sonia Hamilton
Sonia Hamilton

Reputation: 4409

The solution was first to cast to uintptr, then cast to unsafe.Pointer ie two separate casts:

func union_to_guint32_ptr(cbytes [8]byte) (result *_Ctype_guint32) {
    buf := bytes.NewBuffer(cbytes[:])
    var ptr uint64
    if err := binary.Read(buf, binary.LittleEndian, &ptr); err == nil {
        uptr := uintptr(ptr)
        return (*_Ctype_guint32)(unsafe.Pointer(uptr))
    }   
    return nil 
}                

I checked this by comparing results with a command line tool, and it's returning correct results.


Context

// gsnmp._Ctype_gpointer -> *gsnmp._Ctype_GNetSnmpVarBind
data := (*C.GNetSnmpVarBind)(out.data)

switch VarBindType(data._type) {
case GNET_SNMP_VARBIND_TYPE_OBJECTID:
    result += "GNET_SNMP_VARBIND_TYPE_OBJECTID" + ":"
    guint32_star := union_to_guint32_ptr(data.value)
    result += OidArrayToString(guint32_star, data.value_len)

Upvotes: 5

zzzz
zzzz

Reputation: 91223

From the CGO documentation:

To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.

So I guess (not tested) the code might be something similar to:

myUint32var := somePtrTo_GNetSnmpVarBind.union_guint32

for accessing the guint32 member of the union of the struct pointed to by somePtrTo_GNetSnmpVarBind

Upvotes: 1

Related Questions