fix some issues related to MISRA C:2012 Rule 9.1
cover fix some issues related to MISRA C:2012 Rule 9.1
This patch series is aimed at discussing different categories of patterns concerning local variables that are possibly not initialized in all code paths, which results in hard-to-prove correctness. The main categories are as follows:
1. Variables initialized by passing a pointer to them to a function. Many such functions are coupled with error handling which results in the variable not being initialized.
2. Some variables are used in switch statements and the control flow ensures that all code paths do initialize them, but due to the presence of goto statements, the flow is harder to follow.
I emphasize that, as far as I can tell, the code is already compliant with the rule, but there is room for improvement, especially on the side of allowing automatic checks to be more effective.
Nicola Vetrini (4):
xen/arm: justify or initialize conditionally uninitialized variables xen/arm64: bitops: justify uninitialized variable inside a macro xen/arm: initialize conditionally uninitialized local variables xen/arm: initialize conditionally uninitialized local variables
docs/misra/safe.json | 24 +++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 ++ xen/arch/arm/arm64/lib/find_next_bit.c | 1 + xen/arch/arm/bootfdt.c | 6 ++++ xen/arch/arm/cpuerrata.c | 6 ++-- xen/arch/arm/decode.c | 2 ++ xen/arch/arm/dm.c | 2 +- xen/arch/arm/domain_build.c | 29 ++++++++++++++---- xen/arch/arm/domctl.c | 8 ++--- xen/arch/arm/efi/efi-boot.h | 6 ++-- xen/arch/arm/gic-v3-its.c | 9 +++--- xen/arch/arm/gic-v3-lpi.c | 17 ++++++----- xen/arch/arm/guest_walk.c | 12 ++++---- xen/arch/arm/include/asm/guest_atomics.h | 3 ++ xen/arch/arm/include/asm/p2m.h | 10 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33 ++++++++++++--------- xen/arch/arm/platforms/xilinx-zynqmp-eemi.c | 10 ++----- xen/arch/arm/psci.c | 10 +++---- xen/drivers/char/pl011.c | 2 +- 20 files changed, 129 insertions(+), 65 deletions(-)
--
2.34.1
RFC PATCH 1/4 xen/arm: justify or initialize conditionally uninitialized variables
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
To achieve this goal, I adopted the following strategies:
- Add a suitably formatted local deviation comment (as indicated in 'docs/misra/documenting-violations.rst') to exempt the following line from checking.
- Provide an initialization for the variable at the declaration.
- Substitute a goto breaking out of control flow logic with a semantically equivalent do { .. } while(0).
--- docs/misra/safe.json | 8 +++++++ xen/arch/arm/arm64/lib/find_next_bit.c | 1 + xen/arch/arm/bootfdt.c | 6 +++++ xen/arch/arm/decode.c | 2 ++ xen/arch/arm/domain_build.c | 29 ++++++++++++++++++---- xen/arch/arm/efi/efi-boot.h | 6 +++-- xen/arch/arm/gic-v3-its.c | 9 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33 +++++++++++++++----------- 9 files changed, 69 insertions(+), 26 deletions(-)
docs/misra/safe.json +8 -0
@@ -12,6 +12,14 @@ }, { "id": "SAF-1-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-2-safe", "analyser": {}, "name": "Sentinel", "text": "Next ID to be used"
xen/arch/arm/arm64/lib/find_next_bit.c +1 -0
@@ -67,6 +67,7 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, {const unsigned long *p = addr +BIT_WORD (offset);unsigned long result = offset & ~(BITS_PER_LONG -1 ); +/* SAF-1-safe MC3R1.R9.1 */ unsigned long tmp;if (offset >= size)
xen/arch/arm/bootfdt.c +6 -0
@@ -34,6 +34,7 @@ static bool __init device_tree_node_matches(const void *fdt, int node,static bool __init device_tree_node_compatible (const void *fdt,int node,const char *match) { +/* SAF-1-safe MC3R1.R9.1 */ int len, l;const void *prop;
@@ -169,7 +170,9 @@ int __init device_tree_for_each_node(const void *fdt, int node,*/ int depth =0 ;const int first_node = node; +/* SAF-1-safe MC3R1.R9.1 */ u32 address_cells[DEVICE_TREE_MAX_DEPTH ]; +/* SAF-1-safe MC3R1.R9.1 */ u32 size_cells[DEVICE_TREE_MAX_DEPTH ];int ret;
@@ -249,8 +252,10 @@ static void __init process_multiboot_node(const void *fdt, int node,const __be32 *cell;bootmodule_kind kind;paddr_t start, size; +/* SAF-1-safe MC3R1.R9.1 */ int len;/* sizeof("/chosen/") + DT_MAX_NAME + '/' + DT_MAX_NAME + '/0' => 92 */ +/* SAF-1-safe MC3R1.R9.1*/ char path[92 ];int parent_node, ret;bool domU;
@@ -326,6 +331,7 @@ static int __init process_chosen_node(const void *fdt, int node, {const struct fdt_property *prop;paddr_t start, end; +/* SAF-1-safe MC3R1.R9.1 */ int len;if (fdt_get_property (fdt, node,"xen,static-heap" ,NULL ) )
xen/arch/arm/decode.c +2 -0
@@ -27,6 +27,7 @@ static void update_dabt(struct hsr_dabt *dabt, int reg,static int decode_thumb2 (register_t pc,struct hsr_dabt *dabt,uint16_t hw1) { +/* SAF-1-safe MC3R1.R9.1 */ uint16_t hw2;uint16_t rt;
@@ -151,6 +152,7 @@ static int decode_arm64(register_t pc, mmio_info_t *info)static int decode_thumb (register_t pc,struct hsr_dabt *dabt) { +/* SAF-1-safe MC3R1.R9.1 */ uint16_t instr;if (raw_copy_from_guest (&instr, (void *__user )pc,sizeof (instr)) )
xen/arch/arm/domain_build.c +24 -5
@@ -62,7 +62,7 @@ custom_param("dom0_mem", parse_dom0_mem);int __init parse_arch_dom0_param (const char *s,const char *e) { -long long val; +long long val =LLONG_MAX ;if ( !parse_signed_integer ("sve" , s, e, &val) ) {
@@ -1077,6 +1077,7 @@ static void __init assign_static_memory_11(struct domain *d,static int __init handle_linux_pci_domain (struct kernel_info *kinfo,const struct dt_device_node *node) { +/* SAF-1-safe MC3R1.R9.1 */ uint16_t segment;int res;
@@ -1351,6 +1352,7 @@ static int __init make_memory_node(const struct domain *d,unsigned int i;int res, reg_size = addrcells + sizecells;int nr_cells =0 ; +/* SAF-1-safe MC3R1.R9.1*/ __be32 reg[NR_MEM_BANKS *4 /* Worst case addrcells + sizecells */ ];__be32 *cells;
@@ -1578,6 +1580,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo,struct rangeset *unalloc_mem;paddr_t start, end;unsigned int i; +/* SAF-1-safe MC3R1.R9.1 */ int res;dt_dprintk ("Find unallocated memory for extended regions\n" );
@@ -1727,6 +1730,7 @@ static int __init find_memory_holes(const struct kernel_info *kinfo,dt_for_each_device_node ( dt_host, np ) {unsigned int naddr; +/* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; naddr =dt_number_of_address (np);
@@ -1976,9 +1980,11 @@ static int __init make_cpus_node(const struct domain *d, void *fdt)const struct dt_device_node *npcpu;unsigned int cpu;const void *compatible =NULL ; +/* SAF-1-safe MC3R1.R9.1 */ u32 len;/* Placeholder for cpu@ + a 32-bit hexadecimal number + \0 */ char buf[13 ]; +/* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency;/* Keep the compiler happy with -Og */ bool clock_valid = false;
@@ -2104,6 +2110,7 @@ static int __init make_gic_node(const struct domain *d, void *fdt,const struct dt_device_node *gic = dt_interrupt_controller;int res =0 ;const void *addrcells, *sizecells; +/* SAF-1-safe MC3R1.R9.1 */ u32 addrcells_len, sizecells_len;/*
@@ -2179,6 +2186,7 @@ static int __init make_timer_node(const struct kernel_info *kinfo)int res;unsigned int irq[MAX_TIMER_PPI ];gic_interrupt_t intrs[3 ]; +/* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency;bool clock_valid;
@@ -2511,6 +2519,7 @@ static int __init handle_device(struct domain *d, struct dt_device_node *dev,unsigned int naddr;unsigned int i;int res; +/* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size;bool own_device = !dt_device_for_passthrough (dev);/*
@@ -2779,6 +2788,7 @@ static int __init make_gicv2_domU_node(struct kernel_info *kinfo) {void *fdt = kinfo->fdt;int res =0 ; +/* SAF-1-safe MC3R1.R9.1*/ __be32 reg[(GUEST_ROOT_ADDRESS_CELLS +GUEST_ROOT_SIZE_CELLS ) *2 ];__be32 *cells;const struct domain *d = kinfo->d;
@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo)void *fdt = kinfo->fdt;int res;gic_interrupt_t intr; +/* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS +GUEST_ROOT_SIZE_CELLS ];__be32 *cells;struct domain *d = kinfo->d;
@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo)paddr_t paddr, len;int node;int res; +/* SAF-1-safe MC3R1.R9.1 */ __be32 val[2 ];__be32 *cellp;void __iomem *initrd;
@@ -3514,6 +3526,7 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np, uint32_t *port, uint32_t *phandle) { const __be32 *prop = NULL; +/* SAF-1-safe MC3R1.R9.1 */ uint32_t len; prop =dt_get_property (np,"xen,evtchn" , &len);
@@ -3538,10 +3551,13 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np,static int __init alloc_domain_evtchn (struct dt_device_node *node) {int rc; +/* SAF-1-safe MC3R1.R9.1 */ uint32_t domU1_port, domU2_port, remote_phandle;struct dt_device_node *remote_node;const struct dt_device_node *p1_node, *p2_node; +/* SAF-1-safe MC3R1.R9.1 */ struct evtchn_alloc_unbound alloc_unbound; +/* SAF-1-safe MC3R1.R9.1 */ struct evtchn_bind_interdomain bind_interdomain;struct domain *d1 =NULL , *d2 =NULL ;
@@ -3789,11 +3805,12 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo)static int __init alloc_xenstore_evtchn (struct domain *d) { -evtchn_alloc_unbound_t alloc; +evtchn_alloc_unbound_t alloc = { + .dom = d->domain_id, + .remote_dom = hardware_domain->domain_id + };int rc; - alloc.dom = d->domain_id; - alloc.remote_dom = hardware_domain->domain_id; rc =evtchn_alloc_unbound (&alloc,0 );if ( rc ) {
@@ -3810,8 +3827,9 @@ static int __init construct_domU(struct domain *d, const struct dt_device_node *node) {struct kernel_info kinfo = {}; -const char *dom0less_enhanced; +const char *dom0less_enhanced =NULL ;int rc; +/* SAF-1-safe MC3R1.R9.1 */ u64 mem;u32 p2m_mem_mb;unsigned long p2m_pages;
@@ -3939,6 +3957,7 @@ void __init create_domUs(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), };unsigned int flags =0U ; +/* SAF-1-safe MC3R1.R9.1 */ uint32_t val;int rc;
xen/arch/arm/efi/efi-boot.h +4 -2
@@ -117,6 +117,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells)static int __init fdt_set_reg (void *fdt,int node,int addr_cells,int size_cells,uint64_t addr,uint64_t len) { +/* SAF-1-safe MC3R1.R9.1 */ __be32 val[4 ];/* At most 2 64 bit values to be stored */ __be32 *cellp;
@@ -308,7 +309,7 @@ fdt_set_fail:static void __init *fdt_increase_size (struct file *fdtfile,int add_size) {EFI_STATUS status; -EFI_PHYSICAL_ADDRESS fdt_addr; +EFI_PHYSICAL_ADDRESS fdt_addr =0 ;int fdt_size;int pages;void *new_fdt;
@@ -433,7 +434,7 @@ static void __init efi_arch_cfg_file_late(const EFI_LOADED_IMAGE *image,static void *__init efi_arch_allocate_mmap_buffer (UINTN map_size) { -void *ptr; +void *ptr =NULL ;EFI_STATUS status; status = efi_bs->AllocatePool (EfiLoaderData, map_size, &ptr);
@@ -538,6 +539,7 @@ static void __init efi_arch_handle_module(const struct file *file, {int node;int chosen; +/* SAF-1-safe MC3R1.R9.1 */ int addr_len, size_len;if ( file == &dtbfile )
xen/arch/arm/gic-v3-its.c +4 -5
@@ -192,8 +192,7 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, cmd[0 ] =GITS_CMD_MAPC ; cmd[1 ] =0x00 ; - cmd[2 ] =encode_rdbase (its, cpu, collection_id); - cmd[2 ] |=GITS_VALID_BIT ; + cmd[2 ] =encode_rdbase (its, cpu, collection_id) |GITS_VALID_BIT ; cmd[3 ] =0x00 ;return its_send_command (its, cmd);
@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, } cmd[0 ] =GITS_CMD_MAPD | ((uint64_t )deviceid <<32 ); cmd[1 ] = size_bits; - cmd[2 ] = itt_addr; -if ( valid ) - cmd[2 ] |=GITS_VALID_BIT ; + cmd[2 ] = itt_addr | (valid ?GITS_VALID_BIT :0x00 ); cmd[3 ] =0x00 ;return its_send_command (its, cmd);
@@ -911,6 +908,7 @@ int gicv3_its_make_hwdom_dt_nodes(const struct domain *d,const struct dt_device_node *gic, void *fdt) { + /* SAF-1-safe MC3R1.R9.1 */ uint32_t len;int res;const void *prop =NULL ;
@@ -1004,6 +1002,7 @@ static void gicv3_its_dt_init(const struct dt_device_node *node)*/ dt_for_each_child_node (node, its) { +/* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size;if ( !dt_device_is_compatible (its,"arm,gic-v3-its" ) )
xen/arch/arm/mm.c +1 -0
@@ -935,6 +935,7 @@ static int xen_pt_update_entry(mfn_t root, unsigned long virt, mfn_t mfn, unsigned int target, unsigned int flags) { + /* SAF-1-safe MC3R1.R9.1 */ int rc;unsigned int level;lpae_t *table;
xen/arch/arm/p2m.c +19 -14
@@ -496,16 +496,18 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,lpae_t entry, *table;int rc;mfn_t mfn =INVALID_MFN ; -p2m_type_t _t; +p2m_type_t _t = p2m_invalid;DECLARE_OFFSETS (offsets, addr);ASSERT (p2m_is_locked (p2m));BUILD_BUG_ON (THIRD_MASK !=PAGE_MASK );/* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; +if ( t ) { + *t = _t; + }else { + t = &_t; + }if ( valid ) *valid = false;
@@ -1031,6 +1033,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m,unsigned int level =0 ;unsigned int target =3 - (page_order /XEN_PT_LPAE_SHIFT );lpae_t *entry, *table, orig_pte; +/* SAF-1-safe MC3R1.R9.1 */ int rc;/* A mapping is removed if the MFN is invalid. */ bool removing_mapping =mfn_eq (smfn,INVALID_MFN );
@@ -1483,6 +1486,7 @@ static inline int p2m_remove_mapping(struct domain *d, {struct p2m_domain *p2m =p2m_get_hostp2m (d);unsigned long i; +/* SAF-1-safe MC3R1.R9.1 */ int rc;p2m_write_lock (p2m);
@@ -1685,20 +1689,21 @@ static int p2m_alloc_vmid(struct domain *d)ASSERT (nr !=INVALID_VMID ); -if ( nr ==MAX_VMID ) - { - rc = -EBUSY ; -printk (XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n" , d->domain_id); - goto out; - } +do { +if ( nr ==MAX_VMID ) + { + rc = -EBUSY ; +printk (XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n" , d->domain_id); +break ; + } -set_bit (nr, vmid_mask); +set_bit (nr, vmid_mask); - p2m->vmid = nr; + p2m->vmid = nr; - rc =0 ; + rc =0 ; + }while (0 ); -out:spin_unlock (&vmid_alloc_lock);return rc; }
Signature · 1 line
2.34.1
RFC PATCH 3/4 xen/arm: initialize conditionally uninitialized local variables
In reply to Nicola VetriniThis patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
An initialization to a safe value is provided to reach this aim.
---
Additional input on which values may be 'safe' in each context is surely welcome, to avoid possibly compromising the correctness of the function semantics.
--- xen/arch/arm/cpuerrata.c | 6 +++--- xen/arch/arm/domctl.c | 8 ++++---- xen/arch/arm/gic-v3-lpi.c | 17 +++++++++-------- xen/arch/arm/include/asm/p2m.h | 10 ++++++---- xen/arch/arm/platforms/xilinx-zynqmp-eemi.c | 10 ++-------- xen/arch/arm/psci.c | 10 +++++----- xen/drivers/char/pl011.c | 2 +- 7 files changed, 30 insertions(+), 33 deletions(-)
xen/arch/arm/cpuerrata.c +3 -3
@@ -159,7 +159,7 @@ extern char __mitigate_spectre_bhb_loop_start_32[],static int enable_smccc_arch_workaround_1 (void *data) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };const struct arm_cpu_capabilities *entry = data;/*
@@ -252,7 +252,7 @@ static int enable_spectre_bhb_workaround(void *data)if (cpus_have_cap (ARM_WORKAROUND_BHB_SMCC_3 ) ) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };if ( smccc_ver <SMCCC_VERSION (1 ,1 ) ) goto warn;
@@ -393,7 +393,7 @@ DEFINE_PER_CPU_READ_MOSTLY(register_t, ssbd_callback_required);static bool has_ssbd_mitigation (const struct arm_cpu_capabilities *entry) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };bool required;if ( smccc_ver <SMCCC_VERSION (1 ,1 ) )
xen/arch/arm/domctl.c +4 -4
@@ -29,10 +29,10 @@ static int handle_vuart_init(struct domain *d, struct xen_domctl_vuart_op *vuart_op) {int rc; -struct vpl011_init_info info; - - info.console_domid = vuart_op->console_domid; - info.gfn =_gfn (vuart_op->gfn); +struct vpl011_init_info info = { + .console_domid = vuart_op->console_domid, + .gfn =_gfn (vuart_op->gfn) + };if ( d->creation_finished )return -EPERM ;
xen/arch/arm/gic-v3-lpi.c +9 -8
@@ -210,7 +210,10 @@ out:void gicv3_lpi_update_host_entry (uint32_t host_lpi,int domain_id,uint32_t virt_lpi) { -union host_lpi *hlpip, hlpi; +union host_lpi *hlpip, hlpi = { + .virt_lpi = virt_lpi, + .dom_id = domain_id + };ASSERT (host_lpi >=LPI_OFFSET );
@@ -218,9 +221,6 @@ void gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id, hlpip = &lpi_data.host_lpis[host_lpi /HOST_LPIS_PER_PAGE ][host_lpi %HOST_LPIS_PER_PAGE ]; - hlpi.virt_lpi = virt_lpi; - hlpi.dom_id = domain_id; -write_u64_atomic (&hlpip->data, hlpi.data); }
@@ -542,14 +542,15 @@ int gicv3_allocate_host_lpi_block(struct domain *d, uint32_t *first_lpi)for ( i =0 ; i <LPI_BLOCK ; i++ ) { -union host_lpi hlpi; -/* * Mark this host LPI as belonging to the domain, but don't assign * any virtual LPI or a VCPU yet. */ - hlpi.virt_lpi =INVALID_LPI ; - hlpi.dom_id = d->domain_id; +union host_lpi hlpi = { + .virt_lpi =INVALID_LPI , + .dom_id = d->domain_id + }; +write_u64_atomic (&lpi_data.host_lpis[chunk][lpi_idx + i].data, hlpi.data);
xen/arch/arm/include/asm/p2m.h +6 -4
@@ -345,7 +345,7 @@ static inline struct page_info *get_page_from_gfn( struct domain *d, unsigned long gfn, p2m_type_t *t, p2m_query_t q) { mfn_t mfn; -p2m_type_t _t; +p2m_type_t _t = p2m_invalid;struct page_info *page;/*
@@ -355,10 +355,12 @@ static inline struct page_info *get_page_from_gfn(if (likely (d != dom_xen) )return p2m_get_page_from_gfn (d,_gfn (gfn), t); -if ( !t ) +/* Allow t to be NULL */ +if ( t ) + *t = _t; +else { t = &_t; - - *t = p2m_invalid; + }/* * DOMID_XEN sees 1-1 RAM. The p2m_type is based on the type of the
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +2 -8
@@ -51,11 +51,11 @@ static inline bool domain_has_reset_access(struct domain *d, uint32_t rst)bool zynqmp_eemi (struct cpu_user_regs *regs) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };uint32_t fid =get_user_reg (regs,0 );uint32_t nodeid =get_user_reg (regs,1 );unsigned int pm_fn = fid &0xFFFF ; -enum pm_ret_status ret; +enum pm_ret_status ret =XST_PM_NO_ACCESS ;switch ( fid ) {
@@ -89,7 +89,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs) {gprintk (XENLOG_WARNING ,"zynqmp-pm: fn=%u No access to node %u\n" , pm_fn, nodeid); - ret =XST_PM_NO_ACCESS ; goto done; } goto forward_to_fw;
@@ -100,7 +99,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs) {gprintk (XENLOG_WARNING ,"zynqmp-pm: fn=%u No access to reset %u\n" , pm_fn, nodeid); - ret =XST_PM_NO_ACCESS ; goto done; } goto forward_to_fw;
@@ -116,7 +114,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs)case EEMI_FID (PM_MMIO_READ ):gprintk (XENLOG_WARNING ,"zynqmp-pm: fn=%u No MMIO access to %u\n" , pm_fn, nodeid); - ret =XST_PM_NO_ACCESS ; goto done;/* Exclusive to the hardware domain. */
@@ -146,14 +143,12 @@ bool zynqmp_eemi(struct cpu_user_regs *regs)if ( !is_hardware_domain (current->domain) ) {gprintk (XENLOG_WARNING ,"eemi: fn=%u No access" , pm_fn); - ret =XST_PM_NO_ACCESS ; goto done; } goto forward_to_fw;/* These calls are never allowed. */ case EEMI_FID (PM_SYSTEM_SHUTDOWN ): - ret =XST_PM_NO_ACCESS ; goto done;case IPI_MAILBOX_FID (IPI_MAILBOX_OPEN ):
@@ -166,7 +161,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs)if ( !is_hardware_domain (current->domain) ) {gprintk (XENLOG_WARNING ,"IPI mailbox: fn=%u No access" , pm_fn); - ret =XST_PM_NO_ACCESS ; goto done; } goto forward_to_fw;
xen/arch/arm/psci.c +5 -5
@@ -38,7 +38,7 @@ static uint32_t psci_cpu_on_nr;int call_psci_cpu_on (int cpu) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };arm_smccc_smc (psci_cpu_on_nr,cpu_logical_map (cpu),__pa (init_secondary), &res);
@@ -50,7 +50,7 @@ void call_psci_cpu_off(void) {if ( psci_ver >PSCI_VERSION (0 ,1 ) ) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };/* If successfull the PSCI cpu_off call doesn't return */ arm_smccc_smc (PSCI_0_2_FN32_CPU_OFF , &res);
@@ -73,7 +73,7 @@ void call_psci_system_reset(void)static int __init psci_features (uint32_t psci_func_id) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };if ( psci_ver <PSCI_VERSION (1 ,0 ) )return PSCI_NOT_SUPPORTED ;
@@ -115,7 +115,7 @@ static void __init psci_init_smccc(void)if (psci_features (ARM_SMCCC_VERSION_FID ) !=PSCI_NOT_SUPPORTED ) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };arm_smccc_smc (ARM_SMCCC_VERSION_FID , &res);if (PSCI_RET (res) !=ARM_SMCCC_NOT_SUPPORTED )
@@ -168,7 +168,7 @@ static int __init psci_init_0_2(void) { /* sentinel */ }, };int ret; -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };if ( acpi_disabled ) {
xen/drivers/char/pl011.c +1 -1
@@ -285,7 +285,7 @@ static int __init pl011_dt_uart_init(struct dt_device_node *dev,const char *config = data;int res;paddr_t addr, size; -uint32_t io_width; +uint32_t io_width =0 ;bool mmio32 = false, sbsa;if (strcmp (config,"" ) )
Signature · 1 line
2.34.1
RFC PATCH 2/4 xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Nicola VetriniThe macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro 'guest_testop'.
--- docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)
docs/misra/safe.json +16 -0
@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-4-safe", "analyser": {}, "name": "Sentinel", "text": "Next ID to be used"
xen/arch/arm/arm64/lib/bitops.c +3 -0
@@ -114,8 +114,11 @@ bitop(change_bit, eor) bitop(clear_bit, bic)bitop (set_bit, orr) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_change_bit, eor) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_clear_bit, bic) +/* SAF-2-safe MC3R1.R9.1 */ testop(test_and_set_bit, orr) static always_inline bool int_clear_mask16(uint16_t mask, volatile uint16_t *p,
xen/arch/arm/include/asm/guest_atomics.h +3 -0
@@ -67,8 +67,11 @@ guest_bitop(change_bit)/* test_bit does not use load-store atomic operations */ #define guest_test_bit (d, nr, p) ((void)(d), test_bit(nr, p)) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_set_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_clear_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_change_bit) #undef guest_testop
Signature · 1 line
2.34.1
RFC PATCH 4/4 xen/arm: initialize conditionally uninitialized local variables
In reply to Nicola VetriniThe function 'guest_walk_tables' contains an initialization pattern for the pointee of parameter 'perms' that is not easy for automatic checkers to reason about.
A modified pattern that does not alter the semantics of the code is introduced.
A const qualifier is added in 'xen/arch/arm/dm.c' because 'copy_to_guest_offset' doesn't modify that parameter.
--- xen/arch/arm/dm.c | 2 +- xen/arch/arm/guest_walk.c | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-)
xen/arch/arm/dm.c +1 -1
@@ -129,7 +129,7 @@ int dm_op(const struct dmop_args *op_args)if ( (!rc || rc == -ERESTART ) && !const_op &©_to_guest_offset (op_args->buf[0 ].h, offset, - (void *)&op.u, op_size[op.op]) ) + (const void *)&op.u, op_size[op.op]) ) rc = -EFAULT ; out:
xen/arch/arm/guest_walk.c +7 -5
@@ -552,15 +552,12 @@ bool guest_walk_tables(const struct vcpu *v, vaddr_t gva, {register_t sctlr =READ_SYSREG (SCTLR_EL1 );register_t tcr =READ_SYSREG (TCR_EL1 ); -unsigned int _perms; +unsigned int _perms =GV2M_READ ;/* We assume that the domain is running on the currently active domain. */ if ( v != current )return false; -/* Allow perms to be NULL. */ - perms = perms ?: &_perms; -/* * Currently, we assume a GVA to IPA translation with EL1 privileges. * Since, valid mappings in the first stage address translation table are
@@ -570,7 +567,12 @@ bool guest_walk_tables(const struct vcpu *v, vaddr_t gva,* attributes that distinguish between EL0 and EL1 permissions (EL0 might * not have permissions on the particular mapping). */ - *perms =GV2M_READ ; +/* Allow perms to be NULL. */ +if ( perms ) { + *perms = _perms; + }else { + perms = &_perms; + }/* If the MMU is disabled, there is no need to translate the gva. */ if ( !(sctlr & SCTLR_Axx_ELx_M) )
Signature · 1 line
2.34.1
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Nicola VetriniHi Nicola,
Quoted 10 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
To achieve this goal, I adopted the following strategies:
Please let's at least one patch per strategy. I would also consider some of the rework separate so they can go in regardless the decision for the SAF-*.
Quoted 9 lines
- Add a suitably formatted local deviation comment (as indicated in '
docs/misra/documenting-violations.rst') to exempt the following line from checking.- Provide an initialization for the variable at the declaration.
- Substitute a goto breaking out of control flow logic with a semantically equivalent do { .. } while(0).
As I already mentioned in private, it is unclear to me how you decided which strategy to use. I still think we need to define our policy before changing the code. Otherwise, it is going to be difficult to decide for new code.
Quoted 19 lines
Signed-off-by Nicola Vetrini <[email protected]>--- docs/misra/safe.json | 8 +++++++ xen/arch/arm/arm64/lib/find_next_bit.c | 1 + xen/arch/arm/bootfdt.c | 6 +++++ xen/arch/arm/decode.c | 2 ++ xen/arch/arm/domain_build.c | 29 ++++++++++++++++++---- xen/arch/arm/efi/efi-boot.h | 6 +++-- xen/arch/arm/gic-v3-its.c | 9 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33 +++++++++++++++----------- 9 files changed, 69 insertions(+), 26 deletions(-)docs/misra/safe.json +0
-0
docs/misra/safe.json line 19
Quoted 8 lines
docs/misra/safe.json +5
-0@@ -12,6 +12,14 @@ }, { "id": "SAF-1-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen."
I am bit concerned which such statement because the code instance was today with the current code. This could change in the future and invalide the reasoning.
It is not clear to me if we have any mechanism to prevent that. If we don't, then I think we need to drastically reduce the number of time this is used (there are a bit too much for my taste).
Quoted 11 lines
docs/misra/safe.json +3
-0+ }, + { + "id": "SAF-2-safe", "analyser": {}, "name": "Sentinel", "text": "Next ID to be used"xen/arch/arm/arm64/lib/find_next_bit.c +0
-0
xen/arch/arm/arm64/lib/find_next_bit.c unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, · line 71
Quoted 5 lines
xen/arch/arm/arm64/lib/find_next_bit.c +1
-0@@ -67,6 +67,7 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, {const unsigned long *p = addr +BIT_WORD (offset);unsigned long result = offset & ~(BITS_PER_LONG -1 ); +/* SAF-1-safe MC3R1.R9.1 */ unsigned long tmp;
This is a file that was copied as-is from Linux. I thought we exclude them.
Anyway, can you provide some details why Eclair thinks this is may be used unitialized?
xen/arch/arm/bootfdt.c static bool __init device_tree_node_matches(const void *fdt, int node, · line 38
Quoted 12 lines
xen/arch/arm/arm64/lib/find_next_bit.c +0
-0if (offset >= size)xen/arch/arm/bootfdt.c +1
-0@@ -34,6 +34,7 @@ static bool __init device_tree_node_matches(const void *fdt, int node,static bool __init device_tree_node_compatible (const void *fdt,int node,const char *match) { +/* SAF-1-safe MC3R1.R9.1 */ int len, l;
It is unclear to me whether the SAF-* applies to 'len' or 'l'.
xen/arch/arm/bootfdt.c int __init device_tree_for_each_node(const void *fdt, int node, · line 176
Quoted 10 lines
xen/arch/arm/bootfdt.c +2
-0const void *prop;@@ -169,7 +170,9 @@ int __init device_tree_for_each_node(const void *fdt, int node,*/ int depth =0 ;const int first_node = node; +/* SAF-1-safe MC3R1.R9.1 */ u32 address_cells[DEVICE_TREE_MAX_DEPTH ]; +/* SAF-1-safe MC3R1.R9.1 */ u32 size_cells[DEVICE_TREE_MAX_DEPTH ];
I have never sent formally, but in the past I proposed to rework the code so {address, size}_cells[0] would be initialized before the loop. See https://lore.kernel.org/all/[email protected]/.
Do you think this would help?
xen/arch/arm/bootfdt.c static void __init process_multiboot_node(const void *fdt, int node, · line 259
Quoted 11 lines
xen/arch/arm/bootfdt.c +2
-0int ret;@@ -249,8 +252,10 @@ static void __init process_multiboot_node(const void *fdt, int node,const __be32 *cell;bootmodule_kind kind;paddr_t start, size; +/* SAF-1-safe MC3R1.R9.1 */ int len;/* sizeof("/chosen/") + DT_MAX_NAME + '/' + DT_MAX_NAME + '/0' => 92 */ +/* SAF-1-safe MC3R1.R9.1*/ char path[92 ];
So the two above, is one category of issue. The variables are passed as argument of function which will fill them.
Can Eclair look at the callers, if so, can we consider to always initialize the values in the callee?
This would reduce the number of SAF-*. There are a few other examples like that below. So I will skip them for now.
[...]
xen/arch/arm/domain_build.c custom_param("dom0_mem", parse_dom0_mem); · line 65
Quoted 10 lines
xen/arch/arm/domain_build.c +1
-1@@ -62,7 +62,7 @@ custom_param("dom0_mem", parse_dom0_mem);int __init parse_arch_dom0_param (const char *s,const char *e) { -long long val; +long long val =LLONG_MAX ;
Can you explain why you decided to initialize rather than SAF-? For clarity, I am not asking to switch to SAF-, I am mainly interested what were your though process.
Quoted 120 lines
xen/arch/arm/domain_build.c +16
-0if ( !parse_signed_integer ("sve" , s, e, &val) ) {@@ -1077,6 +1077,7 @@ static void __init assign_static_memory_11(struct domain *d,static int __init handle_linux_pci_domain (struct kernel_info *kinfo,const struct dt_device_node *node) { +/* SAF-1-safe MC3R1.R9.1 */ uint16_t segment;int res;@@ -1351,6 +1352,7 @@ static int __init make_memory_node(const struct domain *d,unsigned int i;int res, reg_size = addrcells + sizecells;int nr_cells =0 ; +/* SAF-1-safe MC3R1.R9.1*/ __be32 reg[NR_MEM_BANKS *4 /* Worst case addrcells + sizecells */ ];__be32 *cells;@@ -1578,6 +1580,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo,struct rangeset *unalloc_mem;paddr_t start, end;unsigned int i; +/* SAF-1-safe MC3R1.R9.1 */ int res;dt_dprintk ("Find unallocated memory for extended regions\n" );@@ -1727,6 +1730,7 @@ static int __init find_memory_holes(const struct kernel_info *kinfo,dt_for_each_device_node ( dt_host, np ) {unsigned int naddr; +/* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; naddr =dt_number_of_address (np);@@ -1976,9 +1980,11 @@ static int __init make_cpus_node(const struct domain *d, void *fdt)const struct dt_device_node *npcpu;unsigned int cpu;const void *compatible =NULL ; +/* SAF-1-safe MC3R1.R9.1 */ u32 len;/* Placeholder for cpu@ + a 32-bit hexadecimal number + \0 */ char buf[13 ]; +/* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency;/* Keep the compiler happy with -Og */ bool clock_valid = false;@@ -2104,6 +2110,7 @@ static int __init make_gic_node(const struct domain *d, void *fdt,const struct dt_device_node *gic = dt_interrupt_controller;int res =0 ;const void *addrcells, *sizecells; +/* SAF-1-safe MC3R1.R9.1 */ u32 addrcells_len, sizecells_len;/* @@ -2179,6 +2186,7 @@ static int __init make_timer_node(const struct kernel_info *kinfo)int res;unsigned int irq[MAX_TIMER_PPI ];gic_interrupt_t intrs[3 ]; +/* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency;bool clock_valid;@@ -2511,6 +2519,7 @@ static int __init handle_device(struct domain *d, struct dt_device_node *dev,unsigned int naddr;unsigned int i;int res; +/* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size;bool own_device = !dt_device_for_passthrough (dev);/* @@ -2779,6 +2788,7 @@ static int __init make_gicv2_domU_node(struct kernel_info *kinfo) {void *fdt = kinfo->fdt;int res =0 ; +/* SAF-1-safe MC3R1.R9.1*/ __be32 reg[(GUEST_ROOT_ADDRESS_CELLS +GUEST_ROOT_SIZE_CELLS ) *2 ];__be32 *cells;const struct domain *d = kinfo->d;@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo)void *fdt = kinfo->fdt;int res;gic_interrupt_t intr; +/* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS +GUEST_ROOT_SIZE_CELLS ];__be32 *cells;struct domain *d = kinfo->d;@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo)paddr_t paddr, len;int node;int res; +/* SAF-1-safe MC3R1.R9.1 */ __be32 val[2 ];__be32 *cellp;void __iomem *initrd;@@ -3514,6 +3526,7 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np, uint32_t *port, uint32_t *phandle) { const __be32 *prop = NULL; +/* SAF-1-safe MC3R1.R9.1 */ uint32_t len; prop =dt_get_property (np,"xen,evtchn" , &len);@@ -3538,10 +3551,13 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np,static int __init alloc_domain_evtchn (struct dt_device_node *node) {int rc; +/* SAF-1-safe MC3R1.R9.1 */ uint32_t domU1_port, domU2_port, remote_phandle;struct dt_device_node *remote_node;const struct dt_device_node *p1_node, *p2_node; +/* SAF-1-safe MC3R1.R9.1 */ struct evtchn_alloc_unbound alloc_unbound; +/* SAF-1-safe MC3R1.R9.1 */ struct evtchn_bind_interdomain bind_interdomain;struct domain *d1 =NULL , *d2 =NULL ;@@ -3789,11 +3805,12 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo)static int __init alloc_xenstore_evtchn (struct domain *d)
xen/arch/arm/domain_build.c static int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 3796
Quoted 10 lines
xen/arch/arm/domain_build.c +4
-3{ -evtchn_alloc_unbound_t alloc; +evtchn_alloc_unbound_t alloc = { + .dom = d->domain_id, + .remote_dom = hardware_domain->domain_id + };int rc; - alloc.dom = d->domain_id; - alloc.remote_dom = hardware_domain->domain_id;
It is not clear why this is modified. There are only two fields in 'alloc' and they are both initialized.
xen/arch/arm/domain_build.c static int __init construct_domU(struct domain *d, · line 3830
Quoted 9 lines
xen/arch/arm/domain_build.c +1
-1rc =evtchn_alloc_unbound (&alloc,0 );if ( rc ) {@@ -3810,8 +3827,9 @@ static int __init construct_domU(struct domain *d, const struct dt_device_node *node) {struct kernel_info kinfo = {}; -const char *dom0less_enhanced; +const char *dom0less_enhanced =NULL ;
If you look at the user below, all the callers assume dom0less_enhanced will be non-NULL. So it is unclear to me how this value is safer.
Looking at the code, I wonder whether we should convert dt_property_read_string() to use ERR_PTR(). So we could remove the last argument and return it instead.
Quoted 56 lines
xen/arch/arm/domain_build.c +2
-0int rc; +/* SAF-1-safe MC3R1.R9.1 */ u64 mem;u32 p2m_mem_mb;unsigned long p2m_pages;@@ -3939,6 +3957,7 @@ void __init create_domUs(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), };unsigned int flags =0U ; +/* SAF-1-safe MC3R1.R9.1 */ uint32_t val;int rc;xen/arch/arm/efi/efi-boot.h +4
-2@@ -117,6 +117,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells)static int __init fdt_set_reg (void *fdt,int node,int addr_cells,int size_cells,uint64_t addr,uint64_t len) { +/* SAF-1-safe MC3R1.R9.1 */ __be32 val[4 ];/* At most 2 64 bit values to be stored */ __be32 *cellp;@@ -308,7 +309,7 @@ fdt_set_fail:static void __init *fdt_increase_size (struct file *fdtfile,int add_size) {EFI_STATUS status; -EFI_PHYSICAL_ADDRESS fdt_addr; +EFI_PHYSICAL_ADDRESS fdt_addr =0 ;int fdt_size;int pages;void *new_fdt;@@ -433,7 +434,7 @@ static void __init efi_arch_cfg_file_late(const EFI_LOADED_IMAGE *image,static void *__init efi_arch_allocate_mmap_buffer (UINTN map_size) { -void *ptr; +void *ptr =NULL ;EFI_STATUS status; status = efi_bs->AllocatePool (EfiLoaderData, map_size, &ptr);@@ -538,6 +539,7 @@ static void __init efi_arch_handle_module(const struct file *file, {int node;int chosen; +/* SAF-1-safe MC3R1.R9.1 */ int addr_len, size_len;if ( file == &dtbfile )xen/arch/arm/gic-v3-its.c +0
-0
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 195
Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1
-2@@ -192,8 +192,7 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, cmd[0 ] =GITS_CMD_MAPC ; cmd[1 ] =0x00 ; - cmd[2 ] =encode_rdbase (its, cpu, collection_id); - cmd[2 ] |=GITS_VALID_BIT ; + cmd[2 ] =encode_rdbase (its, cpu, collection_id) |GITS_VALID_BIT ;
Hmmm... How is this even considered as unitialized variable?
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, · line 217
Quoted 11 lines
xen/arch/arm/gic-v3-its.c +1
-3cmd[3 ] =0x00 ;return its_send_command (its, cmd);@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, } cmd[0 ] =GITS_CMD_MAPD | ((uint64_t )deviceid <<32 ); cmd[1 ] = size_bits; - cmd[2 ] = itt_addr; -if ( valid ) - cmd[2 ] |=GITS_VALID_BIT ; + cmd[2 ] = itt_addr | (valid ?GITS_VALID_BIT :0x00 );
Same here.
Quoted 26 lines
xen/arch/arm/mm.c +1
-0@@ -935,6 +935,7 @@ static int xen_pt_update_entry(mfn_t root, unsigned long virt, mfn_t mfn, unsigned int target, unsigned int flags) { + /* SAF-1-safe MC3R1.R9.1 */ int rc;unsigned int level;lpae_t *table;xen/arch/arm/p2m.c +1
-1@@ -496,16 +496,18 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,lpae_t entry, *table;int rc;mfn_t mfn =INVALID_MFN ; -p2m_type_t _t; +p2m_type_t _t = p2m_invalid;DECLARE_OFFSETS (offsets, addr);ASSERT (p2m_is_locked (p2m));BUILD_BUG_ON (THIRD_MASK !=PAGE_MASK );
xen/arch/arm/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, · line 510
Quoted 10 lines
xen/arch/arm/p2m.c +5
-3/* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; +if ( t ) { + *t = _t; + }else { + t = &_t; + }
What was the problem with the previous code?
This is also not conformant to Xen coding style.
xen/arch/arm/p2m.c static int __p2m_set_entry(struct p2m_domain *p2m, · line 1037
Quoted 9 lines
xen/arch/arm/p2m.c +1
-0if ( valid ) *valid = false;@@ -1031,6 +1033,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m,unsigned int level =0 ;unsigned int target =3 - (page_order /XEN_PT_LPAE_SHIFT );lpae_t *entry, *table, orig_pte; +/* SAF-1-safe MC3R1.R9.1 */ int rc;
Can you provide some details why Eclair thinks it is unitialized?
xen/arch/arm/p2m.c static inline int p2m_remove_mapping(struct domain *d, · line 1490
Quoted 8 lines
xen/arch/arm/p2m.c +1
-0/* A mapping is removed if the MFN is invalid. */ bool removing_mapping =mfn_eq (smfn,INVALID_MFN );@@ -1483,6 +1486,7 @@ static inline int p2m_remove_mapping(struct domain *d, {struct p2m_domain *p2m =p2m_get_hostp2m (d);unsigned long i; +/* SAF-1-safe MC3R1.R9.1 */ int rc;
Can you provide some details why Eclair thinks it is unitialized?
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1692
Quoted 13 lines
xen/arch/arm/p2m.c +1
-6p2m_write_lock (p2m);@@ -1685,20 +1689,21 @@ static int p2m_alloc_vmid(struct domain *d)ASSERT (nr !=INVALID_VMID ); -if ( nr ==MAX_VMID ) - { - rc = -EBUSY ; -printk (XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n" , d->domain_id); - goto out; - } + do {
I don't understand this change. How is this making better for Eclair?
Quoted 21 lines
xen/arch/arm/p2m.c +10
-4+if ( nr ==MAX_VMID ) + { + rc = -EBUSY ; +printk (XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n" , d->domain_id); +break ; + } -set_bit (nr, vmid_mask); +set_bit (nr, vmid_mask); - p2m->vmid = nr; + p2m->vmid = nr; - rc =0 ; + rc =0 ; + }while (0 ); -out:spin_unlock (&vmid_alloc_lock);return rc; }
Signature · 1 line
Julien Grall
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Nicola VetriniHi,
Quoted 3 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
The code is pretty straightforward. So I am not entirely sure why Eclair is not happy. Is it because the value is set by assembly code?
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 3/4] xen/arm: initialize conditionally uninitialized local variables
In reply to Nicola VetriniHi,
Quoted 31 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
An initialization to a safe value is provided to reach this aim.
Signed-off-by Nicola Vetrini <[email protected]>---
Additional input on which values may be 'safe' in each context is surely welcome, to avoid possibly compromising the correctness of the function semantics.--- xen/arch/arm/cpuerrata.c | 6 +++--- xen/arch/arm/domctl.c | 8 ++++---- xen/arch/arm/gic-v3-lpi.c | 17 +++++++++-------- xen/arch/arm/include/asm/p2m.h | 10 ++++++---- xen/arch/arm/platforms/xilinx-zynqmp-eemi.c | 10 ++-------- xen/arch/arm/psci.c | 10 +++++----- xen/drivers/char/pl011.c | 2 +- 7 files changed, 30 insertions(+), 33 deletions(-)xen/arch/arm/cpuerrata.c +0
-0
xen/arch/arm/cpuerrata.c extern char __mitigate_spectre_bhb_loop_start_32[], · line 162
Quoted 5 lines
xen/arch/arm/cpuerrata.c +1
-1@@ -159,7 +159,7 @@ extern char __mitigate_spectre_bhb_loop_start_32[],static int enable_smccc_arch_workaround_1 (void *data) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
I understand you desire to make happy. But I am not sure that initializing to 0 is the right thing. If the SMCC were not properly setting the register, then we most likely don't want to install the workaround. Instead, we most likely want to warn.
So you want (int)res.a0 to be negative. We don't care about the other fields.
xen/arch/arm/cpuerrata.c static int enable_spectre_bhb_workaround(void *data) · line 255
Quoted 9 lines
xen/arch/arm/cpuerrata.c +1
-1const struct arm_cpu_capabilities *entry = data;/* @@ -252,7 +252,7 @@ static int enable_spectre_bhb_workaround(void *data)if (cpus_have_cap (ARM_WORKAROUND_BHB_SMCC_3 ) ) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
Same remark here.
xen/arch/arm/cpuerrata.c DEFINE_PER_CPU_READ_MOSTLY(register_t, ssbd_callback_required); · line 396
Quoted 9 lines
xen/arch/arm/cpuerrata.c +1
-1if ( smccc_ver <SMCCC_VERSION (1 ,1 ) ) goto warn;@@ -393,7 +393,7 @@ DEFINE_PER_CPU_READ_MOSTLY(register_t, ssbd_callback_required);static bool has_ssbd_mitigation (const struct arm_cpu_capabilities *entry) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
Here you would want (int)res.a0 to be equal to ARM_SMCCC_NOT_SUPPORTED.
Quoted 9 lines
xen/arch/arm/cpuerrata.c +0
-0bool required;if ( smccc_ver <SMCCC_VERSION (1 ,1 ) )xen/arch/arm/domctl.c +0
-0@@ -29,10 +29,10 @@ static int handle_vuart_init(struct domain *d, struct xen_domctl_vuart_op *vuart_op)
xen/arch/arm/domctl.c static int handle_vuart_init(struct domain *d, · line 35
Quoted 10 lines
xen/arch/arm/domctl.c +4
-4{int rc; -struct vpl011_init_info info; - - info.console_domid = vuart_op->console_domid; - info.gfn =_gfn (vuart_op->gfn); +struct vpl011_init_info info = { + .console_domid = vuart_op->console_domid, + .gfn =_gfn (vuart_op->gfn) + };
I am not against, this change. But I don't quite understand how this makes Eclair much happier?
Also, if this is the desired way, then I think this should be written down in the CODING_STYLE.
Quoted 65 lines
xen/arch/arm/domctl.c +0
-0if ( d->creation_finished )return -EPERM ;xen/arch/arm/gic-v3-lpi.c +9
-8@@ -210,7 +210,10 @@ out:void gicv3_lpi_update_host_entry (uint32_t host_lpi,int domain_id,uint32_t virt_lpi) { -union host_lpi *hlpip, hlpi; +union host_lpi *hlpip, hlpi = { + .virt_lpi = virt_lpi, + .dom_id = domain_id + };ASSERT (host_lpi >=LPI_OFFSET );@@ -218,9 +221,6 @@ void gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id, hlpip = &lpi_data.host_lpis[host_lpi /HOST_LPIS_PER_PAGE ][host_lpi %HOST_LPIS_PER_PAGE ]; - hlpi.virt_lpi = virt_lpi; - hlpi.dom_id = domain_id; -write_u64_atomic (&hlpip->data, hlpi.data); }@@ -542,14 +542,15 @@ int gicv3_allocate_host_lpi_block(struct domain *d, uint32_t *first_lpi)for ( i =0 ; i <LPI_BLOCK ; i++ ) { -union host_lpi hlpi; -/* * Mark this host LPI as belonging to the domain, but don't assign * any virtual LPI or a VCPU yet. */ - hlpi.virt_lpi =INVALID_LPI ; - hlpi.dom_id = d->domain_id; +union host_lpi hlpi = { + .virt_lpi =INVALID_LPI , + .dom_id = d->domain_id + }; +write_u64_atomic (&lpi_data.host_lpis[chunk][lpi_idx + i].data, hlpi.data);xen/arch/arm/include/asm/p2m.h +1
-1@@ -345,7 +345,7 @@ static inline struct page_info *get_page_from_gfn( struct domain *d, unsigned long gfn, p2m_type_t *t, p2m_query_t q) { mfn_t mfn; -p2m_type_t _t; +p2m_type_t _t = p2m_invalid;struct page_info *page;/* @@ -355,10 +355,12 @@ static inline struct page_info *get_page_from_gfn(if (likely (d != dom_xen) )return p2m_get_page_from_gfn (d,_gfn (gfn), t);
xen/arch/arm/include/asm/p2m.h static inline struct page_info *get_page_from_gfn( · line 363
Quoted 10 lines
xen/arch/arm/include/asm/p2m.h +5
-3-if ( !t ) +/* Allow t to be NULL */ +if ( t ) + *t = _t; +else { t = &_t; - - *t = p2m_invalid; + }
I understand the exist construct is not liked by Eclair. But your new construct is not easily readable by a human. Before I can propose a different approach, can you clarify why Eclair is unhappy this the existing code?
Quoted 7 lines
xen/arch/arm/include/asm/p2m.h +0
-0/* * DOMID_XEN sees 1-1 RAM. The p2m_type is based on the type of the xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-0
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c static inline bool domain_has_reset_access(struct domain *d, uint32_t rst) · line 58
Quoted 10 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +2
-2@@ -51,11 +51,11 @@ static inline bool domain_has_reset_access(struct domain *d, uint32_t rst)bool zynqmp_eemi (struct cpu_user_regs *regs) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };uint32_t fid =get_user_reg (regs,0 );uint32_t nodeid =get_user_reg (regs,1 );unsigned int pm_fn = fid &0xFFFF ; -enum pm_ret_status ret; +enum pm_ret_status ret =XST_PM_NO_ACCESS ;
It is not clear to me why Eclair is unhappy here?
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c bool zynqmp_eemi(struct cpu_user_regs *regs) · line 92
Quoted 9 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-1switch ( fid ) {@@ -89,7 +89,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs) {gprintk (XENLOG_WARNING ,"zynqmp-pm: fn=%u No access to node %u\n" , pm_fn, nodeid); - ret =XST_PM_NO_ACCESS ; goto done;
I find the new approach less obvious. How about introduce a macro that will set the result and the use replace the two lines with:
set_result(regs, XST_PM_NO_ACCESS);
return 0;
where set_result(regs, ret) is
set_user_reg(regs, 0, ret)
xen/arch/arm/psci.c static uint32_t psci_cpu_on_nr; · line 41
Quoted 10 lines
xen/arch/arm/psci.c +1
-1@@ -38,7 +38,7 @@ static uint32_t psci_cpu_on_nr;int call_psci_cpu_on (int cpu) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
I don't this we should initialize a0 to 0 as this would indicate a success. Instead, we want an invalid value.
xen/arch/arm/psci.c void call_psci_cpu_off(void) · line 53
Quoted 9 lines
xen/arch/arm/psci.c +1
-1arm_smccc_smc (psci_cpu_on_nr,cpu_logical_map (cpu),__pa (init_secondary), &res);@@ -50,7 +50,7 @@ void call_psci_cpu_off(void) {if ( psci_ver >PSCI_VERSION (0 ,1 ) ) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
Same here.
xen/arch/arm/psci.c void call_psci_system_reset(void) · line 76
Quoted 9 lines
xen/arch/arm/psci.c +1
-1/* If successfull the PSCI cpu_off call doesn't return */ arm_smccc_smc (PSCI_0_2_FN32_CPU_OFF , &res);@@ -73,7 +73,7 @@ void call_psci_system_reset(void)static int __init psci_features (uint32_t psci_func_id) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
Same here.
xen/arch/arm/psci.c static void __init psci_init_smccc(void) · line 118
Quoted 9 lines
xen/arch/arm/psci.c +1
-1if ( psci_ver <PSCI_VERSION (1 ,0 ) )return PSCI_NOT_SUPPORTED ;@@ -115,7 +115,7 @@ static void __init psci_init_smccc(void)if (psci_features (ARM_SMCCC_VERSION_FID ) !=PSCI_NOT_SUPPORTED ) { -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
Same here.
xen/arch/arm/psci.c static int __init psci_init_0_2(void) · line 171
Quoted 9 lines
xen/arch/arm/psci.c +1
-1arm_smccc_smc (ARM_SMCCC_VERSION_FID , &res);if (PSCI_RET (res) !=ARM_SMCCC_NOT_SUPPORTED )@@ -168,7 +168,7 @@ static int __init psci_init_0_2(void) { /* sentinel */ }, };int ret; -struct arm_smccc_res res; +struct arm_smccc_res res = {0 };
Same here.
xen/drivers/char/pl011.c static int __init pl011_dt_uart_init(struct dt_device_node *dev, · line 288
Quoted 13 lines
xen/arch/arm/psci.c +0
-0if ( acpi_disabled ) {xen/drivers/char/pl011.c +1
-1@@ -285,7 +285,7 @@ static int __init pl011_dt_uart_init(struct dt_device_node *dev,const char *config = data;int res;paddr_t addr, size; -uint32_t io_width; +uint32_t io_width =0 ;
We have many use of dt_property_read_*() within the code base. I would like us te decide of a policy first.
xen/drivers/char/pl011.c static int __init pl011_dt_uart_init(struct dt_device_node *dev, · line 291
Quoted 3 lines
xen/drivers/char/pl011.c +0
-0bool mmio32 = false, sbsa;if (strcmp (config,"" ) )
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Nicola VetriniQuoted 31 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)docs/misra/safe.json +4
-0@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed",
docs/misra/safe.json line 36
Quoted 10 lines
docs/misra/safe.json +10
-0+ "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },
Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */, also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Quoted 41 lines
docs/misra/safe.json +2
-0+ { + "id": "SAF-4-safe", "analyser": {}, "name": "Sentinel", "text": "Next ID to be used"xen/arch/arm/arm64/lib/bitops.c +3
-0@@ -114,8 +114,11 @@ bitop(change_bit, eor) bitop(clear_bit, bic)bitop (set_bit, orr) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_change_bit, eor) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_clear_bit, bic) +/* SAF-2-safe MC3R1.R9.1 */ testop(test_and_set_bit, orr) static always_inline bool int_clear_mask16(uint16_t mask, volatile uint16_t *p,xen/arch/arm/include/asm/guest_atomics.h +3
-0@@ -67,8 +67,11 @@ guest_bitop(change_bit)/* test_bit does not use load-store atomic operations */ #define guest_test_bit (d, nr, p) ((void)(d), test_bit(nr, p)) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_set_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_clear_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_change_bit) #undef guest_testopSignature · 1 line
2.34.1
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
Quoted 40 lines · Luca Fancellu
Quoted 37 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)docs/misra/safe.json +9
-0@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe", + "analyser": {
Quoted 8 lines
docs/misra/safe.json line 28
Quoted 5 lines
docs/misra/safe.json +5
-0+ "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */, also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Sorry, I see there was in a patch before a SAF-1-safe with the same justification, so I suggest you use SAF-3-safe as tag and drop the new justifications introduced here
Quoted 43 lines
Quoted 42 lines
docs/misra/safe.json +2
-0+ { + "id": "SAF-4-safe", "analyser": {}, "name": "Sentinel", "text": "Next ID to be used"xen/arch/arm/arm64/lib/bitops.c +3
-0@@ -114,8 +114,11 @@ bitop(change_bit, eor) bitop(clear_bit, bic)bitop (set_bit, orr) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_change_bit, eor) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_clear_bit, bic) +/* SAF-2-safe MC3R1.R9.1 */ testop(test_and_set_bit, orr) static always_inline bool int_clear_mask16(uint16_t mask, volatile uint16_t *p,xen/arch/arm/include/asm/guest_atomics.h +3
-0@@ -67,8 +67,11 @@ guest_bitop(change_bit)/* test_bit does not use load-store atomic operations */ #define guest_test_bit (d, nr, p) ((void)(d), test_bit(nr, p)) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_set_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_clear_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_change_bit) #undef guest_testopSignature · 1 line
2.34.1
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
"id": "SAF-2-safe",docs/misra/safe.json +14
-0+ "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },
Quoted 4 lines
Quoted 2 lines
Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */, also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Sorry, I see there was in a patch before a SAF-1-safe with the same justification, so I suggest you use SAF-3-safe as tag and drop the new justifications introduced here
I meant “use SAF-1-safe”, sorry, I should do less multitasking
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Luca FancelluQuoted 49 lines · Luca Fancellu
Quoted 40 lines · Luca Fancellu
Quoted 37 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)docs/misra/safe.json +9
-0@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe", + "analyser": {Quoted 8 lines
docs/misra/safe.json line 28
Quoted 5 lines
docs/misra/safe.json +5
-0+ "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */, also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Quoted 1 line
Sorry, I see there was in a patch before a SAF-1-safe with the same justification, so I suggest you use SAF-3-safe as tag and drop the new justifications introduced here
I was a bit uncertain in choosing whether to use different IDs for bitops.c and guest_atomics.h, as they are very similar. Maybe a catch-all justification is too broad, but I have no strong opinion here.
As for the rule identifier after the tag, that's not a big problem (I added it mostly to emphasize why the deviation is there at a glance).
Quoted 45 lines
Quoted 44 lines
Quoted 43 lines
docs/misra/safe.json +2
-0+ { + "id": "SAF-4-safe", "analyser": {}, "name": "Sentinel", "text": "Next ID to be used"xen/arch/arm/arm64/lib/bitops.c +3
-0@@ -114,8 +114,11 @@ bitop(change_bit, eor) bitop(clear_bit, bic)bitop (set_bit, orr) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_change_bit, eor) +/* SAF-2-safe MC3R1.R9.1 */testop (test_and_clear_bit, bic) +/* SAF-2-safe MC3R1.R9.1 */ testop(test_and_set_bit, orr) static always_inline bool int_clear_mask16(uint16_t mask, volatile uint16_t *p,xen/arch/arm/include/asm/guest_atomics.h +3
-0@@ -67,8 +67,11 @@ guest_bitop(change_bit)/* test_bit does not use load-store atomic operations */ #define guest_test_bit (d, nr, p) ((void)(d), test_bit(nr, p)) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_set_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_clear_bit) +/* SAF-3-safe MC3R1.R9.1 */ guest_testop(test_and_change_bit) #undef guest_testopSignature · 1 line
2.34.1
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Julien GrallQuoted 12 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
The code is pretty straightforward. So I am not entirely sure why Eclair is not happy. Is it because the value is set by assembly code?
Hi Julien,
Yes I agree that it’s strange, do you think that that if at line 97 we initialize oldbit to zero, it would solve the issue? @Nicola
Cheers,
Luca
Quoted 6 lines
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
Hi,
Quoted 9 lines · Luca Fancellu
Quoted 9 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Quoted 5 lines
Quoted 1 line
The code is pretty straightforward. So I am not entirely sure why Eclair is not happy. Is it because the value is set by assembly code?
Hi Julien,
Yes I agree that it’s strange, do you think that that if at line 97 we initialize oldbit to zero, it would solve the issue?
At the moment, I am not entirely happy to zero oldbit because we have other places in Xen where we need assembly instructions to read a value (e.g. READ_SYSREG()).
So I would first like to understand why Eclair can't cope with it.
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Julien GrallQuoted 21 lines · Julien Grall
Hi Nicola,
Quoted 10 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
To achieve this goal, I adopted the following strategies:
Please let's at least one patch per strategy. I would also consider some of the rework separate so they can go in regardless the decision for the SAF-*.
Quoted 5 lines
- Add a suitably formatted local deviation comment (as indicated in '
docs/misra/documenting-violations.rst') to exempt the following line from checking.
Quoted 8 lines
Quoted 5 lines
- Provide an initialization for the variable at the declaration.
- Substitute a goto breaking out of control flow logic with a semantically
equivalent do { .. } while(0).As I already mentioned in private, it is unclear to me how you decided which strategy to use. I still think we need to define our policy before changing the code. Otherwise, it is going to be difficult to decide for new code.
The main point of this RFC is doing so. From what I gathered, it's not an easy task: sometimes there are no 'safe' values to initialize variables to and sometimes there is no easy way to prove that indeed something is always initialized or not accessed at all.
Quoted 30 lines
Quoted 27 lines
Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 8 +++++++xen/arch/arm/arm64/lib/find_next_bit.c| 1 + xen/arch/arm/bootfdt.c | 6 +++++ xen/arch/arm/decode.c | 2 ++ xen/arch/arm/domain_build.c | 29 ++++++++++++++++++---- xen/arch/arm/efi/efi-boot.h | 6 +++-- xen/arch/arm/gic-v3-its.c | 9 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33 +++++++++++++++-----------9 files changed, 69 insertions(+), 26 deletions(-)docs/misra/safe.json +5
-0@@ -12,6 +12,14 @@ }, { "id": "SAF-1-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possiblyQuoted 1 line
subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen."
I am bit concerned which such statement because the code instance was today with the current code. This could change in the future and invalide the reasoning.
Quoted 2 lines
It is not clear to me if we have any mechanism to prevent that. If we don't, then I think we need to drastically reduce the number of time this is used (there are a bit too much for my taste).
Indeed, the purpose of such a deviation is that the sound overapproximation computed by the tool requires a human to look at the code and think twice before modifying it (i.e., if ever that code is touched, the reviewer ought to assess whether that justification still holds or some other thing should be done about it.
Quoted 11 lines
Quoted 11 lines
+ },
+ {
+ "id": "SAF-2-safe",
"analyser": {},
"name": "Sentinel",
"text": "Next ID to be used"xen/arch/arm/arm64/lib/find_next_bit.c +0
-0@@ -67,6 +67,7 @@ unsigned long find_next_zero_bit(const unsigned long
Quoted 8 lines
xen/arch/arm/arm64/lib/find_next_bit.c unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, · line 71
Quoted 6 lines
xen/arch/arm/arm64/lib/find_next_bit.c +1
-0*addr, unsigned long size, { const unsigned long *p = addr + BIT_WORD(offset); unsigned long result = offset & ~(BITS_PER_LONG-1); + /* SAF-1-safe MC3R1.R9.1 */ unsigned long tmp;This is a file that was copied as-is from Linux. I thought we exclude them.
There was an expansion location in the report that caused that file to appear in the output. Thanks for reporting it.
Quoted 2 lines
Anyway, can you provide some details why Eclair thinks this is may be used unitialized?
The problem lies in the goto skipping out of control flow structures. It can't be refactored with a "do { tmp=...; break; } while(0)", as done elsewhere, because there are two distinct labels in there, so the semantics would be altered. The same comment applies in other instances as well.
Quoted 15 lines
xen/arch/arm/bootfdt.c static bool __init device_tree_node_matches(const · line 39
Quoted 13 lines
xen/arch/arm/arm64/lib/find_next_bit.c +0
-0if (offset >= size)xen/arch/arm/bootfdt.c +1
-0@@ -34,6 +34,7 @@ static bool __init device_tree_node_matches(const void *fdt, int node, static bool __init device_tree_node_compatible(const void *fdt, int node, const char *match) { + /* SAF-1-safe MC3R1.R9.1 */ int len, l;It is unclear to me whether the SAF-* applies to 'len' or 'l'.
Just len, they need to be two separate declarations if it is agreed not to initialize it. Good catch.
Quoted 14 lines
xen/arch/arm/bootfdt.c int __init device_tree_for_each_node(const void · line 177
Quoted 11 lines
xen/arch/arm/bootfdt.c +2
-0const void *prop;@@ -169,7 +170,9 @@ int __init device_tree_for_each_node(const void*fdt, int node, */ int depth = 0; const int first_node = node; + /* SAF-1-safe MC3R1.R9.1 */ u32 address_cells[DEVICE_TREE_MAX_DEPTH]; + /* SAF-1-safe MC3R1.R9.1 */ u32 size_cells[DEVICE_TREE_MAX_DEPTH];I have never sent formally, but in the past I proposed to rework the code so {address, size}_cells[0] would be initialized before the loop. See https://lore.kernel.org/all/[email protected]/.
Quoted 1 line
Do you think this would help?
If that initialization is applied to all the array elements yes, because the Amplification of R9.1 says:
"For the purposes of this rule, an array element or structure member shall be considered as a discrete
object".
Otherwise, initializing just the first element does not improve the situation in any way, and this is currently not within of the analysis capabilities of ECLAIR.
Quoted 15 lines
xen/arch/arm/bootfdt.c static void __init process_multiboot_node(const · line 260
Quoted 12 lines
xen/arch/arm/bootfdt.c +2
-0int ret;@@ -249,8 +252,10 @@ static void __init process_multiboot_node(const void *fdt, int node, const __be32 *cell; bootmodule_kind kind; paddr_t start, size; + /* SAF-1-safe MC3R1.R9.1 */ int len; /* sizeof("/chosen/") + DT_MAX_NAME + '/' + DT_MAX_NAME + '/0' => 92 */ + /* SAF-1-safe MC3R1.R9.1*/ char path[92];So the two above, is one category of issue. The variables are passed as argument of function which will fill them.
Quoted 6 lines
Can Eclair look at the callers, if so, can we consider to always initialize the values in the callee?
This would reduce the number of SAF-*. There are a few other examples like that below. So I will skip them for now.
[...]
If the value is always initialized in the callee, then there's no problem configuring ECLAIR so that it knows that this parameter is always written, and therefore any subsequent use in the caller is ok.
Another possibility is stating that a function never reads the pointee before writing to it (it may or may not write it, but if it doesn't, then the pointee is not read either). The 'strncmp' after 'fdt_get_path' does get in the way, though, because this property is not strong enough to ensure that we can use 'path' after returning from the function.
Quoted 12 lines
xen/arch/arm/domain_build.c custom_param("dom0_mem", parse_dom0_mem); · line 64
Quoted 9 lines
xen/arch/arm/domain_build.c +1
-1@@ -62,7 +62,7 @@ custom_param("dom0_mem", parse_dom0_mem); int __init parse_arch_dom0_param(const char *s, const char *e) { - long long val; + long long val = LLONG_MAX;Can you explain why you decided to initialize rather than SAF-? For clarity, I am not asking to switch to SAF-, I am mainly interested what were your though process.
I saw that putting a large enough value here would be out of range and trigger the printk even if the conditional leads to somehow enter the if branch with an invalid value.
Quoted 127 lines
Quoted 127 lines
xen/arch/arm/domain_build.c +6
-0if ( !parse_signed_integer("sve", s, e, &val) ) {@@ -1077,6 +1077,7 @@ static void __init assign_static_memory_11(struct domain *d, static int __init handle_linux_pci_domain(struct kernel_info *kinfo, const struct dt_device_node *node) { + /* SAF-1-safe MC3R1.R9.1 */ uint16_t segment; int res;@@ -1351,6 +1352,7 @@ static int __init make_memory_node(const structdomain *d, unsigned int i; int res, reg_size = addrcells + sizecells; int nr_cells = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[NR_MEM_BANKS * 4 /* Worst case addrcells + sizecells */]; __be32 *cells;@@ -1578,6 +1580,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo, struct rangeset *unalloc_mem; paddr_t start, end; unsigned int i; + /* SAF-1-safe MC3R1.R9.1 */ int res; dt_dprintk("Find unallocated memory for extended regions\n");@@ -1727,6 +1730,7 @@ static int __init find_memory_holes(const structkernel_info *kinfo, dt_for_each_device_node( dt_host, np ) { unsigned int naddr; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; naddr = dt_number_of_address(np);@@ -1976,9 +1980,11 @@ static int __init make_cpus_node(const struct domain *d, void *fdt) const struct dt_device_node *npcpu; unsigned int cpu; const void *compatible = NULL; + /* SAF-1-safe MC3R1.R9.1 */ u32 len; /* Placeholder for cpu@ + a 32-bit hexadecimal number + \0 */ char buf[13]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; /* Keep the compiler happy with -Og */bool
clock_valid= false;xen/arch/arm/domain_build.c +3
-0@@ -2104,6 +2110,7 @@ static int __init make_gic_node(const struct domain *d, void *fdt, const struct dt_device_node *gic = dt_interrupt_controller; int res = 0; const void *addrcells, *sizecells; + /* SAF-1-safe MC3R1.R9.1 */ u32 addrcells_len, sizecells_len; /*@@ -2179,6 +2186,7 @@ static int __init make_timer_node(const struct kernel_info *kinfo) int res; unsigned int irq[MAX_TIMER_PPI]; gic_interrupt_t intrs[3]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; bool clock_valid;@@ -2511,6 +2519,7 @@ static int __init handle_device(struct domain *d, struct dt_device_node *dev, unsigned int naddr; unsigned int i; int res; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; bool own_device = !dt_device_for_passthrough(dev);/*
xen/arch/arm/domain_build.c +1
-0@@ -2779,6 +2788,7 @@ static int __init make_gicv2_domU_node(struct kernel_info *kinfo) { void *fdt = kinfo->fdt; int res = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[(GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS) * 2]; __be32 *cells;const struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo) void *fdt = kinfo->fdt; int res; gic_interrupt_t intr; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS]; __be32 *cells;struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo) paddr_t paddr, len; int node; int res; + /* SAF-1-safe MC3R1.R9.1 */ __be32 val[2]; __be32 *cellp;void
__iomem*initrd;xen/arch/arm/domain_build.c +5
-1@@ -3514,6 +3526,7 @@ static int __init get_evtchn_dt_property(conststruct dt_device_node *np, uint32_t *port, uint32_t *phandle) { const __be32 *prop = NULL; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t len; prop = dt_get_property(np, "xen,evtchn", &len);@@ -3538,10 +3551,13 @@ static int __init get_evtchn_dt_property(conststruct dt_device_node *np, static int __init alloc_domain_evtchn(struct dt_device_node *node) { int rc; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t domU1_port, domU2_port, remote_phandle; struct dt_device_node *remote_node; const struct dt_device_node *p1_node, *p2_node; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_alloc_unbound alloc_unbound; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_bind_interdomain bind_interdomain; struct domain *d1 = NULL, *d2 = NULL;@@ -3789,11 +3805,12 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo) static int __init alloc_xenstore_evtchn(struct domain *d) { - evtchn_alloc_unbound_t alloc; + evtchn_alloc_unbound_t alloc = {
Quoted 9 lines
xen/arch/arm/domain_build.c static int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 3796
Quoted 6 lines
xen/arch/arm/domain_build.c +3
-2+ .dom = d->domain_id, + .remote_dom = hardware_domain->domain_id + }; int rc; - alloc.dom = d->domain_id; - alloc.remote_dom = hardware_domain->domain_id;It is not clear why this is modified. There are only two fields in 'alloc' and they are both initialized.
There are three fields in the struct, and the third is clearly to be set by an initalization function. If, as above with 'path', the init function ensures that the third field is always written then there's no problem, otherwise this initialization prevents any use of an uninitialized port.
struct evtchn_alloc_unbound {
/* IN parameters */
domid_t dom, remote_dom;
/* OUT parameters */
evtchn_port_t port;
};typedef struct evtchn_alloc_unbound evtchn_alloc_unbound_t;
Quoted 11 lines
xen/arch/arm/domain_build.c static int __init construct_domU(struct domain *d, · line 3830
Quoted 9 lines
xen/arch/arm/domain_build.c +1
-1rc = evtchn_alloc_unbound(&alloc, 0); if ( rc ) {@@ -3810,8 +3827,9 @@ static int __init construct_domU(struct domain *d, const struct dt_device_node *node) { struct kernel_info kinfo = {}; - const char *dom0less_enhanced; + const char *dom0less_enhanced = NULL;If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL. So it is unclear to me how this value is safer.
Quoted 2 lines
Quoted 1 line
Looking at the code, I wonder whether we should convert
dt_property_read_string()to useERR_PTR(). So we could remove the last argument and return it instead.
Is relying on that assumption somehow safer? The suggestion to remove the last parameter seems better.
Quoted 55 lines
Quoted 55 lines
int rc;
+ /* SAF-1-safe MC3R1.R9.1 */
u64 mem;
u32p2m_mem_mb;
unsigned longp2m_pages;xen/arch/arm/domain_build.c +1
-0@@ -3939,6 +3957,7 @@ void __init create_domUs(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), }; unsigned int flags = 0U; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t val; int rc;xen/arch/arm/efi/efi-boot.h +1
-0@@ -117,6 +117,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells) static int __init fdt_set_reg(void *fdt, int node, int addr_cells, int size_cells, uint64_t addr, uint64_t len) { + /* SAF-1-safe MC3R1.R9.1 */
__be32val[4]; /* At most 2 64 bit values to be stored */__be32*cellp;xen/arch/arm/efi/efi-boot.h +3
-2@@ -308,7 +309,7 @@ fdt_set_fail: static void __init *fdt_increase_size(struct file *fdtfile, int add_size) { EFI_STATUS status; - EFI_PHYSICAL_ADDRESS fdt_addr; + EFI_PHYSICAL_ADDRESS fdt_addr = 0; int fdt_size; int pages; void *new_fdt;@@ -433,7 +434,7 @@ static void __init efi_arch_cfg_file_late(constEFI_LOADED_IMAGE *image, static void *__init efi_arch_allocate_mmap_buffer(UINTN map_size) { - void *ptr; + void *ptr = NULL; EFI_STATUS status; status = efi_bs->AllocatePool(EfiLoaderData, map_size, &ptr);@@ -538,6 +539,7 @@ static void __init efi_arch_handle_module(conststruct file *file, { int node; int chosen; + /* SAF-1-safe MC3R1.R9.1 */ int addr_len, size_len; if ( file == &dtbfile )xen/arch/arm/gic-v3-its.c +0
-0@@ -192,8 +192,7 @@ static int its_send_cmd_mapc(struct host_its *its,
Quoted 9 lines
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 195
Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1
-2uint32_t collection_id, cmd[0] = GITS_CMD_MAPC; cmd[1] = 0x00; - cmd[2] = encode_rdbase(its, cpu, collection_id); - cmd[2] |= GITS_VALID_BIT; + cmd[2] = encode_rdbase(its, cpu, collection_id) | GITS_VALID_BIT;Hmmm... How is this even considered as unitialized variable?
The analysis here could use some more precision, but the modified construct is entirely equivalent.
Quoted 52 lines
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapd(struct host_its *its, · line 218
Quoted 11 lines
xen/arch/arm/gic-v3-its.c +1
-3cmd[3] = 0x00; return its_send_command(its, cmd);@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its,uint32_t deviceid, } cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32); cmd[1] = size_bits; - cmd[2] = itt_addr; - if ( valid ) - cmd[2] |= GITS_VALID_BIT; + cmd[2] = itt_addr | (valid ? GITS_VALID_BIT : 0x00);Same here.
Quoted 26 lines
xen/arch/arm/mm.c +1
-0@@ -935,6 +935,7 @@ static int xen_pt_update_entry(mfn_t root,unsigned long virt, mfn_t mfn, unsigned int target, unsigned int flags) { + /* SAF-1-safe MC3R1.R9.1 */ int rc; unsigned int level;
lpae_t*table;xen/arch/arm/p2m.c +1
-1@@ -496,16 +496,18 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m,gfn_t gfn, lpae_t entry, *table; int rc; mfn_t mfn = INVALID_MFN; - p2m_type_t _t; + p2m_type_t _t = p2m_invalid; DECLARE_OFFSETS(offsets, addr); ASSERT(p2m_is_locked(p2m));xen/arch/arm/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, · line 509
Quoted 10 lines
xen/arch/arm/p2m.c +5
-3BUILD_BUG_ON(THIRD_MASK != PAGE_MASK); /* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; + if( t ) { + *t = _t; + } else { + t = &_t; + }What was the problem with the previous code?
Quoted 2 lines
This is also not conformant to Xen coding style.
The problem is that _t may be uninitialized, hence assigning its address to t could be problematic. Another way to address this is to initialize _t to a bad value and use this variable in the body, then assign to t based on the value just before returning. Point taken on the coding style.
Quoted 10 lines
xen/arch/arm/p2m.c static int __p2m_set_entry(struct p2m_domain *p2m, · line 1037
Quoted 8 lines
xen/arch/arm/p2m.c +1
-0if ( valid ) *valid = false;@@ -1031,6 +1033,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m, unsigned int level = 0; unsigned int target = 3 - (page_order / XEN_PT_LPAE_SHIFT); lpae_t *entry, *table, orig_pte; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same issue with gotos explained above, can't be refactored because of the for enclosing the goto.
Quoted 14 lines
xen/arch/arm/p2m.c static inline int p2m_remove_mapping(struct · line 1491
Quoted 10 lines
xen/arch/arm/p2m.c +1
-0/* A mapping is removed if the MFN is invalid. */ bool removing_mapping = mfn_eq(smfn, INVALID_MFN);@@ -1483,6 +1486,7 @@ static inline int p2m_remove_mapping(struct domain *d, { struct p2m_domain *p2m = p2m_get_hostp2m(d); unsigned long i; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same as above.
Quoted 5 lines
Quoted 5 lines
xen/arch/arm/p2m.c +0
-2p2m_write_lock(p2m);@@ -1685,20 +1689,21 @@ static int p2m_alloc_vmid(struct domain *d) ASSERT(nr != INVALID_VMID); - if ( nr == MAX_VMID ) - {
Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1692
Quoted 6 lines
xen/arch/arm/p2m.c +1
-4- rc = -EBUSY; - printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); - goto out; - } + do {I don't understand this change. How is this making better for Eclair?
This is an example where the goto can be eliminated, which in turn allows to automatically check the correctness.
Quoted 10 lines
Quoted 10 lines
xen/arch/arm/p2m.c +7
-2+ if ( nr == MAX_VMID ) + { + rc = -EBUSY; + printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); + break; + } - set_bit(nr, vmid_mask); + set_bit(nr, vmid_mask); - p2m->vmid = nr;
Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1709
Quoted 8 lines
xen/arch/arm/p2m.c +3
-2+ p2m->vmid = nr; - rc = 0; + rc = 0; + } while ( 0 ); -out: spin_unlock(&vmid_alloc_lock); return rc; }
Considering all of the replies above, a first draft of a strategy/policy I can think of is having:
- Initializer functions that always write their parameter, so that the strongest "pointee always written" property can be stated. This causes all further uses to be marked safe.
- Initialize the variable when there exists a known safe value that does not alter the semantics of the function. The initialization does not need to be at the declaration, but doing so simplifies the code.
- Deviate all cases where any of the previous does not apply, with a comment deviation that refers to a justification reporting that the code has been checked to respect the rule (keep in mind that _violations_ to a Mandatory rule such as R9.1 are not allowed to claim MISRA compliance).
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Julien GrallQuoted 13 lines · Julien Grall
Hi,
Quoted 10 lines · Luca Fancellu
Quoted 10 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
The code is pretty straightforward. So I am not entirely sure why Eclair is not happy. Is it because the value is set by assembly code?
Exactly. The reason why I didn't just state that oldbit is always written or never read before being written in that function is that I was unsure about the meaning of the assembly.
Quoted 11 lines
Quoted 4 lines
Hi Julien,
Yes I agree that it’s strange, do you think that that if at line 97 we initialize oldbit to zero, it would solve the issue?
At the moment, I am not entirely happy to zero oldbit because we have other places in Xen where we need assembly instructions to read a value (e.g.
READ_SYSREG()).So I would first like to understand why Eclair can't cope with it.
Cheers,
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Nicola VetriniHi Nicola,
Quoted 24 lines · Nicola Vetrini
Quoted 23 lines · Julien Grall
Hi Nicola,
Quoted 10 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
To achieve this goal, I adopted the following strategies:
Please let's at least one patch per strategy. I would also consider some of the rework separate so they can go in regardless the decision for the SAF-*.
Quoted 7 lines
- Add a suitably formatted local deviation comment (as indicated in '
docs/misra/documenting-violations.rst') to exempt the following line from checking.- Provide an initialization for the variable at the declaration.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
- Substitute a goto breaking out of control flow logic with a semantically
equivalent do { .. } while(0).As I already mentioned in private, it is unclear to me how you decided which strategy to use. I still think we need to define our policy before changing the code. Otherwise, it is going to be difficult to decide for new code.
The main point of this RFC is doing so. From what I gathered, it's not an easy task: sometimes there are no 'safe' values to initialize variables to and sometimes there is no easy way to prove that indeed something is always initialized or not accessed at all.
But you wrote the code. So you should be able to explain how you took the decision between one and the others.
Also, even if this is an RFC, it would have been good to summarize any discussion that happened in private and if there were concern try to come up with ideas or at least listing the concerns after '---.
Quoted 31 lines
Quoted 31 lines
Quoted 28 lines
Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 8 +++++++xen/arch/arm/arm64/lib/find_next_bit.c| 1 + xen/arch/arm/bootfdt.c | 6 +++++ xen/arch/arm/decode.c | 2 ++ xen/arch/arm/domain_build.c | 29 ++++++++++++++++++---- xen/arch/arm/efi/efi-boot.h | 6 +++-- xen/arch/arm/gic-v3-its.c | 9 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33 +++++++++++++++-----------9 files changed, 69 insertions(+), 26 deletions(-)docs/misra/safe.json +5
-0@@ -12,6 +12,14 @@ }, { "id": "SAF-1-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possiblyQuoted 1 line
subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen."
I am bit concerned which such statement because the code instance was today with the current code. This could change in the future and invalide the reasoning.
Quoted 4 lines
Quoted 2 lines
It is not clear to me if we have any mechanism to prevent that. If we don't, then I think we need to drastically reduce the number of time this is used (there are a bit too much for my taste).
Indeed, the purpose of such a deviation is that the sound overapproximation computed by the tool requires a human to look at the code and think twice before modifying it (i.e., if ever that code is touched, the reviewer ought to assess whether that justification still holds or some other thing should be done about it.
Your assumption is the reviewer will notice there is an existing devitation and be able to assess it has changed. I view this assumption as risky in the long term.
Have you investigate to improve the automatic tooling?
Quoted 60 lines
Quoted 12 lines
Quoted 12 lines
+ },
+ {
+ "id": "SAF-2-safe",
"analyser": {},
"name": "Sentinel",
"text": "Next ID to be used"xen/arch/arm/arm64/lib/find_next_bit.c +0
-0@@ -67,6 +67,7 @@ unsigned long find_next_zero_bit(const unsignedQuoted 8 lines
xen/arch/arm/arm64/lib/find_next_bit.c unsigned long find_next_zero_bit(const unsigned · line 72
Quoted 6 lines
xen/arch/arm/arm64/lib/find_next_bit.c +1
-0long *addr, unsigned long size, { const unsigned long *p = addr + BIT_WORD(offset); unsigned long result = offset & ~(BITS_PER_LONG-1); + /* SAF-1-safe MC3R1.R9.1 */ unsigned long tmp;This is a file that was copied as-is from Linux. I thought we exclude them.
There was an expansion location in the report that caused that file to appear in the output. Thanks for reporting it.
Quoted 2 lines
Anyway, can you provide some details why Eclair thinks this is may be used unitialized?
The problem lies in the goto skipping out of control flow structures. It can't be refactored with a "do { tmp=...; break; } while(0)", as done elsewhere, because there are two distinct labels in there, so the semantics would be altered. The same comment applies in other instances as well.
Quoted 15 lines
xen/arch/arm/bootfdt.c static bool __init device_tree_node_matches(const · line 39
Quoted 13 lines
xen/arch/arm/arm64/lib/find_next_bit.c +0
-0if (offset >= size)xen/arch/arm/bootfdt.c +1
-0@@ -34,6 +34,7 @@ static bool __init device_tree_node_matches(const void *fdt, int node, static bool __init device_tree_node_compatible(const void *fdt, int node, const char *match) { + /* SAF-1-safe MC3R1.R9.1 */ int len, l;It is unclear to me whether the SAF-* applies to 'len' or 'l'.
Just len, they need to be two separate declarations if it is agreed not to initialize it. Good catch.
Quoted 14 lines
xen/arch/arm/bootfdt.c int __init device_tree_for_each_node(const void · line 177
Quoted 11 lines
xen/arch/arm/bootfdt.c +2
-0const void *prop;@@ -169,7 +170,9 @@ int __init device_tree_for_each_node(const void*fdt, int node, */ int depth = 0; const int first_node = node; + /* SAF-1-safe MC3R1.R9.1 */ u32 address_cells[DEVICE_TREE_MAX_DEPTH]; + /* SAF-1-safe MC3R1.R9.1 */ u32 size_cells[DEVICE_TREE_MAX_DEPTH];I have never sent formally, but in the past I proposed to rework the code so {address, size}_cells[0] would be initialized before the loop. See https://lore.kernel.org/all/[email protected]/.
Quoted 6 lines
Quoted 1 line
Do you think this would help?
If that initialization is applied to all the array elements yes, because the Amplification of R9.1 says:
"For the purposes of this rule, an array element or structure member shall be considered as a discrete
object".
Otherwise, initializing just the first element does not improve the situation in any way, and this is currently not within of the analysis capabilities of ECLAIR.
Any plan to improve ECLAIR?
Quoted 18 lines
Quoted 18 lines
xen/arch/arm/bootfdt.c static void __init process_multiboot_node(const · line 260
Quoted 13 lines
xen/arch/arm/bootfdt.c +2
-0int ret;@@ -249,8 +252,10 @@ static void __init process_multiboot_node(const void *fdt, int node, const __be32 *cell; bootmodule_kind kind; paddr_t start, size; + /* SAF-1-safe MC3R1.R9.1 */ int len; /* sizeof("/chosen/") + DT_MAX_NAME + '/' + DT_MAX_NAME + '/0' => 92 */ + /* SAF-1-safe MC3R1.R9.1*/ char path[92];So the two above, is one category of issue. The variables are passed as argument of function which will fill them.
Can Eclair look at the callers, if so, can we consider to always initialize the values in the callee?
Quoted 8 lines
Quoted 4 lines
This would reduce the number of SAF-*. There are a few other examples like that below. So I will skip them for now.
[...]
If the value is always initialized in the callee, then there's no problem configuring ECLAIR so that it knows that this parameter is always written, and therefore any subsequent use in the caller is ok.
Another possibility is stating that a function never reads the pointee before writing to it (it may or may not write it, but if it doesn't, then the pointee is not read either). The 'strncmp' after '
fdt_get_path' does get in the way, though, because this property is not strong enough to ensure that we can use 'path' after returning from the function.
I am not sure I fully understand what you wrote. Can you provide a C example?
Quoted 14 lines
Quoted 13 lines
xen/arch/arm/domain_build.c custom_param("dom0_mem", parse_dom0_mem); · line 64
Quoted 10 lines
xen/arch/arm/domain_build.c +1
-1@@ -62,7 +62,7 @@ custom_param("dom0_mem", parse_dom0_mem); int __init parse_arch_dom0_param(const char *s, const char *e) { - long long val; + long long val = LLONG_MAX;Can you explain why you decided to initialize rather than SAF-? For clarity, I am not asking to switch to SAF-, I am mainly interested what were your though process.
Quoted 1 line
I saw that putting a large enough value here would be out of range and trigger the printk even if the conditional leads to somehow enter the if branch with an invalid value.
Ok. This wants to be documented on top of the assignment.
Quoted 138 lines
Quoted 128 lines
Quoted 128 lines
xen/arch/arm/domain_build.c +6
-0if ( !parse_signed_integer("sve", s, e, &val) ) {@@ -1077,6 +1077,7 @@ static void __init assign_static_memory_11(struct domain *d, static int __init handle_linux_pci_domain(struct kernel_info *kinfo, const struct dt_device_node *node) { + /* SAF-1-safe MC3R1.R9.1 */ uint16_t segment; int res;@@ -1351,6 +1352,7 @@ static int __init make_memory_node(const structdomain *d, unsigned int i; int res, reg_size = addrcells + sizecells; int nr_cells = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[NR_MEM_BANKS * 4 /* Worst case addrcells + sizecells */]; __be32 *cells;@@ -1578,6 +1580,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo, struct rangeset *unalloc_mem; paddr_t start, end; unsigned int i; + /* SAF-1-safe MC3R1.R9.1 */ int res; dt_dprintk("Find unallocated memory for extended regions\n");@@ -1727,6 +1730,7 @@ static int __init find_memory_holes(conststruct kernel_info *kinfo, dt_for_each_device_node( dt_host, np ) { unsigned int naddr; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; naddr = dt_number_of_address(np);@@ -1976,9 +1980,11 @@ static int __init make_cpus_node(const struct domain *d, void *fdt) const struct dt_device_node *npcpu; unsigned int cpu; const void *compatible = NULL; + /* SAF-1-safe MC3R1.R9.1 */ u32 len; /* Placeholder for cpu@ + a 32-bit hexadecimal number + \0 */ char buf[13]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; /* Keep the compiler happy with -Og */bool
clock_valid= false;xen/arch/arm/domain_build.c +3
-0@@ -2104,6 +2110,7 @@ static int __init make_gic_node(const struct domain *d, void *fdt, const struct dt_device_node *gic = dt_interrupt_controller; int res = 0; const void *addrcells, *sizecells; + /* SAF-1-safe MC3R1.R9.1 */ u32 addrcells_len, sizecells_len; /*@@ -2179,6 +2186,7 @@ static int __init make_timer_node(const struct kernel_info *kinfo) int res; unsigned int irq[MAX_TIMER_PPI]; gic_interrupt_t intrs[3]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; bool clock_valid;@@ -2511,6 +2519,7 @@ static int __init handle_device(struct domain *d, struct dt_device_node *dev, unsigned int naddr; unsigned int i; int res; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; bool own_device = !dt_device_for_passthrough(dev);/*
xen/arch/arm/domain_build.c +1
-0@@ -2779,6 +2788,7 @@ static int __init make_gicv2_domU_node(struct kernel_info *kinfo) { void *fdt = kinfo->fdt; int res = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[(GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS) * 2]; __be32 *cells;const struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo) void *fdt = kinfo->fdt; int res; gic_interrupt_t intr; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS]; __be32 *cells;struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo) paddr_t paddr, len; int node; int res; + /* SAF-1-safe MC3R1.R9.1 */ __be32 val[2]; __be32 *cellp;void
__iomem*initrd;xen/arch/arm/domain_build.c +5
-1@@ -3514,6 +3526,7 @@ static int __init get_evtchn_dt_property(conststruct dt_device_node *np, uint32_t *port, uint32_t *phandle) { const __be32 *prop = NULL; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t len; prop = dt_get_property(np, "xen,evtchn", &len);@@ -3538,10 +3551,13 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np, static int __init alloc_domain_evtchn(struct dt_device_node *node) { int rc; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t domU1_port, domU2_port, remote_phandle; struct dt_device_node *remote_node; const struct dt_device_node *p1_node, *p2_node; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_alloc_unbound alloc_unbound; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_bind_interdomain bind_interdomain; struct domain *d1 = NULL, *d2 = NULL;@@ -3789,11 +3805,12 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo) static int __init alloc_xenstore_evtchn(struct domain *d) { - evtchn_alloc_unbound_t alloc; + evtchn_alloc_unbound_t alloc = {Quoted 9 lines
xen/arch/arm/domain_build.c static int __init construct_domain(struct · line 3795
Quoted 6 lines
xen/arch/arm/domain_build.c +3
-2+ .dom = d->domain_id, + .remote_dom = hardware_domain->domain_id + }; int rc; - alloc.dom = d->domain_id; - alloc.remote_dom = hardware_domain->domain_id;It is not clear why this is modified. There are only two fields in 'alloc' and they are both initialized.
Quoted 8 lines
There are three fields in the struct, and the third is clearly to be set by an initalization function. If, as above with 'path', the init function ensures that the third field is always written then there's no problem, otherwise this initialization prevents any use of an uninitialized port.
Quoted 1 line
struct
evtchn_alloc_unbound{/* IN parameters */
domid_tdom,remote_dom;
/* OUT parameters */
evtchn_port_tport;
};
typedef structevtchn_alloc_unboundevtchn_alloc_unbound_t;
Ah, I didn't spot the third field because it was defined on the same line. I would prefer if the value is explicitely initialized to 0.
Also, I think this ought to be in a separate patch (with other similar pattern).
Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/arm/domain_build.c +0
-0rc = evtchn_alloc_unbound(&alloc, 0); if ( rc ) {@@ -3810,8 +3827,9 @@ static int __init construct_domU(struct domain *d, const struct dt_device_node *node)Quoted 8 lines
xen/arch/arm/domain_build.c static int __init construct_domU(struct domain *d, · line 3830
Quoted 4 lines
xen/arch/arm/domain_build.c +1
-1{ struct kernel_info kinfo = {}; - const char *dom0less_enhanced; + const char *dom0less_enhanced = NULL;If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL. So it is unclear to me how this value is safer. > Looking at the code, I wonder whether we should convertdt_property_read_string()to useERR_PTR(). So we could remove the last argument and return it instead.
Quoted 1 line
Is relying on that assumption somehow safer?
I am assuming you are referring to "If you look at the user below, all the callers assume dom0less_enhanced will be non-NULL". Note that I didn't suggest it is safer. I am only pointed out that you didn't specify how this was better in the context of the code.
Quoted 67 lines
The suggestion to remove the last parameter seems better.
Quoted 55 lines
Quoted 55 lines
xen/arch/arm/domain_build.c +2
-0int rc; + /* SAF-1-safe MC3R1.R9.1 */ u64 mem; u32 p2m_mem_mb; unsigned long p2m_pages;@@ -3939,6 +3957,7 @@ void __init create_domUs(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), }; unsigned int flags = 0U; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t val; int rc;xen/arch/arm/efi/efi-boot.h +1
-0@@ -117,6 +117,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells) static int __init fdt_set_reg(void *fdt, int node, int addr_cells, int size_cells, uint64_t addr, uint64_t len) { + /* SAF-1-safe MC3R1.R9.1 */
__be32val[4]; /* At most 2 64 bit values to be stored */__be32*cellp;xen/arch/arm/efi/efi-boot.h +3
-2@@ -308,7 +309,7 @@ fdt_set_fail: static void __init *fdt_increase_size(struct file *fdtfile, int add_size) { EFI_STATUS status; - EFI_PHYSICAL_ADDRESS fdt_addr; + EFI_PHYSICAL_ADDRESS fdt_addr = 0; int fdt_size; int pages; void *new_fdt;@@ -433,7 +434,7 @@ static void __init efi_arch_cfg_file_late(constEFI_LOADED_IMAGE *image, static void *__init efi_arch_allocate_mmap_buffer(UINTN map_size) { - void *ptr; + void *ptr = NULL; EFI_STATUS status; status = efi_bs->AllocatePool(EfiLoaderData, map_size, &ptr);@@ -538,6 +539,7 @@ static void __init efi_arch_handle_module(conststruct file *file, { int node; int chosen; + /* SAF-1-safe MC3R1.R9.1 */ int addr_len, size_len; if ( file == &dtbfile )xen/arch/arm/gic-v3-its.c +0
-0@@ -192,8 +192,7 @@ static int its_send_cmd_mapc(struct host_itsQuoted 9 lines
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its · line 195
Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1
-2*its, uint32_t collection_id, cmd[0] = GITS_CMD_MAPC; cmd[1] = 0x00; - cmd[2] = encode_rdbase(its, cpu, collection_id); - cmd[2] |= GITS_VALID_BIT; + cmd[2] = encode_rdbase(its, cpu, collection_id) | GITS_VALID_BIT;Hmmm... How is this even considered as unitialized variable?
Quoted 1 line
The analysis here could use some more precision, but the modified construct is entirely equivalent.
I agree that they are equivalent. But in general, we don't change the style of the construct without explaining why.
In this case, the first step would be to improve Eclair.
Quoted 53 lines
Quoted 53 lines
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapd(struct host_its · line 218
Quoted 12 lines
xen/arch/arm/gic-v3-its.c +1
-3cmd[3] = 0x00; return its_send_command(its, cmd);@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, } cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32); cmd[1] = size_bits; - cmd[2] = itt_addr; - if ( valid ) - cmd[2] |= GITS_VALID_BIT; + cmd[2] = itt_addr | (valid ? GITS_VALID_BIT : 0x00);Same here.
Quoted 26 lines
xen/arch/arm/mm.c +1
-0@@ -935,6 +935,7 @@ static int xen_pt_update_entry(mfn_t root,unsigned long virt, mfn_t mfn, unsigned int target, unsigned int flags) { + /* SAF-1-safe MC3R1.R9.1 */ int rc; unsigned int level;
lpae_t*table;xen/arch/arm/p2m.c +1
-1@@ -496,16 +496,18 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m,gfn_t gfn, lpae_t entry, *table; int rc; mfn_t mfn = INVALID_MFN; - p2m_type_t _t; + p2m_type_t _t = p2m_invalid; DECLARE_OFFSETS(offsets, addr); ASSERT(p2m_is_locked(p2m));xen/arch/arm/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, · line 509
Quoted 10 lines
xen/arch/arm/p2m.c +5
-3BUILD_BUG_ON(THIRD_MASK != PAGE_MASK); /* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; + if( t ) { + *t = _t; + } else { + t = &_t; + }What was the problem with the previous code?
Quoted 4 lines
Quoted 2 lines
This is also not conformant to Xen coding style.
The problem is that _t may be uninitialized, hence assigning its address to t could be problematic.
But the value is set right after. IOW, there is no read between. So how is this prob
Quoted 1 line
Another way to address this is to initialize _t to a bad value and use this variable in the body, then assign to t based on the value just before returning.
IHMO, neither solution are ideal. I think we should investigate whether Eclair can be improved.
[...]
Quoted 45 lines
Quoted 10 lines
xen/arch/arm/p2m.c static int __p2m_set_entry(struct p2m_domain *p2m, · line 1037
Quoted 8 lines
xen/arch/arm/p2m.c +1
-0if ( valid ) *valid = false;@@ -1031,6 +1033,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m, unsigned int level = 0; unsigned int target = 3 - (page_order / XEN_PT_LPAE_SHIFT); lpae_t *entry, *table, orig_pte; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same issue with gotos explained above, can't be refactored because of the for enclosing the goto.
Quoted 14 lines
xen/arch/arm/p2m.c static inline int p2m_remove_mapping(struct · line 1491
Quoted 10 lines
xen/arch/arm/p2m.c +1
-0/* A mapping is removed if the MFN is invalid. */ bool removing_mapping = mfn_eq(smfn, INVALID_MFN);@@ -1483,6 +1486,7 @@ static inline int p2m_remove_mapping(struct domain *d, { struct p2m_domain *p2m = p2m_get_hostp2m(d); unsigned long i; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same as above.
Quoted 5 lines
Quoted 5 lines
xen/arch/arm/p2m.c +0
-2p2m_write_lock(p2m);@@ -1685,20 +1689,21 @@ static int p2m_alloc_vmid(struct domain *d) ASSERT(nr != INVALID_VMID); - if ( nr == MAX_VMID ) - {Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1691
Quoted 6 lines
xen/arch/arm/p2m.c +1
-4- rc = -EBUSY; - printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); - goto out; - } + do {I don't understand this change. How is this making better for Eclair?
Quoted 1 line
This is an example where the goto can be eliminated, which in turn allows to automatically check the correctness.
If you want to eliminate the 'goto' then they are better way to do it. Like:
xen/arch/arm/p2m.c +0 -0
@@ -186,16 +186,14 @@ int p2m_alloc_vmid(struct domain *d) { rc = -EBUSY ; printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n",
d->domain_id);
- goto out;
+ }
+ else
+ {
+ set_bit(nr, vmid_mask);
+ p2m->vmid = nr;
+ rc = 0;
}- set_bit(nr, vmid_mask);
-
- p2m->vmid = nr;
-
- rc = 0;
-
-out:
spin_unlock(&vmid_alloc_lock);
return rc;}
I have a slight preference with the goto version, but I could accept it if Eclair can't cope with the construct. In any case, this is the sort of change that deserve its own patch as you want to explain why Eclair can't cope with such construct (I don't view it as complex).
Quoted 22 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/p2m.c +7
-2+ if ( nr == MAX_VMID ) + { + rc = -EBUSY; + printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); + break; + } - set_bit(nr, vmid_mask); + set_bit(nr, vmid_mask); - p2m->vmid = nr;Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1705
Quoted 8 lines
xen/arch/arm/p2m.c +3
-2+ p2m->vmid = nr; - rc = 0; + rc = 0; + } while ( 0 ); -out: spin_unlock(&vmid_alloc_lock); return rc; }
Quoted 7 lines
Considering all of the replies above, a first draft of a strategy/policy I can think of is having:
- Initializer functions that always write their parameter, so that the
strongest "pointee always written" property can be stated. This causes all further uses to be marked safe.- Initialize the variable when there exists a known safe value that does
not alter the semantics of the function. The initialization does not need to be at the declaration, but doing so simplifies the code.
As I mentionned in private there are two risks with that:
1. You silence compiler to spot other issues
2. You may now get warning from Coverity if it spots you set a value
that get overwritten before its first use.
So I think such approach should be used with parcimony. Instead, we should look at reworking the code when possible.
Quoted 3 lines
- Deviate all cases where any of the previous does not apply, with a
comment deviation that refers to a justification reporting that the code has been checked to respect the rule (keep in mind that _violations_ to a Mandatory rule such as R9.1 are not allowed to claim MISRA compliance).
See above for my concern about adding so many deviations. But I am confused with what you wrote. If the rule is mandatory, then why are you trying to add deviation in Xen? Who is going to solve them to make Xen MISRA compliant?
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Nicola VetriniHi,
Quoted 10 lines · Nicola Vetrini
Quoted 7 lines · Julien Grall
Hi,
Quoted 4 lines · Luca Fancellu
Quoted 4 lines · Julien Grall
Hi,
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines · Nicola Vetrini
Quoted 3 lines
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
The code is pretty straightforward. So I am not entirely sure why Eclair is not happy. Is it because the value is set by assembly code?
Exactly.
I am a bit puzzled. There are other values read from assembly (e.g. READ_SYSREG(), read_atomic()) within Xen. This is including 'res' in the same function. So can you elaborate, why 'oldbit' is a problem but not the others?
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Luca FancelluQuoted 35 lines · Luca Fancellu
Quoted 35 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)docs/misra/safe.json +8
-0@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe",
Quoted 8 lines
docs/misra/safe.json line 28
Quoted 6 lines
docs/misra/safe.json +6
-0+ "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */,
+1
I'm puzzled by the wording vs comment placement though: The comments are inserted ahead of the macro invocations, so there are no "following local variables". Plus does this imply the comment would suppress the checking on _all_ of them, rather than just the one that was confirmed to be safe? What if another new one was added, that actually introduces a problem?
Quoted 1 line
also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Me, at least. The annotations should be tool-agnostic imo, or else the more tools we use, the longer these comments might get.
Jan
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Nicola VetriniQuoted 8 lines · Nicola Vetrini
Quoted 7 lines · Julien Grall
Quoted 6 lines · Luca Fancellu
Quoted 6 lines · Julien Grall
Quoted 3 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
The code is pretty straightforward. So I am not entirely sure why Eclair is not happy. Is it because the value is set by assembly code?
Exactly. The reason why I didn't just state that oldbit is always written or never read before being written in that function is that I was unsure about the meaning of the assembly.
So I'm pretty sure the tool wouldn't take apart the string literal passed first to the asm(). Instead I expect it goes from the operands specified, which for oldbit is "=&r". There's nothing conditional here, so if the tool didn't trust that outputs are written, it would need to flag such an issue on about any asm() having outputs.
I hope the issue isn't that the tool doesn't properly deal with the do/while.
In any event: I may misremember earlier discussions, but isn't this a pretty obvious false positive? In which case didn't we mean to flag those as such (because really an improved checking tool could avoid them)?
Jan
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
Quoted 22 lines · Jan Beulich
Quoted 8 lines · Nicola Vetrini
Quoted 7 lines · Julien Grall
Quoted 6 lines · Luca Fancellu
Quoted 6 lines · Julien Grall
Quoted 3 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
The code is pretty straightforward. So I am not entirely sure why Eclair is not happy. Is it because the value is set by assembly code?
Exactly. The reason why I didn't just state that oldbit is always written or never read before being written in that function is that I was unsure about the meaning of the assembly.
So I'm pretty sure the tool wouldn't take apart the string literal passed first to the
asm(). Instead I expect it goes from the operands specified, which for oldbit is "=&r". There's nothing conditional here, so if the tool didn't trust that outputs are written, it would need to flag such an issue on about anyasm()having outputs.
Quoted 9 lines
I hope the issue isn't that the tool doesn't properly deal with the do/while.
In any event: I may misremember earlier discussions, but isn't this a pretty obvious false positive? In which case didn't we mean to flag those as such (because really an improved checking tool could avoid them)?
Jan
Actually, given that the function indeed always writes oldbit in the asm instruction, I can state that this post-condition holds for the function.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Jan BeulichQuoted 54 lines · Jan Beulich
Quoted 35 lines · Luca Fancellu
Quoted 35 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)docs/misra/safe.json +8
-0@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe",Quoted 8 lines
docs/misra/safe.json line 28
Quoted 6 lines
docs/misra/safe.json +6
-0+ "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */,
+1
I'm puzzled by the wording vs comment placement though: The comments are inserted ahead of the macro invocations, so there are no "following local variables". Plus does this imply the comment would suppress the checking on _all_ of them, rather than just the one that was confirmed to be safe? What if another new one was added, that actually introduces a problem?
Quoted 6 lines
Quoted 1 line
also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Me, at least. The annotations should be tool-agnostic imo, or else the more tools we use, the longer these comments might get.
Jan
No problem for both: Given the earlier remarks by Julien on patch 1/4, I think we can devise something along the lines of
/* SAF-x-safe MISRA:C 2012 Rule 9.1 <Justification> */ int var;
and then write a generic justification in docs/misra/safe.json, while
<Justification> contains a specific one (e.g., this loop does so and so to ensure that no access to unset variables happens). Thus any modification to that variable would give who submits the patch and the reviewer a chance to spot any inconsistency when the code is modified.
However, given my other response to this same patch, this does not apply here specifically.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
Quoted 65 lines · Nicola Vetrini
Quoted 54 lines · Jan Beulich
Quoted 35 lines · Luca Fancellu
Quoted 35 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)docs/misra/safe.json +8
-0@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe",Quoted 8 lines
docs/misra/safe.json line 28
Quoted 6 lines
docs/misra/safe.json +6
-0+ "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */,
+1
I'm puzzled by the wording vs comment placement though: The comments are inserted ahead of the macro invocations, so there are no "following local variables". Plus does this imply the comment would suppress the checking on _all_ of them, rather than just the one that was confirmed to be safe? What if another new one was added, that actually introduces a problem?
Quoted 4 lines
Quoted 1 line
also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Me, at least. The annotations should be tool-agnostic imo, or else the more tools we use, the longer these comments might get.
No problem for both: Given the earlier remarks by Julien on patch 1/4, I think we can devise something along the lines of
Quoted 6 lines
/* SAF-x-safe MISRA:C 2012 Rule 9.1 <Justification> */ int var;
and then write a generic justification in
docs/misra/safe.json, while <Justification> contains a specific one (e.g., this loop does so and so to ensure that no access to unset variables happens).
But that still mentions the rule. What if a new MISRA:C 2025 appears, with different rule numbers? I don't mind the comment mentioning, in a sufficiently terse way, what problem is circumvented. But I don't think tools or specifications should be referenced here. That's the purpose of the referenced "database" entry.
Jan
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Nicola VetriniHi,
Quoted 7 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.
Would you be able to check if the code below (only compile tested so far) would silence Eclair?
xen/arch/arm/arm64/lib/bitops.c +3 -2
@@ -64,13 +64,15 @@ bool name##_timeout(int nr, volatile void *p, unsigned int max_try) \ }#define testop (name, instr) \ -static always_inline bool int_##name(int nr, volatile void *p, int *oldbit, \ - bool timeout, unsigned int max_try) \ +static always_inline int int_##name(int nr, volatile void *p, \ + bool allow_timeout, bool *timeout, \ + unsigned int max_try) \ { \ volatile uint32_t *ptr = (uint32_t *)p + BITOP_WORD((unsigned
int)nr); \
unsigned int bit = (unsigned int)nr % BITOP_BITS_PER_WORD;
\
const uint32_t mask = BITOP_MASK(bit);
\
unsigned long res, tmp;
\
+ int oldbit;
\
\
do
\
{
\
@@ -79,27 +81,30 @@ static always_inline bool int_##name(int nr, volatile void *p, int *oldbit, \
" lsr %w1, %w3, %w5 // Save old value of bit\n"
\
" " __stringify(instr) " %w3, %w3, %w4 // Toggle bit\n"
\
" stlxr %w0, %w3, %2\n"
\
- : "=&r" (res), "=&r" (*oldbit), "+Q" (*ptr), "=&r" (tmp)
\
+ : "=&r" (res), "=&r" (oldbit), "+Q" (*ptr), "=&r" (tmp)
\
: "r" (mask), "r" (bit)
\
: "memory");
\
\
if ( !res )
\
break;
\
- } while ( !timeout || ((--max_try) > 0) );
\
+ } while ( !allow_timeout || ((--max_try) > 0) );
\
\
dmb(ish);
\
\
- *oldbit &= 1;
\
+ ASSERT(!allow_timeout || timeout);
\
+ if ( timeout )
\
+ *timeout = !res;
\
+ else if ( !res )
\
+ ASSERT_UNREACHABLE();
\
\
- return !res;
\
+ return (oldbit & 1);
\
}
\
\
int name(int nr, volatile void *p)
\
{
\
int oldbit;
\
\
- if ( !int_##name(nr, p, &oldbit, false, 0) )
\
- ASSERT_UNREACHABLE();
\
+ oldbit = int_##name(nr, p, false, NULL, 0);
\
\Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
Quoted 10 lines · Julien Grall
Quoted 7 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Would you be able to check if the code below (only compile tested so far) would silence Eclair?
Hmmm.. I think my e-mail client mangled the diff. Here an unmangled version if needed:
http://paste.debian.net/1286154/
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Nicola VetriniHi Nicola,
I am currently looking at the code to check if we can avoid some SAF-*. But I need some clarification on the usage.
xen/arch/arm/domain_build.c static int __init make_vpl011_uart_node(struct kernel_info *kinfo) · line 2926
Quoted 4 lines · Nicola Vetrini
xen/arch/arm/domain_build.c +0
-0@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo)void *fdt = kinfo->fdt;int res;gic_interrupt_t intr;
This value will be passed to set_interrupt() which contains the following code:
__be32 *cells = interrupt;
[...]dt_set_cells(&cells, ....);
Where gic_interrupt_t is:
typedef __be32 gic_interrupt[3];
This is very similar to the pattern in ...
xen/arch/arm/domain_build.c static void __init initrd_load(struct kernel_info *kinfo) · line 3450
Quoted 10 lines
xen/arch/arm/domain_build.c +2
-0+/* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS +GUEST_ROOT_SIZE_CELLS ];__be32 *cells;struct domain *d = kinfo->d;@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo)paddr_t paddr, len;int node;int res; +/* SAF-1-safe MC3R1.R9.1 */ __be32 val[2 ];
... here.
cellp = (__be32 *)val;dt_set_cell(&cellp, ..., load_addr);
Would you be able to explain why Eclair is complaining for this one but not the previous one?
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
Quoted 37 lines · Julien Grall
Hi Nicola,
I am currently looking at the code to check if we can avoid some SAF-*. But I need some clarification on the usage.
xen/arch/arm/domain_build.c static int __init make_vpl011_uart_node(struct · line 2927
Quoted 5 lines · Nicola Vetrini
xen/arch/arm/domain_build.c +0
-0@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo) void *fdt = kinfo->fdt; int res; gic_interrupt_t intr;This value will be passed to
set_interrupt()which contains the following code:
__be32*cells = interrupt;
[...]dt_set_cells(&cells, ....);Where
gic_interrupt_tis:typedef
__be32gic_interrupt[3];This is very similar to the pattern in ...
xen/arch/arm/domain_build.c static void __init initrd_load(struct · line 3451
Quoted 11 lines
xen/arch/arm/domain_build.c +1
-0+ /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS]; __be32 *cells;struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo) paddr_t paddr, len; int node; int res; + /* SAF-1-safe MC3R1.R9.1 */ __be32 val[2];... here.
Quoted 7 lines
cellp = (
__be32*)val;dt_set_cell(&cellp, ..., load_addr);Would you be able to explain why Eclair is complaining for this one but not the previous one?
Cheers,
It did complain, but I specified an always_write property on the first parameter of set_interrupt, because dt_set_cell does always write it trough the argument cells. (there are three calls to dt_set_cell, which means that the array is fully initialized). The difference is that initrd_load calls dt_set_cell directly. dt_set_cell does indeed initialize val[2], but only because it's called with the appropriate size. If the latter is wrapped in a function or macro, then I can add the same property as above and avoid the deviation.
Regards,
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Jan BeulichQuoted 73 lines · Jan Beulich
Quoted 65 lines · Nicola Vetrini
Quoted 54 lines · Jan Beulich
Quoted 35 lines · Luca Fancellu
Quoted 35 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 16 ++++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 +++ xen/arch/arm/include/asm/guest_atomics.h | 3 +++ 3 files changed, 22 insertions(+)docs/misra/safe.json +8
-0@@ -20,6 +20,22 @@ }, { "id": "SAF-2-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + }, + { + "id": "SAF-3-safe",Quoted 8 lines
docs/misra/safe.json line 28
Quoted 6 lines
docs/misra/safe.json +6
-0+ "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possibly subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen." + },Since the rule and the justification are the same, you can declare only once and use the same tag on top of the offending lines, so /* SAF-2-safe MC3R1.R9.1 */,
+1
I'm puzzled by the wording vs comment placement though: The comments are inserted ahead of the macro invocations, so there are no "following local variables". Plus does this imply the comment would suppress the checking on _all_ of them, rather than just the one that was confirmed to be safe? What if another new one was added, that actually introduces a problem?
Quoted 4 lines
Quoted 1 line
also, I remember some maintainers not happy about the misra rule being put after the tag, now I don’t recall who
Me, at least. The annotations should be tool-agnostic imo, or else the more tools we use, the longer these comments might get.
No problem for both: Given the earlier remarks by Julien on patch 1/4, I think we can devise something along the lines of
Quoted 6 lines
/* SAF-x-safe MISRA:C 2012 Rule 9.1 <Justification> */ int var;
and then write a generic justification in
docs/misra/safe.json, while <Justification> contains a specific one (e.g., this loop does so and so to ensure that no access to unset variables happens).
Quoted 7 lines
But that still mentions the rule. What if a new MISRA:C 2025 appears, with different rule numbers? I don't mind the comment mentioning, in a sufficiently terse way, what problem is circumvented. But I don't think tools or specifications should be referenced here. That's the purpose of the referenced "database" entry.
Jan
Ok
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
In reply to Julien GrallQuoted 12 lines · Julien Grall
Quoted 9 lines · Julien Grall
Quoted 7 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.
Quoted 8 lines
Quoted 1 line
Would you be able to check if the code below (only compile tested so far) would silence Eclair?
Hmmm.. I think my e-mail client mangled the diff. Here an unmangled version if needed:
http://paste.debian.net/1286154/
Cheers,
I have a question: wouldn't this patch also imply an update of xen/arch/arm/include/asm/guest_atomics.h ?
This in particular is the bit of code that needs to be reworked to use the newer *_timeout definition.
bool succeed;
succeed = name##_timeout(nr, p, &oldbit,
this_cpu(guest_safe_atomic_max));
if ( succeed )
return oldbit;
Probably something similar to
int succeed;
succeed = name##_timeout(nr, p, false, NULL, [...]); if ( succeed != 0 )
return succeed;
I'm not really sure whether the third param should be true or false, but I assumed false because it's similar to the use in bitops.c
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 2/4] xen/arm64: bitops: justify uninitialized variable inside a macro
Hi,
Quoted 19 lines · Nicola Vetrini
Quoted 16 lines · Julien Grall
Quoted 10 lines · Julien Grall
Quoted 7 lines · Nicola Vetrini
The macro 'testop' expands to a function that declares the local variable 'oldbit', which is written before being set, but is such a way that is not amenable to automatic checking.
Therefore, a deviation comment, is introduced to document this situation.
A similar reasoning applies to macro '
guest_testop'.Would you be able to check if the code below (only compile tested so far) would silence Eclair?
Hmmm.. I think my e-mail client mangled the diff. Here an unmangled version if needed:
Quoted 8 lines
Quoted 4 lines
http://paste.debian.net/1286154/
Cheers,
I have a question: wouldn't this patch also imply an update of
xen/arch/arm/include/asm/guest_atomics.h?This in particular is the bit of code that needs to be reworked to use the newer
*_timeoutdefinition.
My patch is not changing the exported *_timeout. It is only changing the definition of int_##name which is not exported.
But I have realized that one chunk were missing. Here the new version:
https://paste.debian.net/1286311/
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 3/4] xen/arm: initialize conditionally uninitialized local variables
In reply to Julien GrallSorry for the late reply.
Quoted 41 lines · Julien Grall
Hi,
Quoted 31 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
An initialization to a safe value is provided to reach this aim.
Signed-off-by Nicola Vetrini <[email protected]>---
Additional input on which values may be 'safe' in each context is surely welcome, to avoid possibly compromising the correctness of the function semantics.
---
xen/arch/arm/cpuerrata.c | 6 +++--- xen/arch/arm/domctl.c | 8 ++++---- xen/arch/arm/gic-v3-lpi.c | 17 +++++++++-------- xen/arch/arm/include/asm/p2m.h | 10 ++++++----xen/arch/arm/platforms/xilinx-zynqmp-eemi.c| 10 ++-------- xen/arch/arm/psci.c | 10 +++++----- xen/drivers/char/pl011.c | 2 +-7 files changed, 30 insertions(+), 33 deletions(-)xen/arch/arm/cpuerrata.c +0
-0xen/arch/arm/cpuerrata.c extern char __mitigate_spectre_bhb_loop_start_32[], · line 161
Quoted 4 lines
xen/arch/arm/cpuerrata.c +1
-1@@ -159,7 +159,7 @@ extern char __mitigate_spectre_bhb_loop_start_32[], static int enable_smccc_arch_workaround_1(void *data) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};I understand you desire to make happy. But I am not sure that initializing to 0 is the right thing. If the SMCC were not properly setting the register, then we most likely don't want to install the workaround. Instead, we most likely want to warn.
Quoted 2 lines
So you want (int)res.a0 to be negative. We don't care about the other fields.
In principle I'm ok with this, but see below.
Quoted 10 lines
xen/arch/arm/cpuerrata.c static int enable_spectre_bhb_workaround(void *data) · line 254
Quoted 7 lines
xen/arch/arm/cpuerrata.c +1
-1const struct arm_cpu_capabilities *entry = data; /*@@ -252,7 +252,7 @@ static int enable_spectre_bhb_workaround(void *data) if ( cpus_have_cap(ARM_WORKAROUND_BHB_SMCC_3) ) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Same remark here.
Quoted 10 lines
xen/arch/arm/cpuerrata.c DEFINE_PER_CPU_READ_MOSTLY(register_t, · line 396
Quoted 8 lines
xen/arch/arm/cpuerrata.c +1
-1if ( smccc_ver < SMCCC_VERSION(1, 1) ) goto warn;@@ -393,7 +393,7 @@ DEFINE_PER_CPU_READ_MOSTLY(register_t, ssbd_callback_required); static bool has_ssbd_mitigation(const struct arm_cpu_capabilities *entry) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Here you would want (int)res.a0 to be equal to
ARM_SMCCC_NOT_SUPPORTED.
I see that ARM_SMCCC_NOT_SUPPORTED is
#define ARM_SMCCC_NOT_SUPPORTED (-1)
thus an assignment to res.a0 would violate Rule 10.3:
"The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category." (signed vs unsigned, and the exception does not apply here).
This rule is not yet under discussion, but I would like to avoid knowingly introducing more violations if there's an alternative.
Do the fields of struct arm_smccc_res really need to be unsigned? If that's not the case, then I'm happy with the proposed changes.
Quoted 13 lines
Quoted 13 lines
xen/arch/arm/cpuerrata.c +0
-0bool required; if ( smccc_ver < SMCCC_VERSION(1, 1) )xen/arch/arm/domctl.c +0
-2@@ -29,10 +29,10 @@ static int handle_vuart_init(struct domain *d, struct xen_domctl_vuart_op *vuart_op) { int rc; - struct vpl011_init_info info; -
Quoted 8 lines
xen/arch/arm/domctl.c static int handle_vuart_init(struct domain *d, · line 35
Quoted 6 lines
xen/arch/arm/domctl.c +4
-2- info.console_domid = vuart_op->console_domid; - info.gfn = _gfn(vuart_op->gfn); + struct vpl011_init_info info = { + .console_domid = vuart_op->console_domid, + .gfn = _gfn(vuart_op->gfn) + };I am not against, this change. But I don't quite understand how this makes Eclair much happier?
It also zero-initializes the third field:
struct vpl011_init_info {
domid_t console_domid;
gfn_t gfn;
evtchn_port_t evtchn;
};Quoted 2 lines
Also, if this is the desired way, then I think this should be written down in the
CODING_STYLE.
This is just a matter of style, I can also set the other field explicitly, if you prefer. Either way, it might be a good idea to settle this in the CODING_STYLE in a different patch.
Quoted 62 lines
Quoted 62 lines
xen/arch/arm/domctl.c +0
-0if ( d->creation_finished ) return -EPERM;xen/arch/arm/gic-v3-lpi.c +9
-8@@ -210,7 +210,10 @@ out: void gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id, uint32_t virt_lpi) { - union host_lpi *hlpip, hlpi; + union host_lpi *hlpip, hlpi = { + .virt_lpi = virt_lpi, + .dom_id = domain_id + }; ASSERT(host_lpi >= LPI_OFFSET);@@ -218,9 +221,6 @@ void gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id, hlpip = &lpi_data.host_lpis[host_lpi / HOST_LPIS_PER_PAGE][host_lpi % HOST_LPIS_PER_PAGE]; - hlpi.virt_lpi = virt_lpi; - hlpi.dom_id = domain_id; - write_u64_atomic(&hlpip->data, hlpi.data); }@@ -542,14 +542,15 @@ int gicv3_allocate_host_lpi_block(struct domain *d, uint32_t *first_lpi) for ( i = 0; i < LPI_BLOCK; i++ ) { - union host_lpi hlpi; - /* * Mark this host LPI as belonging to the domain, but don't assign * any virtual LPI or a VCPU yet. */ - hlpi.virt_lpi = INVALID_LPI; - hlpi.dom_id = d->domain_id; + union host_lpi hlpi = { + .virt_lpi = INVALID_LPI, + .dom_id = d->domain_id + }; + write_u64_atomic(&lpi_data.host_lpis[chunk][lpi_idx + i].data, hlpi.data);xen/arch/arm/include/asm/p2m.h +3
-2@@ -345,7 +345,7 @@ static inline struct page_info *get_page_from_gfn( struct domain *d, unsigned long gfn, p2m_type_t *t, p2m_query_t q) { mfn_t mfn; - p2m_type_t _t; + p2m_type_t _t = p2m_invalid; struct page_info *page; /*@@ -355,10 +355,12 @@ static inline struct page_info *get_page_from_gfn( if ( likely(d != dom_xen) ) return p2m_get_page_from_gfn(d, _gfn(gfn), t); - if ( !t ) + /* Allow t to be NULL */ + if ( t )
Quoted 9 lines
xen/arch/arm/include/asm/p2m.h static inline struct page_info *get_page_from_gfn( · line 363
Quoted 6 lines
xen/arch/arm/include/asm/p2m.h +3
-2+ *t = _t; + else { t = &_t; - - *t = p2m_invalid; + }I understand the exist construct is not liked by Eclair. But your new construct is not easily readable by a human. Before I can propose a different approach, can you clarify why Eclair is unhappy this the existing code?
This has been addressed in the reply to patch 1/4.
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/include/asm/p2m.h +0
-0/** DOMID_XEN sees 1-1 RAM. The p2m_type is based on the type of the xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-1@@ -51,11 +51,11 @@ static inline bool domain_has_reset_access(struct domain *d, uint32_t rst) bool zynqmp_eemi(struct cpu_user_regs *regs) { - struct arm_smccc_res res;
Quoted 8 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c static inline bool domain_has_reset_access(struct domain *d, uint32_t rst) · line 58
Quoted 6 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +2
-1+ struct arm_smccc_res res = {0}; uint32_t fid = get_user_reg(regs, 0); uint32_t nodeid = get_user_reg(regs, 1); unsigned int pm_fn = fid & 0xFFFF; - enum pm_ret_status ret; + enum pm_ret_status ret = XST_PM_NO_ACCESS;It is not clear to me why Eclair is unhappy here?
Gotos, as explained in the reply to patch 1/4. Since 'ret' here already sets a value in every branch, I am in favour of initializing it before the switch with that value. The zero-ed 'arm_smccc_res' is ok here?
Quoted 19 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c bool zynqmp_eemi(struct cpu_user_regs *regs) · line 92
Quoted 9 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-1switch ( fid ) {@@ -89,7 +89,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs) { gprintk(XENLOG_WARNING, "zynqmp-pm: fn=%u No access to node %u\n", pm_fn, nodeid); - ret = XST_PM_NO_ACCESS; goto done;I find the new approach less obvious. How about introduce a macro that will set the result and the use replace the two lines with:
set_result(regs, XST_PM_NO_ACCESS);
return 0;where
set_result(regs, ret)is
set_user_reg(regs, 0, ret)
Quoted 11 lines
xen/arch/arm/psci.c static uint32_t psci_cpu_on_nr; · line 40
Quoted 9 lines
xen/arch/arm/psci.c +1
-1@@ -38,7 +38,7 @@ static uint32_t psci_cpu_on_nr; int call_psci_cpu_on(int cpu) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};I don't this we should initialize a0 to 0 as this would indicate a success. Instead, we want an invalid value.
Same remark as stated above about the invalid value being negative.
Quoted 43 lines
xen/arch/arm/psci.c void call_psci_cpu_off(void) · line 53
Quoted 9 lines
xen/arch/arm/psci.c +1
-1arm_smccc_smc(psci_cpu_on_nr, cpu_logical_map(cpu), __pa(init_secondary), &res);@@ -50,7 +50,7 @@ void call_psci_cpu_off(void) { if ( psci_ver > PSCI_VERSION(0, 1) ) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Same here.
xen/arch/arm/psci.c void call_psci_system_reset(void) · line 75
Quoted 7 lines
xen/arch/arm/psci.c +1
-1/* If successfull the PSCI cpu_off call doesn't return */ arm_smccc_smc(PSCI_0_2_FN32_CPU_OFF, &res);@@ -73,7 +73,7 @@ void call_psci_system_reset(void) static int __init psci_features(uint32_t psci_func_id) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Same here.
xen/arch/arm/psci.c static void __init psci_init_smccc(void) · line 117
Quoted 7 lines
xen/arch/arm/psci.c +1
-1if ( psci_ver < PSCI_VERSION(1, 0) ) return PSCI_NOT_SUPPORTED;@@ -115,7 +115,7 @@ static void __init psci_init_smccc(void) if ( psci_features(ARM_SMCCC_VERSION_FID) != PSCI_NOT_SUPPORTED ) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Same here.
xen/arch/arm/psci.c static int __init psci_init_0_2(void) · line 171
Quoted 8 lines
xen/arch/arm/psci.c +1
-1arm_smccc_smc(ARM_SMCCC_VERSION_FID, &res); if ( PSCI_RET(res) != ARM_SMCCC_NOT_SUPPORTED )@@ -168,7 +168,7 @@ static int __init psci_init_0_2(void) { /* sentinel */ }, }; int ret; - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Same here.
Quoted 16 lines
xen/drivers/char/pl011.c static int __init pl011_dt_uart_init(struct · line 289
Quoted 13 lines
xen/arch/arm/psci.c +0
-0if ( acpi_disabled ) {xen/drivers/char/pl011.c +1
-1@@ -285,7 +285,7 @@ static int __init pl011_dt_uart_init(structdt_device_node *dev, const char *config = data; int res; paddr_t addr, size; - uint32_t io_width; + uint32_t io_width = 0;We have many use of dt_property_read_*() within the code base. I would like us te decide of a policy first.
Well, the best outcome for me is if all these functions (i.e., dt_property_*, dt_get_property_*) always write the parameter they are supposed to set. I understand if this is not always feasible, but the alternative is not clean (e.g., deviate or refactor in such a way that the code is less readable).
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 3/4] xen/arm: initialize conditionally uninitialized local variables
Hi,
Quoted 71 lines · Nicola Vetrini
Quoted 41 lines · Julien Grall
Hi,
Quoted 31 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
An initialization to a safe value is provided to reach this aim.
Signed-off-by Nicola Vetrini <[email protected]>---
Additional input on which values may be 'safe' in each context is surely welcome, to avoid possibly compromising the correctness of the function semantics.
---
xen/arch/arm/cpuerrata.c | 6 +++--- xen/arch/arm/domctl.c | 8 ++++---- xen/arch/arm/gic-v3-lpi.c | 17 +++++++++-------- xen/arch/arm/include/asm/p2m.h | 10 ++++++----xen/arch/arm/platforms/xilinx-zynqmp-eemi.c| 10 ++-------- xen/arch/arm/psci.c | 10 +++++----- xen/drivers/char/pl011.c | 2 +-7 files changed, 30 insertions(+), 33 deletions(-)xen/arch/arm/cpuerrata.c +0
-0xen/arch/arm/cpuerrata.c extern char __mitigate_spectre_bhb_loop_start_32[], · line 161
Quoted 4 lines
xen/arch/arm/cpuerrata.c +1
-1@@ -159,7 +159,7 @@ extern char __mitigate_spectre_bhb_loop_start_32[], static int enable_smccc_arch_workaround_1(void *data) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};I understand you desire to make happy. But I am not sure that initializing to 0 is the right thing. If the SMCC were not properly setting the register, then we most likely don't want to install the workaround. Instead, we most likely want to warn.
Quoted 2 lines
So you want (int)res.a0 to be negative. We don't care about the other fields.
In principle I'm ok with this, but see below.
Quoted 10 lines
xen/arch/arm/cpuerrata.c static int enable_spectre_bhb_workaround(void *data) · line 254
Quoted 7 lines
xen/arch/arm/cpuerrata.c +1
-1const struct arm_cpu_capabilities *entry = data; /*@@ -252,7 +252,7 @@ static int enable_spectre_bhb_workaround(void *data) if ( cpus_have_cap(ARM_WORKAROUND_BHB_SMCC_3) ) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Same remark here.
Quoted 10 lines
xen/arch/arm/cpuerrata.c DEFINE_PER_CPU_READ_MOSTLY(register_t, · line 396
Quoted 8 lines
xen/arch/arm/cpuerrata.c +1
-1if ( smccc_ver < SMCCC_VERSION(1, 1) ) goto warn;@@ -393,7 +393,7 @@ DEFINE_PER_CPU_READ_MOSTLY(register_t, ssbd_callback_required); static bool has_ssbd_mitigation(const struct arm_cpu_capabilities *entry) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Here you would want (int)res.a0 to be equal to
ARM_SMCCC_NOT_SUPPORTED.I see that
ARM_SMCCC_NOT_SUPPORTEDis
#define ARM_SMCCC_NOT_SUPPORTED (-1)
Quoted 7 lines
thus an assignment to res.a0 would violate Rule 10.3:
"The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category." (signed vs unsigned, and the exception does not apply here).This rule is not yet under discussion, but I would like to avoid knowingly introducing more violations if there's an alternative.
Do the fields of struct
arm_smccc_resreally need to be unsigned?
Yes, all the fields represent a register. Also, in this context, only the first 32-bit of the register should be taken into account.
That why you will see code using (int)res.a0.
Quoted 31 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/cpuerrata.c +0
-0bool required; if ( smccc_ver < SMCCC_VERSION(1, 1) )xen/arch/arm/domctl.c +0
-2@@ -29,10 +29,10 @@ static int handle_vuart_init(struct domain *d, struct xen_domctl_vuart_op *vuart_op) { int rc; - struct vpl011_init_info info; -Quoted 8 lines
xen/arch/arm/domctl.c static int handle_vuart_init(struct domain *d, · line 35
Quoted 6 lines
xen/arch/arm/domctl.c +4
-2- info.console_domid = vuart_op->console_domid; - info.gfn = _gfn(vuart_op->gfn); + struct vpl011_init_info info = { + .console_domid = vuart_op->console_domid, + .gfn = _gfn(vuart_op->gfn) + };I am not against, this change. But I don't quite understand how this makes Eclair much happier?
It also zero-initializes the third field:
struct
vpl011_init_info{
domid_tconsole_domid;
gfn_tgfn;
evtchn_port_tevtchn;
};Quoted 1 line
Quoted 3 lines
Quoted 1 line
Also, if this is the desired way, then I think this should be written down in the
CODING_STYLE.This is just a matter of style, I can also set the other field explicitly, if you prefer.
I am confused. In a previous reply, I thought you said the following would also make ECLAIR unhappy:
info.console_domid = <...>;
info.gfn = <...>;
info.evtchn = <...>;
Quoted 1 line
Either way, it might be a good idea to settle this in the
CODING_STYLEin a different patch.
Well... The pattern you complain about is fairly common in Xen and I personally prefer to use in some situation over your proposal. So I think we need to agree first that your proposal is the way to go before modifying the code everywhere.
[...]
Quoted 5 lines
Quoted 3 lines
I understand the exist construct is not liked by Eclair. But your new construct is not easily readable by a human. Before I can propose a different approach, can you clarify why Eclair is unhappy this the existing code?
This has been addressed in the reply to patch 1/4.
AFAICT the conversation wasn't settled. We will continue there to avoid duplication.
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/domctl.c +0
-0/** DOMID_XEN sees 1-1 RAM. The p2m_type is based on the type of the xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-1@@ -51,11 +51,11 @@ static inline bool domain_has_reset_access(struct domain *d, uint32_t rst) bool zynqmp_eemi(struct cpu_user_regs *regs) { - struct arm_smccc_res res;Quoted 8 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c static inline bool domain_has_reset_access(struct domain *d, uint32_t rst) · line 58
Quoted 6 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +2
-1+ struct arm_smccc_res res = {0}; uint32_t fid = get_user_reg(regs, 0); uint32_t nodeid = get_user_reg(regs, 1); unsigned int pm_fn = fid & 0xFFFF; - enum pm_ret_status ret; + enum pm_ret_status ret = XST_PM_NO_ACCESS;It is not clear to me why Eclair is unhappy here?
Quoted 1 line
Gotos, as explained in the reply to patch 1/4. Since 'ret' here already sets a value in every branch, I am in favour of initializing it before the switch with that value. The zero-ed '
arm_smccc_res' is ok here?
I am not in favor of this approach. Have you considered my proposal...
Quoted 13 lines
Quoted 13 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c bool zynqmp_eemi(struct cpu_user_regs *regs) · line 92
Quoted 10 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-1switch ( fid ) {@@ -89,7 +89,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs) { gprintk(XENLOG_WARNING, "zynqmp-pm: fn=%u No access to node %u\n", pm_fn, nodeid); - ret = XST_PM_NO_ACCESS; goto done;I find the new approach less obvious. How about introduce a macro that will set the result and the use replace the two lines with:
Quoted 6 lines
Quoted 6 lines
set_result(regs, XST_PM_NO_ACCESS);
return 0;where
set_result(regs, ret)is
set_user_reg(regs, 0, ret)
... here?
[...]
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
xen/drivers/char/pl011.c +0
-0@@ -285,7 +285,7 @@ static int __init pl011_dt_uart_init(structQuoted 9 lines
xen/drivers/char/pl011.c static int __init pl011_dt_uart_init(struct dt_device_node *dev, · line 288
Quoted 6 lines
xen/drivers/char/pl011.c +1
-1dt_device_node *dev, const char *config = data; int res; paddr_t addr, size; - uint32_t io_width; + uint32_t io_width = 0;We have many use of dt_property_read_*() within the code base. I would like us te decide of a policy first.
Quoted 1 line
Well, the best outcome for me is if all these functions (i.e., dt_property_*, dt_get_property_*) always write the parameter they are supposed to set. I understand if this is not always feasible, but the alternative is not clean (e.g., deviate or refactor in such a way that the code is less readable).
I have had a look at the helpers. Here some thoughts
* dt_get_property(): Always setting '*lenp' could result to user starting relying on the 'len' rather than the error code. This could potentially be mitigated by forcing the code to check the value (i.e. adding __must_check).
* dt_property_read_u*(): The best would be to let the user decide what would a default sane value by adding an extra parameter.
* dt_property_read_string(): I would return the string and use ERR_PTR(...) when there is an error.
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 3/4] xen/arm: initialize conditionally uninitialized local variables
Quoted 79 lines · Julien Grall
Hi,
Quoted 76 lines · Nicola Vetrini
Quoted 41 lines · Julien Grall
Hi,
Quoted 31 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
An initialization to a safe value is provided to reach this aim.
Signed-off-by Nicola Vetrini <[email protected]>---
Additional input on which values may be 'safe' in each context is surely welcome, to avoid possibly compromising the correctness of the function semantics.
---
xen/arch/arm/cpuerrata.c | 6 +++--- xen/arch/arm/domctl.c | 8 ++++---- xen/arch/arm/gic-v3-lpi.c | 17 +++++++++-------- xen/arch/arm/include/asm/p2m.h | 10 ++++++----xen/arch/arm/platforms/xilinx-zynqmp-eemi.c| 10 ++-------- xen/arch/arm/psci.c | 10 +++++----- xen/drivers/char/pl011.c | 2 +-7 files changed, 30 insertions(+), 33 deletions(-)xen/arch/arm/cpuerrata.c +0
-0xen/arch/arm/cpuerrata.c extern char __mitigate_spectre_bhb_loop_start_32[], · line 161
Quoted 4 lines
xen/arch/arm/cpuerrata.c +1
-1@@ -159,7 +159,7 @@ extern char __mitigate_spectre_bhb_loop_start_32[], static int enable_smccc_arch_workaround_1(void *data) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};I understand you desire to make happy. But I am not sure that initializing to 0 is the right thing. If the SMCC were not properly setting the register, then we most likely don't want to install the workaround. Instead, we most likely want to warn.
Quoted 2 lines
So you want (int)res.a0 to be negative. We don't care about the other fields.
In principle I'm ok with this, but see below.
Quoted 11 lines
xen/arch/arm/cpuerrata.c static int enable_spectre_bhb_workaround(void · line 255
Quoted 8 lines
xen/arch/arm/cpuerrata.c +1
-1const struct arm_cpu_capabilities *entry = data; /*@@ -252,7 +252,7 @@ static int enable_spectre_bhb_workaround(void *data) if ( cpus_have_cap(ARM_WORKAROUND_BHB_SMCC_3) ) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Same remark here.
Quoted 10 lines
xen/arch/arm/cpuerrata.c DEFINE_PER_CPU_READ_MOSTLY(register_t, · line 396
Quoted 8 lines
xen/arch/arm/cpuerrata.c +1
-1if ( smccc_ver < SMCCC_VERSION(1, 1) ) goto warn;@@ -393,7 +393,7 @@ DEFINE_PER_CPU_READ_MOSTLY(register_t, ssbd_callback_required); static bool has_ssbd_mitigation(const struct arm_cpu_capabilities *entry) { - struct arm_smccc_res res; + struct arm_smccc_res res = {0};Here you would want (int)res.a0 to be equal to
ARM_SMCCC_NOT_SUPPORTED.I see that
ARM_SMCCC_NOT_SUPPORTEDis
#define ARM_SMCCC_NOT_SUPPORTED (-1)thus an assignment to res.a0 would violate Rule 10.3:
"The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category." (signed vs unsigned, and the exception does not apply here).
Quoted 8 lines
Quoted 3 lines
This rule is not yet under discussion, but I would like to avoid knowingly introducing more violations if there's an alternative.
Do the fields of struct
arm_smccc_resreally need to be unsigned?Yes, all the fields represent a register. Also, in this context, only the first 32-bit of the register should be taken into account.
That why you will see code using (int)res.a0.
If the purpose of defining it to -1 is to have the value 0xffffffffffffffff, then it could be defined as ~0UL.
Quoted 31 lines
Quoted 31 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/cpuerrata.c +0
-0bool required; if ( smccc_ver < SMCCC_VERSION(1, 1) )xen/arch/arm/domctl.c +0
-2@@ -29,10 +29,10 @@ static int handle_vuart_init(struct domain *d, struct xen_domctl_vuart_op *vuart_op) { int rc; - struct vpl011_init_info info; -Quoted 8 lines
xen/arch/arm/domctl.c static int handle_vuart_init(struct domain *d, · line 35
Quoted 6 lines
xen/arch/arm/domctl.c +4
-2- info.console_domid = vuart_op->console_domid; - info.gfn = _gfn(vuart_op->gfn); + struct vpl011_init_info info = { + .console_domid = vuart_op->console_domid, + .gfn = _gfn(vuart_op->gfn) + };I am not against, this change. But I don't quite understand how this makes Eclair much happier?
It also zero-initializes the third field:
struct
vpl011_init_info{
domid_tconsole_domid;
gfn_tgfn;
evtchn_port_tevtchn;
};Quoted 1 line
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
Also, if this is the desired way, then I think this should be written down in the
CODING_STYLE.This is just a matter of style, I can also set the other field explicitly, if you prefer.
I am confused. In a previous reply, I thought you said the following would also make ECLAIR unhappy:
Quoted 1 line
info.console_domid = <...>;
info.gfn = <...>;
info.evtchn = <...>;
If I did (I can't find it right now, but I'll try to dig it up), I'd say that I was either wrong or I forgot to mention some other field that wasn't initialized by the series of statements.
Quoted 7 lines
Quoted 1 line
Either way, it might be a good idea to settle this in the
CODING_STYLEin a different patch.Well... The pattern you complain about is fairly common in Xen and I personally prefer to use in some situation over your proposal. So I think we need to agree first that your proposal is the way to go before modifying the code everywhere.
[...]
Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I understand the exist construct is not liked by Eclair. But your new construct is not easily readable by a human. Before I can propose a different approach, can you clarify why Eclair is unhappy this the existing code?
This has been addressed in the reply to patch 1/4.
AFAICT the conversation wasn't settled. We will continue there to avoid duplication.
Sure.
Quoted 38 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/domctl.c +0
-0/** DOMID_XEN sees 1-1 RAM. The p2m_type is based on the type of the xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-1@@ -51,11 +51,11 @@ static inline bool domain_has_reset_access(struct domain *d, uint32_t rst) bool zynqmp_eemi(struct cpu_user_regs *regs) { - struct arm_smccc_res res;Quoted 8 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c static inline bool domain_has_reset_access(struct domain *d, uint32_t rst) · line 58
Quoted 6 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +2
-1+ struct arm_smccc_res res = {0}; uint32_t fid = get_user_reg(regs, 0); uint32_t nodeid = get_user_reg(regs, 1); unsigned int pm_fn = fid & 0xFFFF; - enum pm_ret_status ret; + enum pm_ret_status ret = XST_PM_NO_ACCESS;It is not clear to me why Eclair is unhappy here?
Quoted 1 line
Gotos, as explained in the reply to patch 1/4. Since 'ret' here already sets a value in every branch, I am in favour of initializing it before the switch with that value. The zero-ed '
arm_smccc_res' is ok here?I am not in favor of this approach. Have you considered my proposal...
Quoted 13 lines
Quoted 13 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c bool zynqmp_eemi(struct cpu_user_regs *regs) · line 92
Quoted 10 lines
xen/arch/arm/platforms/xilinx-zynqmp-eemi.c +0
-1switch ( fid ) {@@ -89,7 +89,6 @@ bool zynqmp_eemi(struct cpu_user_regs *regs) { gprintk(XENLOG_WARNING, "zynqmp-pm: fn=%u No access to node %u\n", pm_fn, nodeid); - ret = XST_PM_NO_ACCESS; goto done;I find the new approach less obvious. How about introduce a macro that will set the result and the use replace the two lines with:
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
set_result(regs, XST_PM_NO_ACCESS);
return 0;where
set_result(regs, ret)is
set_user_reg(regs, 0, ret)... here?
Sorry, I missed it when looking at the reply. I'll look into it, but it seems good. It would remove the need for the local variable, if I understand it correctly.
Quoted 18 lines
[...]
Quoted 15 lines
Quoted 14 lines
xen/drivers/char/pl011.c static int __init pl011_dt_uart_init(struct · line 289
Quoted 11 lines
xen/drivers/char/pl011.c +1
-1@@ -285,7 +285,7 @@ static int __init pl011_dt_uart_init(structdt_device_node *dev, const char *config = data; int res; paddr_t addr, size; - uint32_t io_width; + uint32_t io_width = 0;We have many use of dt_property_read_*() within the code base. I would like us te decide of a policy first.
Quoted 6 lines
Quoted 1 line
Well, the best outcome for me is if all these functions (i.e., dt_property_*, dt_get_property_*) always write the parameter they are supposed to set. I understand if this is not always feasible, but the alternative is not clean (e.g., deviate or refactor in such a way that the code is less readable).
I have had a look at the helpers. Here some thoughts
*
dt_get_property(): Always setting '*lenp' could result to user starting relying on the 'len' rather than the error code. This could potentially be mitigated by forcing the code to check the value (i.e. adding__must_check).
Note that there is a Directive (D4.10) already in docs/misra/rules.rst that prescribes the caller to check return values:
"If a function returns error information, then that error information shall be tested" (by checking we mean testing it in some way, but as is the case for many directives, the checking this is not fully automated).
Quoted 4 lines
* dt_property_read_u*(): The best would be to let the user decide what would a default sane value by adding an extra parameter.
Quoted 1 line
*
dt_property_read_string(): I would return the string and use
ERR_PTR(...)when there is an error.
Ok, good.
Regards,
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Julien GrallQuoted 31 lines · Julien Grall
Hi Nicola,
Quoted 28 lines · Nicola Vetrini
Quoted 27 lines · Julien Grall
Hi Nicola,
Quoted 10 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
To achieve this goal, I adopted the following strategies:
Please let's at least one patch per strategy. I would also consider some of the rework separate so they can go in regardless the decision for the SAF-*.
Quoted 10 lines
- Add a suitably formatted local deviation comment (as indicated in '
docs/misra/documenting-violations.rst') to exempt the following line from checking.- Provide an initialization for the variable at the declaration.
- Substitute a goto breaking out of control flow logic with a semantically
equivalent do { .. } while(0).
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
As I already mentioned in private, it is unclear to me how you decided which strategy to use. I still think we need to define our policy before changing the code. Otherwise, it is going to be difficult to decide for new code.
The main point of this RFC is doing so. From what I gathered, it's not an easy task: sometimes there are no 'safe' values to initialize variables to and sometimes there is no easy way to prove that indeed something is always initialized or not accessed at all.
But you wrote the code. So you should be able to explain how you took the decision between one and the others.
Also, even if this is an RFC, it would have been good to summarize any discussion that happened in private and if there were concern try to come up with ideas or at least listing the concerns after '---.
I'll keep this if the need arises in the future.
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 29 lines
Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 8 +++++++xen/arch/arm/arm64/lib/find_next_bit.c| 1 + xen/arch/arm/bootfdt.c | 6 +++++ xen/arch/arm/decode.c | 2 ++ xen/arch/arm/domain_build.c | 29 ++++++++++++++++++---- xen/arch/arm/efi/efi-boot.h | 6 +++-- xen/arch/arm/gic-v3-its.c | 9 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33
+++++++++++++++-----------9 files changed, 69 insertions(+), 26 deletions(-)docs/misra/safe.json +5
-0@@ -12,6 +12,14 @@ }, { "id": "SAF-1-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possiblyQuoted 1 line
subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen."
I am bit concerned which such statement because the code instance was today with the current code. This could change in the future and invalide the reasoning.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
It is not clear to me if we have any mechanism to prevent that. If we don't, then I think we need to drastically reduce the number of time this is used (there are a bit too much for my taste).
Indeed, the purpose of such a deviation is that the sound overapproximation computed by the tool requires a human to look at the code and think twice before modifying it (i.e., if ever that code is touched, the reviewer ought to assess whether that justification still holds or some other thing should be done about it.
Your assumption is the reviewer will notice there is an existing devitation and be able to assess it has changed. I view this assumption as risky in the long term.
Have you investigate to improve the automatic tooling?
Well, as discussed elsewhere in the thread, a slightly modified version of this deviation comment can list the specific reason why such a thing was deviated directly at the declaration or where the caution is, if you think this is better.
Example:
// <- SAF-x here
int var;
[...]
// <- or HEREf(&var);
An alternative approach to justification, partly discussed with Stefano in private is a macro that looks like an attribute to signal that the variable is intentionally uninitialized. This does not have the benefit of a written justification with a proper comment or an entry in the json file, but is less intrusive and the justification for all occurrences of __uninit w.r.t R9.1 would be included in the static analysis tool configuration, which would be part of the MISRA compliance documentation. This does imply a coarse justification like the one above, but if further clarification is needed it can be provided locally in the code, as guidance for contributors.
Example:
#define __uninit
__uninit int x;
Quoted 60 lines
Quoted 60 lines
Quoted 12 lines
Quoted 12 lines
+ },
+ {
+ "id": "SAF-2-safe",
"analyser": {},
"name": "Sentinel",
"text": "Next ID to be used"xen/arch/arm/arm64/lib/find_next_bit.c +0
-0@@ -67,6 +67,7 @@ unsigned long find_next_zero_bit(const unsignedQuoted 8 lines
xen/arch/arm/arm64/lib/find_next_bit.c unsigned long find_next_zero_bit(const unsigned · line 72
Quoted 6 lines
xen/arch/arm/arm64/lib/find_next_bit.c +1
-0long *addr, unsigned long size, { const unsigned long *p = addr + BIT_WORD(offset); unsigned long result = offset & ~(BITS_PER_LONG-1); + /* SAF-1-safe MC3R1.R9.1 */ unsigned long tmp;This is a file that was copied as-is from Linux. I thought we exclude them.
There was an expansion location in the report that caused that file to appear in the output. Thanks for reporting it.
Quoted 2 lines
Anyway, can you provide some details why Eclair thinks this is may be used unitialized?
The problem lies in the goto skipping out of control flow structures. It can't be refactored with a "do { tmp=...; break; } while(0)", as done elsewhere, because there are two distinct labels in there, so the semantics would be altered. The same comment applies in other instances as well.
Quoted 15 lines
xen/arch/arm/bootfdt.c static bool __init device_tree_node_matches(const · line 39
Quoted 13 lines
xen/arch/arm/arm64/lib/find_next_bit.c +0
-0if (offset >= size)xen/arch/arm/bootfdt.c +1
-0@@ -34,6 +34,7 @@ static bool __init device_tree_node_matches(const void *fdt, int node, static bool __init device_tree_node_compatible(const void *fdt, int node, const char *match) { + /* SAF-1-safe MC3R1.R9.1 */ int len, l;It is unclear to me whether the SAF-* applies to 'len' or 'l'.
Just len, they need to be two separate declarations if it is agreed not to initialize it. Good catch.
Quoted 14 lines
xen/arch/arm/bootfdt.c int __init device_tree_for_each_node(const void · line 177
Quoted 11 lines
xen/arch/arm/bootfdt.c +2
-0const void *prop;@@ -169,7 +170,9 @@ int __init device_tree_for_each_node(const void*fdt, int node, */ int depth = 0; const int first_node = node; + /* SAF-1-safe MC3R1.R9.1 */ u32 address_cells[DEVICE_TREE_MAX_DEPTH]; + /* SAF-1-safe MC3R1.R9.1 */ u32 size_cells[DEVICE_TREE_MAX_DEPTH];I have never sent formally, but in the past I proposed to rework the code so {address, size}_cells[0] would be initialized before the loop. See https://lore.kernel.org/all/[email protected]/.
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
Do you think this would help?
If that initialization is applied to all the array elements yes, because the Amplification of R9.1 says:
"For the purposes of this rule, an array element or structure member shall be considered as a discrete
object".
Otherwise, initializing just the first element does not improve the situation in any way, and this is currently not within of the analysis capabilities of ECLAIR.Any plan to improve ECLAIR?
Not in the short term on this. See further below.
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
xen/arch/arm/bootfdt.c static void __init process_multiboot_node(const · line 260
Quoted 13 lines
xen/arch/arm/bootfdt.c +2
-0int ret;@@ -249,8 +252,10 @@ static void __init process_multiboot_node(const void *fdt, int node, const __be32 *cell; bootmodule_kind kind; paddr_t start, size; + /* SAF-1-safe MC3R1.R9.1 */ int len; /* sizeof("/chosen/") + DT_MAX_NAME + '/' + DT_MAX_NAME + '/0' => 92 */ + /* SAF-1-safe MC3R1.R9.1*/ char path[92];So the two above, is one category of issue. The variables are passed as argument of function which will fill them.
Can Eclair look at the callers, if so, can we consider to always initialize the values in the callee?
This would reduce the number of SAF-*. There are a few other examples like that below. So I will skip them for now.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
[...]
If the value is always initialized in the callee, then there's no problem configuring ECLAIR so that it knows that this parameter is always written, and therefore any subsequent use in the caller is ok.
Another possibility is stating that a function never reads the pointee before writing to it (it may or may not write it, but if it doesn't, then the pointee is not read either). The 'strncmp' after '
fdt_get_path' does get in the way, though, because this property is not strong enough to ensure that we can use 'path' after returning from the function.I am not sure I fully understand what you wrote. Can you provide a C example?
void f(int *x) {
if(x) {
*x = 10;
int y =*x; // read the pointee after it's initialized
} else {
int z; // in this branch the pointee is not read nor written
}
// we can say that f never reads *x before (possibly) writing to it.
}
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
xen/arch/arm/domain_build.c custom_param("dom0_mem", parse_dom0_mem); · line 64
Quoted 10 lines
xen/arch/arm/domain_build.c +1
-1@@ -62,7 +62,7 @@ custom_param("dom0_mem", parse_dom0_mem); int __init parse_arch_dom0_param(const char *s, const char *e) { - long long val; + long long val = LLONG_MAX;Can you explain why you decided to initialize rather than SAF-? For clarity, I am not asking to switch to SAF-, I am mainly interested what were your though process.
Quoted 4 lines
Quoted 1 line
I saw that putting a large enough value here would be out of range and trigger the printk even if the conditional leads to somehow enter the if branch with an invalid value.
Ok. This wants to be documented on top of the assignment.
Ok. While testing this I also switched to LONG_MAX, whose definition is already available in that file, and does not alter the semantics anyway.
Quoted 147 lines
Quoted 138 lines
Quoted 128 lines
Quoted 128 lines
xen/arch/arm/domain_build.c +6
-0if ( !parse_signed_integer("sve", s, e, &val) ) {@@ -1077,6 +1077,7 @@ static void __init assign_static_memory_11(struct domain *d, static int __init handle_linux_pci_domain(struct kernel_info *kinfo, const struct dt_device_node *node) { + /* SAF-1-safe MC3R1.R9.1 */ uint16_t segment; int res;@@ -1351,6 +1352,7 @@ static int __init make_memory_node(conststruct domain *d, unsigned int i; int res, reg_size = addrcells + sizecells; int nr_cells = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[NR_MEM_BANKS * 4 /* Worst case addrcells + sizecells */]; __be32 *cells;@@ -1578,6 +1580,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo, struct rangeset *unalloc_mem; paddr_t start, end; unsigned int i; + /* SAF-1-safe MC3R1.R9.1 */ int res; dt_dprintk("Find unallocated memory for extended regions\n");@@ -1727,6 +1730,7 @@ static int __init find_memory_holes(conststruct kernel_info *kinfo, dt_for_each_device_node( dt_host, np ) { unsigned int naddr; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; naddr = dt_number_of_address(np);@@ -1976,9 +1980,11 @@ static int __init make_cpus_node(const struct domain *d, void *fdt) const struct dt_device_node *npcpu; unsigned int cpu; const void *compatible = NULL; + /* SAF-1-safe MC3R1.R9.1 */ u32 len; /* Placeholder for cpu@ + a 32-bit hexadecimal number + \0 */ char buf[13]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; /* Keep the compiler happy with -Og */bool
clock_valid= false;xen/arch/arm/domain_build.c +3
-0@@ -2104,6 +2110,7 @@ static int __init make_gic_node(const struct domain *d, void *fdt, const struct dt_device_node *gic = dt_interrupt_controller; int res = 0; const void *addrcells, *sizecells; + /* SAF-1-safe MC3R1.R9.1 */ u32 addrcells_len, sizecells_len; /*@@ -2179,6 +2186,7 @@ static int __init make_timer_node(const struct kernel_info *kinfo) int res; unsigned int irq[MAX_TIMER_PPI]; gic_interrupt_t intrs[3]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; bool clock_valid;@@ -2511,6 +2519,7 @@ static int __init handle_device(struct domain *d, struct dt_device_node *dev, unsigned int naddr; unsigned int i; int res; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; bool own_device = !dt_device_for_passthrough(dev);/*
xen/arch/arm/domain_build.c +1
-0@@ -2779,6 +2788,7 @@ static int __init make_gicv2_domU_node(struct kernel_info *kinfo) { void *fdt = kinfo->fdt; int res = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[(GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS) * 2]; __be32 *cells;const struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo) void *fdt = kinfo->fdt; int res; gic_interrupt_t intr; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS]; __be32 *cells;struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo) paddr_t paddr, len; int node; int res; + /* SAF-1-safe MC3R1.R9.1 */ __be32 val[2]; __be32 *cellp;void
__iomem*initrd;xen/arch/arm/domain_build.c +5
-1@@ -3514,6 +3526,7 @@ static int __init get_evtchn_dt_property(conststruct dt_device_node *np, uint32_t *port, uint32_t *phandle) { const __be32 *prop = NULL; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t len; prop = dt_get_property(np, "xen,evtchn", &len);@@ -3538,10 +3551,13 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np, static int __init alloc_domain_evtchn(struct dt_device_node *node) { int rc; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t domU1_port, domU2_port, remote_phandle; struct dt_device_node *remote_node; const struct dt_device_node *p1_node, *p2_node; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_alloc_unbound alloc_unbound; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_bind_interdomain bind_interdomain; struct domain *d1 = NULL, *d2 = NULL;@@ -3789,11 +3805,12 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo) static int __init alloc_xenstore_evtchn(struct domain *d) { - evtchn_alloc_unbound_t alloc; + evtchn_alloc_unbound_t alloc = {Quoted 9 lines
xen/arch/arm/domain_build.c static int __init construct_domain(struct · line 3795
Quoted 6 lines
xen/arch/arm/domain_build.c +3
-2+ .dom = d->domain_id, + .remote_dom = hardware_domain->domain_id + }; int rc; - alloc.dom = d->domain_id; - alloc.remote_dom = hardware_domain->domain_id;It is not clear why this is modified. There are only two fields in 'alloc' and they are both initialized.
Quoted 8 lines
There are three fields in the struct, and the third is clearly to be set by an initalization function. If, as above with 'path', the init function ensures that the third field is always written then there's no problem, otherwise this initialization prevents any use of an uninitialized port.
Quoted 1 line
struct
evtchn_alloc_unbound{/* IN parameters */
domid_tdom,remote_dom;
/* OUT parameters */
evtchn_port_tport;
};
typedef structevtchn_alloc_unboundevtchn_alloc_unbound_t;
Quoted 4 lines
Ah, I didn't spot the third field because it was defined on the same line. I would prefer if the value is explicitely initialized to 0.
Also, I think this ought to be in a separate patch (with other similar pattern).
Ok.
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
xen/arch/arm/domain_build.c static int __init construct_domU(struct domain · line 3831
Quoted 10 lines
xen/arch/arm/domain_build.c +1
-1rc = evtchn_alloc_unbound(&alloc, 0); if ( rc ) {@@ -3810,8 +3827,9 @@ static int __init construct_domU(struct domain *d, const struct dt_device_node *node) { struct kernel_info kinfo = {}; - const char *dom0less_enhanced; + const char *dom0less_enhanced = NULL;If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL. So it is unclear to me how this value is safer. > Looking at the code, I wonder whether we should convertdt_property_read_string()to useERR_PTR(). So we could remove the last argument and return it instead.
Quoted 4 lines
Quoted 1 line
Is relying on that assumption somehow safer?
I am assuming you are referring to "If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL". Note that I didn't suggest it is safer. I am only pointed out that you didn't specify how this was better in the context of the code.
This should be probably discussed after deciding on the refactoring 'dt_property_read_string'
Quoted 67 lines
Quoted 67 lines
The suggestion to remove the last parameter seems better.
Quoted 64 lines
Quoted 55 lines
xen/arch/arm/domain_build.c +1
-0int rc; + /* SAF-1-safe MC3R1.R9.1 */ u64 mem; u32 p2m_mem_mb;unsigned long
p2m_pages;xen/arch/arm/domain_build.c +1
-0@@ -3939,6 +3957,7 @@ void __init create_domUs(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), }; unsigned int flags = 0U; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t val; int rc;xen/arch/arm/efi/efi-boot.h +1
-0@@ -117,6 +117,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells) static int __init fdt_set_reg(void *fdt, int node, int addr_cells, int size_cells, uint64_t addr, uint64_t len) { + /* SAF-1-safe MC3R1.R9.1 */
__be32val[4]; /* At most 2 64 bit values to be stored */__be32*cellp;xen/arch/arm/efi/efi-boot.h +3
-2@@ -308,7 +309,7 @@ fdt_set_fail: static void __init *fdt_increase_size(struct file *fdtfile, int add_size) { EFI_STATUS status; - EFI_PHYSICAL_ADDRESS fdt_addr; + EFI_PHYSICAL_ADDRESS fdt_addr = 0; int fdt_size; int pages; void *new_fdt;@@ -433,7 +434,7 @@ static void __init efi_arch_cfg_file_late(constEFI_LOADED_IMAGE *image, static void *__init efi_arch_allocate_mmap_buffer(UINTN map_size) { - void *ptr; + void *ptr = NULL; EFI_STATUS status; status = efi_bs->AllocatePool(EfiLoaderData, map_size, &ptr);@@ -538,6 +539,7 @@ static void __init efi_arch_handle_module(conststruct file *file, { int node; int chosen; + /* SAF-1-safe MC3R1.R9.1 */ int addr_len, size_len; if ( file == &dtbfile )xen/arch/arm/gic-v3-its.c +0
-0xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its · line 195
Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1
-2@@ -192,8 +192,7 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, cmd[0] = GITS_CMD_MAPC; cmd[1] = 0x00; - cmd[2] = encode_rdbase(its, cpu, collection_id); - cmd[2] |= GITS_VALID_BIT; + cmd[2] = encode_rdbase(its, cpu, collection_id) | GITS_VALID_BIT;Hmmm... How is this even considered as unitialized variable?
Quoted 6 lines
Quoted 1 line
The analysis here could use some more precision, but the modified construct is entirely equivalent.
I agree that they are equivalent. But in general, we don't change the style of the construct without explaining why.
In this case, the first step would be to improve Eclair.
The changes needed for this kind of analysis are not trivial: we've looked into this, but there's no easy way to support this in a timely manner. I understand that this is an estabilished pattern, but what would you think of an initializer using designators?
uint64_t cmd[4] = {
.[0] = GITS_CMD_MAPC;
.[1] = 0x00;
.[2] = encode_rdbase(its, cpu, collection_id) | GITS_VALID_BIT;
.[3] = 0x00;
}Quoted 58 lines
Quoted 53 lines
Quoted 53 lines
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapd(struct host_its · line 218
Quoted 12 lines
xen/arch/arm/gic-v3-its.c +1
-3cmd[3] = 0x00; return its_send_command(its, cmd);@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, } cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32); cmd[1] = size_bits; - cmd[2] = itt_addr; - if ( valid ) - cmd[2] |= GITS_VALID_BIT; + cmd[2] = itt_addr | (valid ? GITS_VALID_BIT : 0x00);Same here.
Quoted 26 lines
xen/arch/arm/mm.c +1
-0@@ -935,6 +935,7 @@ static int xen_pt_update_entry(mfn_t root,unsigned long virt, mfn_t mfn, unsigned int target, unsigned int flags) { + /* SAF-1-safe MC3R1.R9.1 */ int rc; unsigned int level;
lpae_t*table;xen/arch/arm/p2m.c +1
-1@@ -496,16 +496,18 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m,gfn_t gfn, lpae_t entry, *table; int rc; mfn_t mfn = INVALID_MFN; - p2m_type_t _t; + p2m_type_t _t = p2m_invalid; DECLARE_OFFSETS(offsets, addr); ASSERT(p2m_is_locked(p2m));xen/arch/arm/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, · line 509
Quoted 10 lines
xen/arch/arm/p2m.c +5
-3BUILD_BUG_ON(THIRD_MASK != PAGE_MASK); /* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; + if( t ) { + *t = _t; + } else { + t = &_t; + }What was the problem with the previous code?
Quoted 4 lines
Quoted 2 lines
This is also not conformant to Xen coding style.
The problem is that _t may be uninitialized, hence assigning its address to t could be problematic.
Quoted 8 lines
But the value is set right after. IOW, there is no read between. So how is this prob
Quoted 1 line
Another way to address this is to initialize _t to a bad value and use this variable in the body, then assign to t based on the value just before returning.
IHMO, neither solution are ideal. I think we should investigate whether Eclair can be improved.
[...]
I'll see what can be done about it, I'll reply when I have an answer.
Quoted 78 lines
Quoted 46 lines
Quoted 11 lines
xen/arch/arm/p2m.c static int __p2m_set_entry(struct p2m_domain · line 1038
Quoted 9 lines
xen/arch/arm/p2m.c +1
-0if ( valid ) *valid = false;@@ -1031,6 +1033,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m, unsigned int level = 0; unsigned int target = 3 - (page_order / XEN_PT_LPAE_SHIFT); lpae_t *entry, *table, orig_pte; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same issue with gotos explained above, can't be refactored because of the for enclosing the goto.
Quoted 14 lines
xen/arch/arm/p2m.c static inline int p2m_remove_mapping(struct · line 1491
Quoted 10 lines
xen/arch/arm/p2m.c +0
-0/* A mapping is removed if the MFN is invalid. */bool
removing_mapping=mfn_eq(smfn, INVALID_MFN);xen/arch/arm/p2m.c +1
-0@@ -1483,6 +1486,7 @@ static inline int p2m_remove_mapping(struct domain *d, { struct p2m_domain *p2m = p2m_get_hostp2m(d); unsigned long i; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same as above.
Quoted 5 lines
Quoted 5 lines
xen/arch/arm/p2m.c +0
-2p2m_write_lock(p2m);@@ -1685,20 +1689,21 @@ static int p2m_alloc_vmid(struct domain *d) ASSERT(nr != INVALID_VMID); - if ( nr == MAX_VMID ) - {Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1691
Quoted 6 lines
xen/arch/arm/p2m.c +1
-4- rc = -EBUSY; - printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); - goto out; - } + do {I don't understand this change. How is this making better for Eclair?
Quoted 1 line
This is an example where the goto can be eliminated, which in turn allows to automatically check the correctness.
If you want to eliminate the 'goto' then they are better way to do it. Like:
xen/arch/arm/p2m.c +6
-8@@ -186,16 +186,14 @@ int p2m_alloc_vmid(struct domain *d) { rc = -EBUSY; printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); - goto out; + } + else + { + set_bit(nr, vmid_mask); + p2m->vmid = nr; + rc = 0; } - set_bit(nr, vmid_mask); - - p2m->vmid = nr; - - rc = 0; - -out: spin_unlock(&vmid_alloc_lock); return rc;}
Quoted 2 lines
I have a slight preference with the goto version, but I could accept it if Eclair can't cope with the construct. In any case, this is the sort of change that deserve its own patch as you want to explain why Eclair can't cope with such construct (I don't view it as complex).
ok
Quoted 30 lines
Quoted 22 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/p2m.c +7
-2+ if ( nr == MAX_VMID ) + { + rc = -EBUSY; + printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); + break; + } - set_bit(nr, vmid_mask); + set_bit(nr, vmid_mask); - p2m->vmid = nr;Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1705
Quoted 8 lines
xen/arch/arm/p2m.c +3
-2+ p2m->vmid = nr; - rc = 0; + rc = 0; + } while ( 0 ); -out: spin_unlock(&vmid_alloc_lock); return rc; }Quoted 7 lines
Considering all of the replies above, a first draft of a strategy/policy I can think of is having:
- Initializer functions that always write their parameter, so that the
strongest "pointee always written" property can be stated. This causes all further uses to be marked safe.- Initialize the variable when there exists a known safe value that
does not alter the semantics of the function. The initialization does not need to be at the declaration, but doing so simplifies the code.
Quoted 7 lines
As I mentionned in private there are two risks with that:
1. You silence compiler to spot other issues
2. You may now get warning from Coverity if it spots you set a value
that get overwritten before its first use.So I think such approach should be used with parcimony. Instead, we should look at reworking the code when possible.
Do you think it would help if you look directly at actual cautions to spot possible functions that can be refactored?
Quoted 6 lines
Quoted 3 lines
- Deviate all cases where any of the previous does not apply, with a
comment deviation that refers to a justification reporting that the code has been checked to respect the rule (keep in mind that _violations_ to a Mandatory rule such as R9.1 are not allowed to claim MISRA compliance).See above for my concern about adding so many deviations. But I am confused with what you wrote. If the rule is mandatory, then why are you trying to add deviation in Xen? Who is going to solve them to make Xen MISRA compliant?
Because only cautions were found, not violations (which cannot be deviated). In the former case Xen can say that the code does not violate the rule, but it can't be proven by the tool.
Regards,
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
Quoted 559 lines · Nicola Vetrini
Quoted 31 lines · Julien Grall
Hi Nicola,
Quoted 28 lines · Nicola Vetrini
Quoted 27 lines · Julien Grall
Hi Nicola,
Quoted 10 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
To achieve this goal, I adopted the following strategies:
Please let's at least one patch per strategy. I would also consider some of the rework separate so they can go in regardless the decision for the SAF-*.
Quoted 10 lines
- Add a suitably formatted local deviation comment (as indicated in '
docs/misra/documenting-violations.rst') to exempt the following line from checking.- Provide an initialization for the variable at the declaration.
- Substitute a goto breaking out of control flow logic with a semantically
equivalent do { .. } while(0).Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
As I already mentioned in private, it is unclear to me how you decided which strategy to use. I still think we need to define our policy before changing the code. Otherwise, it is going to be difficult to decide for new code.
The main point of this RFC is doing so. From what I gathered, it's not an easy task: sometimes there are no 'safe' values to initialize variables to and sometimes there is no easy way to prove that indeed something is always initialized or not accessed at all.
But you wrote the code. So you should be able to explain how you took the decision between one and the others.
Also, even if this is an RFC, it would have been good to summarize any discussion that happened in private and if there were concern try to come up with ideas or at least listing the concerns after '---.
I'll keep this if the need arises in the future.
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 29 lines
Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 8 +++++++xen/arch/arm/arm64/lib/find_next_bit.c| 1 + xen/arch/arm/bootfdt.c | 6 +++++ xen/arch/arm/decode.c | 2 ++ xen/arch/arm/domain_build.c | 29 ++++++++++++++++++---- xen/arch/arm/efi/efi-boot.h | 6 +++-- xen/arch/arm/gic-v3-its.c | 9 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33
+++++++++++++++-----------9 files changed, 69 insertions(+), 26 deletions(-)docs/misra/safe.json +5
-0@@ -12,6 +12,14 @@ }, { "id": "SAF-1-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possiblyQuoted 1 line
subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen."
I am bit concerned which such statement because the code instance was today with the current code. This could change in the future and invalide the reasoning.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
It is not clear to me if we have any mechanism to prevent that. If we don't, then I think we need to drastically reduce the number of time this is used (there are a bit too much for my taste).
Indeed, the purpose of such a deviation is that the sound overapproximation computed by the tool requires a human to look at the code and think twice before modifying it (i.e., if ever that code is touched, the reviewer ought to assess whether that justification still holds or some other thing should be done about it.
Your assumption is the reviewer will notice there is an existing devitation and be able to assess it has changed. I view this assumption as risky in the long term.
Have you investigate to improve the automatic tooling?
Well, as discussed elsewhere in the thread, a slightly modified version of this deviation comment can list the specific reason why such a thing was deviated directly at the declaration or where the caution is, if you think this is better.
Example:
// <- SAF-x here
int var;[...]
// <- or HERE
f(&var);An alternative approach to justification, partly discussed with Stefano in private is a macro that looks like an attribute to signal that the variable is intentionally uninitialized. This does not have the benefit of a written justification with a proper comment or an entry in the json file, but is less intrusive and the justification for all occurrences of
__uninitw.r.t R9.1 would be included in the static analysis tool configuration, which would be part of the MISRA compliance documentation. This does imply a coarse justification like the one above, but if further clarification is needed it can be provided locally in the code, as guidance for contributors.Example:
#define__uninit
__uninitint x;Quoted 60 lines
Quoted 60 lines
Quoted 12 lines
Quoted 12 lines
+ },
+ {
+ "id": "SAF-2-safe",
"analyser": {},
"name": "Sentinel",
"text": "Next ID to be used"xen/arch/arm/arm64/lib/find_next_bit.c +0
-0@@ -67,6 +67,7 @@ unsigned long find_next_zero_bit(const unsignedQuoted 8 lines
xen/arch/arm/arm64/lib/find_next_bit.c unsigned long find_next_zero_bit(const unsigned · line 72
Quoted 6 lines
xen/arch/arm/arm64/lib/find_next_bit.c +1
-0long *addr, unsigned long size, { const unsigned long *p = addr + BIT_WORD(offset); unsigned long result = offset & ~(BITS_PER_LONG-1); + /* SAF-1-safe MC3R1.R9.1 */ unsigned long tmp;This is a file that was copied as-is from Linux. I thought we exclude them.
There was an expansion location in the report that caused that file to appear in the output. Thanks for reporting it.
Quoted 2 lines
Anyway, can you provide some details why Eclair thinks this is may be used unitialized?
The problem lies in the goto skipping out of control flow structures. It can't be refactored with a "do { tmp=...; break; } while(0)", as done elsewhere, because there are two distinct labels in there, so the semantics would be altered. The same comment applies in other instances as well.
Quoted 15 lines
xen/arch/arm/bootfdt.c static bool __init device_tree_node_matches(const · line 39
Quoted 13 lines
xen/arch/arm/arm64/lib/find_next_bit.c +0
-0if (offset >= size)xen/arch/arm/bootfdt.c +1
-0@@ -34,6 +34,7 @@ static bool __init device_tree_node_matches(const void *fdt, int node, static bool __init device_tree_node_compatible(const void *fdt, int node, const char *match) { + /* SAF-1-safe MC3R1.R9.1 */ int len, l;It is unclear to me whether the SAF-* applies to 'len' or 'l'.
Just len, they need to be two separate declarations if it is agreed not to initialize it. Good catch.
Quoted 14 lines
xen/arch/arm/bootfdt.c int __init device_tree_for_each_node(const void · line 177
Quoted 11 lines
xen/arch/arm/bootfdt.c +2
-0const void *prop;@@ -169,7 +170,9 @@ int __init device_tree_for_each_node(const void*fdt, int node, */ int depth = 0; const int first_node = node; + /* SAF-1-safe MC3R1.R9.1 */ u32 address_cells[DEVICE_TREE_MAX_DEPTH]; + /* SAF-1-safe MC3R1.R9.1 */ u32 size_cells[DEVICE_TREE_MAX_DEPTH];I have never sent formally, but in the past I proposed to rework the code so {address, size}_cells[0] would be initialized before the loop. See https://lore.kernel.org/all/[email protected]/.
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
Do you think this would help?
If that initialization is applied to all the array elements yes, because the Amplification of R9.1 says:
"For the purposes of this rule, an array element or structure member shall be considered as a discrete
object".
Otherwise, initializing just the first element does not improve the situation in any way, and this is currently not within of the analysis capabilities of ECLAIR.Any plan to improve ECLAIR?
Not in the short term on this. See further below.
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
xen/arch/arm/bootfdt.c static void __init · line 260
Quoted 13 lines
xen/arch/arm/bootfdt.c +2
-0int ret;@@ -249,8 +252,10 @@ static void __init process_multiboot_node(const void *fdt, int node, const __be32 *cell; bootmodule_kind kind; paddr_t start, size; + /* SAF-1-safe MC3R1.R9.1 */ int len; /* sizeof("/chosen/") + DT_MAX_NAME + '/' + DT_MAX_NAME + '/0' => 92 */ + /* SAF-1-safe MC3R1.R9.1*/ char path[92];So the two above, is one category of issue. The variables are passed as argument of function which will fill them.
Can Eclair look at the callers, if so, can we consider to always initialize the values in the callee?
This would reduce the number of SAF-*. There are a few other examples like that below. So I will skip them for now.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
[...]
If the value is always initialized in the callee, then there's no problem configuring ECLAIR so that it knows that this parameter is always written, and therefore any subsequent use in the caller is ok.
Another possibility is stating that a function never reads the pointee before writing to it (it may or may not write it, but if it doesn't, then the pointee is not read either). The 'strncmp' after '
fdt_get_path' does get in the way, though, because this property is not strong enough to ensure that we can use 'path' after returning from the function.I am not sure I fully understand what you wrote. Can you provide a C example?
void
f(int *x){
if(x) {
*x = 10;
int y =*x; // read the pointee after it's initialized
} else {
int z; // in this branch the pointee is not read nor written }
// we can say that f never reads *x before (possibly) writing to it.
}Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
xen/arch/arm/domain_build.c custom_param("dom0_mem", parse_dom0_mem); · line 64
Quoted 10 lines
xen/arch/arm/domain_build.c +1
-1@@ -62,7 +62,7 @@ custom_param("dom0_mem", parse_dom0_mem); int __init parse_arch_dom0_param(const char *s, const char *e) { - long long val; + long long val = LLONG_MAX;Can you explain why you decided to initialize rather than SAF-? For clarity, I am not asking to switch to SAF-, I am mainly interested what were your though process.
Quoted 4 lines
Quoted 1 line
I saw that putting a large enough value here would be out of range and trigger the printk even if the conditional leads to somehow enter the if branch with an invalid value.
Ok. This wants to be documented on top of the assignment.
Ok. While testing this I also switched to
LONG_MAX, whose definition is already available in that file, and does not alter the semantics anyway.Quoted 147 lines
Quoted 138 lines
Quoted 128 lines
Quoted 128 lines
xen/arch/arm/domain_build.c +6
-0if ( !parse_signed_integer("sve", s, e, &val) ) {@@ -1077,6 +1077,7 @@ static void __init assign_static_memory_11(struct domain *d, static int __init handle_linux_pci_domain(struct kernel_info *kinfo, const struct dt_device_node *node) { + /* SAF-1-safe MC3R1.R9.1 */ uint16_t segment; int res;@@ -1351,6 +1352,7 @@ static int __init make_memory_node(conststruct domain *d, unsigned int i; int res, reg_size = addrcells + sizecells; int nr_cells = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[NR_MEM_BANKS * 4 /* Worst case addrcells + sizecells */]; __be32 *cells;@@ -1578,6 +1580,7 @@ static int __init find_unallocated_memory(const struct kernel_info *kinfo, struct rangeset *unalloc_mem; paddr_t start, end; unsigned int i; + /* SAF-1-safe MC3R1.R9.1 */ int res; dt_dprintk("Find unallocated memory for extended regions\n");@@ -1727,6 +1730,7 @@ static int __init find_memory_holes(conststruct kernel_info *kinfo, dt_for_each_device_node( dt_host, np ) { unsigned int naddr; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; naddr = dt_number_of_address(np);@@ -1976,9 +1980,11 @@ static int __init make_cpus_node(const struct domain *d, void *fdt) const struct dt_device_node *npcpu; unsigned int cpu; const void *compatible = NULL; + /* SAF-1-safe MC3R1.R9.1 */ u32 len; /* Placeholder for cpu@ + a 32-bit hexadecimal number + \0 */ char buf[13]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; /* Keep the compiler happy with -Og */bool
clock_valid= false;xen/arch/arm/domain_build.c +3
-0@@ -2104,6 +2110,7 @@ static int __init make_gic_node(const struct domain *d, void *fdt, const struct dt_device_node *gic = dt_interrupt_controller; int res = 0; const void *addrcells, *sizecells; + /* SAF-1-safe MC3R1.R9.1 */ u32 addrcells_len, sizecells_len; /*@@ -2179,6 +2186,7 @@ static int __init make_timer_node(const struct kernel_info *kinfo) int res; unsigned int irq[MAX_TIMER_PPI]; gic_interrupt_t intrs[3]; + /* SAF-1-safe MC3R1.R9.1 */ u32 clock_frequency; bool clock_valid;@@ -2511,6 +2519,7 @@ static int __init handle_device(struct domain *d, struct dt_device_node *dev, unsigned int naddr; unsigned int i; int res; + /* SAF-1-safe MC3R1.R9.1 */ paddr_t addr, size; bool own_device = !dt_device_for_passthrough(dev);/*
xen/arch/arm/domain_build.c +1
-0@@ -2779,6 +2788,7 @@ static int __init make_gicv2_domU_node(struct kernel_info *kinfo) { void *fdt = kinfo->fdt; int res = 0; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[(GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS) * 2]; __be32 *cells;const struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -2914,6 +2924,7 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo) void *fdt = kinfo->fdt; int res; gic_interrupt_t intr; + /* SAF-1-safe MC3R1.R9.1*/ __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS]; __be32 *cells;struct domain *d =
kinfo->d;xen/arch/arm/domain_build.c +1
-0@@ -3435,6 +3446,7 @@ static void __init initrd_load(struct kernel_info *kinfo) paddr_t paddr, len; int node; int res; + /* SAF-1-safe MC3R1.R9.1 */ __be32 val[2]; __be32 *cellp;void
__iomem*initrd;xen/arch/arm/domain_build.c +5
-1@@ -3514,6 +3526,7 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np, uint32_t *port, uint32_t *phandle) { const __be32 *prop = NULL; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t len; prop = dt_get_property(np, "xen,evtchn", &len);@@ -3538,10 +3551,13 @@ static int __init get_evtchn_dt_property(const struct dt_device_node *np, static int __init alloc_domain_evtchn(struct dt_device_node *node) { int rc; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t domU1_port, domU2_port, remote_phandle; struct dt_device_node *remote_node; const struct dt_device_node *p1_node, *p2_node; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_alloc_unbound alloc_unbound; + /* SAF-1-safe MC3R1.R9.1 */ struct evtchn_bind_interdomain bind_interdomain; struct domain *d1 = NULL, *d2 = NULL;@@ -3789,11 +3805,12 @@ static int __init construct_domain(struct domain *d, struct kernel_info *kinfo) static int __init alloc_xenstore_evtchn(struct domain *d) { - evtchn_alloc_unbound_t alloc; + evtchn_alloc_unbound_t alloc = {Quoted 9 lines
xen/arch/arm/domain_build.c static int __init construct_domain(struct · line 3795
Quoted 6 lines
xen/arch/arm/domain_build.c +3
-2+ .dom = d->domain_id, + .remote_dom = hardware_domain->domain_id + }; int rc; - alloc.dom = d->domain_id; - alloc.remote_dom = hardware_domain->domain_id;It is not clear why this is modified. There are only two fields in 'alloc' and they are both initialized.
Quoted 8 lines
There are three fields in the struct, and the third is clearly to be set by an initalization function. If, as above with 'path', the init function ensures that the third field is always written then there's no problem, otherwise this initialization prevents any use of an uninitialized port.
Quoted 1 line
struct
evtchn_alloc_unbound{/* IN parameters */
domid_tdom,remote_dom;
/* OUT parameters */
evtchn_port_tport;
};
typedef structevtchn_alloc_unboundevtchn_alloc_unbound_t;Quoted 4 lines
Ah, I didn't spot the third field because it was defined on the same line. I would prefer if the value is explicitely initialized to 0.
Also, I think this ought to be in a separate patch (with other similar pattern).
Ok.
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
xen/arch/arm/domain_build.c static int __init construct_domU(struct · line 3831
Quoted 10 lines
xen/arch/arm/domain_build.c +1
-1rc = evtchn_alloc_unbound(&alloc, 0); if ( rc ) {@@ -3810,8 +3827,9 @@ static int __init construct_domU(struct domain *d, const struct dt_device_node *node) { struct kernel_info kinfo = {}; - const char *dom0less_enhanced; + const char *dom0less_enhanced = NULL;If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL. So it is unclear to me how this value is safer. > Looking at the code, I wonder whether we should convertdt_property_read_string()to useERR_PTR(). So we could remove the last argument and return it instead.Quoted 4 lines
Quoted 1 line
Is relying on that assumption somehow safer?
I am assuming you are referring to "If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL". Note that I didn't suggest it is safer. I am only pointed out that you didn't specify how this was better in the context of the code.This should be probably discussed after deciding on the refactoring '
dt_property_read_string'Quoted 67 lines
Quoted 67 lines
The suggestion to remove the last parameter seems better.
Quoted 64 lines
Quoted 55 lines
xen/arch/arm/domain_build.c +1
-0int rc; + /* SAF-1-safe MC3R1.R9.1 */ u64 mem; u32 p2m_mem_mb;unsigned long
p2m_pages;xen/arch/arm/domain_build.c +1
-0@@ -3939,6 +3957,7 @@ void __init create_domUs(void) .grant_opts = XEN_DOMCTL_GRANT_version(opt_gnttab_max_version), }; unsigned int flags = 0U; + /* SAF-1-safe MC3R1.R9.1 */ uint32_t val; int rc;xen/arch/arm/efi/efi-boot.h +1
-0@@ -117,6 +117,7 @@ static int __init setup_chosen_node(void *fdt, int *addr_cells, int *size_cells) static int __init fdt_set_reg(void *fdt, int node, int addr_cells, int size_cells, uint64_t addr, uint64_t len) { + /* SAF-1-safe MC3R1.R9.1 */
__be32val[4]; /* At most 2 64 bit values to be stored */__be32*cellp;xen/arch/arm/efi/efi-boot.h +3
-2@@ -308,7 +309,7 @@ fdt_set_fail: static void __init *fdt_increase_size(struct file *fdtfile, int add_size) { EFI_STATUS status; - EFI_PHYSICAL_ADDRESS fdt_addr; + EFI_PHYSICAL_ADDRESS fdt_addr = 0; int fdt_size; int pages; void *new_fdt;@@ -433,7 +434,7 @@ static void __init efi_arch_cfg_file_late(constEFI_LOADED_IMAGE *image, static void *__init efi_arch_allocate_mmap_buffer(UINTN map_size) { - void *ptr; + void *ptr = NULL; EFI_STATUS status; status = efi_bs->AllocatePool(EfiLoaderData, map_size, &ptr);@@ -538,6 +539,7 @@ static void __init efi_arch_handle_module(conststruct file *file, { int node; int chosen; + /* SAF-1-safe MC3R1.R9.1 */ int addr_len, size_len; if ( file == &dtbfile )xen/arch/arm/gic-v3-its.c +0
-0xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its · line 195
Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1
-2@@ -192,8 +192,7 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, cmd[0] = GITS_CMD_MAPC; cmd[1] = 0x00; - cmd[2] = encode_rdbase(its, cpu, collection_id); - cmd[2] |= GITS_VALID_BIT; + cmd[2] = encode_rdbase(its, cpu, collection_id) | GITS_VALID_BIT;Hmmm... How is this even considered as unitialized variable?
Quoted 6 lines
Quoted 1 line
The analysis here could use some more precision, but the modified construct is entirely equivalent.
I agree that they are equivalent. But in general, we don't change the style of the construct without explaining why.
In this case, the first step would be to improve Eclair.
The changes needed for this kind of analysis are not trivial: we've looked into this, but there's no easy way to support this in a timely manner. I understand that this is an estabilished pattern, but what would you think of an initializer using designators?
uint64_tcmd[4] = {
.[0] =GITS_CMD_MAPC;
.[1] =0x00;
.[2] =encode_rdbase(its, cpu, collection_id)|GITS_VALID_BIT; .[3] =0x00;
}Quoted 60 lines
Quoted 53 lines
Quoted 53 lines
xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapd(struct host_its · line 218
Quoted 12 lines
xen/arch/arm/gic-v3-its.c +1
-3cmd[3] = 0x00; return its_send_command(its, cmd);@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, } cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32); cmd[1] = size_bits; - cmd[2] = itt_addr; - if ( valid ) - cmd[2] |= GITS_VALID_BIT; + cmd[2] = itt_addr | (valid ? GITS_VALID_BIT : 0x00);Same here.
Quoted 26 lines
xen/arch/arm/mm.c +1
-0@@ -935,6 +935,7 @@ static int xen_pt_update_entry(mfn_t root,unsigned long virt, mfn_t mfn, unsigned int target, unsigned int flags) { + /* SAF-1-safe MC3R1.R9.1 */ int rc; unsigned int level;
lpae_t*table;xen/arch/arm/p2m.c +1
-1@@ -496,16 +496,18 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m,gfn_t gfn, lpae_t entry, *table; int rc; mfn_t mfn = INVALID_MFN; - p2m_type_t _t; + p2m_type_t _t = p2m_invalid; DECLARE_OFFSETS(offsets, addr); ASSERT(p2m_is_locked(p2m));xen/arch/arm/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, · line 509
Quoted 10 lines
xen/arch/arm/p2m.c +5
-3BUILD_BUG_ON(THIRD_MASK != PAGE_MASK); /* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; + if( t ) { + *t = _t; + } else { + t = &_t; + }What was the problem with the previous code?
Quoted 4 lines
Quoted 2 lines
This is also not conformant to Xen coding style.
The problem is that _t may be uninitialized, hence assigning its address to t could be problematic.
But the value is set right after. IOW, there is no read between. So how is this prob
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
Another way to address this is to initialize _t to a bad value and use this variable in the body, then assign to t based on the value just before returning.
IHMO, neither solution are ideal. I think we should investigate whether Eclair can be improved.
[...]
I'll see what can be done about it, I'll reply when I have an answer.
What about this:
- p2m_type_t _t;
+ p2m_type_t _t = p2m_invalid;
[...]
t = t ?: &_t;
- *t = p2m_invalid;
+ *t = _t;
Quoted 132 lines
Quoted 77 lines
Quoted 46 lines
Quoted 11 lines
xen/arch/arm/p2m.c static int __p2m_set_entry(struct p2m_domain · line 1038
Quoted 9 lines
xen/arch/arm/p2m.c +1
-0if ( valid ) *valid = false;@@ -1031,6 +1033,7 @@ static int __p2m_set_entry(struct p2m_domain *p2m, unsigned int level = 0; unsigned int target = 3 - (page_order / XEN_PT_LPAE_SHIFT); lpae_t *entry, *table, orig_pte; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same issue with gotos explained above, can't be refactored because of the for enclosing the goto.
Quoted 14 lines
xen/arch/arm/p2m.c static inline int p2m_remove_mapping(struct · line 1491
Quoted 10 lines
xen/arch/arm/p2m.c +0
-0/* A mapping is removed if the MFN is invalid. */bool
removing_mapping=mfn_eq(smfn, INVALID_MFN);xen/arch/arm/p2m.c +1
-0@@ -1483,6 +1486,7 @@ static inline int p2m_remove_mapping(struct domain *d, { struct p2m_domain *p2m = p2m_get_hostp2m(d); unsigned long i; + /* SAF-1-safe MC3R1.R9.1 */ int rc;Can you provide some details why Eclair thinks it is unitialized?
Same as above.
Quoted 5 lines
Quoted 5 lines
xen/arch/arm/p2m.c +0
-2p2m_write_lock(p2m);@@ -1685,20 +1689,21 @@ static int p2m_alloc_vmid(struct domain *d) ASSERT(nr != INVALID_VMID); - if ( nr == MAX_VMID ) - {Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1691
Quoted 6 lines
xen/arch/arm/p2m.c +1
-4- rc = -EBUSY; - printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); - goto out; - } + do {I don't understand this change. How is this making better for Eclair?
Quoted 1 line
This is an example where the goto can be eliminated, which in turn allows to automatically check the correctness.
If you want to eliminate the 'goto' then they are better way to do it. Like:
xen/arch/arm/p2m.c +6
-8@@ -186,16 +186,14 @@ int p2m_alloc_vmid(struct domain *d) { rc = -EBUSY; printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); - goto out; + } + else + { + set_bit(nr, vmid_mask); + p2m->vmid = nr; + rc = 0; } - set_bit(nr, vmid_mask); - - p2m->vmid = nr; - - rc = 0; - -out: spin_unlock(&vmid_alloc_lock); return rc; }Quoted 2 lines
I have a slight preference with the goto version, but I could accept it if Eclair can't cope with the construct. In any case, this is the sort of change that deserve its own patch as you want to explain why Eclair can't cope with such construct (I don't view it as complex).
ok
Quoted 27 lines
Quoted 27 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/p2m.c +7
-2+ if ( nr == MAX_VMID ) + { + rc = -EBUSY; + printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n", d->domain_id); + break; + } - set_bit(nr, vmid_mask); + set_bit(nr, vmid_mask); - p2m->vmid = nr;Quoted 9 lines
xen/arch/arm/p2m.c static int p2m_alloc_vmid(struct domain *d) · line 1705
Quoted 8 lines
xen/arch/arm/p2m.c +3
-2+ p2m->vmid = nr; - rc = 0; + rc = 0; + } while ( 0 ); -out: spin_unlock(&vmid_alloc_lock); return rc; }Considering all of the replies above, a first draft of a strategy/policy I can think of is having:
- Initializer functions that always write their parameter, so that
the strongest "pointee always written" property can be stated. This causes all further uses to be marked safe.Quoted 9 lines
Quoted 2 lines
- Initialize the variable when there exists a known safe value that
does not alter the semantics of the function. The initialization does not need to be at the declaration, but doing so simplifies the code.As I mentionned in private there are two risks with that:
1. You silence compiler to spot other issues
2. You may now get warning from Coverity if it spots you set a value that get overwritten before its first use.So I think such approach should be used with parcimony. Instead, we should look at reworking the code when possible.
Do you think it would help if you look directly at actual cautions to spot possible functions that can be refactored?
Quoted 6 lines
Quoted 3 lines
- Deviate all cases where any of the previous does not apply, with a
comment deviation that refers to a justification reporting that the code has been checked to respect the rule (keep in mind that _violations_ to a Mandatory rule such as R9.1 are not allowed to claim MISRA compliance).See above for my concern about adding so many deviations. But I am confused with what you wrote. If the rule is mandatory, then why are you trying to add deviation in Xen? Who is going to solve them to make Xen MISRA compliant?
Because only cautions were found, not violations (which cannot be deviated). In the former case Xen can say that the code does not violate the rule, but it can't be proven by the tool.
Regards,
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Nicola VetriniHi Nicola,
Quoted 100 lines · Nicola Vetrini
Quoted 31 lines · Julien Grall
Hi Nicola,
Quoted 28 lines · Nicola Vetrini
Quoted 27 lines · Julien Grall
Hi Nicola,
Quoted 10 lines · Nicola Vetrini
This patch aims to fix some occurrences of possibly uninitialized variables, that may be read before being written. This behaviour would violate MISRA C:2012 Rule 9.1, besides being generally undesirable.
In all the analyzed cases, such accesses were actually safe, but it's quite difficult to prove so by automatic checking, therefore a safer route is to change the code so as to avoid the behaviour from occurring, while preserving the semantics.
To achieve this goal, I adopted the following strategies:
Please let's at least one patch per strategy. I would also consider some of the rework separate so they can go in regardless the decision for the SAF-*.
Quoted 10 lines
- Add a suitably formatted local deviation comment (as indicated in '
docs/misra/documenting-violations.rst') to exempt the following line from checking.- Provide an initialization for the variable at the declaration.
- Substitute a goto breaking out of control flow logic with a semantically
equivalent do { .. } while(0).Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
As I already mentioned in private, it is unclear to me how you decided which strategy to use. I still think we need to define our policy before changing the code. Otherwise, it is going to be difficult to decide for new code.
The main point of this RFC is doing so. From what I gathered, it's not an easy task: sometimes there are no 'safe' values to initialize variables to and sometimes there is no easy way to prove that indeed something is always initialized or not accessed at all.
But you wrote the code. So you should be able to explain how you took the decision between one and the others.
Also, even if this is an RFC, it would have been good to summarize any discussion that happened in private and if there were concern try to come up with ideas or at least listing the concerns after '---.
I'll keep this if the need arises in the future.
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 29 lines
Signed-off-by Nicola Vetrini <[email protected]>---
docs/misra/safe.json | 8 +++++++xen/arch/arm/arm64/lib/find_next_bit.c| 1 + xen/arch/arm/bootfdt.c | 6 +++++ xen/arch/arm/decode.c | 2 ++ xen/arch/arm/domain_build.c | 29 ++++++++++++++++++---- xen/arch/arm/efi/efi-boot.h | 6 +++-- xen/arch/arm/gic-v3-its.c | 9 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33
+++++++++++++++-----------9 files changed, 69 insertions(+), 26 deletions(-)docs/misra/safe.json +5
-0@@ -12,6 +12,14 @@ }, { "id": "SAF-1-safe", + "analyser": { + "eclair": "MC3R1.R9.1" + }, + "name": "Rule 9.1: initializer not needed", + "text": "The following local variables are possiblyQuoted 1 line
subject to being read before being written, but code inspection ensured that the control flow in the construct where they appear ensures that no such event may happen."
I am bit concerned which such statement because the code instance was today with the current code. This could change in the future and invalide the reasoning.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
It is not clear to me if we have any mechanism to prevent that. If we don't, then I think we need to drastically reduce the number of time this is used (there are a bit too much for my taste).
Indeed, the purpose of such a deviation is that the sound overapproximation computed by the tool requires a human to look at the code and think twice before modifying it (i.e., if ever that code is touched, the reviewer ought to assess whether that justification still holds or some other thing should be done about it.
Your assumption is the reviewer will notice there is an existing devitation and be able to assess it has changed. I view this assumption as risky in the long term.
Have you investigate to improve the automatic tooling?
Well, as discussed elsewhere in the thread, a slightly modified version of this deviation comment can list the specific reason why such a thing was deviated directly at the declaration or where the caution is, if you think this is better.
Example:
// <- SAF-x here
int var;[...]
// <- or HERE
f(&var);
Quoted 6 lines
An alternative approach to justification, partly discussed with Stefano in private is a macro that looks like an attribute to signal that the variable is intentionally uninitialized. This does not have the benefit of a written justification with a proper comment or an entry in the json file, but is less intrusive and the justification for all occurrences of
__uninitw.r.t R9.1 would be included in the static analysis tool configuration, which would be part of the MISRA compliance documentation. This does imply a coarse justification like the one above, but if further clarification is needed it can be provided locally in the code, as guidance for contributors.Example:
#define__uninit
__uninitint x;
IHMO none of the example really help. You are still expecting the reviewer/developper to drop __uninit or any comment if the behavior has changed.
I don't have a good idea how we can mitigate it other than reworking the code in a way that makes both ECLAIR and the maintainers happy. Maybe the others will.
[...]
Quoted 29 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
docs/misra/safe.json static void __init · line 260
Quoted 12 lines
int ret;
docs/misra/safe.json +2
-0@@ -249,8 +252,10 @@ static void __init process_multiboot_node(const void *fdt, int node, const __be32 *cell; bootmodule_kind kind; paddr_t start, size; + /* SAF-1-safe MC3R1.R9.1 */ int len; /* sizeof("/chosen/") + DT_MAX_NAME + '/' + DT_MAX_NAME + '/0' => 92 */ + /* SAF-1-safe MC3R1.R9.1*/ char path[92];So the two above, is one category of issue. The variables are passed as argument of function which will fill them.
Can Eclair look at the callers, if so, can we consider to always initialize the values in the callee?
This would reduce the number of SAF-*. There are a few other examples like that below. So I will skip them for now.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
[...]
If the value is always initialized in the callee, then there's no problem configuring ECLAIR so that it knows that this parameter is always written, and therefore any subsequent use in the caller is ok.
Another possibility is stating that a function never reads the pointee before writing to it (it may or may not write it, but if it doesn't, then the pointee is not read either). The 'strncmp' after '
fdt_get_path' does get in the way, though, because this property is not strong enough to ensure that we can use 'path' after returning from the function.I am not sure I fully understand what you wrote. Can you provide a C example?
Quoted 9 lines
void
f(int *x){
if(x) {
*x = 10;
int y =*x; // read the pointee after it's initialized
} else {
int z; // in this branch the pointee is not read nor written }
// we can say that f never reads *x before (possibly) writing to it.
}
I am having trouble to understand it in the context of fdt_get_path(). Is 'f' meant to be fdt_get_path()?
[...]
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
docs/misra/safe.json static int __init construct_domU(struct · line 3831
Quoted 10 lines
rc =
evtchn_alloc_unbound(&alloc, 0);
if ( rc )
{docs/misra/safe.json +1
-1@@ -3810,8 +3827,9 @@ static int __init construct_domU(struct domain *d, const struct dt_device_node *node) { struct kernel_info kinfo = {}; - const char *dom0less_enhanced; + const char *dom0less_enhanced = NULL;If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL. So it is unclear to me how this value is safer. > Looking at the code, I wonder whether we should convertdt_property_read_string()to useERR_PTR(). So we could remove the last argument and return it instead.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Is relying on that assumption somehow safer?
I am assuming you are referring to "If you look at the user below, all the callers assume
dom0less_enhancedwill be non-NULL". Note that I didn't suggest it is safer. I am only pointed out that you didn't specify how this was better in the context of the code.This should be probably discussed after deciding on the refactoring '
dt_property_read_string'
FAOD, I think we should refactor dt_property_read_string(). I am happy to write a patch if you want.
[...]
Quoted 7 lines
Quoted 6 lines
Quoted 1 line
The analysis here could use some more precision, but the modified construct is entirely equivalent.
I agree that they are equivalent. But in general, we don't change the style of the construct without explaining why.
In this case, the first step would be to improve Eclair.
Quoted 8 lines
The changes needed for this kind of analysis are not trivial: we've looked into this, but there's no easy way to support this in a timely manner. I understand that this is an estabilished pattern, but what would you think of an initializer using designators?
uint64_tcmd[4] = {
.[0] =GITS_CMD_MAPC;
.[1] =0x00;
.[2] =encode_rdbase(its, cpu, collection_id)|GITS_VALID_BIT; .[3] =0x00;
}
The reability is Ok here. But this may not be the case here. In particular...
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
docs/misra/safe.json static int its_send_cmd_mapd(struct host_its · line 218
Quoted 13 lines
docs/misra/safe.json +1
-3cmd[3] = 0x00; return its_send_command(its, cmd);@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, } cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32); cmd[1] = size_bits; - cmd[2] = itt_addr; - if ( valid ) - cmd[2] |= GITS_VALID_BIT; + cmd[2] = itt_addr | (valid ? GITS_VALID_BIT : 0x00);Same here.
here... I much prefer the existing version.
[...]
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 10 lines
Quoted 10 lines
docs/misra/safe.json +4
-0+ if ( nr == MAX_VMID ) + { + rc = -EBUSY; + printk(XENLOG_ERR "p2m.c: dom%d: VMID pool exhausted\n",
d->domain_id);
+ break;
+ }
-set_bit(nr, vmid_mask);
+set_bit(nr, vmid_mask);
-p2m->vmid= nr;Quoted 9 lines
Quoted 8 lines
+
p2m->vmid= nr;
- rc = 0;
+ rc = 0;
+ } while ( 0 );
-out:
spin_unlock(&vmid_alloc_lock);
return rc;
}Considering all of the replies above, a first draft of a strategy/policy I can think of is having:
- Initializer functions that always write their parameter, so that
the strongest "pointee always written" property can be stated. This causes all further uses to be marked safe.- Initialize the variable when there exists a known safe value that
does not alter the semantics of the function. The initialization does not need to be at the declaration, but doing so simplifies the code.
Quoted 8 lines
Quoted 6 lines
As I mentionned in private there are two risks with that:
1. You silence compiler to spot other issues
2. You may now get warning from Coverity if it spots you set a value that get overwritten before its first use.So I think such approach should be used with parcimony. Instead, we should look at reworking the code when possible.
Do you think it would help if you look directly at actual cautions to spot possible functions that can be refactored?
I have already looked at some. Can we focus on them and see how much it helps?
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Nicola VetriniHi,
The e-mail is getting quite long. Can you trim the unnecessary bits when replying?
Quoted 14 lines · Nicola Vetrini
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
The problem is that _t may be uninitialized, hence assigning its address to t could be problematic.
But the value is set right after. IOW, there is no read between. So how is this prob
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
Another way to address this is to initialize _t to a bad value and use this variable in the body, then assign to t based on the value just before returning.
IHMO, neither solution are ideal. I think we should investigate whether Eclair can be improved.
[...]
I'll see what can be done about it, I'll reply when I have an answer.
Quoted 8 lines
What about this:
-
p2m_type_t_t;
+p2m_type_t_t =p2m_invalid;
[...]
t = t ?: &_t;
- *t =p2m_invalid;
+ *t = _t;
The resulting code is still quite confusing. I am still not quite sure why ECLAIR can't understand the construct. Apologies if this was already said, but this thread is getting quite long with many different issues. So it is a bit difficult to navigate (which is why I suggested to split and have a commit message explaining the rationale for each).
Anyway, if we can't improve Eclair, then my preference would be the following:
xen/arch/arm/p2m.c +4 -6
@@ -496,16 +496,13 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,lpae_t entry, *table;int rc;mfn_t mfn =INVALID_MFN ; -p2m_type_t _t;DECLARE_OFFSETS (offsets, addr);ASSERT (p2m_is_locked (p2m));BUILD_BUG_ON (THIRD_MASK !=PAGE_MASK ); -/* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; +if ( t ) + *t = p2m_invalid;if ( valid ) *valid = false;
@@ -549,7 +546,8 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn,if (p2m_is_valid (entry) ) { - *t = entry.p2m.type; +if ( t ) + *t = entry.p2m.type;if ( a ) *a =p2m_mem_access_radix_get (p2m, gfn);
Cheers,
Signature · 1 line
Julien Grall
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
Quoted 4 lines · Julien Grall
Hi,
The e-mail is getting quite long. Can you trim the unnecessary bits when replying?
Ok.
Quoted 54 lines · Nicola Vetrini
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
The problem is that _t may be uninitialized, hence assigning its address to t could be problematic.
But the value is set right after. IOW, there is no read between. So how is this prob
Quoted 9 lines
Quoted 6 lines
Quoted 1 line
Another way to address this is to initialize _t to a bad value and use this variable in the body, then assign to t based on the value just before returning.
IHMO, neither solution are ideal. I think we should investigate whether Eclair can be improved.
[...]
I'll see what can be done about it, I'll reply when I have an answer.
Quoted 8 lines
What about this:
-
p2m_type_t_t;
+p2m_type_t_t =p2m_invalid;
[...]
t = t ?: &_t;
- *t =p2m_invalid;
+ *t = _t;The resulting code is still quite confusing. I am still not quite sure why ECLAIR can't understand the construct. Apologies if this was already said, but this thread is getting quite long with many different issues. So it is a bit difficult to navigate (which is why I suggested to split and have a commit message explaining the rationale for each).
Anyway, if we can't improve Eclair, then my preference would be the following:
xen/arch/arm/p2m.c +2
-5@@ -496,16 +496,13 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, lpae_t entry, *table; int rc; mfn_t mfn = INVALID_MFN; - p2m_type_t _t; DECLARE_OFFSETS(offsets, addr); ASSERT(p2m_is_locked(p2m)); BUILD_BUG_ON(THIRD_MASK != PAGE_MASK); - /* Allow t to be NULL */ - t = t ?: &_t; - - *t = p2m_invalid; + if ( t ) + *t = p2m_invalid; if ( valid )*valid = false;
xen/arch/arm/p2m.c mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, · line 553
Quoted 9 lines
xen/arch/arm/p2m.c +2
-1@@ -549,7 +546,8 @@ mfn_t p2m_get_entry(struct p2m_domain *p2m, gfn_t gfn, if ( p2m_is_valid(entry) ) { - *t = entry.p2m.type; + if ( t ) + *t = entry.p2m.type; if ( a ) *a = p2m_mem_access_radix_get(p2m, gfn);
Ok, I'll make a separate patch.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [RFC PATCH 1/4] xen/arm: justify or initialize conditionally uninitialized variables
In reply to Julien GrallQuoted 18 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
If the value is always initialized in the callee, then there's no problem configuring ECLAIR so that it knows that this parameter is always written, and therefore any subsequent use in the caller is ok.
Another possibility is stating that a function never reads the pointee before writing to it (it may or may not write it, but if it doesn't, then the pointee is not read either). The 'strncmp' after '
fdt_get_path' does get in the way, though, because this property is not strong enough to ensure that we can use 'path' after returning from the function.I am not sure I fully understand what you wrote. Can you provide a C example?
Quoted 9 lines
void
f(int *x){
if(x) {
*x = 10;
int y =*x; // read the pointee after it's initialized
} else {
int z; // in this branch the pointee is not read nor written }
// we can say that f never reads *x before (possibly) writing to it. }
Quoted 2 lines
I am having trouble to understand it in the context of
fdt_get_path(). Is 'f' meant to befdt_get_path()?
Yes, exactly. The point is that 'fdt_get_path' surely ensures not to read uninitialized addresses from the path array, therefore if the strcmp can be somehow incorporated in a function or macro e.g.
int fdt_compare_path(fdt, node, path, str) {
/* Check that the node is under "/chosen" (first 7 chars of path) */
ret = fdt_get_path(fdt, node, path, sizeof (path));
if ( ret != 0 || strncmp(path, "/chosen", 7) )
return ret;
return 0;
}called in bootfdt as fdt_compare_path(fdt, node, path, "/chosen");
then 'fdt_compare_path' has the, shall we say, "no read before write" property and because path isn't used anywhere else in 'process_multiboot_node' that is enough to make ECLAIR happy.
Quoted 5 lines
Quoted 2 lines
This should be probably discussed after deciding on the refactoring '
dt_property_read_string'FAOD, I think we should refactor
dt_property_read_string(). I am happy to write a patch if you want.
That would be perfect, I'll ll test it when I see it, so that I can give you feedback on that patch directly.
Quoted 35 lines
Quoted 8 lines
Quoted 7 lines
Quoted 2 lines
The analysis here could use some more precision, but the modified construct is entirely equivalent.
I agree that they are equivalent. But in general, we don't change the style of the construct without explaining why.
In this case, the first step would be to improve Eclair.
Quoted 8 lines
The changes needed for this kind of analysis are not trivial: we've looked into this, but there's no easy way to support this in a timely manner. I understand that this is an estabilished pattern, but what would you think of an initializer using designators?
uint64_tcmd[4] = {
.[0] =GITS_CMD_MAPC;
.[1] =0x00;
.[2] =encode_rdbase(its, cpu, collection_id)|GITS_VALID_BIT; .[3] =0x00;
}The reability is Ok here. But this may not be the case here. In particular...
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Review static int its_send_cmd_mapd(struct host_its · line 218
Quoted 13 lines
cmd[3] =
0x00;
returnits_send_command(its, cmd);(quoted diff) +1
-3@@ -215,9 +214,7 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid, } cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32); cmd[1] = size_bits; - cmd[2] = itt_addr; - if ( valid ) - cmd[2] |= GITS_VALID_BIT; + cmd[2] = itt_addr | (valid ? GITS_VALID_BIT : 0x00);Same here.
Quoted 2 lines
here... I much prefer the existing version.
Well, that if can be kept as well. Like this:
cmd = { .[0] = .., .[2] = itt_addr, ... };
if(valid)
cmd[2] |= GITS_VALID_BIT;
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Considering all of the replies above, a first draft of a strategy/policy I can think of is having:
- Initializer functions that always write their parameter, so that
the strongest "pointee always written" property can be stated. This causes all further uses to be marked safe.- Initialize the variable when there exists a known safe value that
does not alter the semantics of the function. The initialization does not need to be at the declaration, but doing so simplifies the code.As I mentionned in private there are two risks with that:
1. You silence compiler to spot other issues
2. You may now get warning from Coverity if it spots you set a value that get overwritten before its first use.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
So I think such approach should be used with parcimony. Instead, we should look at reworking the code when possible.
Do you think it would help if you look directly at actual cautions to spot possible functions that can be refactored?
I have already looked at some. Can we focus on them and see how much it helps?
Yes. It would reduce the noise for me too
Regards,
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
For the record, as I mentioned during the call today, I asked to postpone the 9.1 work for later, because it is going to take a lot of work and discussions to figure out a good way forward for all these cases. There are at least 3-5 different sub-classes for this issues. So I think it would be better for the Xen community to make more progress with other rules and violations fixes first.
Quoted 48 lines · Nicola Vetrini
This patch series is aimed at discussing different categories of patterns concerning local variables that are possibly not initialized in all code paths, which results in hard-to-prove correctness. The main categories are as follows:
1. Variables initialized by passing a pointer to them to a function. Many such functions are coupled with error handling which results in the variable not being initialized.
2. Some variables are used in switch statements and the control flow ensures that all code paths do initialize them, but due to the presence of goto statements, the flow is harder to follow.
I emphasize that, as far as I can tell, the code is already compliant with the rule, but there is room for improvement, especially on the side of allowing automatic checks to be more effective.
Nicola Vetrini (4):
xen/arm: justify or initialize conditionally uninitialized variables xen/arm64: bitops: justify uninitialized variable inside a macro xen/arm: initialize conditionally uninitialized local variables xen/arm: initialize conditionally uninitialized local variablesdocs/misra/safe.json | 24 +++++++++++++++ xen/arch/arm/arm64/lib/bitops.c | 3 ++ xen/arch/arm/arm64/lib/find_next_bit.c | 1 + xen/arch/arm/bootfdt.c | 6 ++++ xen/arch/arm/cpuerrata.c | 6 ++-- xen/arch/arm/decode.c | 2 ++ xen/arch/arm/dm.c | 2 +- xen/arch/arm/domain_build.c | 29 ++++++++++++++---- xen/arch/arm/domctl.c | 8 ++--- xen/arch/arm/efi/efi-boot.h | 6 ++-- xen/arch/arm/gic-v3-its.c | 9 +++--- xen/arch/arm/gic-v3-lpi.c | 17 ++++++----- xen/arch/arm/guest_walk.c | 12 ++++---- xen/arch/arm/include/asm/guest_atomics.h | 3 ++ xen/arch/arm/include/asm/p2m.h | 10 ++++--- xen/arch/arm/mm.c | 1 + xen/arch/arm/p2m.c | 33 ++++++++++++--------- xen/arch/arm/platforms/xilinx-zynqmp-eemi.c | 10 ++----- xen/arch/arm/psci.c | 10 +++---- xen/drivers/char/pl011.c | 2 +- 20 files changed, 129 insertions(+), 65 deletions(-)--
2.34.1