[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: Dmytro Prokopchuk1 <dmytro_prokopchuk1@xxxxxxxx>
  • From: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • Date: Fri, 04 Dec 2026 10:24:47 +0100
  • Arc-authentication-results: i=1; bugseng.com; arc=none smtp.remote-ip=162.55.131.47
  • Arc-message-signature: i=1; d=bugseng.com; s=openarc; a=rsa-sha256; c=relaxed/relaxed; t=1796376287; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:X-Sender:Organization:Content-Type: Content-Transfer-Encoding; bh=8Mf83oeH5YAUHJpyNS7m+F6N3Oh29eczM9a+Y10Id7k=; b=tn7XrlXvmTRblZokZRVi/gXBcy1OmolLX+uMosq9cWShAY6ps28sBOsM2SZPtR1O90OL BRdt2rFQXwy+LnbF3RMA7VbyShJD+d0p29Q5hJmVDMFqvbauHpiml9KfDCam+owkrI90Z iS09L6JAhPZ6WizpF4mdVTnAz7aFGjixfhnULH6d8S3/qoYYna/DEMlBq/yoLhf1K5FpM zszLZ5aLGcSZBZs2/2YQVmfVONuS/ueNHdPd3lMxXP5GRU28n2XyVYeuD6mG+bJIDzgN7 dYvIowYoy8KQtqil/wbxBRz8E0/hPrZJbnHmlLyAg7T47pXlvw4SgJC3/8pu/PxFJhqAL OaFy2NuPB6OSqzm73LjqBNfY/YB89kQz11KoIYO4VXcioPyG2202sLNeOcNk692PT+bNC rxzIoSgWM6fu6gXy+h8gBjMubHAp0MQDvIC0PK6FBHGZNJNv4c/OPKIKUjR/GiGjmavmW p4/sQ92gG+nmnSGA3n8oGZWhprLXgOcHfR5eTt0Agjx/DQkrSJjd3D2Dx9S0repGLInZm O0E+kyMnLELoggF4jAsnZmyAqT3uoOrWHXeJ8tuMx4xDJGYWZRI3wx9qOUzmwIyg9UrbR /sZ7OnXZQUXElEsEFCZS1njF1E4fSgSh4cwSsUf3eTS4j4h8iNdy5C+TsylPcUo=
  • Arc-seal: i=1; d=bugseng.com; s=openarc; a=rsa-sha256; cv=none; t=1796376287; b=rUKCujGW5d0BROql4ljht3wFXlIOFbEdrtQOVYuKCFrO7kdKzAdIODtyq0qPspNM00Hu 6/dsJmK5/vK5YpniVT37WhybkiTsQJplkhiiSrYh+Ees1slI927rqGS8bhBivgCHTL76F ruQ0h9hMFS4cmo7dMyMFvNtEwaQ3N/57FMVvxRYY/HHjsEqja0QowhLXJvOT5ak6skMWz v+zRRg/54Lb1vQC6IKrXxS+h8HumeWmn2bbKFxmKaeol5QLzJqEb4OaoWmGIMIDjS1FK0 LQ4clBkbbkCQynRwX/HkN1RWnyTmKehjnJW7o6PS9wYx0qnsk5vp40dF5b0vbvEwSXKF4 l88BIqc1FtiZ+gE2mJcgM4ciBxnBYXiqGnhJZf8ZdBq0hZCk1/ZgZdZ27lAk3c7GxmhGz UyNYrYKdzLELZAfvXNwcQspMJu27oibjk6TW52d+3YxySxBMJIcGJBJZGYbUksN+lBttg STYdQczd2/dFk7a6E5g2GeAUyna/mRsQ+Xpkmu9qBj2a7y25TzK2wRnawIVJ8RLO7B530 2fbjje2dfxjuuGbSpXmz3UsDICVQUHx01bYaV+0hr4l+h98cdy+O0FNp/Ohs8tDwMSTfJ mDOTa3LpXwcY6wex08mIWWH6gGdL9aNGkdgvswycctTA98NCxGSBYcTIUg2ElLo=
  • Authentication-results: eu.smtp.expurgate.cloud; none
  • Authentication-results: bugseng.com; arc=none smtp.remote-ip=162.55.131.47
  • 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 09:24:51 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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.

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.

Deviate the rule for comparisons encapsulated by these macros.

Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@xxxxxxxx>
---
Changes in v2:
- restrict the deviation to the is_kernel*() macros instead of covering
  comparisons against all linker symbols
- update the commit subject, message and deviations.rst accordingly

With v2 applied the remaining R18.3 violations (ARM64 and x86_64) are all
unrelated to linker symbols.

Test CI pipeline:
https://gitlab.com/xen-project/people/dimaprkp4k/xen/-/pipelines/3152444935
---
 automation/eclair_analysis/ECLAIR/deviations.ecl | 5 +++++
 docs/misra/deviations.rst                        | 7 +++++++
 2 files changed, 12 insertions(+)

diff --git a/automation/eclair_analysis/ECLAIR/deviations.ecl b/automation/eclair_analysis/ECLAIR/deviations.ecl
index cdcdb4622c..b7a7fb2a52 100644
--- a/automation/eclair_analysis/ECLAIR/deviations.ecl
+++ b/automation/eclair_analysis/ECLAIR/deviations.ecl
@@ -630,6 +630,11 @@ C99 Undefined Behaviour 45: Pointers that do not point into, or just beyond, the -config=MC3A2.R18.2,reports+={safe, "any_area(any_loc(any_exp(macro(^page_to_mfn$))))"}
 -doc_end

+-doc_begin="The following 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 and simply yields false." +-config=MC3A2.R18.3,reports+={safe, "any_area(any_loc(any_exp(macro(^is_kernel(_text|_rodata|_inittext)?$))))"}
+-doc_end
+
-doc_begin="Flexible array members are deliberately used and XEN developers are aware of the dangers related to them: unexpected result when the structure is given as argument to a sizeof() operator and the truncation in assignment between structures."
 -config=MC3A2.R18.7,reports+={deliberate, "any()"}
diff --git a/docs/misra/deviations.rst b/docs/misra/deviations.rst
index eb5817f88b..df77f8c3cd 100644
--- a/docs/misra/deviations.rst
+++ b/docs/misra/deviations.rst
@@ -644,6 +644,13 @@ Deviations related to MISRA C:2012 Rules:
        are safe.
      - Tagged as `safe` for ECLAIR.

+   * - R18.3
+     - Relational comparisons between pointers encapsulated by macros
+ is_kernel(), is_kernel_text(), is_kernel_rodata(), is_kernel_inittext() + are safe, as they check whether an arbitrary address lies within a
+       section of the Xen image.
+     - Tagged as `safe` for ECLAIR.
+
    * - R20.4
- The override of the keyword \"inline\" in xen/compiler.h is present so that section contents checks pass when the compiler chooses not to

--
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253



 


Rackspace

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