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

Re: [PATCH] misra: consider conversion from UL or (void*) to function pointer as safe


  • To: Dmytro Prokopchuk1 <dmytro_prokopchuk1@xxxxxxxx>
  • From: Nicola Vetrini <nicola.vetrini@xxxxxxxxxxx>
  • Date: Mon, 25 Aug 2025 14:53:41 +0200
  • 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=1756126421; h=DKIM-Signature:MIME-Version:Date:From:To:Cc:Subject:In-Reply-To: References:Message-ID:X-Sender:Organization:Content-Type: Content-Transfer-Encoding; bh=NooWRAUvWfucIFkhhxHUk2cEIO4DVIs7fRzZRF99NfM=; b=BSqYVVgc88g3ovEbcKSyDQv2yY4YrM5cUB3kNx3j5xkqp2RyEoduo/8lAM8qg3uPfdym 668qP4PRv6myAClf+msvzNdAjo/OpzrLMbg2GNYhgUqOUlfebhR0MS2edMWpUZ8ZKyX6x OHYrL7+9muUFvr9swDyMkL12HsJtqNzineAKwKH5xrFhHrfSQeNBLSG2u8SENXrQEgqyJ v/3JfvrDRWX/aw0ocp1x+WdIxHv78HTbGH+iqMuTFPshxVsac3ZjN9dG6bhayb7cHLGic U9/leS9a9b75hEnJVccMRe4y/AzyumUj6ToSD927wyP9ESYFhMsILaYyNHYRnfgoqn8lv Wl/BEvnN4Lkl3nhb0aJdyHv6ob6avriWc1+a/6NriNZx+5NGuwxW/hFCm7WHQSoOg8roJ nH76a75ST69g3HciPmgacyccJrkAFiS817Dy4U8mNPyE3lw97c7CV9Ne2lFeNxgmyLfYs lAPuzqfqvHNiAHIasl0WnILJVXTk/lIE3rQsRDdwGVhz/YmIjYxNvFnFy0z/7Jg/QLMPO s5SZsLU6FJ/ADcAItG6+az67B5uxDR7xeB9cLr0Sjh4NTQo7ln3JEGfLqxVcqBBkrleT4 LgTrKWH2d2hlkR+Tta4anyVdyYWvjKwMX/4nSi0pmwJlZDrF8TM+q5gMf9U+ZnY=
  • Arc-seal: i=1; d=bugseng.com; s=openarc; a=rsa-sha256; cv=none; t=1756126421; b=hvCl2HpqOtbKs6UjiHEToaGiQC1LwtIl8a6oFH1MP4xNaQW+S0ZYx8OivKDcsJdyUfjD 62u7z6piuq6RrzOgswzpvkQoJ4WT+qti8kYBGy+Ryr5a63gy1ntYajKAQbG93MdfbS6N1 3ncQ0vO+CfbFUCfch+NOjHzSgmBpV1Qkxup1MABdQGUGzxQBTaegntSQxUJKqgAEl9TdK EU2UNBk1FfWlr72FCIrJrQXrRYMj3mUj3JVCMSY+0xQQeUoq9jnolGRmnUpadSAq89aYW fYLK1biFVpDW0I+0sYxxQTlGeU4PJ7bNIREXT5rSQsaqqZ3DaBYLFtj2sRREjOhuRNlHp 1j7u7G3xoPCCChwOcjzzBKmF6TLEvXgdXVil23n57taXIwvr0xP5/5RDLCStPQ6iBebIn 3hLZ5FJJ3BKzRtd8n6dW71DFwoAUNywCrZcmZZvnG418X5ZsbqYI+qGmf2tLIo4LrK713 GuVWli4FmLIZmnJgkkDRFQzzAvhaDi1anrd+yEclDzVlw2RtC8xUH+vSiHmGDd/nXbmut 7aWMMEvUcJPSQfV0Eejwu+aHVKAPM2JTRreYJISYSGYQQXIHS8S+QOjCx9A1W7jyq2sRC uC2lB+I8lFb+MhUuLqLhq2PzWEWqnJFs/14jcccTsyVkTxro72ghogBVUJZA878=
  • Authentication-results: bugseng.com; arc=none smtp.remote-ip=162.55.131.47
  • Cc: Jan Beulich <jbeulich@xxxxxxxx>, Doug Goldstein <cardoe@xxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Julien Grall <julien@xxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • Delivery-date: Mon, 25 Aug 2025 12:53:56 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 2025-08-22 18:34, Dmytro Prokopchuk1 wrote:
