From nobody Mon Feb 9 18:48:00 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1598919108; cv=none; d=zohomail.com; s=zohoarc; b=AlBjjGpcn3bp8j9Gtdqt6qqkNs7sHFDK9FOeIxdtrGLgzP4LlvcHY+0BEfmmeNjscszp8cyCqGsIZjQRabS1t79wkDDG5CQ+RObXFfSvVN+JsDTms2duKJssmBs7Qyh2eW6sS8jrj0TVZe6umwD5oFtGVPCXQ+5FVrx5OtUX0QM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598919108; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=5ChwAZPKBJXfRIooObF/mR23x75fGlNBFyiMAw9Jq/8=; b=SzdDo/uWK82e62qCrV2s0iZdaBKibRyhF3Fo5vD7q9eVjBf9vvNM6FhlsuYSr9hMSXJ/IS76kTAJTojUGV46NqXKk8w8911ZQZE+RpOXMDlAd7Q9QfmjiX4NbSmG3RyyuH8fgXSIwDFCPNHouyBRaWUSdKRWWK1mvYVxbg8w+0A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 159891910837492.02768808382825; Mon, 31 Aug 2020 17:11:48 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kCttN-0001sX-PN; Tue, 01 Sep 2020 00:11:17 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kCttM-0001rt-BB for xen-devel@lists.xenproject.org; Tue, 01 Sep 2020 00:11:16 +0000 Received: from mail-qt1-x830.google.com (unknown [2607:f8b0:4864:20::830]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8e8d1671-645a-4483-aff3-ca16b319ce19; Tue, 01 Sep 2020 00:11:08 +0000 (UTC) Received: by mail-qt1-x830.google.com with SMTP id k18so6147929qtm.10 for ; Mon, 31 Aug 2020 17:11:08 -0700 (PDT) Received: from six.home (cpe-67-241-56-252.twcny.res.rr.com. [67.241.56.252]) by smtp.gmail.com with ESMTPSA id d10sm3371675qkk.1.2020.08.31.17.11.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Aug 2020 17:11:06 -0700 (PDT) X-Inumbo-ID: 8e8d1671-645a-4483-aff3-ca16b319ce19 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=5ChwAZPKBJXfRIooObF/mR23x75fGlNBFyiMAw9Jq/8=; b=mwQrqXOu1DSz3/WiI3xwBvdp6E90F3+aImy0JdN4xrN23XTwtcNMnkf/PJofPG3nRM CLQ5oipb9OgvWbqxsH6yW5ZNyQw5QLf8vmUcH5u6KoU/Us2VWo8bvTYG1nbcfD3EZyHn OpDb8CL+7Y2jWM73S9ytmndJm3NqVvVZfDDmqbYSnk8WSqJU9n4zOsnm7q6sDdHeALZe IMMd4LyaTGBnzUuN5xh+EOXztvDqdwUJHwRXp0i2/LllnKZH7u7thVurgEeHSa44LMxv DqbUw8vLFiSzfvC+SS4cib1f9Mf75b3N1gbEiGUO4yniildP37Ud3Js0GzKBN+o6P5Ox Dq0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=5ChwAZPKBJXfRIooObF/mR23x75fGlNBFyiMAw9Jq/8=; b=ncp2fdVvzh//YrjujOEb7qST/QZonwO2gK5h4bl1F9nTUWxTUS0caChsUtm7OlMvU1 u8vKOqHxzAuJhIZJEAdNK3USmaK1aoFZDw8d88gLkygR8qa/BFLwXmDPHOO8BYMSPQ1j 0aRf2Kh5L6dlVieA6g6ODHOiTgkX4sBKxB0zRsymFgd++wDDMOiWhUPfQ0KLFAvIWoDq 8kHLhR2+xzdXIAWtZoLgD8tFH+U7CHc+dCG4WtqFZ8GX+/203vK2LwMgtpVFXWRt7i9q wsaw+36p7gTU/CXH2wvA7yuotdDxKcIXhegEaWheirz4i2mouj7a01TzGx+j7UE3yPza 2pUw== X-Gm-Message-State: AOAM5335c4kKA/0yvIyPiL6M7BrYPi0KJ6D/WnjlsdqE0LECd45uRsVy QdCrmElgDOCb+2ZwejwL2nw22i1B1A0l6g== X-Google-Smtp-Source: ABdhPJy715YUYk/ht8ekkXDjWBE9mg5R91qYbPlbr31ucbKHNdWjJf2jY7mBATo7Pc82fybnts1luA== X-Received: by 2002:ac8:4548:: with SMTP id z8mr3978566qtn.291.1598919067240; Mon, 31 Aug 2020 17:11:07 -0700 (PDT) From: Nick Rosbrook X-Google-Original-From: Nick Rosbrook To: xen-devel@lists.xenproject.org Cc: Nick Rosbrook , George Dunlap , Ian Jackson , Wei Liu Subject: [PATCH 2/2] golang/xenlight: use struct pointers in keyed union fields Date: Mon, 31 Aug 2020 20:10:58 -0400 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently, when marshalig Go types with keyed union fields, we assign the value of the struct (e.g. DomainBuildInfoTypeUnionHvm) which implements the interface of the keyed union field (e.g. DomainBuildInfoTypeUnion). As-is, this means that if a populated DomainBuildInfo is marshaled to e.g. JSON, unmarshaling back to DomainBuildInfo will fail. When the encoding/json is unmarshaling data into a Go type, and encounters a JSON object, it basically can either marshal the data into an empty interface, a map, or a struct. It cannot, however, marshal data into an interface with at least one method defined on it (e.g. DomainBuildInfoTypeUnion). Before this check is done, however, the decoder will check if the Go type is a pointer, and dereference it if so. It will then use the type of this value as the "target" type. This means that if the TypeUnion field is populated with a DomainBuildInfoTypeUnion, the decoder will see a non-empty interface and fail. If the TypeUnion field is populated with a *DomainBuildInfoTypeUnionHvm, it dereferences the pointer and sees a struct instead, allowing decoding to continue normally. Since there does not appear to be a strict need for NOT using pointers in these fields, update code generation to set keyed union fields to pointers of their implementing structs. Signed-off-by: Nick Rosbrook --- tools/golang/xenlight/gengotypes.py | 4 +-- tools/golang/xenlight/helpers.gen.go | 44 ++++++++++++++-------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tools/golang/xenlight/gengotypes.py b/tools/golang/xenlight/ge= ngotypes.py index 9acc8c0b49..a802c371b6 100644 --- a/tools/golang/xenlight/gengotypes.py +++ b/tools/golang/xenlight/gengotypes.py @@ -397,7 +397,7 @@ def xenlight_golang_union_from_C(ty =3D None, union_nam= e =3D '', struct_name =3D ''): s +=3D 'if err :=3D {0}.fromC(xc);'.format(goname) s +=3D 'err !=3D nil {{\n return fmt.Errorf("converting field {0}:= %v", err)\n}}\n'.format(goname) =20 - s +=3D 'x.{0} =3D {1}\n'.format(field_name, goname) + s +=3D 'x.{0} =3D &{1}\n'.format(field_name, goname) =20 # End switch statement s +=3D 'default:\n' @@ -563,7 +563,7 @@ def xenlight_golang_union_to_C(ty =3D None, union_name = =3D '', gotype =3D xenlight_golang_fmt_name(cgotype) =20 field_name =3D xenlight_golang_fmt_name('{0}_union'.format(keyname= )) - s +=3D 'tmp, ok :=3D x.{0}.({1})\n'.format(field_name,gotype) + s +=3D 'tmp, ok :=3D x.{0}.(*{1})\n'.format(field_name,gotype) s +=3D 'if !ok {\n' s +=3D 'return errors.New("wrong type for union key {0}")\n'.forma= t(keyname) s +=3D '}\n' diff --git a/tools/golang/xenlight/helpers.gen.go b/tools/golang/xenlight/h= elpers.gen.go index 152c7e8e6b..1a7ff89c7c 100644 --- a/tools/golang/xenlight/helpers.gen.go +++ b/tools/golang/xenlight/helpers.gen.go @@ -436,7 +436,7 @@ var connectionPty ChannelinfoConnectionUnionPty if err :=3D connectionPty.fromC(xc);err !=3D nil { return fmt.Errorf("converting field connectionPty: %v", err) } -x.ConnectionUnion =3D connectionPty +x.ConnectionUnion =3D &connectionPty case ChannelConnectionSocket: x.ConnectionUnion =3D nil case ChannelConnectionUnknown: @@ -476,7 +476,7 @@ switch x.Connection{ case ChannelConnectionUnknown: break case ChannelConnectionPty: -tmp, ok :=3D x.ConnectionUnion.(ChannelinfoConnectionUnionPty) +tmp, ok :=3D x.ConnectionUnion.(*ChannelinfoConnectionUnionPty) if !ok { return errors.New("wrong type for union key connection") } @@ -1097,7 +1097,7 @@ var typeHvm DomainBuildInfoTypeUnionHvm if err :=3D typeHvm.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typeHvm: %v", err) } -x.TypeUnion =3D typeHvm +x.TypeUnion =3D &typeHvm case DomainTypeInvalid: x.TypeUnion =3D nil case DomainTypePv: @@ -1105,13 +1105,13 @@ var typePv DomainBuildInfoTypeUnionPv if err :=3D typePv.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typePv: %v", err) } -x.TypeUnion =3D typePv +x.TypeUnion =3D &typePv case DomainTypePvh: var typePvh DomainBuildInfoTypeUnionPvh if err :=3D typePvh.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typePvh: %v", err) } -x.TypeUnion =3D typePvh +x.TypeUnion =3D &typePvh default: return fmt.Errorf("invalid union key '%v'", x.Type)} x.ArchArm.GicVersion =3D GicVersion(xc.arch_arm.gic_version) @@ -1426,7 +1426,7 @@ xc.tee =3D C.libxl_tee_type(x.Tee) xc._type =3D C.libxl_domain_type(x.Type) switch x.Type{ case DomainTypeHvm: -tmp, ok :=3D x.TypeUnion.(DomainBuildInfoTypeUnionHvm) +tmp, ok :=3D x.TypeUnion.(*DomainBuildInfoTypeUnionHvm) if !ok { return errors.New("wrong type for union key type") } @@ -1544,7 +1544,7 @@ hvm.mca_caps =3D C.uint64_t(tmp.McaCaps) hvmBytes :=3D C.GoBytes(unsafe.Pointer(&hvm),C.sizeof_libxl_domain_build_i= nfo_type_union_hvm) copy(xc.u[:],hvmBytes) case DomainTypePv: -tmp, ok :=3D x.TypeUnion.(DomainBuildInfoTypeUnionPv) +tmp, ok :=3D x.TypeUnion.(*DomainBuildInfoTypeUnionPv) if !ok { return errors.New("wrong type for union key type") } @@ -1569,7 +1569,7 @@ return fmt.Errorf("converting field E820Host: %v", er= r) pvBytes :=3D C.GoBytes(unsafe.Pointer(&pv),C.sizeof_libxl_domain_build_inf= o_type_union_pv) copy(xc.u[:],pvBytes) case DomainTypePvh: -tmp, ok :=3D x.TypeUnion.(DomainBuildInfoTypeUnionPvh) +tmp, ok :=3D x.TypeUnion.(*DomainBuildInfoTypeUnionPvh) if !ok { return errors.New("wrong type for union key type") } @@ -2149,7 +2149,7 @@ var typeHostdev DeviceUsbdevTypeUnionHostdev if err :=3D typeHostdev.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typeHostdev: %v", err) } -x.TypeUnion =3D typeHostdev +x.TypeUnion =3D &typeHostdev default: return fmt.Errorf("invalid union key '%v'", x.Type)} =20 @@ -2176,7 +2176,7 @@ xc.port =3D C.int(x.Port) xc._type =3D C.libxl_usbdev_type(x.Type) switch x.Type{ case UsbdevTypeHostdev: -tmp, ok :=3D x.TypeUnion.(DeviceUsbdevTypeUnionHostdev) +tmp, ok :=3D x.TypeUnion.(*DeviceUsbdevTypeUnionHostdev) if !ok { return errors.New("wrong type for union key type") } @@ -2367,7 +2367,7 @@ var connectionSocket DeviceChannelConnectionUnionSock= et if err :=3D connectionSocket.fromC(xc);err !=3D nil { return fmt.Errorf("converting field connectionSocket: %v", err) } -x.ConnectionUnion =3D connectionSocket +x.ConnectionUnion =3D &connectionSocket case ChannelConnectionUnknown: x.ConnectionUnion =3D nil default: @@ -2403,7 +2403,7 @@ break case ChannelConnectionPty: break case ChannelConnectionSocket: -tmp, ok :=3D x.ConnectionUnion.(DeviceChannelConnectionUnionSocket) +tmp, ok :=3D x.ConnectionUnion.(*DeviceChannelConnectionUnionSocket) if !ok { return errors.New("wrong type for union key connection") } @@ -3938,7 +3938,7 @@ var typeDiskEject EventTypeUnionDiskEject if err :=3D typeDiskEject.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typeDiskEject: %v", err) } -x.TypeUnion =3D typeDiskEject +x.TypeUnion =3D &typeDiskEject case EventTypeDomainCreateConsoleAvailable: x.TypeUnion =3D nil case EventTypeDomainDeath: @@ -3948,13 +3948,13 @@ var typeDomainShutdown EventTypeUnionDomainShutdown if err :=3D typeDomainShutdown.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typeDomainShutdown: %v", err) } -x.TypeUnion =3D typeDomainShutdown +x.TypeUnion =3D &typeDomainShutdown case EventTypeOperationComplete: var typeOperationComplete EventTypeUnionOperationComplete if err :=3D typeOperationComplete.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typeOperationComplete: %v", err) } -x.TypeUnion =3D typeOperationComplete +x.TypeUnion =3D &typeOperationComplete default: return fmt.Errorf("invalid union key '%v'", x.Type)} =20 @@ -4009,7 +4009,7 @@ xc.for_user =3D C.uint64_t(x.ForUser) xc._type =3D C.libxl_event_type(x.Type) switch x.Type{ case EventTypeDomainShutdown: -tmp, ok :=3D x.TypeUnion.(EventTypeUnionDomainShutdown) +tmp, ok :=3D x.TypeUnion.(*EventTypeUnionDomainShutdown) if !ok { return errors.New("wrong type for union key type") } @@ -4020,7 +4020,7 @@ copy(xc.u[:],domain_shutdownBytes) case EventTypeDomainDeath: break case EventTypeDiskEject: -tmp, ok :=3D x.TypeUnion.(EventTypeUnionDiskEject) +tmp, ok :=3D x.TypeUnion.(*EventTypeUnionDiskEject) if !ok { return errors.New("wrong type for union key type") } @@ -4033,7 +4033,7 @@ return fmt.Errorf("converting field Disk: %v", err) disk_ejectBytes :=3D C.GoBytes(unsafe.Pointer(&disk_eject),C.sizeof_libxl_= event_type_union_disk_eject) copy(xc.u[:],disk_ejectBytes) case EventTypeOperationComplete: -tmp, ok :=3D x.TypeUnion.(EventTypeUnionOperationComplete) +tmp, ok :=3D x.TypeUnion.(*EventTypeUnionOperationComplete) if !ok { return errors.New("wrong type for union key type") } @@ -4108,13 +4108,13 @@ var typeCat PsrHwInfoTypeUnionCat if err :=3D typeCat.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typeCat: %v", err) } -x.TypeUnion =3D typeCat +x.TypeUnion =3D &typeCat case PsrFeatTypeMba: var typeMba PsrHwInfoTypeUnionMba if err :=3D typeMba.fromC(xc);err !=3D nil { return fmt.Errorf("converting field typeMba: %v", err) } -x.TypeUnion =3D typeMba +x.TypeUnion =3D &typeMba default: return fmt.Errorf("invalid union key '%v'", x.Type)} =20 @@ -4153,7 +4153,7 @@ xc.id =3D C.uint32_t(x.Id) xc._type =3D C.libxl_psr_feat_type(x.Type) switch x.Type{ case PsrFeatTypeCat: -tmp, ok :=3D x.TypeUnion.(PsrHwInfoTypeUnionCat) +tmp, ok :=3D x.TypeUnion.(*PsrHwInfoTypeUnionCat) if !ok { return errors.New("wrong type for union key type") } @@ -4164,7 +4164,7 @@ cat.cdp_enabled =3D C.bool(tmp.CdpEnabled) catBytes :=3D C.GoBytes(unsafe.Pointer(&cat),C.sizeof_libxl_psr_hw_info_ty= pe_union_cat) copy(xc.u[:],catBytes) case PsrFeatTypeMba: -tmp, ok :=3D x.TypeUnion.(PsrHwInfoTypeUnionMba) +tmp, ok :=3D x.TypeUnion.(*PsrHwInfoTypeUnionMba) if !ok { return errors.New("wrong type for union key type") } --=20 2.17.1