qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: "Juan Quintela" <quintela@redhat.com>,
	qemu-devel@nongnu.org,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Michael Roth" <michael.roth@amd.com>,
	"John Snow" <jsnow@redhat.com>,
	"Victor Toso" <victortoso@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: Re: [PULL 03/15] multifd: Make no compression operations into its own structure
Date: Tue, 19 Jul 2022 16:52:38 +0200	[thread overview]
Message-ID: <87tu7dupc9.fsf@pond.sub.org> (raw)
In-Reply-To: <CAFEAcA9SqOgVWQpR5Z=_wLbrxxGOCqtKn2_0owPmtu4nb96XCQ@mail.gmail.com> (Peter Maydell's message of "Tue, 12 Apr 2022 20:04:11 +0100")

This fell through the cracks.  My apologies.

Peter Maydell <peter.maydell@linaro.org> writes:

> On Fri, 28 Feb 2020 at 09:26, Juan Quintela <quintela@redhat.com> wrote:
>>
>> It will be used later.
>>
>> Signed-off-by: Juan Quintela <quintela@redhat.com>
>> Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
>>
>
> Hi; Coverity thinks there might be a buffer overrun here.
> It's probably wrong,

It is; details below.

>                      but it's not completely obvious why
> it can't happen, so an assert somewhere would help...
> (This is CID 1487239.)
>
>> +MultiFDCompression migrate_multifd_compression(void)
>> +{
>> +    MigrationState *s;
>> +
>> +    s = migrate_get_current();
>> +
>> +    return s->parameters.multifd_compression;
>
> This function returns an enum of type MultiFDCompression,
> whose (autogenerated from QAPI) definition is:
>
> typedef enum MultiFDCompression {
>     MULTIFD_COMPRESSION_NONE,
>     MULTIFD_COMPRESSION_ZLIB,
> #if defined(CONFIG_ZSTD)
>     MULTIFD_COMPRESSION_ZSTD,
> #endif /* defined(CONFIG_ZSTD) */
>     MULTIFD_COMPRESSION__MAX,
> } MultiFDCompression;
>

Generated from

    { 'enum': 'MultiFDCompression',
      'data': [ 'none', 'zlib',
                { 'name': 'zstd', 'if': 'CONFIG_ZSTD' } ] }

>> @@ -604,6 +745,7 @@ int multifd_save_setup(Error **errp)
>>      multifd_send_state->pages = multifd_pages_init(page_count);
>>      qemu_sem_init(&multifd_send_state->channels_ready, 0);
>>      atomic_set(&multifd_send_state->exiting, 0);
>> +    multifd_send_state->ops = multifd_ops[migrate_multifd_compression()];
>
> Here we take the result of the function and use it as an
> array index into multifd_ops, whose definition is
>  static MultiFDMethods *multifd_ops[MULTIFD_COMPRESSION__MAX] = { ... }
>
> Coverity doesn't see any reason why the return value from
> migrate_multifd_compression() can't be MULTIFD_COMPRESSION__MAX,
> so it complains that if it is then we are going to index off the
> end of the array.

Yes.

migrate_multifd_compression() returns
current_migration->parameters.multifd_compression.

.multifd_compression is zero-initialized to MULTIFD_COMPRESSION_NONE,
and modified only by qmp_migrate_set_parameters().

qmp_migrate_set_parameters() can be called on behalf of QMP command
migrate-set-parameters, and on behalf of HMP command
migrate_set_parameter.  In either case, the value is the result of
parsing a string with qapi_enum_parse(), via visit_type_enum() and
visit_type_MultiFDCompression() with an input visitor (qobject for QMP,
string for HMP).  Never assigns the enum's __MAX.

> An assert in migrate_multifd_compression() that the value being
> returned is within the expected range would probably placate it.

Yes.

> Alternatively, if the qapi type codegen didn't put the __MAX
> value as a possible value of the enum type then Coverity
> and probably also the compiler wouldn't consider it to be
> a possible value of this kind of variable. But that might
> have other awkward side-effects.

Yes.  Coding the __MAX as a member of the enum is easy to write and easy
to understand, but gets in the way in places.

We could do something like

    typedef enum MultiFDCompression {
        MULTIFD_COMPRESSION_NONE,
        MULTIFD_COMPRESSION_ZLIB,
    #if defined(CONFIG_ZSTD)
        MULTIFD_COMPRESSION_ZSTD,
    #endif /* defined(CONFIG_ZSTD) */
    } MultiFDCompression;

    #define MULTIFD_COMPRESSION__MAX ???

where ??? is 3 if defined(CONFIG_ZSTD), else 2.  The more conditionals,
the more awkward this gets.

The alternative is holes in the enum, like this:

    typedef enum MultiFDCompression {
        MULTIFD_COMPRESSION_NONE = 0,
        MULTIFD_COMPRESSION_ZLIB = 1,
    #if defined(CONFIG_ZSTD)
        MULTIFD_COMPRESSION_ZSTD = 2,
    #endif /* defined(CONFIG_ZSTD) */
    } MultiFDCompression;

    #define MULTIFD_COMPRESSION__MAX 3

Also puts holes into the lookup tables.

We'd need to review code to make sure we're not breaking "no holes"
assumptions.

Changing the __MAX from enum member to macro could conceivably break
something, too.  

The quick fix is an assertion.



  parent reply	other threads:[~2022-07-19 14:53 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-28  9:24 [PULL 00/15] Pull migration patches Juan Quintela
2020-02-28  9:24 ` [PULL 01/15] multifd: Add multifd-compression parameter Juan Quintela
2020-02-28  9:24 ` [PULL 02/15] migration: Add support for modules Juan Quintela
2020-02-28  9:24 ` [PULL 03/15] multifd: Make no compression operations into its own structure Juan Quintela
2022-04-12 19:04   ` Peter Maydell
2022-05-13 17:56     ` Peter Maydell
2022-07-19 14:06       ` Peter Maydell
2022-07-19 14:52     ` Markus Armbruster [this message]
2020-02-28  9:24 ` [PULL 04/15] multifd: Add multifd-zlib-level parameter Juan Quintela
2020-02-28  9:24 ` [PULL 05/15] multifd: Add zlib compression multifd support Juan Quintela
2020-02-28  9:24 ` [PULL 06/15] configure: Enable test and libs for zstd Juan Quintela
2020-02-29 20:06   ` Richard Henderson
2020-03-02  8:00     ` Juan Quintela
2020-03-02  8:32       ` Alex Bennée
2020-03-17 17:09   ` Peter Maydell
2020-03-17 17:40     ` Juan Quintela
2020-02-28  9:24 ` [PULL 07/15] multifd: Add multifd-zstd-level parameter Juan Quintela
2020-02-28  9:24 ` [PULL 08/15] multifd: Add zstd compression multifd support Juan Quintela
2020-02-28  9:24 ` [PULL 09/15] migration/vmstate: Remove redundant statement in vmstate_save_state_v() Juan Quintela
2020-02-28  9:24 ` [PULL 10/15] test-vmstate: Fix memleaks in test_load_qlist Juan Quintela
2020-02-28  9:24 ` [PULL 11/15] migration/savevm: release gslist after dump_vmstate_json Juan Quintela
2020-02-28  9:24 ` [PULL 12/15] migration/block: rename BLOCK_SIZE macro Juan Quintela
2022-05-12 16:22   ` Peter Maydell
2020-02-28  9:24 ` [PULL 13/15] migration: fix COLO broken caused by a previous commit Juan Quintela
2020-02-28  9:24 ` [PULL 14/15] migration/colo: wrap incoming checkpoint process into new helper Juan Quintela
2020-02-28  9:24 ` [PULL 15/15] savevm: Don't call colo_init_ram_cache twice Juan Quintela
2020-02-28 16:01 ` [PULL 00/15] Pull migration patches Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tu7dupc9.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=michael.roth@amd.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=victortoso@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).