From: Nick Rosbrook <rosbrookn@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: Nick Rosbrook <rosbrookn@ainfosec.com>,
George Dunlap <george.dunlap@citrix.com>,
Ian Jackson <iwj@xenproject.org>, Wei Liu <wl@xen.org>
Subject: [PATCH 02/12] golang/xenlight: fix StringList toC conversion
Date: Wed, 28 Apr 2021 20:14:24 -0400 [thread overview]
Message-ID: <0a15ed9eb6cd70416995f5d9805c98572eb6bd50.1619655104.git.rosbrookn@ainfosec.com> (raw)
In-Reply-To: <cover.1619650820.git.rosbrookn@ainfosec.com>
In-Reply-To: <29e665fc1c9313f5e221e9e5e15d7c2d9c1eb4a7.1619655104.git.rosbrookn@ainfosec.com>
The current implementation of StringList.toC does not correctly account
for how libxl_string_list is expected to be laid out in C, which is clear
when one looks at libxl_string_list_length in libxl.c. In particular,
StringList.toC does not account for the extra memory that should be
allocated for the "sentinel" entry. And, when using the "slice trick" to
create a slice that can address C memory, the unsafe.Pointer conversion
should be on a C.libxl_string_list, not *C.libxl_string_list.
Fix these problems by (1) allocating an extra slot in the slice used to
address the C memory, and explicity set the last entry to nil so the C
memory will be zeroed out, and (2) dereferencing csl in the
unsafe.Pointer conversion.
Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
---
tools/golang/xenlight/xenlight.go | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index b9189dec5c..13171d0ad1 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -491,13 +491,14 @@ func (sl *StringList) fromC(csl *C.libxl_string_list) error {
func (sl StringList) toC(csl *C.libxl_string_list) error {
var char *C.char
- size := len(sl)
+ size := len(sl) + 1
*csl = (C.libxl_string_list)(C.malloc(C.ulong(size) * C.ulong(unsafe.Sizeof(char))))
- clist := (*[1 << 30]*C.char)(unsafe.Pointer(csl))[:size:size]
+ clist := (*[1 << 30]*C.char)(unsafe.Pointer(*csl))[:size:size]
for i, v := range sl {
clist[i] = C.CString(v)
}
+ clist[len(clist)-1] = nil
return nil
}
--
2.17.1
next prev parent reply other threads:[~2021-04-29 0:14 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-29 0:14 [PATCH 00/12] golang/xenlight: domain life cycle support Nick Rosbrook
2021-04-29 0:14 ` [PATCH 01/12] golang/xenlight: update generated code Nick Rosbrook
2021-04-29 0:14 ` Nick Rosbrook [this message]
2021-04-29 0:14 ` [PATCH 03/12] golang/xenlight: fix string conversion in generated toC functions Nick Rosbrook
2021-04-29 0:14 ` [PATCH 04/12] golang/xenlight: export keyed union interface types Nick Rosbrook
2021-04-29 0:14 ` [PATCH 05/12] golang/xenlight: use struct pointers in keyed union fields Nick Rosbrook
2021-04-29 0:14 ` [PATCH 06/12] golang/xenlight: rename Ctx receivers to ctx Nick Rosbrook
2021-04-29 0:14 ` [PATCH 07/12] golang/xenlight: add logging conveniences for within xenlight Nick Rosbrook
2021-04-29 0:14 ` [PATCH 08/12] golang/xenlight: add functional options to configure Context Nick Rosbrook
2021-04-29 0:14 ` [PATCH 09/12] golang/xenlight: add DomainDestroy wrapper Nick Rosbrook
2021-04-29 0:14 ` [PATCH 10/12] golang/xenlight: add SendTrigger wrapper Nick Rosbrook
2021-04-29 0:14 ` [PATCH 11/12] golang/xenlight: do not negate ret when converting to Error Nick Rosbrook
2021-04-29 0:14 ` [PATCH 12/12] golang/xenlight: add NotifyDomainDeath method to Context Nick Rosbrook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0a15ed9eb6cd70416995f5d9805c98572eb6bd50.1619655104.git.rosbrookn@ainfosec.com \
--to=rosbrookn@gmail.com \
--cc=george.dunlap@citrix.com \
--cc=iwj@xenproject.org \
--cc=rosbrookn@ainfosec.com \
--cc=wl@xen.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).