[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] misra: add ASSERT_UNREACHABLE() in default clauses


  • To: Julien Grall <julien@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • From: Dmytro Prokopchuk1 <dmytro_prokopchuk1@xxxxxxxx>
  • Date: Wed, 13 Aug 2025 09:44:07 +0000
  • Accept-language: en-US, uk-UA, ru-RU
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Nlm47EtQuFAD7qCZ5Xyeyfx5Ro4LCAj2vS7viQWl120=; b=Qfyihqb6dFukUHuElvIxIeXSQiZvfeMaJnbSlutSFWnKfUutKgIsdzNWgHC/7MKF8n9CDguxwknn+ckbbNETI7Ar4sEV3D5hY4ELxczpGjPmiOL3J3QtDK35sucVXzRe3l+UkB316ysyaIU1l0J5ssY2Q96t9Xlo4xHDaMG81BkVSk6c7Zj6KQ2YcBRAi4HX0q7eHURf2m70SicsSXZWapWjrd2Eo1LHTO+M8wGp9L1SUfA5XrIUiqe0hntP9qrMbRS0gVUcyfPL+1WyZd0a5uGhkYoINToPvc70a8sph0xEo+U1wLJtm2AvkiU8vGOBpHSROBPRAyjf8j2IKM7MzQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cgXK8dIcT30lUMEQTA5RWNBPy8EOqOj+ErWGxPadqxdP78gQ3dCSt4pRUXXCgR+UY153HU9TwSSoUb2M6bvgUYfJfTU2RCLVbv2GhZm0mAbGoXP7cAxNnsDEETHunMSO6vsnj+dF/Qrk6e1DvGaOkQ352wA4f5ewULgVx/Bygdv2/Z0gbwq18WaNn+dPn+o9p1k7pzMYClJZy1KRxvdjORUscRN0yp3vTyTqALvUejEpdgKJJBFCbu2EAikqR5jlDq5urVrxyjicph7P+e5YgmoA5k2Sy3KJeEledWkuL50YKgQPRf0KHrfAaW4S2eEfpa+25H//1P6ieN0XooHphA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=epam.com;
  • Cc: Stefano Stabellini <sstabellini@xxxxxxxxxx>, Bertrand Marquis <bertrand.marquis@xxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Wed, 13 Aug 2025 09:44:21 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHcCv7M4bE5kwDncUeSEAS4rjYqyLRd9fOAgACqo4CAAQGdgIAAtW4A
  • Thread-topic: [PATCH] misra: add ASSERT_UNREACHABLE() in default clauses


On 8/13/25 01:54, Julien Grall wrote:
> Hi Jan,
> 
> On 12/08/2025 08:32, Jan Beulich wrote:
>> On 11.08.2025 23:21, Julien Grall wrote:
>>> On 11/08/2025 21:30, Dmytro Prokopchuk1 wrote:
>>>> --- a/xen/common/grant_table.c
>>>> +++ b/xen/common/grant_table.c
>>>> @@ -330,9 +330,12 @@ shared_entry_header(struct grant_table *t, 
>>>> grant_ref_t ref)
>>>>            /* Returned values should be independent of speculative 
>>>> execution */
>>>>            block_speculation();
>>>>            return &shared_entry_v2(t, ref).hdr;
>>>> +
>>>> +    default:
>>>> +        ASSERT_UNREACHABLE();
>>>> +        break;
>>>>        }
>>>> -    ASSERT_UNREACHABLE();
>>>   >       block_speculation();>
>>>>        return NULL;
>>>
>>> I know you are trying to apply the MISRA rule. But this is odd that you
>>> move the ASSERT_UNREACHABLE() but then code after is still only
>>> reachable from the default. In fact, this is introducing a risk if
>>> someone decides to add a new case but then forgot to return a value.
>>>
>>> By moving the two other lines, the compiler should be able to throw an
>>> error if you forgot a return.
>>
>> I think we did discuss this pattern in the past. While moving 
>> everything up
>> to the "return" into the default: handling will please Eclair / Misra, 
>> we'll
>> then end up with no return statement at the end of a non-void function.
>> Beyond being good practice (imo) to have such a "main" return statement,
>> that's actually another rule, just one we apparently didn't accept 
>> (15.5).
> 
> Reading 15.5, this seems to be about having a single return in the 
> function. Unless I misunderstood something, this is different from what 
> you suggest.
> 
> Anyway, my main problem with this change is that ASSERT_UNREACHABLE() is 
> moved. I could possibly settle with:
> 
> default:
>    break;
> }
> 
> ASSERT_UNREACHABLE();
> ...
> 
> But at least to me, this pattern is more difficult to read because I 
> have to look through the switch to understand the patch is only meant ot 
> be used by the "default" case.
> 
> Cheers,
> 

Hi all!

Let's summarize the discussion.

1. There are two cases, where  `switch' statement has a controlling 
value that is completely covered by its labeled statements.
Here:
     switch ( opB & 0x3 )
And here:
     switch ( pte.walk.dt )

Originally it violates rule 16.4 "`switch' statement has no `default' 
labels".

Well, adding empty
     default: break;
is not a solution, because it starts to violate rule 2.1 "`break' 
statement is unreachable".

Adding
     default: ASSERT_UNREACHABLE(); break;
looks fine from Eclair point of view, but actually `default' case is 
still unreachable.

I think the easiest way is to insert SAF-xx marker instead of the 
`default' case.

Changing these to enums - not fine as for me.

Maybe there is a way to configure Eclair to ignore the rule 16.4 in such 
cases.
Maybe Nicola can suggest something...


2. Regarding moving ASSERT_UNREACHABLE(); inside `default' case.
Actually switch check Granttable version.
     switch ( gt->gt_version )

And it must be '1' or '2'. Other values are wrong.
I think `default' case should be with assert:

      default:
          ASSERT(false);
          return;

This can catch wrong 'gt_version' values.

Dmytro.

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.