qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted?
@ 2016-08-04 18:46 Peter Maydell
  2016-08-04 18:49 ` John Snow
  2016-08-04 20:39 ` Eric Blake
  0 siblings, 2 replies; 6+ messages in thread
From: Peter Maydell @ 2016-08-04 18:46 UTC (permalink / raw)
  To: QEMU Developers; +Cc: John Snow

I've upgraded to a more recent version of clang, which now produces
undefined-behaviour warnings for passing NULL pointers to some library
functions. One of the things it has shown up is that some of the
qtest tests ask for "memset" with size zero. In our current implementation
this results in qtest.c calling g_malloc(0), which returns NULL, and
then calling memset(NULL, chr, 0), which is UB.

So should we:
(1) declare the qtest protocol commands 'memset', 'read', 'write'
etc which operate on a lump of guest memory of specified size to
support size == 0 as meaning "do nothing"
(2) declare that size == 0 is not valid and make it return a failure
code back down the qtest pipe (and fix the offending tests)

?

The offending tests are i386/ahci/flush/simple and i386/ahci/max
(because ahci_io() calls qmemset() with a zero size.)

thanks
-- PMM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted?
  2016-08-04 18:46 [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted? Peter Maydell
@ 2016-08-04 18:49 ` John Snow
  2016-08-04 20:11   ` Peter Maydell
  2016-08-04 20:39 ` Eric Blake
  1 sibling, 1 reply; 6+ messages in thread
From: John Snow @ 2016-08-04 18:49 UTC (permalink / raw)
  To: Peter Maydell, QEMU Developers



On 08/04/2016 02:46 PM, Peter Maydell wrote:
> I've upgraded to a more recent version of clang, which now produces
> undefined-behaviour warnings for passing NULL pointers to some library
> functions. One of the things it has shown up is that some of the
> qtest tests ask for "memset" with size zero. In our current implementation
> this results in qtest.c calling g_malloc(0), which returns NULL, and
> then calling memset(NULL, chr, 0), which is UB.
>
> So should we:
> (1) declare the qtest protocol commands 'memset', 'read', 'write'
> etc which operate on a lump of guest memory of specified size to
> support size == 0 as meaning "do nothing"

This would be easy to do.

> (2) declare that size == 0 is not valid and make it return a failure
> code back down the qtest pipe (and fix the offending tests)
>

This is probably the nicer thing to do -- if memset of length 0 is 
undefined, probably qmemset and friends should also be undefined by 
extension.

I reserve the right to change my mind depending on how gnarly it is to 
untangle.

I assume you're hoping for 2.7.

> ?
>
> The offending tests are i386/ahci/flush/simple and i386/ahci/max
> (because ahci_io() calls qmemset() with a zero size.)
>
> thanks
> -- PMM
>

--js

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted?
  2016-08-04 18:49 ` John Snow
@ 2016-08-04 20:11   ` Peter Maydell
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Maydell @ 2016-08-04 20:11 UTC (permalink / raw)
  To: John Snow; +Cc: QEMU Developers

On 4 August 2016 at 19:49, John Snow <jsnow@redhat.com> wrote:
>
>
> On 08/04/2016 02:46 PM, Peter Maydell wrote:
>> (2) declare that size == 0 is not valid and make it return a failure
>> code back down the qtest pipe (and fix the offending tests)
>>
>
> This is probably the nicer thing to do -- if memset of length 0 is
> undefined, probably qmemset and friends should also be undefined by
> extension.

That was my thought.

> I reserve the right to change my mind depending on how gnarly it is to
> untangle.
>
> I assume you're hoping for 2.7.

I dunno. If the patch turns out easy we can put it in 2.7.
But it's only test code and it's been working fine for
a long time til we noticed it, so I don't think it matters
if we let it slide til 2.8.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted?
  2016-08-04 18:46 [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted? Peter Maydell
  2016-08-04 18:49 ` John Snow
@ 2016-08-04 20:39 ` Eric Blake
  2016-08-05  6:46   ` Markus Armbruster
  1 sibling, 1 reply; 6+ messages in thread
From: Eric Blake @ 2016-08-04 20:39 UTC (permalink / raw)
  To: Peter Maydell, QEMU Developers; +Cc: John Snow

[-- Attachment #1: Type: text/plain, Size: 1571 bytes --]

On 08/04/2016 12:46 PM, Peter Maydell wrote:
> I've upgraded to a more recent version of clang, which now produces
> undefined-behaviour warnings for passing NULL pointers to some library
> functions. One of the things it has shown up is that some of the
> qtest tests ask for "memset" with size zero. In our current implementation
> this results in qtest.c calling g_malloc(0), which returns NULL, and

I never understood why glib made that choice on g_malloc(0). I would
much prefer it to ALWAYS return something, just as glibc malloc(0) does.

> then calling memset(NULL, chr, 0), which is UB.

Indeed, although I really wish POSIX could be loosened to say that the
source pointer is untouched if the length is 0 (I've debated about
filing a POSIX bug report to that effect, but have not done so yet), so
that the UB only happens when passing NULL with a non-zero size.

> 
> So should we:
> (1) declare the qtest protocol commands 'memset', 'read', 'write'
> etc which operate on a lump of guest memory of specified size to
> support size == 0 as meaning "do nothing"

My preference - even if we have to special case things to avoid UB at
the lower level, presenting well-defined behavior at the upper level is
easier to think about.

> (2) declare that size == 0 is not valid and make it return a failure
> code back down the qtest pipe (and fix the offending tests)

Doable, but not as fun to audit, and not my preference.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted?
  2016-08-04 20:39 ` Eric Blake
@ 2016-08-05  6:46   ` Markus Armbruster
  2016-08-05  9:47     ` Peter Maydell
  0 siblings, 1 reply; 6+ messages in thread
From: Markus Armbruster @ 2016-08-05  6:46 UTC (permalink / raw)
  To: Eric Blake; +Cc: Peter Maydell, QEMU Developers, John Snow

Eric Blake <eblake@redhat.com> writes:

> On 08/04/2016 12:46 PM, Peter Maydell wrote:
>> I've upgraded to a more recent version of clang, which now produces
>> undefined-behaviour warnings for passing NULL pointers to some library
>> functions. One of the things it has shown up is that some of the
>> qtest tests ask for "memset" with size zero. In our current implementation
>> this results in qtest.c calling g_malloc(0), which returns NULL, and
>
> I never understood why glib made that choice on g_malloc(0). I would
> much prefer it to ALWAYS return something, just as glibc malloc(0) does.

I guess it made the choice because unlike malloc(0), it can't cause
confusion.  malloc() returning null can mean either error or nothing.
The caller should pick nothing when it passed zero.  g_malloc()
returning null can only mean nothing.

>> then calling memset(NULL, chr, 0), which is UB.
>
> Indeed, although I really wish POSIX could be loosened to say that the
> source pointer is untouched if the length is 0 (I've debated about
> filing a POSIX bug report to that effect, but have not done so yet), so
> that the UB only happens when passing NULL with a non-zero size.

For me, this is one of those pointless UBs, whose only effect on the
real world is wasting programmer time.  Seriously, what else could a
sane memset(NULL, 0, 0) do?  What could anyone gain from an insane
memset() other than the glee of telling people "your program is wrong,
and you're <insert derogatory word>!".

The mission of this standard is to codify existing practice.  Show me a
real-world implementation of memset() that does something other than
nothing when passed a zero size, and whose maintainers don't consider
that a bug in need of fixing.

>> So should we:
>> (1) declare the qtest protocol commands 'memset', 'read', 'write'
>> etc which operate on a lump of guest memory of specified size to
>> support size == 0 as meaning "do nothing"
>
> My preference - even if we have to special case things to avoid UB at
> the lower level, presenting well-defined behavior at the upper level is
> easier to think about.
>
>> (2) declare that size == 0 is not valid and make it return a failure
>> code back down the qtest pipe (and fix the offending tests)
>
> Doable, but not as fun to audit, and not my preference.

Concur.  We shouldn't slavishly replicate an underlying interface's
complexities when we can just as well provide a more regular interface
instead.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted?
  2016-08-05  6:46   ` Markus Armbruster
@ 2016-08-05  9:47     ` Peter Maydell
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Maydell @ 2016-08-05  9:47 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: Eric Blake, QEMU Developers, John Snow

On 5 August 2016 at 07:46, Markus Armbruster <armbru@redhat.com> wrote:
> For me, this is one of those pointless UBs, whose only effect on the
> real world is wasting programmer time.  Seriously, what else could a
> sane memset(NULL, 0, 0) do?

Since the function call is annotated in the system headers
to indicate that the first argument isn't NULL, it gives
the compiler more information which it can then use to
delete clearly-never-reachable "if (foo == NULL) { ... }"
codepaths for you :-)

thanks
-- PMM

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-08-05  9:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-04 18:46 [Qemu-devel] qtest protocol: should memset/read/write etc of a size of 0 bytes be permitted? Peter Maydell
2016-08-04 18:49 ` John Snow
2016-08-04 20:11   ` Peter Maydell
2016-08-04 20:39 ` Eric Blake
2016-08-05  6:46   ` Markus Armbruster
2016-08-05  9:47     ` Peter Maydell

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).