diff --git a/automation/eclair_analysis/ECLAIR/deviations.ecl b/automation/eclair_analysis/ECLAIR/deviations.ecl index 7f3fd35a33..219ba6993b 100644 --- a/automation/eclair_analysis/ECLAIR/deviations.ecl +++ b/automation/eclair_analysis/ECLAIR/deviations.ecl @@ -375,6 +375,14 @@ constant expressions are required.\"" } -doc_end +-doc_begin="Conversion from unsigned long or (void *) to a function pointer can restore full information, provided that the source type has enough bits to restore it." +-config=MC3A2.R11.1,casts+={safe, + "from(type(canonical(builtin(unsigned long)||pointer(builtin(void))))) + &&to(type(canonical(__function_pointer_types))) + &&relation(definitely_preserves_value)" +} +-doc_end + -doc_begin="The conversion from a function pointer to a boolean has a well-known semantics that do not lead to unexpected behaviour." -config=MC3A2.R11.1,casts+={safe, "from(type(canonical(__function_pointer_types))) diff --git a/docs/misra/deviations.rst b/docs/misra/deviations.rst index 3271317206..b3431ef24e 100644 --- a/docs/misra/deviations.rst +++ b/docs/misra/deviations.rst @@ -366,11 +366,17 @@ Deviations related to MISRA C:2012 Rules: - Tagged as `safe` for ECLAIR. * - R11.1 - - The conversion from a function pointer to unsigned long or (void \*) does + - The conversion from a function pointer to unsigned long or '(void *)' does not lose any information, provided that the target type has enough bits to store it. - Tagged as `safe` for ECLAIR. + * - R11.1 + - Conversion from unsigned long or '(void *)' to a function pointer can + restore full information, provided that the source type has enough bits + to restore it. + - Tagged as `safe` for ECLAIR. + * - R11.1 - The conversion from a function pointer to a boolean has a well-known semantics that do not lead to unexpected behaviour. diff --git a/docs/misra/rules.rst b/docs/misra/rules.rst index 4388010ec9..4e94251887 100644 --- a/docs/misra/rules.rst +++ b/docs/misra/rules.rst @@ -431,7 +431,12 @@ 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. + Example:: + + unsigned long func_addr = (unsigned long)&some_function; + void (*restored_func)(void) = (void (*)(void))func_addr; * - `Rule 11.2 `_ - Required diff --git a/xen/common/version.c b/xen/common/version.c index 553b97ba9b..80e7a9b981 100644 --- a/xen/common/version.c +++ b/xen/common/version.c @@ -217,6 +217,25 @@ void __init xen_build_init(void) #endif /* CONFIG_X86 */ } #endif /* BUILD_ID */ + +/* + * This assertion checks compatibility between 'unsigned long', 'void *', + * and function pointers. This is true for most supported architectures, + * including X86 (x86_64) and ARM (arm, aarch64). + * + * For more context on architecture-specific preprocessor guards, see + * docs/misc/C-language-toolchain.rst. + * + * If porting Xen to a new architecture where this compatibility does not hold, + * exclude that architecture from these checks and provide suitable commentary + * and/or alternative checks as appropriate. + */ +static void __init __maybe_unused build_assertions(void) +{ + BUILD_BUG_ON(sizeof(unsigned long) != sizeof(void (*)(void))); + BUILD_BUG_ON(sizeof(void *) != sizeof(void (*)(void))); +} + /* * Local variables: * mode: C