[PATCH nbdkit] pattern: Accurately update b & offset in loop
It's better to update the 'b' pointer and the 'offset' using the calculated increment 'n', rather than using '8-o' which might be larger than the remainder of the buffer. Previously we could create a dangling pointer beyond the end of the output buffer, and this should not matter, but it's best not to do this if we can easily avoid it. Also improve the test by replacing qemu-io with nbdsh. It is able to make non-aligned requests so we can now test those. --- plugins/pattern/pattern.c | 4 +- tests/test-pattern.sh | 98 ++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/plugins/pattern/pattern.c b/plugins/pattern/pattern.c index 3d2bb6040..3db1a21eb 100644 --- a/plugins/pattern/pattern.c +++ b/plugins/pattern/pattern.c @@ -119,8 +119,8 @@ pattern_pread (void *handle, void *buf, uint32_t count, uint64_t offset, o = offset & 7; n = MIN (count, 8-o); memcpy (b, (char *)&d + o, n); - b += 8-o; - offset += 8-o; + b += n; + offset += n; count -= n; } diff --git a/tests/test-pattern.sh b/tests/test-pattern.sh index 1b1bd56fb..6025aea78 100755 --- a/tests/test-pattern.sh +++ b/tests/test-pattern.sh @@ -31,61 +31,51 @@ # SUCH DAMAGE. # Test the pattern plugin. -# -# Note we don't have any client which can issue misaligned NBD -# requests. qemu-io will issue 512-byte aligned requests no matter -# what read parameters we give it. Hence these tests are rather -# limited. (XXX) source ./functions.sh set -e -requires qemu-io --version - -sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX) -files="pattern.out pattern.pid $sock" -rm -f $files -cleanup_fn rm -f $files - -# Run nbdkit with pattern plugin. -start_nbdkit -P pattern.pid -U $sock pattern 1G - -qemu-io -r -f raw "nbd+unix://?socket=$sock" \ - -c 'r -v 0 512' | grep -E '^[[:xdigit:]]+:' > pattern.out -if [ "$(cat pattern.out)" != "00000000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 ................ -00000010: 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 18 ................ -00000020: 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00 28 ................ -00000030: 00 00 00 00 00 00 00 30 00 00 00 00 00 00 00 38 .......0.......8 -00000040: 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 48 ...............H -00000050: 00 00 00 00 00 00 00 50 00 00 00 00 00 00 00 58 .......P.......X -00000060: 00 00 00 00 00 00 00 60 00 00 00 00 00 00 00 68 ...............h -00000070: 00 00 00 00 00 00 00 70 00 00 00 00 00 00 00 78 .......p.......x -00000080: 00 00 00 00 00 00 00 80 00 00 00 00 00 00 00 88 ................ -00000090: 00 00 00 00 00 00 00 90 00 00 00 00 00 00 00 98 ................ -000000a0: 00 00 00 00 00 00 00 a0 00 00 00 00 00 00 00 a8 ................ -000000b0: 00 00 00 00 00 00 00 b0 00 00 00 00 00 00 00 b8 ................ -000000c0: 00 00 00 00 00 00 00 c0 00 00 00 00 00 00 00 c8 ................ -000000d0: 00 00 00 00 00 00 00 d0 00 00 00 00 00 00 00 d8 ................ -000000e0: 00 00 00 00 00 00 00 e0 00 00 00 00 00 00 00 e8 ................ -000000f0: 00 00 00 00 00 00 00 f0 00 00 00 00 00 00 00 f8 ................ -00000100: 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01 08 ................ -00000110: 00 00 00 00 00 00 01 10 00 00 00 00 00 00 01 18 ................ -00000120: 00 00 00 00 00 00 01 20 00 00 00 00 00 00 01 28 ................ -00000130: 00 00 00 00 00 00 01 30 00 00 00 00 00 00 01 38 .......0.......8 -00000140: 00 00 00 00 00 00 01 40 00 00 00 00 00 00 01 48 ...............H -00000150: 00 00 00 00 00 00 01 50 00 00 00 00 00 00 01 58 .......P.......X -00000160: 00 00 00 00 00 00 01 60 00 00 00 00 00 00 01 68 ...............h -00000170: 00 00 00 00 00 00 01 70 00 00 00 00 00 00 01 78 .......p.......x -00000180: 00 00 00 00 00 00 01 80 00 00 00 00 00 00 01 88 ................ -00000190: 00 00 00 00 00 00 01 90 00 00 00 00 00 00 01 98 ................ -000001a0: 00 00 00 00 00 00 01 a0 00 00 00 00 00 00 01 a8 ................ -000001b0: 00 00 00 00 00 00 01 b0 00 00 00 00 00 00 01 b8 ................ -000001c0: 00 00 00 00 00 00 01 c0 00 00 00 00 00 00 01 c8 ................ -000001d0: 00 00 00 00 00 00 01 d0 00 00 00 00 00 00 01 d8 ................ -000001e0: 00 00 00 00 00 00 01 e0 00 00 00 00 00 00 01 e8 ................ -000001f0: 00 00 00 00 00 00 01 f0 00 00 00 00 00 00 01 f8 ................" ] -then - echo "$0: unexpected pattern:" - cat pattern.out - exit 1 -fi +requires_run +requires_nbdsh_uri + +nbdkit pattern 2M --run 'nbdsh -u "$uri" -c -' <<EOF + +# Read from an aligned offset at the beginning of the disk. +expected = bytearray() +for i in range(0,64,8): + expected = expected + i.to_bytes(8, "big") +print("expected = %r" % expected) +actual = h.pread(64, 0) +print("actual = %r" % actual) +assert actual == expected + +# Read from an unaligned offset. +actual = bytearray(4) + h.pread(60, 4) +print("actual = %r" % actual) +assert actual == expected + +# Read to an unaligned offset. +actual = h.pread(60, 0) +print("actual = %r" % actual) +assert actual == expected[:60] + +# Read from an aligned offset further in the disk. +expected = bytearray() +for i in range(1000000,1000064,8): + expected = expected + i.to_bytes(8, "big") +print("expected = %r" % expected) +actual = h.pread(64, 1000000) +print("actual = %r" % actual) +assert actual == expected + +# Read from an unaligned offset. +actual = bytearray(3) + h.pread(61, 1000003) +print("actual = %r" % actual) +assert actual == expected + +# Read to an unaligned offset. +actual = h.pread(60, 1000000) +print("actual = %r" % actual) +assert actual == expected[:60] + +'EOF' -- 2.42.0
On Fri, Nov 03, 2023 at 06:53:34PM +0000, Richard W.M. Jones wrote:
It's better to update the 'b' pointer and the 'offset' using the calculated increment 'n', rather than using '8-o' which might be larger than the remainder of the buffer. Previously we could create a dangling pointer beyond the end of the output buffer, and this should not matter, but it's best not to do this if we can easily avoid it.
Also improve the test by replacing qemu-io with nbdsh. It is able to make non-aligned requests so we can now test those. --- plugins/pattern/pattern.c | 4 +- tests/test-pattern.sh | 98 ++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 56 deletions(-)
+++ b/tests/test-pattern.sh +requires_run +requires_nbdsh_uri + +nbdkit pattern 2M --run 'nbdsh -u "$uri" -c -' <<EOF
Should we use 'pattern 5G' so that we can test reads that cross the boundary from 0x00000000ffffffff to 0x0000000100000000, to ensure we don't have any accidental sign problems?
+ +# Read from an aligned offset at the beginning of the disk. +expected = bytearray() +for i in range(0,64,8): + expected = expected + i.to_bytes(8, "big") +print("expected = %r" % expected) +actual = h.pread(64, 0) +print("actual = %r" % actual) +assert actual == expected + +# Read from an unaligned offset. +actual = bytearray(4) + h.pread(60, 4) +print("actual = %r" % actual) +assert actual == expected
Couldn't we do: actual = h.pread(60, 4) print("actual = %r" % actual) assert actual == expected[4:] instead of having to append to a temporary bytearray(4)?
+ +# Read to an unaligned offset. +actual = h.pread(60, 0) +print("actual = %r" % actual) +assert actual == expected[:60] + +# Read from an aligned offset further in the disk. +expected = bytearray() +for i in range(1000000,1000064,8): + expected = expected + i.to_bytes(8, "big")
This offset is still less than 4G, but definitely gets into a more interesting part of the pattern output.
+print("expected = %r" % expected) +actual = h.pread(64, 1000000) +print("actual = %r" % actual) +assert actual == expected + +# Read from an unaligned offset. +actual = bytearray(3) + h.pread(61, 1000003) +print("actual = %r" % actual) +assert actual == expected + +# Read to an unaligned offset. +actual = h.pread(60, 1000000) +print("actual = %r" % actual) +assert actual == expected[:60]
Whether or not you add even more corner case coverage (such as by reading around the 4G boundarh), this is definitely improved. Reviewed-by: Eric Blake <eblake@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
On Fri, Nov 03, 2023 at 02:04:51PM -0500, Eric Blake wrote:
On Fri, Nov 03, 2023 at 06:53:34PM +0000, Richard W.M. Jones wrote:
It's better to update the 'b' pointer and the 'offset' using the calculated increment 'n', rather than using '8-o' which might be larger than the remainder of the buffer. Previously we could create a dangling pointer beyond the end of the output buffer, and this should not matter, but it's best not to do this if we can easily avoid it.
Also improve the test by replacing qemu-io with nbdsh. It is able to make non-aligned requests so we can now test those. --- plugins/pattern/pattern.c | 4 +- tests/test-pattern.sh | 98 ++++++++++++++++++--------------------- 2 files changed, 46 insertions(+), 56 deletions(-)
+++ b/tests/test-pattern.sh +requires_run +requires_nbdsh_uri + +nbdkit pattern 2M --run 'nbdsh -u "$uri" -c -' <<EOF
Should we use 'pattern 5G' so that we can test reads that cross the boundary from 0x00000000ffffffff to 0x0000000100000000, to ensure we don't have any accidental sign problems?
+ +# Read from an aligned offset at the beginning of the disk. +expected = bytearray() +for i in range(0,64,8): + expected = expected + i.to_bytes(8, "big") +print("expected = %r" % expected) +actual = h.pread(64, 0) +print("actual = %r" % actual) +assert actual == expected + +# Read from an unaligned offset. +actual = bytearray(4) + h.pread(60, 4) +print("actual = %r" % actual) +assert actual == expected
Couldn't we do:
actual = h.pread(60, 4) print("actual = %r" % actual) assert actual == expected[4:]
instead of having to append to a temporary bytearray(4)?
+ +# Read to an unaligned offset. +actual = h.pread(60, 0) +print("actual = %r" % actual) +assert actual == expected[:60] + +# Read from an aligned offset further in the disk. +expected = bytearray() +for i in range(1000000,1000064,8): + expected = expected + i.to_bytes(8, "big")
This offset is still less than 4G, but definitely gets into a more interesting part of the pattern output.
+print("expected = %r" % expected) +actual = h.pread(64, 1000000) +print("actual = %r" % actual) +assert actual == expected + +# Read from an unaligned offset. +actual = bytearray(3) + h.pread(61, 1000003) +print("actual = %r" % actual) +assert actual == expected + +# Read to an unaligned offset. +actual = h.pread(60, 1000000) +print("actual = %r" % actual) +assert actual == expected[:60]
Whether or not you add even more corner case coverage (such as by reading around the 4G boundarh), this is definitely improved.
Let me spin a better version of this ... Rich.
Reviewed-by: Eric Blake <eblake@redhat.com>
-- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
participants (2)
-
Eric Blake -
Richard W.M. Jones