Skip to contentSkip to conversation

tools/golang: When returning pointers, actually allocate structrues

PATCH Superseded George Dunlap 2 participants 5 messages Apr 19, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 tools/golang: When returning pointers, actually allocate structrues

In a handful of cases, it was decided to return a pointer to a structure rather than the plain structure itself, due to the size. However, in these cases the structure was never allocated, leading to a nil pointer exception when calling the relevant fromC method.

Allocate structures before attempting to fill them in.

---
This has been compile-tested only; Tobias, I'd appreciate a test if you get a chance.

CC Nick Rosbrook <[email protected]>
CC Anthony PERARD <[email protected]>
---
 tools/golang/xenlight/xenlight.go | 3 +++
 1 file changed, 3 insertions(+)
tools/golang/xenlight/xenlight.go +3 -0
index a45c636952..d793f172e5 100644
@@ -999,6 +999,7 @@ func (ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) {
 		err = Error(ret)
 		return
 	}
+	physinfo = &Physinfo{}
 	err = physinfo.fromC(&cphys)
 
 	return
@@ -1010,6 +1011,7 @@ func (ctx *Context) GetVersionInfo() (info *VersionInfo, err error) {
 
 	cinfo = C.libxl_get_version_info(ctx.ctx)
 
+	info = &VersionInfo{}
 	err = info.fromC(cinfo)
 
 	return
@@ -1027,6 +1029,7 @@ func (ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) {
 		return
 	}
 
+	di = &Dominfo{}
 	err = di.fromC(&cdi)
 
 	return
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/3 golang/xenlight: Run `go fmt` on non-generated golang files

No functional change.

---
CC: Nick Rosbrook <[email protected]>
CC: Anthony PERARD <[email protected]>

---
 tools/golang/xenlight/xenlight.go | 55 +++++++++++++++++--------------
 1 file changed, 30 insertions(+), 25 deletions(-)
tools/golang/xenlight/xenlight.go +30 -25
index d793f172e5..0ff28590ee 100644
@@ -633,9 +633,11 @@ func (ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo, err error) {
 }
 
 // int libxl_cpupool_create(libxl_ctx *ctx, const char *name,
-//                          libxl_scheduler sched,
-//                          libxl_bitmap cpumap, libxl_uuid *uuid,
-//                          uint32_t *poolid);
+//
+//	libxl_scheduler sched,
+//	libxl_bitmap cpumap, libxl_uuid *uuid,
+//	uint32_t *poolid);
+//
 // FIXME: uuid
 // FIXME: Setting poolid
 func (ctx *Context) CpupoolCreate(Name string, Scheduler Scheduler, Cpumap Bitmap) (err error, Poolid uint32) {
@@ -688,7 +690,8 @@ func (ctx *Context) CpupoolCpuadd(Poolid uint32, Cpu int) (err error) {
 }
 
 // int libxl_cpupool_cpuadd_cpumap(libxl_ctx *ctx, uint32_t poolid,
-//                                 const libxl_bitmap *cpumap);
+//
+//	const libxl_bitmap *cpumap);
 func (ctx *Context) CpupoolCpuaddCpumap(Poolid uint32, Cpumap Bitmap) (err error) {
 	var cbm C.libxl_bitmap
 	if err = Cpumap.toC(&cbm); err != nil {
@@ -717,7 +720,8 @@ func (ctx *Context) CpupoolCpuremove(Poolid uint32, Cpu int) (err error) {
 }
 
 // int libxl_cpupool_cpuremove_cpumap(libxl_ctx *ctx, uint32_t poolid,
-//                                    const libxl_bitmap *cpumap);
+//
+//	const libxl_bitmap *cpumap);
 func (ctx *Context) CpupoolCpuremoveCpumap(Poolid uint32, Cpumap Bitmap) (err error) {
 	var cbm C.libxl_bitmap
 	if err = Cpumap.toC(&cbm); err != nil {
@@ -789,9 +793,7 @@ func (ctx *Context) CpupoolMovedomain(Poolid uint32, Id Domid) (err error) {
 	return
 }
 
-//
 // Utility functions
-//
 func (ctx *Context) CpupoolFindByName(name string) (info Cpupoolinfo, found bool) {
 	plist := ctx.ListCpupool()
 
@@ -939,7 +941,7 @@ func (bm Bitmap) String() (s string) {
 	return
 }
 
-//int libxl_get_max_cpus(libxl_ctx *ctx);
+// int libxl_get_max_cpus(libxl_ctx *ctx);
 func (ctx *Context) GetMaxCpus() (maxCpus int, err error) {
 	ret := C.libxl_get_max_cpus(ctx.ctx)
 	if ret < 0 {
@@ -950,7 +952,7 @@ func (ctx *Context) GetMaxCpus() (maxCpus int, err error) {
 	return
 }
 
-//int libxl_get_online_cpus(libxl_ctx *ctx);
+// int libxl_get_online_cpus(libxl_ctx *ctx);
 func (ctx *Context) GetOnlineCpus() (onCpus int, err error) {
 	ret := C.libxl_get_online_cpus(ctx.ctx)
 	if ret < 0 {
@@ -961,7 +963,7 @@ func (ctx *Context) GetOnlineCpus() (onCpus int, err error) {
 	return
 }
 
-//int libxl_get_max_nodes(libxl_ctx *ctx);
+// int libxl_get_max_nodes(libxl_ctx *ctx);
 func (ctx *Context) GetMaxNodes() (maxNodes int, err error) {
 	ret := C.libxl_get_max_nodes(ctx.ctx)
 	if ret < 0 {
@@ -972,7 +974,7 @@ func (ctx *Context) GetMaxNodes() (maxNodes int, err error) {
 	return
 }
 
-//int libxl_get_free_memory(libxl_ctx *ctx, uint64_t *memkb);
+// int libxl_get_free_memory(libxl_ctx *ctx, uint64_t *memkb);
 func (ctx *Context) GetFreeMemory() (memkb uint64, err error) {
 	var cmem C.uint64_t
 	ret := C.libxl_get_free_memory(ctx.ctx, &cmem)
@@ -987,7 +989,7 @@ func (ctx *Context) GetFreeMemory() (memkb uint64, err error) {
 
 }
 
-//int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
+// int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
 func (ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) {
 	var cphys C.libxl_physinfo
 	C.libxl_physinfo_init(&cphys)
@@ -1005,7 +1007,7 @@ func (ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) {
 	return
 }
 
-//const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx);
+// const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx);
 func (ctx *Context) GetVersionInfo() (info *VersionInfo, err error) {
 	var cinfo *C.libxl_version_info
 
@@ -1044,7 +1046,7 @@ func (ctx *Context) DomainUnpause(Id Domid) (err error) {
 	return
 }
 
-//int libxl_domain_pause(libxl_ctx *ctx, uint32_t domain);
+// int libxl_domain_pause(libxl_ctx *ctx, uint32_t domain);
 func (ctx *Context) DomainPause(id Domid) (err error) {
 	ret := C.libxl_domain_pause(ctx.ctx, C.uint32_t(id), nil)
 
@@ -1054,7 +1056,7 @@ func (ctx *Context) DomainPause(id Domid) (err error) {
 	return
 }
 
-//int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid);
+// int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid);
 func (ctx *Context) DomainShutdown(id Domid) (err error) {
 	ret := C.libxl_domain_shutdown(ctx.ctx, C.uint32_t(id), nil)
 
@@ -1064,7 +1066,7 @@ func (ctx *Context) DomainShutdown(id Domid) (err error) {
 	return
 }
 
-//int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid);
+// int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid);
 func (ctx *Context) DomainReboot(id Domid) (err error) {
 	ret := C.libxl_domain_reboot(ctx.ctx, C.uint32_t(id), nil)
 
@@ -1074,8 +1076,8 @@ func (ctx *Context) DomainReboot(id Domid) (err error) {
 	return
 }
 
-//libxl_dominfo * libxl_list_domain(libxl_ctx*, int *nb_domain_out);
-//void libxl_dominfo_list_free(libxl_dominfo *list, int nb_domain);
+// libxl_dominfo * libxl_list_domain(libxl_ctx*, int *nb_domain_out);
+// void libxl_dominfo_list_free(libxl_dominfo *list, int nb_domain);
 func (ctx *Context) ListDomain() (glist []Dominfo) {
 	var nbDomain C.int
 	clist := C.libxl_list_domain(ctx.ctx, &nbDomain)
@@ -1095,9 +1097,11 @@ func (ctx *Context) ListDomain() (glist []Dominfo) {
 	return
 }
 
-//libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
-//				int *nb_vcpu, int *nr_cpus_out);
-//void libxl_vcpuinfo_list_free(libxl_vcpuinfo *, int nr_vcpus);
+// libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
+//
+//	int *nb_vcpu, int *nr_cpus_out);
+//
+// void libxl_vcpuinfo_list_free(libxl_vcpuinfo *, int nr_vcpus);
 func (ctx *Context) ListVcpu(id Domid) (glist []Vcpuinfo) {
 	var nbVcpu C.int
 	var nrCpu C.int
@@ -1126,8 +1130,8 @@ func (ct ConsoleType) String() (str string) {
 	return
 }
 
-//int libxl_console_get_tty(libxl_ctx *ctx, uint32_t domid, int cons_num,
-//libxl_console_type type, char **path);
+// int libxl_console_get_tty(libxl_ctx *ctx, uint32_t domid, int cons_num,
+// libxl_console_type type, char **path);
 func (ctx *Context) ConsoleGetTty(id Domid, consNum int, conType ConsoleType) (path string, err error) {
 	var cpath *C.char
 	ret := C.libxl_console_get_tty(ctx.ctx, C.uint32_t(id), C.int(consNum), C.libxl_console_type(conType), &cpath)
@@ -1141,8 +1145,9 @@ func (ctx *Context) ConsoleGetTty(id Domid, consNum int, conType ConsoleType) (p
 	return
 }
 
-//int libxl_primary_console_get_tty(libxl_ctx *ctx, uint32_t domid_vm,
-//					char **path);
+// int libxl_primary_console_get_tty(libxl_ctx *ctx, uint32_t domid_vm,
+//
+//	char **path);
 func (ctx *Context) PrimaryConsoleGetTty(domid uint32) (path string, err error) {
 	var cpath *C.char
 	ret := C.libxl_primary_console_get_tty(ctx.ctx, C.uint32_t(domid), &cpath)
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/3] golang/xenlight: Run `go fmt` on non-generated golang files

Quoted 16 lines · George Dunlap

No functional change.

---
CC: Nick Rosbrook <[email protected]>
CC: Anthony PERARD <[email protected]>

---
 tools/golang/xenlight/xenlight.go | 55 +++++++++++++++++--------------
 1 file changed, 30 insertions(+), 25 deletions(-)
tools/golang/xenlight/xenlight.go +0 -0
index d793f172e5..0ff28590ee 100644

tools/golang/xenlight/xenlight.go func (ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo, err error) { · line 639

Quoted 10 lines
tools/golang/xenlight/xenlight.go +4 -3
@@ -633,9 +633,11 @@ func (ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo, err error) {
 }
 
 // int libxl_cpupool_create(libxl_ctx *ctx, const char *name,
-//                          libxl_scheduler sched,
-//                          libxl_bitmap cpumap, libxl_uuid *uuid,
-//                          uint32_t *poolid);
+//
+//     libxl_scheduler sched,
+//     libxl_bitmap cpumap, libxl_uuid *uuid,
+//     uint32_t *poolid);

Gah, sorry, I forgot to delete some of these blank lines that were added in. I'll fix it up in v2.

-George

#3 lore raw

PATCH 3/3 tools/golang: Run `go vet` as part of the build process

In reply to George Dunlap

---
CC: Nick Rosbrook <[email protected]>
CC: Anthony PERARD <[email protected]>

---
 tools/golang/xenlight/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/golang/xenlight/Makefile +1 -0
index c5bb6b94a8..645e7b3a82 100644
@@ -29,6 +29,7 @@ $(subst .gen.,.%.,$(GOXL_GEN_FILES)): gengotypes.py $(LIBXL_SRC_DIR)/libxl_types
 # so that it can find the actual library.
 .PHONY: build
 build: xenlight.go $(GOXL_GEN_FILES)
+	CGO_CFLAGS="$(CFLAGS_libxenlight) $(CFLAGS_libxentoollog) $(APPEND_CFLAGS)" CGO_LDFLAGS="$(call xenlibs-ldflags,light toollog) $(APPEND_LDFLAGS)" $(GO) vet
 	CGO_CFLAGS="$(CFLAGS_libxenlight) $(CFLAGS_libxentoollog) $(APPEND_CFLAGS)" CGO_LDFLAGS="$(call xenlibs-ldflags,light toollog) $(APPEND_LDFLAGS)" $(GO) build -x
 
 .PHONY: install
Signature · 1 line
2.25.1
#4 lore raw
In reply to George Dunlap

Thank you for the fast response, George. I can confirm that I can call all three functions without errors now and this patch fixes the errors I encountered.

Am 19. April 2024 12:50:35 MESZ schrieb George Dunlap <[email protected]>:

Quoted 49 lines

In a handful of cases, it was decided to return a pointer to a structure rather than the plain structure itself, due to the size. However, in these cases the structure was never allocated, leading to a nil pointer exception when calling the relevant fromC method.

Allocate structures before attempting to fill them in.

---
This has been compile-tested only; Tobias, I'd appreciate a test if you get a chance.

CC Nick Rosbrook <[email protected]>
CC Anthony PERARD <[email protected]>

---
tools/golang/xenlight/xenlight.go | 3 +++

1 file changed, 3 insertions(+)
tools/golang/xenlight/xenlight.go +3 -0
index a45c636952..d793f172e5 100644
@@ -999,6 +999,7 @@ func (ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) {
 		err = Error(ret)
 		return
 	}
+	physinfo = &Physinfo{}
 	err = physinfo.fromC(&cphys)
 
 	return
@@ -1010,6 +1011,7 @@ func (ctx *Context) GetVersionInfo() (info *VersionInfo, err error) {
 
 	cinfo = C.libxl_get_version_info(ctx.ctx)
 
+	info = &VersionInfo{}
 	err = info.fromC(cinfo)
 
 	return
@@ -1027,6 +1029,7 @@ func (ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) {
 		return
 	}
 
+	di = &Dominfo{}
 	err = di.fromC(&cdi)
 
 	return
Signature · 1 line
2.25.1