Skip to contentSkip to conversation

golang/xenlight: Fix handling of marshalling of empty elements for keyed unions

PATCH In review George Dunlap 2 participants 2 messages Mar 6, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 golang/xenlight: Fix handling of marshalling of empty elements for keyed unions

Keyed types in libxl_types.idl can have elements of type 'None'. The golang type generator (correctly) don't implement any union types for these empty elements. However, the toC and fromC helper generators incorrectly treat these elements as invalid.

Consider for example, libxl_channelinfo. The idl contains the following keyed element:

    ("u", KeyedUnion(None, libxl_channel_connection, "connection",
           [("unknown", None),
            ("pty", Struct(None, [("path", string),])),
            ("socket", None),
           ])),

But the toC marshaller currently looks like this:

	switch x.Connection {
	case ChannelConnectionPty:
		tmp, ok := x.ConnectionUnion.(ChannelinfoConnectionUnionPty)
		if !ok {
			return errors.New("wrong type for union key connection")
		}
		var pty C.libxl_channelinfo_connection_union_pty
		if tmp.Path != "" {
			pty.path = C.CString(tmp.Path)
		}
		ptyBytes := C.GoBytes(unsafe.Pointer(&pty), C.sizeof_libxl_channelinfo_connection_union_pty)
		copy(xc.u[:], ptyBytes)
	default:
		return fmt.Errorf("invalid union key '%v'", x.Connection)
	}

Which means toC() will fail for ChannelConnectionUnknown or ChannelConnectionSocket.

Modify the generator to handle keyed union elements of type 'None'. For fromC, set the value to 'nil'; for toC, leave things as-is.

---
In toC, instead of leaving the union alone for "empty" elements, we could zero the whole union out. That's probably not necessary.

CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
CC Nick Rosbrook <[email protected]>
---
 tools/golang/xenlight/gengotypes.py  | 19 +++++---
 tools/golang/xenlight/helpers.gen.go | 66 ++++++++++++++++++++--------
 tools/golang/xenlight/types.gen.go   |  1 +
 3 files changed, 63 insertions(+), 23 deletions(-)
tools/golang/xenlight/gengotypes.py +14 -5
index f81271f3c0..50dada309b 100644
@@ -335,6 +335,7 @@ def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''):
     gokeyname = xenlight_golang_fmt_name(keyname)
     keytype   = ty.keyvar.type.typename
     gokeytype = xenlight_golang_fmt_name(keytype)
+    field_name = xenlight_golang_fmt_name('{}_union'.format(keyname))
 
     interface_name = '{}_{}_union'.format(struct_name, keyname)
     interface_name = xenlight_golang_fmt_name(interface_name, exported=False)
@@ -351,11 +352,11 @@ def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''):
 
         # Add to list of cases to make for the switch
         # statement below.
+        cases[f.name] = (val, f.type)
+
         if f.type is None:
             continue
 
-        cases[f.name] = val
-
         # Define fromC func for 'union' struct.
         typename   = '{}_{}_union_{}'.format(struct_name,keyname,f.name)
         gotypename = xenlight_golang_fmt_name(typename)
@@ -382,9 +383,15 @@ def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''):
 
     # Create switch statement to determine which 'union element'
     # to populate in the Go struct.
-    for case_name, case_val in cases.items():
+    for case_name, case_tuple in cases.items():
+        (case_val, case_type) = case_tuple
+
         s += 'case {}:\n'.format(case_val)
 
+        if case_type is None:
+            s += "x.{} = nil\n".format(field_name)
+            continue
+
         gotype = '{}_{}_union_{}'.format(struct_name,keyname,case_name)
         gotype = xenlight_golang_fmt_name(gotype)
         goname = '{}_{}'.format(keyname,case_name)
@@ -394,7 +401,6 @@ def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''):
         s += 'if err := {}.fromC(xc);'.format(goname)
         s += 'err != nil {{\n return fmt.Errorf("converting field {}: %v", err) \n}}\n'.format(goname)
 
-        field_name = xenlight_golang_fmt_name('{}_union'.format(keyname))
         s += 'x.{} = {}\n'.format(field_name, goname)
 
     # End switch statement
@@ -551,10 +557,13 @@ def xenlight_golang_union_to_C(ty = None, union_name = '',
     for f in ty.fields:
         key_val = '{}_{}'.format(keytype, f.name)
         key_val = xenlight_golang_fmt_name(key_val)
+
+        s += 'case {}:\n'.format(key_val)
+
         if f.type is None:
+            s += "break\n"
             continue
 
-        s += 'case {}:\n'.format(key_val)
         cgotype = '{}_{}_union_{}'.format(struct_name,keyname,f.name)
         gotype  = xenlight_golang_fmt_name(cgotype)
 
tools/golang/xenlight/helpers.gen.go +48 -18

180 lines not on this page · raw message

tools/golang/xenlight/types.gen.go +1 -0
index ede49b4886..4aaee20b95 100644
@@ -403,6 +403,7 @@ type DomainCreateInfo struct {
 	Ssidref           uint32
 	SsidLabel         string
 	Name              string
+	Domid             Domid
 	Uuid              Uuid
 	Xsdata            KeyValueList
 	Platformdata      KeyValueList
Signature · 7 lines
2.25.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines

Keyed types in libxl_types.idl can have elements of type 'None'.  The golang type generator (correctly) don't implement any union types for these empty elements.  However, the toC and fromC helper generators incorrectly treat these elements as invalid.

Consider for example, libxl_channelinfo.  The idl contains the following keyed element:

    ("u", KeyedUnion(None, libxl_channel_connection, "connection", [("unknown", None),
            ("pty", Struct(None, [("path", string),])), ("socket", None),
           ])),

But the toC marshaller currently looks like this:

        switch x.Connection {
        case ChannelConnectionPty:
                tmp, ok := x.ConnectionUnion.(ChannelinfoConnectionUnionPty)
                if !ok {
                        return errors.New("wrong type for union key connection")
                }
                var pty C.libxl_channelinfo_connection_union_pty
                if tmp.Path != "" {
                        pty.path = C.CString(tmp.Path)
                }
                ptyBytes := C.GoBytes(unsafe.Pointer(&pty), C.sizeof_libxl_channelinfo_connection_union_pty) copy(xc.u[:], ptyBytes)
        default:
                return fmt.Errorf("invalid union key '%v'", x.Connection)
        }

Quoted 7 lines

Which means toC() will fail for ChannelConnectionUnknown or ChannelConnectionSocket.

Modify the generator to handle keyed union elements of type 'None'. For fromC, set the value to 'nil'; for toC, leave things as-is.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel