[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros
- To: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
- From: Dmytro Prokopchuk <dmytro_prokopchuk1@xxxxxxxx>
- Date: Fri, 4 Dec 2026 13:15:35 +0200
- 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=IZn8hOy5num3kpnoe5Ai+3ut91Bdf74JG6KkrWvPYpk=; b=T4HFUonsTFhgMYzQN8ln8YwWeMWFp4721reAkLUzT8X354fB6WrQzxkUMWXJz3Du7JzTKbz+wSMHt7LHoNhHpdAsnrJpj0J2VYoN+ihN5eyZEooxMqwNwHepI6UVnDvpKaLsoY7HjLDrvAn1Hd65uja15Ud8jkzGKnq/7x76XA9zmUQuW0oI6WKOq90oQSSNkVbXzi7e4lhO9QLEz0xR+ctW/yvw0Mq4+h7uXihnSomBgGyTwANlgwMRTiW7Suo11xqtnRfiGhEgMZTQjNXLNxJO21Ccy0OaYPh9m/ZJ+pT/cwXXUMZVPOuzctWuY/y6G3T1UcfgBTqkyrL3mF6ZFw==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=V1+YOJQotuBmkZQSmNUHCnKxbtvQPy/z8izP4uLKEChGyruIuePgCAQX99EEysjcHicd6OAynglarkGP+w+AIA7YcNBoT93bp4xyUVhF+mZ+eeXxHoU3Qov3RW14F+cGvDH6l0FR6RmtPgVd+vdDPMuDGag5rDikBKFtYK+tPIBMnTov+C+38Dr5eV4hlKhb1pZsG7h9s7/ntSZpnYgmxn1rFDj3bib62sA5X1lkwM5g8+iXdDwgnJhg36EDXOLF6NXZQc4Ei8kFmkiEyMGaKeGfNUTJqEbdcWZw0zCj7O/VBMm6xoBUGpkWqeWrUbGa5nlUeLw16Z4kM2Ryhx1yMA==
- Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=epam.com header.i="@epam.com" header.h="From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
- Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=epam.com;
- Cc: xen-devel@xxxxxxxxxxxxxxxxxxxx, Doug Goldstein <cardoe@xxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>
- Delivery-date: Fri, 04 Dec 2026 11:15:55 +0000
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
On 12/4/26 11:24, Nicola Vetrini wrote:
On 2026-12-04 09:44, Dmytro Prokopchuk1 wrote:
MISRA C Rule 18.3 states: "The relational operators >, >=, < and <= shall
not be applied to objects of pointer type except where they point into
the same object".
The is_kernel(), is_kernel_text(), is_kernel_rodata() and
is_kernel_inittext() macros check whether an arbitrary address lies
within a section of the Xen image by comparing it against linker-defined
section boundaries. A pointer outside the section is a legitimate input
for them and simply yields false, hence such comparisons are safe.
Why would this matter? The standard only guarantees defined behavior if
you compare pointers inside the same array, no matter that the compiler
doesn't take advantage of it (probably due to the cast on p). To me, it
looks like a more reasonable approach would be to do the comparison in
unsigned long and then rely on the assumption that no information is
lost in the char* -> unsigned long conversion.
Looks like Xen has this deviation established already:
-doc_begin="Conversions from and to integral types are safe, in the
assumption that the target type has enough bits to store the value.
See also Section \"4.7 Arrays and Pointers\" of \"GCC_MANUAL\""
-config=MC3A2.R11.6,casts+={safe, ...
e.g.
extern char _stext[], _etext[];
#define is_kernel_text(p) ({ \
unsigned long __p = (unsigned long)(p); \
(__p >= (unsigned long)_stext) && (__p < (unsigned long)_etext);
\
})
in this way you avoid the violation at the root.
And we can update macros without new violations.
And all four macros should be updated: is_kernel, is_kernel_text,
is_kernel_rodata, is_kernel_inittext, to be aligned.
Well, let's wait someone else review.
BR, Dmytro.
|