On 8/21/25 11:25, Nicola Vetrini wrote:
On 2025-08-21 10:01, Jan Beulich wrote:
On 19.08.2025 20:55, Dmytro Prokopchuk1 wrote:
Rule 11.1 states as following: "Conversions shall not be performed
between a pointer to a function and any other type."

The conversion from unsigned long or (void *) to a function pointer
is safe in Xen because the architectures it supports (e.g., x86 and
ARM) guarantee compatible representations between these types.

I think we need to be as precise as possible here. The architectures
guarantee nothing, they only offer necessary fundamentals. In the
Windows x86 ABI, for example, you can't convert pointers to/from longs
without losing data. What we build upon is what respective ABIs say,
possibly in combination of implementation specifics left to compilers.


+1, a mention of the compilers and targets this deviation relies upon is
needed.

Maybe with this wording:

This deviation is based on the guarantees provided by the specific ABIs
(e.g., ARM AAPCS) and compilers (e.g., GCC) supported in Xen. These ABIs

s/supported in/supported by/

guarantee compatible representations for 'void *', 'unsigned long' and
function pointers for the supported target platforms. This behavior is

It's not just about the guarantees of the ABIs: it's the behavior of the compiler for those ABIs that makes this safe or unsafe. If present, such documentation should be included

architecture-specific and may not be portable outside of supported
environments.


--- a/docs/misra/deviations.rst
+++ b/docs/misra/deviations.rst
@@ -370,6 +370,16 @@ Deviations related to MISRA C:2012 Rules:
        to store it.
      - Tagged as `safe` for ECLAIR.

+   * - R11.1
+     - The conversion from unsigned long or (void \*) to a function
pointer does
+       not lose any information or violate type safety assumptions
if unsigned
+       long or (void \*) type is guaranteed to be the same bit size
as a
+       function pointer. This ensures that the function pointer can
be fully
+       represented without truncation or corruption. The macro
BUILD_BUG_ON is
+       integrated into xen/common/version.c to confirm conversion
compatibility
+       across all target platforms.
+     - Tagged as `safe` for ECLAIR.

Why the escaping of * here, when ...

--- a/docs/misra/rules.rst
+++ b/docs/misra/rules.rst
@@ -431,7 +431,13 @@ maintainers if you want to suggest a change.
      - All conversions to integer types are permitted if the
destination
        type has enough bits to hold the entire value. Conversions to
bool
        and void* are permitted. Conversions from 'void noreturn (*)
(...)'
-       to 'void (*)(...)' are permitted.
+       to 'void (*)(...)' are permitted. Conversions from unsigned
long or
+       (void \*) to a function pointer are permitted if the source
type has
+       enough bits to restore function pointer without truncation or
corruption.
+       Example::
+
+           unsigned long func_addr = (unsigned long)&some_function;
+           void (*restored_func)(void) = (void (*)(void))func_addr;

... context here suggests they work fine un-escaped, and you even add
some un-
escaped instances as well. Perhaps I'm simply unaware of some
peculiarity?


This is a literal rst block, while the other is not (* acts as a bullet
point in rst iirc)

This is how "sphinx-build" tool interprets this.
1. * inside single quotes '' -> looks normal, e.g. ‘void (*)(…)’
2. * without quotes -> warning
deviations.rst:369: WARNING: Inline emphasis start-string without
end-string. [docutils]
3. \* -> looks normal, e.g. (void *)

Because that we need such format: \*

Dmytro.


Jan


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