[PATCH 0/4] libguestfs modifications in order to pass "make check"
In my Fedora 34 environment, with all libguestfs features enabled except the Erlang and Haskell bindings, "make check" passes if: - these patches are applied on top of current master (f47e0bb67254), and - the btrfs-progs package is downgraded to 5.13.1-1.fc34.x86_64, according to <https://bugzilla.redhat.com/show_bug.cgi?id=2005529>. Thanks, Laszlo Laszlo Ersek (4): test-9p: fix the base directory that's exported to the guest test-md-and-lvm-devices: work around RAID0 regression in Linux v3.14/v5.4 tests: xfs: remove lazy-counter disablement test Go bindings: fix "C array of strings" -- char** -- allocation generator/actions_core.ml | 6 ---- generator/golang.ml | 46 +++++++++++++++++++++++------ tests/9p/test-9p.sh | 2 +- tests/md/test-md-and-lvm-devices.sh | 12 +++++--- 4 files changed, 46 insertions(+), 20 deletions(-) -- 2.19.1.3.g30247aa5d201
In commit 6d32773e8118 ("tests: Run the tests in parallel.", 2021-03-18), the "abs_srcdir" macro value that the 9p test would see changed from ".../tests/9p" to just ".../tests" -- the last component got dropped. (Said commit updated some "abs_srcdir"-based references accordingly, for example under "tests/disks", but "tests/9p/test-9p.sh" was missed.) Therefore, the guest-visible location of the "/test-9p.sh" file changed to "/9p/test-9p.sh", and a non-recursive listing of the guest-visible root directory would not return the file. Thus, the test fails now. Restore the host-side base directory to ".../tests/9p". Fixes: 6d32773e811882f78dbd8c2a39a2b7a9c3cfca7c Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- tests/9p/test-9p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/9p/test-9p.sh b/tests/9p/test-9p.sh index b4bdbe56e077..4fd5de7fdafe 100755 --- a/tests/9p/test-9p.sh +++ b/tests/9p/test-9p.sh @@ -45,7 +45,7 @@ guestfish <<EOF sparse test-9p.img 1M config -device '$virtio_9p,fsdev=test9p,mount_tag=test9p' -config -fsdev 'local,id=test9p,path=${abs_srcdir},security_model=passthrough' +config -fsdev 'local,id=test9p,path=${abs_srcdir}/9p,security_model=passthrough' run -- 2.19.1.3.g30247aa5d201
On 09/20/21 07:23, Laszlo Ersek wrote:
In commit 6d32773e8118 ("tests: Run the tests in parallel.", 2021-03-18), the "abs_srcdir" macro value that the 9p test would see changed from ".../tests/9p" to just ".../tests" -- the last component got dropped.
(Said commit updated some "abs_srcdir"-based references accordingly, for example under "tests/disks", but "tests/9p/test-9p.sh" was missed.)
Therefore, the guest-visible location of the "/test-9p.sh" file changed to "/9p/test-9p.sh", and a non-recursive listing of the guest-visible root directory would not return the file. Thus, the test fails now.
Restore the host-side base directory to ".../tests/9p".
Fixes: 6d32773e811882f78dbd8c2a39a2b7a9c3cfca7c Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- tests/9p/test-9p.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/9p/test-9p.sh b/tests/9p/test-9p.sh index b4bdbe56e077..4fd5de7fdafe 100755 --- a/tests/9p/test-9p.sh +++ b/tests/9p/test-9p.sh @@ -45,7 +45,7 @@ guestfish <<EOF sparse test-9p.img 1M
config -device '$virtio_9p,fsdev=test9p,mount_tag=test9p' -config -fsdev 'local,id=test9p,path=${abs_srcdir},security_model=passthrough' +config -fsdev 'local,id=test9p,path=${abs_srcdir}/9p,security_model=passthrough'
run
This patch has been pushed as commit 8156c1652062. Thanks, Laszlo
The "test-md-and-lvm-devices" test case creates, among other things, a RAID0 array (md127) that spans two *differently sized* block devices (sda1: 20MB, lv0: 16MB). In Linux v3.14, the layout of such arrays was changed incompatibly and undetectably. If an array were created with a pre-v3.14 kernel and assembled on a v3.14+ kernel, or vice versa, data could be corrupted. In Linux v5.4, a mitigation was added, requiring the user to specify the layout version of such RAID0 arrays explicitly, as a module parameter. If the user fails to specify a layout version, the v5.4+ kernel refuses to assemble such arrays. This is why "test-md-and-lvm-devices" currently fails, with any v5.4+ appliance kernel. Until we implement a more general solution (see the bugzilla link below), work around the issue by sizing sda1 and lv0 identically. For this, increase the size of sdb1 to 24MB: when one 4MB extent is spent on LVM metadata, the resultant lv0 size (20MB) will precisely match the size of sda1. This workaround only affects sizes, and does not interfere with the original purpose of this test case, which is to test various *stackings* between disk partitions, software RAID (md), and LVM logical volumes. Related: https://bugzilla.redhat.com/show_bug.cgi?id=2005485 Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- tests/md/test-md-and-lvm-devices.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/md/test-md-and-lvm-devices.sh b/tests/md/test-md-and-lvm-devices.sh index 5e82e3a4ff69..54f1c3bfb3f5 100755 --- a/tests/md/test-md-and-lvm-devices.sh +++ b/tests/md/test-md-and-lvm-devices.sh @@ -53,7 +53,7 @@ trap cleanup INT QUIT TERM EXIT # sda2: 20M PV (vg1) # sda3: 20M MD (md125) # -# sdb1: 20M PV (vg0) +# sdb1: 24M PV (vg0) [*] # sdb2: 20M PV (vg2) # sdb3: 20M MD (md125) # @@ -66,6 +66,9 @@ trap cleanup INT QUIT TERM EXIT # vg3 : VG (md125) # lv3 : LV (vg3) # +# [*] The reason for making sdb1 4M larger than sda1 is that the LVM metadata +# will consume one 4MB extent, and we need lv0 to offer exactly as much space +# as sda1 does, for combining them in md127. Refer to RHBZ#2005485. guestfish <<EOF # Add 2 empty disks @@ -79,9 +82,10 @@ part-add /dev/sda p 64 41023 part-add /dev/sda p 41024 81983 part-add /dev/sda p 81984 122943 part-init /dev/sdb mbr -part-add /dev/sdb p 64 41023 -part-add /dev/sdb p 41024 81983 -part-add /dev/sdb p 81984 122943 +part-add /dev/sdb p 64 49215 +part-add /dev/sdb p 49216 90175 +part-add /dev/sdb p 90176 131135 + # Create volume group and logical volume on sdb1 pvcreate /dev/sdb1 -- 2.19.1.3.g30247aa5d201
On 09/20/21 07:23, Laszlo Ersek wrote:
The "test-md-and-lvm-devices" test case creates, among other things, a RAID0 array (md127) that spans two *differently sized* block devices (sda1: 20MB, lv0: 16MB).
In Linux v3.14, the layout of such arrays was changed incompatibly and undetectably. If an array were created with a pre-v3.14 kernel and assembled on a v3.14+ kernel, or vice versa, data could be corrupted.
In Linux v5.4, a mitigation was added, requiring the user to specify the layout version of such RAID0 arrays explicitly, as a module parameter. If the user fails to specify a layout version, the v5.4+ kernel refuses to assemble such arrays. This is why "test-md-and-lvm-devices" currently fails, with any v5.4+ appliance kernel.
Until we implement a more general solution (see the bugzilla link below), work around the issue by sizing sda1 and lv0 identically. For this, increase the size of sdb1 to 24MB: when one 4MB extent is spent on LVM metadata, the resultant lv0 size (20MB) will precisely match the size of sda1.
This workaround only affects sizes, and does not interfere with the original purpose of this test case, which is to test various *stackings* between disk partitions, software RAID (md), and LVM logical volumes.
Related: https://bugzilla.redhat.com/show_bug.cgi?id=2005485 Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- tests/md/test-md-and-lvm-devices.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/tests/md/test-md-and-lvm-devices.sh b/tests/md/test-md-and-lvm-devices.sh index 5e82e3a4ff69..54f1c3bfb3f5 100755 --- a/tests/md/test-md-and-lvm-devices.sh +++ b/tests/md/test-md-and-lvm-devices.sh @@ -53,7 +53,7 @@ trap cleanup INT QUIT TERM EXIT # sda2: 20M PV (vg1) # sda3: 20M MD (md125) # -# sdb1: 20M PV (vg0) +# sdb1: 24M PV (vg0) [*] # sdb2: 20M PV (vg2) # sdb3: 20M MD (md125) # @@ -66,6 +66,9 @@ trap cleanup INT QUIT TERM EXIT # vg3 : VG (md125) # lv3 : LV (vg3) # +# [*] The reason for making sdb1 4M larger than sda1 is that the LVM metadata +# will consume one 4MB extent, and we need lv0 to offer exactly as much space +# as sda1 does, for combining them in md127. Refer to RHBZ#2005485.
guestfish <<EOF # Add 2 empty disks @@ -79,9 +82,10 @@ part-add /dev/sda p 64 41023 part-add /dev/sda p 41024 81983 part-add /dev/sda p 81984 122943 part-init /dev/sdb mbr -part-add /dev/sdb p 64 41023 -part-add /dev/sdb p 41024 81983 -part-add /dev/sdb p 81984 122943 +part-add /dev/sdb p 64 49215 +part-add /dev/sdb p 49216 90175 +part-add /dev/sdb p 90176 131135 +
I've removed the stray empty line just above, and pushed the patch as commit 24f98939ae0c. Thanks Laszlo
# Create volume group and logical volume on sdb1 pvcreate /dev/sdb1
According to xfs_admin(8):
-c 0|1 Enable (1) or disable (0) lazy-counters in the filesys‐ tem.
Lazy-counters may not be disabled on Version 5 su‐ perblock filesystems (i.e. those with metadata CRCs en‐ abled).
[...]
According to mkfs.xfs(1):
-m global_metadata_options Section Name: [metadata] These options specify metadata format options that ei‐ ther apply to the entire filesystem or aren't easily characterised by a specific functionality group. The valid global_metadata_options are:
[...]
crc=value This is used to create a filesystem which maintains and checks CRC information in all metadata objects on disk. The value is ei‐ ther 0 to disable the feature, or 1 to en‐ able the use of CRCs.
[...]
By default, mkfs.xfs will enable metadata CRCs.
Consistently with the above, the first "xfs_admin" test case in "generator/actions_core.ml", which attempts to disable lazy counters, always fails:
534/550 test_xfs_admin_0 libguestfs: error: xfs_admin: /dev/sda1: Cannot disable lazy-counters on V5 fs
We can resolve this test failure in three ways: (1) Extend do_mkfs() [daemon/mkfs.c], possibly even introduce do_mkfs_xfs(), and permit the caller to specify "-m crc=0" for mkfs.xfs. Then use this option when the temporary filesystem is created in the XFS test that disables lazy counters. (2) Extend the "guestfs_int_xfsinfo" structure in the libguestfs-common project, with an "xfs_crc" field. Extend parse_xfs_info() [daemon/xfs.c] to populate the field from "meta-data=...crc=[01]". Modify the test case to check the following post-condition: xfs_crc || xfs_lazycount == 0 instead of the current xfs_lazycount == 0 effectively ignoring "xfs_lazycount" when "xfs_crc" is set. (3) Remove the test altogether that attempts to disable lazy counters after filesystem creation. Given that new XFS filesystems are created with metadata CRCs enabled by default, and several XFS features depend on metadata CRCs being enabled, this patch implements option (3). Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/actions_core.ml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/generator/actions_core.ml b/generator/actions_core.ml index bb602ee02dfc..5933282dcf90 100644 --- a/generator/actions_core.ml +++ b/generator/actions_core.ml @@ -7644,12 +7644,6 @@ with zeroes)." }; style = RErr, [String (Device, "device")], [OBool "extunwritten"; OBool "imgfile"; OBool "v2log"; OBool "projid32bit"; OBool "lazycounter"; OString "label"; OString "uuid"]; optional = Some "xfs"; tests = [ - InitEmpty, Always, TestResult ( - [["part_disk"; "/dev/sda"; "mbr"]; - ["mkfs"; "xfs"; "/dev/sda1"; ""; "NOARG"; ""; ""; "NOARG"]; - ["xfs_admin"; "/dev/sda1"; ""; ""; ""; ""; "false"; "NOARG"; "NOARG"]; - ["mount"; "/dev/sda1"; "/"]; - ["xfs_info"; "/"]], "ret->xfs_lazycount == 0"), []; InitEmpty, Always, TestResultString ( [["part_disk"; "/dev/sda"; "mbr"]; ["mkfs"; "xfs"; "/dev/sda1"; ""; "NOARG"; ""; ""; "NOARG"]; -- 2.19.1.3.g30247aa5d201
On 09/20/21 07:23, Laszlo Ersek wrote:
According to xfs_admin(8):
-c 0|1 Enable (1) or disable (0) lazy-counters in the filesys‐ tem.
Lazy-counters may not be disabled on Version 5 su‐ perblock filesystems (i.e. those with metadata CRCs en‐ abled).
[...]
According to mkfs.xfs(1):
-m global_metadata_options Section Name: [metadata] These options specify metadata format options that ei‐ ther apply to the entire filesystem or aren't easily characterised by a specific functionality group. The valid global_metadata_options are:
[...]
crc=value This is used to create a filesystem which maintains and checks CRC information in all metadata objects on disk. The value is ei‐ ther 0 to disable the feature, or 1 to en‐ able the use of CRCs.
[...]
By default, mkfs.xfs will enable metadata CRCs.
Consistently with the above, the first "xfs_admin" test case in "generator/actions_core.ml", which attempts to disable lazy counters, always fails:
534/550 test_xfs_admin_0 libguestfs: error: xfs_admin: /dev/sda1: Cannot disable lazy-counters on V5 fs
We can resolve this test failure in three ways:
(1) Extend do_mkfs() [daemon/mkfs.c], possibly even introduce do_mkfs_xfs(), and permit the caller to specify "-m crc=0" for mkfs.xfs. Then use this option when the temporary filesystem is created in the XFS test that disables lazy counters.
(2) Extend the "guestfs_int_xfsinfo" structure in the libguestfs-common project, with an "xfs_crc" field. Extend parse_xfs_info() [daemon/xfs.c] to populate the field from "meta-data=...crc=[01]". Modify the test case to check the following post-condition:
xfs_crc || xfs_lazycount == 0
instead of the current
xfs_lazycount == 0
effectively ignoring "xfs_lazycount" when "xfs_crc" is set.
(3) Remove the test altogether that attempts to disable lazy counters after filesystem creation.
Given that new XFS filesystems are created with metadata CRCs enabled by default, and several XFS features depend on metadata CRCs being enabled, this patch implements option (3).
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/actions_core.ml | 6 ------ 1 file changed, 6 deletions(-)
diff --git a/generator/actions_core.ml b/generator/actions_core.ml index bb602ee02dfc..5933282dcf90 100644 --- a/generator/actions_core.ml +++ b/generator/actions_core.ml @@ -7644,12 +7644,6 @@ with zeroes)." }; style = RErr, [String (Device, "device")], [OBool "extunwritten"; OBool "imgfile"; OBool "v2log"; OBool "projid32bit"; OBool "lazycounter"; OString "label"; OString "uuid"]; optional = Some "xfs"; tests = [ - InitEmpty, Always, TestResult ( - [["part_disk"; "/dev/sda"; "mbr"]; - ["mkfs"; "xfs"; "/dev/sda1"; ""; "NOARG"; ""; ""; "NOARG"]; - ["xfs_admin"; "/dev/sda1"; ""; ""; ""; ""; "false"; "NOARG"; "NOARG"]; - ["mount"; "/dev/sda1"; "/"]; - ["xfs_info"; "/"]], "ret->xfs_lazycount == 0"), []; InitEmpty, Always, TestResultString ( [["part_disk"; "/dev/sda"; "mbr"]; ["mkfs"; "xfs"; "/dev/sda1"; ""; "NOARG"; ""; ""; "NOARG"];
Patch pushed as commit 627f808e4b0a. Thanks Laszlo
The current "arg_string_list" and "free_string_list" implementations go back to commit b6f01f32606d ("Add Go (language) bindings.", 2013-07-03). There are two problems with them: - "free_string_list" doesn't actually free anything, - at the *first* such g.Internal_test() call site that passes an Ostringlist member inside the Optargs argument, namely:
g.Internal_test ("abc", string_addr ("def"), []string{}, false, 0, 0, "123", "456", []byte{'a', 'b', 'c', '\000', 'a', 'b', 'c'}, &guestfs.OptargsInternal_test{Ostringlist_is_set: true, Ostringlist: []string{} } )
the "golang/run-bindtests" case crashes:
panic: runtime error: cgo argument has Go pointer to Go pointer
goroutine 1 [running]: libguestfs.org/guestfs.(*Guestfs).Internal_test.func7(0xc000018180, 0xadfb60, 0xadfb80, 0xc000010048, 0x0, 0x0, 0x0, 0xae3e10, 0xae3e30, 0xade3a0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0xa9 libguestfs.org/guestfs.(*Guestfs).Internal_test(0xc000018180, 0x4ee3a5, 0x3, 0xc000061be8, 0xc000061af8, 0x0, 0x0, 0xc000061a00, 0x0, 0x0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0x3c9 main.main() golang/bindtests/bindtests.go:77 +0x151e exit status 2 FAIL run-bindtests (exit status: 1)
It turns out that a C-language array containing C-language pointers can only be allocated in C: https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-... Rewrite the "arg_string_list" and "free_string_list" functions almost entirely in C. Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/golang.ml | 46 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/generator/golang.ml b/generator/golang.ml index d328abe4ed08..5db478e8a494 100644 --- a/generator/golang.ml +++ b/generator/golang.ml @@ -52,6 +52,39 @@ _go_guestfs_create_flags (unsigned flags) { return guestfs_create_flags (flags); } + +// +// A C-language array of C-language pointers needs to be allocated in C. +// https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-... +// +typedef char *pChar; + +pChar *allocStringArray (size_t nmemb) +{ + pChar *array; + + array = malloc (sizeof (pChar) * (nmemb + 1)); + array[nmemb] = NULL; + return array; +} + +void storeString (pChar *array, size_t idx, pChar string) +{ + array[idx] = string; +} + +void freeStringArray (pChar *array) +{ + pChar *position; + pChar string; + + position = array; + while ((string = *position) != NULL) { + free (string); + position++; + } + free (array); +} */ import \"C\" @@ -148,12 +181,11 @@ func (g *Guestfs) Close () *GuestfsError { * C strings and golang []string. */ func arg_string_list (xs []string) **C.char { - r := make ([]*C.char, 1 + len (xs)) + r := C.allocStringArray (C.size_t (len (xs))) for i, x := range xs { - r[i] = C.CString (x) + C.storeString (r, C.size_t (i), C.CString (x)); } - r[len (xs)] = nil - return &r[0] + return (**C.char) (r) } func count_string_list (argv **C.char) int { @@ -167,11 +199,7 @@ func count_string_list (argv **C.char) int { } func free_string_list (argv **C.char) { - for *argv != nil { - //C.free (*argv) - argv = (**C.char) (unsafe.Pointer (uintptr (unsafe.Pointer (argv)) + - unsafe.Sizeof (*argv))) - } + C.freeStringArray ((*C.pChar) (argv)) } func return_string_list (argv **C.char) []string { -- 2.19.1.3.g30247aa5d201
On Mon, Sep 20, 2021 at 07:23:35AM +0200, Laszlo Ersek wrote:
The current "arg_string_list" and "free_string_list" implementations go back to commit b6f01f32606d ("Add Go (language) bindings.", 2013-07-03). There are two problems with them:
- "free_string_list" doesn't actually free anything,
- at the *first* such g.Internal_test() call site that passes an Ostringlist member inside the Optargs argument, namely:
g.Internal_test ("abc", string_addr ("def"), []string{}, false, 0, 0, "123", "456", []byte{'a', 'b', 'c', '\000', 'a', 'b', 'c'}, &guestfs.OptargsInternal_test{Ostringlist_is_set: true, Ostringlist: []string{} } )
the "golang/run-bindtests" case crashes:
panic: runtime error: cgo argument has Go pointer to Go pointer
goroutine 1 [running]: libguestfs.org/guestfs.(*Guestfs).Internal_test.func7(0xc000018180, 0xadfb60, 0xadfb80, 0xc000010048, 0x0, 0x0, 0x0, 0xae3e10, 0xae3e30, 0xade3a0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0xa9 libguestfs.org/guestfs.(*Guestfs).Internal_test(0xc000018180, 0x4ee3a5, 0x3, 0xc000061be8, 0xc000061af8, 0x0, 0x0, 0xc000061a00, 0x0, 0x0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0x3c9 main.main() golang/bindtests/bindtests.go:77 +0x151e exit status 2 FAIL run-bindtests (exit status: 1)
It turns out that a C-language array containing C-language pointers can only be allocated in C:
https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-...
Rewrite the "arg_string_list" and "free_string_list" functions almost entirely in C.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/golang.ml | 46 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-)
diff --git a/generator/golang.ml b/generator/golang.ml index d328abe4ed08..5db478e8a494 100644 --- a/generator/golang.ml +++ b/generator/golang.ml @@ -52,6 +52,39 @@ _go_guestfs_create_flags (unsigned flags) { return guestfs_create_flags (flags); } + +// +// A C-language array of C-language pointers needs to be allocated in C. +// https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-... +// +typedef char *pChar; + +pChar *allocStringArray (size_t nmemb) +{ + pChar *array; + + array = malloc (sizeof (pChar) * (nmemb + 1)); + array[nmemb] = NULL; + return array; +} + +void storeString (pChar *array, size_t idx, pChar string) +{ + array[idx] = string; +} + +void freeStringArray (pChar *array) +{ + pChar *position; + pChar string; + + position = array; + while ((string = *position) != NULL) { + free (string); + position++; + } + free (array); +} */ import \"C\"
@@ -148,12 +181,11 @@ func (g *Guestfs) Close () *GuestfsError { * C strings and golang []string. */ func arg_string_list (xs []string) **C.char { - r := make ([]*C.char, 1 + len (xs)) + r := C.allocStringArray (C.size_t (len (xs))) for i, x := range xs { - r[i] = C.CString (x) + C.storeString (r, C.size_t (i), C.CString (x)); } - r[len (xs)] = nil - return &r[0] + return (**C.char) (r) }
func count_string_list (argv **C.char) int { @@ -167,11 +199,7 @@ func count_string_list (argv **C.char) int { }
func free_string_list (argv **C.char) { - for *argv != nil { - //C.free (*argv) - argv = (**C.char) (unsafe.Pointer (uintptr (unsafe.Pointer (argv)) + - unsafe.Sizeof (*argv))) - } + C.freeStringArray ((*C.pChar) (argv)) }
func return_string_list (argv **C.char) []string {
I had to do something similar to this in libnbd recently. Thanks, ACK series. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
On Mon, Sep 20, 2021 at 07:23:35AM +0200, Laszlo Ersek wrote:
The current "arg_string_list" and "free_string_list" implementations go back to commit b6f01f32606d ("Add Go (language) bindings.", 2013-07-03). There are two problems with them:
- "free_string_list" doesn't actually free anything,
Doh.
- at the *first* such g.Internal_test() call site that passes an Ostringlist member inside the Optargs argument, namely:
g.Internal_test ("abc", string_addr ("def"), []string{}, false, 0, 0, "123", "456", []byte{'a', 'b', 'c', '\000', 'a', 'b', 'c'}, &guestfs.OptargsInternal_test{Ostringlist_is_set: true, Ostringlist: []string{} } )
the "golang/run-bindtests" case crashes:
panic: runtime error: cgo argument has Go pointer to Go pointer
goroutine 1 [running]: libguestfs.org/guestfs.(*Guestfs).Internal_test.func7(0xc000018180, 0xadfb60, 0xadfb80, 0xc000010048, 0x0, 0x0, 0x0, 0xae3e10, 0xae3e30, 0xade3a0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0xa9 libguestfs.org/guestfs.(*Guestfs).Internal_test(0xc000018180, 0x4ee3a5, 0x3, 0xc000061be8, 0xc000061af8, 0x0, 0x0, 0xc000061a00, 0x0, 0x0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0x3c9 main.main() golang/bindtests/bindtests.go:77 +0x151e exit status 2 FAIL run-bindtests (exit status: 1)
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
It turns out that a C-language array containing C-language pointers can only be allocated in C:
https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-...
Rewrite the "arg_string_list" and "free_string_list" functions almost entirely in C.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/golang.ml | 46 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-)
diff --git a/generator/golang.ml b/generator/golang.ml index d328abe4ed08..5db478e8a494 100644 --- a/generator/golang.ml +++ b/generator/golang.ml @@ -52,6 +52,39 @@ _go_guestfs_create_flags (unsigned flags) { return guestfs_create_flags (flags); } + +// +// A C-language array of C-language pointers needs to be allocated in C. +// https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-... +// +typedef char *pChar; + +pChar *allocStringArray (size_t nmemb) +{ + pChar *array; + + array = malloc (sizeof (pChar) * (nmemb + 1)); + array[nmemb] = NULL; + return array; +} + +void storeString (pChar *array, size_t idx, pChar string) +{ + array[idx] = string; +} + +void freeStringArray (pChar *array) +{ + pChar *position; + pChar string; + + position = array; + while ((string = *position) != NULL) { + free (string); + position++; + } + free (array); +} */ import \"C\"
@@ -148,12 +181,11 @@ func (g *Guestfs) Close () *GuestfsError { * C strings and golang []string. */ func arg_string_list (xs []string) **C.char { - r := make ([]*C.char, 1 + len (xs)) + r := C.allocStringArray (C.size_t (len (xs))) for i, x := range xs { - r[i] = C.CString (x) + C.storeString (r, C.size_t (i), C.CString (x)); } - r[len (xs)] = nil - return &r[0] + return (**C.char) (r) }
This could be done purely in Go without helper functions func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) } func array_size(arr **C.char, len int) C.size_t { return C.size_t(unsafe.Sizeof(*arr) * (1 + uintptr(len))) } func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(array_size(len(xs)))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r }
func count_string_list (argv **C.char) int { @@ -167,11 +199,7 @@ func count_string_list (argv **C.char) int { }
func free_string_list (argv **C.char) { - for *argv != nil { - //C.free (*argv) - argv = (**C.char) (unsafe.Pointer (uintptr (unsafe.Pointer (argv)) + - unsafe.Sizeof (*argv))) - } + C.freeStringArray ((*C.pChar) (argv)) }
This can be done entirely in Go func free_string_list (argv **C.char) { for i := 0; ; i++ { str := array_elem(argv, i) if *str == nil { break } C.free(unsafe.Pointer(*str)) } C.free(unsafe.Pointer(argv)) }
func return_string_list (argv **C.char) []string { -- 2.19.1.3.g30247aa5d201
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On Mon, Sep 20, 2021 at 11:37:02AM +0100, Daniel P. Berrangé wrote:
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
For me this started to happen after upgrading to golang-bin-1.17-2.fc36.x86_64 in Rawhide. It also caused this error: https://gitlab.com/nbdkit/libnbd/-/commit/a550beef2a941fe21d4f834775d7357009... Fixed by looking at what libvirt-go was doing ... Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
On Mon, Sep 20, 2021 at 12:03:51PM +0100, Richard W.M. Jones wrote:
On Mon, Sep 20, 2021 at 11:37:02AM +0100, Daniel P. Berrangé wrote:
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
For me this started to happen after upgrading to golang-bin-1.17-2.fc36.x86_64 in Rawhide. It also caused this error:
Hmm, I still cant reproduce the problem that Laszlo is fixing $ cat str.c #include <stdio.h> void foo(char **str) { for (int i = 0; str[i] != NULL; i++) { fprintf(stderr, "%d: %s (0x%p)\n", i, str[i], str[i]); } } $ cat str.go package main /* #cgo LDFLAGS: -L/home/berrange/t/lib -lstr #include <stdlib.h> extern void foo(char **str); */ import "C" import ( "fmt" "unsafe" ) func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) } func arg_string_list1(xs []string) **C.char { r := make([]*C.char, 1+len(xs)) for i, x := range xs { r[i] = C.CString(x) } r[len(xs)] = nil return &r[0] } func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(*r) * (1 + uintptr(len(xs)))))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r } func free_string_list(argv **C.char) { for i := 0; ; i++ { str := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + (unsafe.Sizeof(*argv) * uintptr(i)))) if *str == nil { break } fmt.Printf("%x\n", *str) C.free(unsafe.Pointer(*str)) } } func bar(str []string) { cstr1 := arg_string_list1(str) defer free_string_list(cstr1) C.foo(cstr1) cstr2 := arg_string_list2(str) defer free_string_list(cstr2) C.foo(cstr2) } func main() { bar([]string{"hello", "world"}) } My interpretation is that arg_string_list1 impl here should have raised the error that Laszlo reports, but both impls work fine $ gcc -fPIC -c -o str.o str.c $ gcc -shared -o libstr.so str.o $ go version go version go1.17 linux/amd64 $ go build -o str str.go $ LD_LIBRARY_PATH=/home/berrange/t/lib ./str 0: hello (0x0x1d68970) 1: world (0x0x1d68990) 0: hello (0x0x1d689d0) 1: world (0x0x1d689f0) 1d689d0 1d689f0 1d68970 1d68990 Is my test scearnio there representative of what the failing test case is doing ? Or is perhaps the C function calling back into the Go code ? The reason I'm curious is that the current code for arrays here matches what libvirt-go-module currently uses in some places, so I'm wondering if that needs fixing too. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On Mon, Sep 20, 2021 at 01:33:31PM +0100, Daniel P. Berrangé wrote:
Is my test scearnio there representative of what the failing test case is doing ? Or is perhaps the C function calling back into the Go code ?
The difference could be that we use the following environment variable when running tests: export GODEBUG=cgocheck=2 Also could you try compiling libnbd after reverting https://gitlab.com/nbdkit/libnbd/-/commit/a550beef2a941fe21d4f834775d7357009... and see if you can reproduce the libnbd error? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
On 09/20/21 14:33, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 12:03:51PM +0100, Richard W.M. Jones wrote:
On Mon, Sep 20, 2021 at 11:37:02AM +0100, Daniel P. Berrangé wrote:
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
For me this started to happen after upgrading to golang-bin-1.17-2.fc36.x86_64 in Rawhide. It also caused this error:
Hmm, I still cant reproduce the problem that Laszlo is fixing
$ cat str.c
#include <stdio.h>
void foo(char **str) { for (int i = 0; str[i] != NULL; i++) { fprintf(stderr, "%d: %s (0x%p)\n", i, str[i], str[i]); } }
$ cat str.go package main
/* #cgo LDFLAGS: -L/home/berrange/t/lib -lstr
#include <stdlib.h>
extern void foo(char **str);
*/ import "C"
import ( "fmt" "unsafe" )
func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) }
func arg_string_list1(xs []string) **C.char { r := make([]*C.char, 1+len(xs)) for i, x := range xs { r[i] = C.CString(x) } r[len(xs)] = nil return &r[0] }
func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(*r) * (1 + uintptr(len(xs)))))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r }
func free_string_list(argv **C.char) { for i := 0; ; i++ { str := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + (unsafe.Sizeof(*argv) * uintptr(i)))) if *str == nil { break } fmt.Printf("%x\n", *str) C.free(unsafe.Pointer(*str)) } }
func bar(str []string) { cstr1 := arg_string_list1(str) defer free_string_list(cstr1) C.foo(cstr1) cstr2 := arg_string_list2(str) defer free_string_list(cstr2) C.foo(cstr2) }
func main() { bar([]string{"hello", "world"}) }
My interpretation is that arg_string_list1 impl here should have raised the error that Laszlo reports, but both impls work fine
Can you create a new structure type, make the C function take the structure (or a pointer to the structure), and in the structure, make the field have this type: char * const * str; Because this is the scenario where the libguestfs test suite fails (panics). The libguestfs test suite has a *different* case that does match your example directly, and *that* case works in the libguestfs test suite flawlessly. The panic surfaces only in the "char*const* embedded in struct" case. (I assume "const" makes no difference, but who knows!)
$ gcc -fPIC -c -o str.o str.c $ gcc -shared -o libstr.so str.o
$ go version go version go1.17 linux/amd64 $ go build -o str str.go $ LD_LIBRARY_PATH=/home/berrange/t/lib ./str 0: hello (0x0x1d68970) 1: world (0x0x1d68990) 0: hello (0x0x1d689d0) 1: world (0x0x1d689f0) 1d689d0 1d689f0 1d68970 1d68990
Is my test scearnio there representative of what the failing test case is doing ?
No, your case represents the one that works fine in libguestfs too. From "golang/bindtests/bindtests.go", generated at commit f47e0bb67254: 41 if err := g.Internal_test ("abc", nil, []string{}, false, 0, 0, "123", "456", []byte{'a', 'b', 'c', '\000', 'a', 'b', 'c'}, &guestfs.OptargsInternal_test{Oint64_is_set: true, Oint64: 1, Ostring_is_set: true, Ostring: "string"}); err != nil { The third argument is such a stringlist, and it works OK. But this one panics due to "Ostringlist": 77 if err := g.Internal_test ("abc", string_addr ("def"), []string{}, false, 0, 0, "123", "456", []byte{'a', 'b', 'c', '\000', 'a', 'b', 'c'}, &guestfs.OptargsInternal_test{Ostringlist_is_set: true, Ostringlist: []string{}}); err != nil { The callee (Internal_test()) is in the also-generated source file "golang/src/libguestfs.org/guestfs/guestfs.go". Thanks! Laszlo
Or is perhaps the C function calling back into the Go code ?
The reason I'm curious is that the current code for arrays here matches what libvirt-go-module currently uses in some places, so I'm wondering if that needs fixing too.
Regards, Daniel
On Mon, Sep 20, 2021 at 05:23:30PM +0200, Laszlo Ersek wrote:
On 09/20/21 14:33, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 12:03:51PM +0100, Richard W.M. Jones wrote:
On Mon, Sep 20, 2021 at 11:37:02AM +0100, Daniel P. Berrangé wrote:
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
For me this started to happen after upgrading to golang-bin-1.17-2.fc36.x86_64 in Rawhide. It also caused this error:
Hmm, I still cant reproduce the problem that Laszlo is fixing
$ cat str.c
#include <stdio.h>
void foo(char **str) { for (int i = 0; str[i] != NULL; i++) { fprintf(stderr, "%d: %s (0x%p)\n", i, str[i], str[i]); } }
$ cat str.go package main
/* #cgo LDFLAGS: -L/home/berrange/t/lib -lstr
#include <stdlib.h>
extern void foo(char **str);
*/ import "C"
import ( "fmt" "unsafe" )
func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) }
func arg_string_list1(xs []string) **C.char { r := make([]*C.char, 1+len(xs)) for i, x := range xs { r[i] = C.CString(x) } r[len(xs)] = nil return &r[0] }
func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(*r) * (1 + uintptr(len(xs)))))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r }
func free_string_list(argv **C.char) { for i := 0; ; i++ { str := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + (unsafe.Sizeof(*argv) * uintptr(i)))) if *str == nil { break } fmt.Printf("%x\n", *str) C.free(unsafe.Pointer(*str)) } }
func bar(str []string) { cstr1 := arg_string_list1(str) defer free_string_list(cstr1) C.foo(cstr1) cstr2 := arg_string_list2(str) defer free_string_list(cstr2) C.foo(cstr2) }
func main() { bar([]string{"hello", "world"}) }
My interpretation is that arg_string_list1 impl here should have raised the error that Laszlo reports, but both impls work fine
Can you create a new structure type, make the C function take the structure (or a pointer to the structure), and in the structure, make the field have this type:
char * const * str;
Because this is the scenario where the libguestfs test suite fails (panics). The libguestfs test suite has a *different* case that does match your example directly, and *that* case works in the libguestfs test suite flawlessly. The panic surfaces only in the "char*const* embedded in struct" case. (I assume "const" makes no difference, but who knows!)
Oh, that makes sense, because you have a Go pointer to the storage for the struct, and then the 'const *const *str' field is initialized with a Go pointer returned from the arg_string_list(). You're allowed to pass a Go pointer to C via CGo, but the memory that points to is not allowed to contained further Go pointers. So the struct fields must strictly use a C pointer. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On 09/20/21 17:30, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 05:23:30PM +0200, Laszlo Ersek wrote:
On 09/20/21 14:33, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 12:03:51PM +0100, Richard W.M. Jones wrote:
On Mon, Sep 20, 2021 at 11:37:02AM +0100, Daniel P. Berrangé wrote:
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
For me this started to happen after upgrading to golang-bin-1.17-2.fc36.x86_64 in Rawhide. It also caused this error:
Hmm, I still cant reproduce the problem that Laszlo is fixing
$ cat str.c
#include <stdio.h>
void foo(char **str) { for (int i = 0; str[i] != NULL; i++) { fprintf(stderr, "%d: %s (0x%p)\n", i, str[i], str[i]); } }
$ cat str.go package main
/* #cgo LDFLAGS: -L/home/berrange/t/lib -lstr
#include <stdlib.h>
extern void foo(char **str);
*/ import "C"
import ( "fmt" "unsafe" )
func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) }
func arg_string_list1(xs []string) **C.char { r := make([]*C.char, 1+len(xs)) for i, x := range xs { r[i] = C.CString(x) } r[len(xs)] = nil return &r[0] }
func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(*r) * (1 + uintptr(len(xs)))))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r }
func free_string_list(argv **C.char) { for i := 0; ; i++ { str := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + (unsafe.Sizeof(*argv) * uintptr(i)))) if *str == nil { break } fmt.Printf("%x\n", *str) C.free(unsafe.Pointer(*str)) } }
func bar(str []string) { cstr1 := arg_string_list1(str) defer free_string_list(cstr1) C.foo(cstr1) cstr2 := arg_string_list2(str) defer free_string_list(cstr2) C.foo(cstr2) }
func main() { bar([]string{"hello", "world"}) }
My interpretation is that arg_string_list1 impl here should have raised the error that Laszlo reports, but both impls work fine
Can you create a new structure type, make the C function take the structure (or a pointer to the structure), and in the structure, make the field have this type:
char * const * str;
Because this is the scenario where the libguestfs test suite fails (panics). The libguestfs test suite has a *different* case that does match your example directly, and *that* case works in the libguestfs test suite flawlessly. The panic surfaces only in the "char*const* embedded in struct" case. (I assume "const" makes no difference, but who knows!)
Oh, that makes sense, because you have a Go pointer to the storage for the struct, and then the 'const *const *str' field is initialized with a Go pointer returned from the arg_string_list().
You're allowed to pass a Go pointer to C via CGo, but the memory that points to is not allowed to contained further Go pointers. So the struct fields must strictly use a C pointer.
If I take your last paragraph here and work it into the commit message / comment of this patch, will you accept the code in the patch? I really don't insist on getting *this* particular patch in. What I'd like to achieve is a clean "make check" baseline, so I can run the test suite regularly, as I get into fixing other libguestfs BZs. I don't intend to "maintain" Go bindings; I consider this a one-off fix. I'm uncomfortable contributing any Go code that's not as "thin" as I can possibly manage. (And honestly I think the swaths of "explicit unsafe" just make things unreadable.) So I could do two things: - push patches #1-#3 and drop #4, allowing someone else to fix the issue described in #4 in more idiomatic Go, - post a v2 of #4 with updated comments / commit message. I think a "less idiomatic but technically correct" Go binding is still an improvement over a panicking Go runtime, but again, if someone can fix this more idiomatically, I'll *gladly* defer to them. Rich, what's your take? Thanks! Laszlo
On Tue, Sep 21, 2021 at 11:25:23AM +0200, Laszlo Ersek wrote:
On 09/20/21 17:30, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 05:23:30PM +0200, Laszlo Ersek wrote:
On 09/20/21 14:33, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 12:03:51PM +0100, Richard W.M. Jones wrote:
On Mon, Sep 20, 2021 at 11:37:02AM +0100, Daniel P. Berrangé wrote:
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
For me this started to happen after upgrading to golang-bin-1.17-2.fc36.x86_64 in Rawhide. It also caused this error:
Hmm, I still cant reproduce the problem that Laszlo is fixing
$ cat str.c
#include <stdio.h>
void foo(char **str) { for (int i = 0; str[i] != NULL; i++) { fprintf(stderr, "%d: %s (0x%p)\n", i, str[i], str[i]); } }
$ cat str.go package main
/* #cgo LDFLAGS: -L/home/berrange/t/lib -lstr
#include <stdlib.h>
extern void foo(char **str);
*/ import "C"
import ( "fmt" "unsafe" )
func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) }
func arg_string_list1(xs []string) **C.char { r := make([]*C.char, 1+len(xs)) for i, x := range xs { r[i] = C.CString(x) } r[len(xs)] = nil return &r[0] }
func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(*r) * (1 + uintptr(len(xs)))))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r }
func free_string_list(argv **C.char) { for i := 0; ; i++ { str := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + (unsafe.Sizeof(*argv) * uintptr(i)))) if *str == nil { break } fmt.Printf("%x\n", *str) C.free(unsafe.Pointer(*str)) } }
func bar(str []string) { cstr1 := arg_string_list1(str) defer free_string_list(cstr1) C.foo(cstr1) cstr2 := arg_string_list2(str) defer free_string_list(cstr2) C.foo(cstr2) }
func main() { bar([]string{"hello", "world"}) }
My interpretation is that arg_string_list1 impl here should have raised the error that Laszlo reports, but both impls work fine
Can you create a new structure type, make the C function take the structure (or a pointer to the structure), and in the structure, make the field have this type:
char * const * str;
Because this is the scenario where the libguestfs test suite fails (panics). The libguestfs test suite has a *different* case that does match your example directly, and *that* case works in the libguestfs test suite flawlessly. The panic surfaces only in the "char*const* embedded in struct" case. (I assume "const" makes no difference, but who knows!)
Oh, that makes sense, because you have a Go pointer to the storage for the struct, and then the 'const *const *str' field is initialized with a Go pointer returned from the arg_string_list().
You're allowed to pass a Go pointer to C via CGo, but the memory that points to is not allowed to contained further Go pointers. So the struct fields must strictly use a C pointer.
If I take your last paragraph here and work it into the commit message / comment of this patch, will you accept the code in the patch?
I really don't insist on getting *this* particular patch in. What I'd like to achieve is a clean "make check" baseline, so I can run the test suite regularly, as I get into fixing other libguestfs BZs. I don't intend to "maintain" Go bindings; I consider this a one-off fix. I'm uncomfortable contributing any Go code that's not as "thin" as I can possibly manage. (And honestly I think the swaths of "explicit unsafe" just make things unreadable.)
So I could do two things:
- push patches #1-#3 and drop #4, allowing someone else to fix the issue described in #4 in more idiomatic Go,
- post a v2 of #4 with updated comments / commit message.
I think a "less idiomatic but technically correct" Go binding is still an improvement over a panicking Go runtime, but again, if someone can fix this more idiomatically, I'll *gladly* defer to them.
Rich, what's your take?
If it improves the code / test even though it might not be a full fix, it's worth including IMHO. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
But worth including a link to this discussion in the commit message so we can refer back to it later. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
On 09/20/21 12:37, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 07:23:35AM +0200, Laszlo Ersek wrote:
The current "arg_string_list" and "free_string_list" implementations go back to commit b6f01f32606d ("Add Go (language) bindings.", 2013-07-03). There are two problems with them:
- "free_string_list" doesn't actually free anything,
Doh.
In fact I do not think that was an oversight (i.e. that C.free() was commented out). I didn't try, but I suspect that calling C.free() triggered the same issue when Rich originally worked on the go bindings, so C.free() was postponed. And then forgotten. (This is just my speculation, again.)
- at the *first* such g.Internal_test() call site that passes an Ostringlist member inside the Optargs argument, namely:
g.Internal_test ("abc", string_addr ("def"), []string{}, false, 0, 0, "123", "456", []byte{'a', 'b', 'c', '\000', 'a', 'b', 'c'}, &guestfs.OptargsInternal_test{Ostringlist_is_set: true, Ostringlist: []string{} } )
the "golang/run-bindtests" case crashes:
panic: runtime error: cgo argument has Go pointer to Go pointer
goroutine 1 [running]: libguestfs.org/guestfs.(*Guestfs).Internal_test.func7(0xc000018180, 0xadfb60, 0xadfb80, 0xc000010048, 0x0, 0x0, 0x0, 0xae3e10, 0xae3e30, 0xade3a0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0xa9 libguestfs.org/guestfs.(*Guestfs).Internal_test(0xc000018180, 0x4ee3a5, 0x3, 0xc000061be8, 0xc000061af8, 0x0, 0x0, 0xc000061a00, 0x0, 0x0, ...) golang/src/libguestfs.org/guestfs/guestfs.go:6729 +0x3c9 main.main() golang/bindtests/bindtests.go:77 +0x151e exit status 2 FAIL run-bindtests (exit status: 1)
What distro / go version do you see this on, as I can't reproduce this pointer problem with a standalone demo app ?
Stock Fedora 34: golang-bin-1.16.6-2.fc34.x86_64 It's reproduced reliably by $ make -C golang V=1 check in libguestfs. Interestingly, this kind of "stringlist" is used in two distinct scenarios in "golang/bindtests/bindtests.go". The first one is when such a stringlist is passed to the generated Internal_test() function [golang/src/libguestfs.org/guestfs/guestfs.go] through the "strlist" parameter. There are no problems then. But when a similar object is embedded in the guestfs.OptargsInternal_test argument, that's when things break.
It turns out that a C-language array containing C-language pointers can only be allocated in C:
https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-...
Rewrite the "arg_string_list" and "free_string_list" functions almost entirely in C.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/golang.ml | 46 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-)
diff --git a/generator/golang.ml b/generator/golang.ml index d328abe4ed08..5db478e8a494 100644 --- a/generator/golang.ml +++ b/generator/golang.ml @@ -52,6 +52,39 @@ _go_guestfs_create_flags (unsigned flags) { return guestfs_create_flags (flags); } + +// +// A C-language array of C-language pointers needs to be allocated in C. +// https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-... +// +typedef char *pChar; + +pChar *allocStringArray (size_t nmemb) +{ + pChar *array; + + array = malloc (sizeof (pChar) * (nmemb + 1)); + array[nmemb] = NULL; + return array; +} + +void storeString (pChar *array, size_t idx, pChar string) +{ + array[idx] = string; +} + +void freeStringArray (pChar *array) +{ + pChar *position; + pChar string; + + position = array; + while ((string = *position) != NULL) { + free (string); + position++; + } + free (array); +} */ import \"C\"
@@ -148,12 +181,11 @@ func (g *Guestfs) Close () *GuestfsError { * C strings and golang []string. */ func arg_string_list (xs []string) **C.char { - r := make ([]*C.char, 1 + len (xs)) + r := C.allocStringArray (C.size_t (len (xs))) for i, x := range xs { - r[i] = C.CString (x) + C.storeString (r, C.size_t (i), C.CString (x)); } - r[len (xs)] = nil - return &r[0] + return (**C.char) (r) }
This could be done purely in Go without helper functions
func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) }
func array_size(arr **C.char, len int) C.size_t { return C.size_t(unsafe.Sizeof(*arr) * (1 + uintptr(len))) }
func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(array_size(len(xs)))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r }
I'm a total noob in Go and I find the syntax very difficult to read. In addition to that, I don't have the slightest idea about the Go runtime. (What I do know however is that the #cgo parser is not a full-blown C language parser; for example it does not accept "sizeof variable" without parens, plus see <https://github.com/golang/go/issues/14210> for another example.) The end result is that I wanted to get out of Go-land as quickly and as *simply* as possible (meaning the syntax of calling C code), and then to do the actual business in C. This is the Go binding of a C library, so I feel this approach is justified. I concede that the commit message / comments that state that the array "must" be allocated like this may be a stretch. Note that it's not without precedent, see this other comment in "generator/golang.ml": // cgo can't deal with variable argument functions. Now another comment in <https://github.com/golang/go/issues/14210>, from Ian Lance Taylor, is: That is the way the current implementation works, a consequence of the way that cgo rewrites calls to C functions. We can try to do better for 1.7, but we aren't going to change this for 1.6. So the language runtime is even a moving target, and it's not stable what can be done and what cannot be done in it. I think the code is justified; perhaps the comments could be relaxed: "it's best to allocate arrays like this". Indeed I don't know *why* Go breaks like that, so I cannot capture that reason in the commit message. On the other hand, I don't know any other reasons either why Go does its things the way it does -- and I still wanted to fix this bug. I'm happy to pass on this issue to someone else, or (if you can propose an exact language for the commit message and the code comment) spin a v2 of this patch. Personally I wouldn't like to modify the *code* in the patch.
func count_string_list (argv **C.char) int { @@ -167,11 +199,7 @@ func count_string_list (argv **C.char) int { }
func free_string_list (argv **C.char) { - for *argv != nil { - //C.free (*argv) - argv = (**C.char) (unsafe.Pointer (uintptr (unsafe.Pointer (argv)) + - unsafe.Sizeof (*argv))) - } + C.freeStringArray ((*C.pChar) (argv)) }
This can be done entirely in Go
func free_string_list (argv **C.char) { for i := 0; ; i++ { str := array_elem(argv, i) if *str == nil { break } C.free(unsafe.Pointer(*str)) } C.free(unsafe.Pointer(argv)) }
I guess it "can", but *should* it? In libguestfs? I think (for any language binding at all, not just Go's), the bindings should be as thin as possible in the other languages, and most of the business should be in C. Thanks, Laszlo
func return_string_list (argv **C.char) []string { -- 2.19.1.3.g30247aa5d201
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
Regards, Daniel
On Mon, Sep 20, 2021 at 05:11:38PM +0200, Laszlo Ersek wrote:
On 09/20/21 12:37, Daniel P. Berrangé wrote:
On Mon, Sep 20, 2021 at 07:23:35AM +0200, Laszlo Ersek wrote:
It turns out that a C-language array containing C-language pointers can only be allocated in C:
https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-...
Rewrite the "arg_string_list" and "free_string_list" functions almost entirely in C.
Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- generator/golang.ml | 46 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-)
diff --git a/generator/golang.ml b/generator/golang.ml index d328abe4ed08..5db478e8a494 100644 --- a/generator/golang.ml +++ b/generator/golang.ml @@ -52,6 +52,39 @@ _go_guestfs_create_flags (unsigned flags) { return guestfs_create_flags (flags); } + +// +// A C-language array of C-language pointers needs to be allocated in C. +// https://stackoverflow.com/questions/35924545/golang-cgo-panic-runtime-error-... +// +typedef char *pChar; + +pChar *allocStringArray (size_t nmemb) +{ + pChar *array; + + array = malloc (sizeof (pChar) * (nmemb + 1)); + array[nmemb] = NULL; + return array; +} + +void storeString (pChar *array, size_t idx, pChar string) +{ + array[idx] = string; +} + +void freeStringArray (pChar *array) +{ + pChar *position; + pChar string; + + position = array; + while ((string = *position) != NULL) { + free (string); + position++; + } + free (array); +} */ import \"C\"
@@ -148,12 +181,11 @@ func (g *Guestfs) Close () *GuestfsError { * C strings and golang []string. */ func arg_string_list (xs []string) **C.char { - r := make ([]*C.char, 1 + len (xs)) + r := C.allocStringArray (C.size_t (len (xs))) for i, x := range xs { - r[i] = C.CString (x) + C.storeString (r, C.size_t (i), C.CString (x)); } - r[len (xs)] = nil - return &r[0] + return (**C.char) (r) }
This could be done purely in Go without helper functions
func array_elem(arr **C.char, idx int) **C.char { return (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(arr)) + (unsafe.Sizeof(arr) * uintptr(idx)))) }
func array_size(arr **C.char, len int) C.size_t { return C.size_t(unsafe.Sizeof(*arr) * (1 + uintptr(len))) }
func arg_string_list2(xs []string) **C.char { var r **C.char r = (**C.char)(C.malloc(array_size(len(xs)))) for i, x := range xs { str := array_elem(r, i) *str = C.CString(x) } str := array_elem(r, len(xs)) *str = nil return r }
I'm a total noob in Go and I find the syntax very difficult to read. In addition to that, I don't have the slightest idea about the Go runtime. (What I do know however is that the #cgo parser is not a full-blown C language parser; for example it does not accept "sizeof variable" without parens, plus see <https://github.com/golang/go/issues/14210> for another example.) The end result is that I wanted to get out of Go-land as quickly and as *simply* as possible (meaning the syntax of calling C code), and then to do the actual business in C. This is the Go binding of a C library, so I feel this approach is justified.
From my POV maintaining Go bindings though, I think that the desirable to use Go code to the greatest extent possible. I'd only use C wrapper functions if it was the only solution - eg where you need 2 consequetive C API calls in the same thread for TLS, or where you need to deal with
I can understand that POV if you're most comfortable being C developer. things like variadic functions.
func count_string_list (argv **C.char) int { @@ -167,11 +199,7 @@ func count_string_list (argv **C.char) int { }
func free_string_list (argv **C.char) { - for *argv != nil { - //C.free (*argv) - argv = (**C.char) (unsafe.Pointer (uintptr (unsafe.Pointer (argv)) + - unsafe.Sizeof (*argv))) - } + C.freeStringArray ((*C.pChar) (argv)) }
This can be done entirely in Go
func free_string_list (argv **C.char) { for i := 0; ; i++ { str := array_elem(argv, i) if *str == nil { break } C.free(unsafe.Pointer(*str)) } C.free(unsafe.Pointer(argv)) }
I guess it "can", but *should* it? In libguestfs? I think (for any language binding at all, not just Go's), the bindings should be as thin as possible in the other languages, and most of the business should be in C.
To me the great benefit of Go's native API layer compared to languages like Python/Perl/Ruby is that you can do nearly everything purely in Go code and so almost never need to create C wrapper functions. Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
participants (3)
-
Daniel P. Berrangé -
Laszlo Ersek -
Richard W.M. Jones