hujun260 opened a new pull request, #22090:
URL: https://github.com/apache/nuttx/pull/22090

   # arch/arm64: parenthesize `MPU_REGION_ENTRY` macro parameters
   
   ## Summary
   
   Wrap the `_name`, `_base` and `_size` parameters of the `MPU_REGION_ENTRY` 
macro
   in parentheses at each point of expansion, clearing MISRA C:2012 Rule 20.7
   findings reported by Coverity. The `_attr` parameter is deliberately left
   unwrapped.
   
   ```diff
    #define MPU_REGION_ENTRY(_name, _base, _size, _attr)  \
      {                                                   \
   -    .name   = _name,                                  \
   -    .base   = _base,                                  \
   -    .size   = _size,                                  \
   +    .name   = (_name),                                \
   +    .base   = (_base),                                \
   +    .size   = (_size),                                \
        .attr   = _attr,                                  \
      }
   ```
   
   Single file, three lines, no functional change.
   
   ## Why `_attr` stays unparenthesized
   
   This is the part worth reviewing, because the inconsistency looks like an
   oversight but is required.
   
   Every `_attr` argument is a brace-enclosed designated-initializer list such 
as
   `REGION_DEVICE_ATTR`:
   
   ```c
   #define REGION_DEVICE_ATTR                                \
     {                                                       \
       .rbar = NOT_EXEC | P_RW_U_NA_MSK | NON_SHAREABLE_MSK, \
       .mair_idx = MPU_MAIR_INDEX_DEVICE,                    \
     }
   ```
   
   Wrapping that in parentheses yields `({ ... })`. GCC parses `({ ... })` as a
   GNU statement expression, and a statement expression is not a constant
   expression, so it cannot appear in a static initializer. Verified with a
   standalone reproduction:
   
   ```text
   $ cat attr_test.c
   struct attr { unsigned long rbar; unsigned long mair_idx; };
   struct region { const char *name; unsigned long base;
                   unsigned long size; struct attr attr; };
   
   #define REGION_DEVICE_ATTR { .rbar = 0x1 | 0x2, .mair_idx = 3, }
   
   #define ENTRY_BAD(_name, _base, _size, _attr) \
     { .name = (_name), .base = (_base), .size = (_size), .attr = (_attr), }
   
   static const struct region bad[] =
     { ENTRY_BAD("a", 0x1000, 0x100, REGION_DEVICE_ATTR) };
   
   $ gcc -c attr_test.c
   attr_test.c:12:64: error: braced-group within expression allowed only inside 
a function
      12 |   { .name = (_name), .base = (_base), .size = (_size), .attr = 
(_attr), }
         |                                                                ^
   ```
   
   So `_attr` is a genuine Rule 20.7 deviation and should be recorded as one 
rather
   than "fixed" into a build break.
   
   ### Why the sibling `MMU_REGION_ENTRY` *does* parenthesize `_attrs`
   
   `arm64_mmu.h` has the same shape of macro with all five parameters wrapped,
   including `_attrs`:
   
   ```c
   #define MMU_REGION_ENTRY(_name, _base_pa, _base_va, _size, _attrs) \
     {                                                                \
       .name       = (_name),                                         \
       .base_pa    = (_base_pa),                                      \
       .base_va    = (_base_va),                                      \
       .size       = (_size),                                         \
       .attrs      = (_attrs),                                        \
     }
   ```
   
   The difference is the argument type, not a style inconsistency: MMU call 
sites
   pass a scalar bitmask expression (`MT_DEVICE_NGNRNE | MT_RW | MT_SECURE`), 
which
   parenthesizes fine, while MPU call sites pass a braced initializer list. 
Anyone
   "aligning" the two macros would break the MPU build.
   
   ## Verification
   
   ### Macro expansion
   
   Expanded with `gcc -E` through a minimal shim, to show where the parentheses
   land:
   
   ```text
   $ gcc -E exp.c | tail -1
   static const struct region r[] = { { .name = ("dev"), .base = (0x1000),
     .size = (0x100), .attr = { .rbar = (1|2), .mair_idx = 3, }, } };
   ```
   
   ### Behaviour is unchanged
   
   The parentheses are semantically inert here. Every parameter is expanded as 
the
   complete right-hand side of a designated initializer, so there is no 
enclosing
   operator for precedence to interact with. Checked with arguments that would
   expose a parenthesization bug if one existed:
   
   ```text
   $ ./rule207
   old: base=1024 size=1024 attr=77
   new: base=1024 size=1024 attr=77
   identical: 1
   ```
   
   `base` was passed as `1024 + 0` and `size` as `4096 / 4`; both expand
   identically with and without the added parentheses. This is expected — the
   change is a compliance fix, not a bug fix.
   
   ### Build
   
   All ARMv8-R (MPU) configurations build clean:
   
   ```text
   $ export PATH=<aarch64-none-elf>/bin:$PATH
   $ tools/configure.sh -a ../apps fvp-armv8r:nsh
   $ make -j$(nproc)
   ...
   LD: nuttx
   Memory region         Used Size  Region Size  %age Used
              ksram:      335104 B       128 MB      0.25%
   CP: nuttx.hex
   CP: nuttx.bin
   ```
   
   | Configuration | Result |
   | --- | --- |
   | `fvp-armv8r:nsh` | PASS |
   | `fvp-armv8r:nsh_smp` | PASS |
   | `fvp-armv8r:citest` | PASS |
   | `fvp-armv8r:pnsh` | PASS |
   
   The MPU object compiles with the new macro:
   
   ```text
   $ ls -l arch/arm64/src/arm64_mpu.o
   -rw-rw-r-- 1 hujun5 hujun5 22352 Nov 20 11:03 arch/arm64/src/arm64_mpu.o
   ```
   
   The MMU (ARMv8-A) path is untouched by this change but was rebuilt and 
booted as
   a regression check:
   
   ```text
   $ tools/configure.sh -a ../apps qemu-armv8a:nsh_smp
   $ make -j$(nproc)
   LD: nuttx
   CP: nuttx.hex
   CP: nuttx.bin
   ```
   
   ```text
   $ qemu-system-aarch64 -cpu cortex-a53 -smp 2 -nographic \
       -machine virt,virtualization=on,gic-version=3 \
       -net none -chardev stdio,id=con,mux=on \
       -serial chardev:con -monitor none -kernel nuttx
   ```
   
   ```text
   - Ready to Boot Primary CPU
   - Boot from EL2
   - Boot from EL1
   - Boot to C runtime for OS Initialize
   - Ready to Boot Second CPU
   - Boot from EL2
   - Boot from EL1
   - Boot to C runtime for OS Initialize
   nsh> uname -a
   NuttX 13.1.1-RC0 4e8a65c1b90 Nov 20 2026 11:05:27 arm64 qemu-armv8a
   nsh> ps
     TID   PID  PPID CPU PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK    
        STACK    USED FILLED COMMAND
       0     0     0   0   0 FIFO     Kthread   - Assigned           
0000000000000000 0016336 0001680  10.2%  CPU0 IDLE
       1     0     0   1   0 FIFO     Kthread   - Running            
0000000000000000 0016336 0000992   6.0%  CPU1 IDLE
       2     0     0 --- 192 RR       Kthread   - Waiting  Semaphore 
0000000000000000 0016272 0001184   7.2%  hpwork 0x4046dba0 0x4046dc28
       4     4     0 --- 100 RR       Task      - Waiting  Semaphore 
0000000000000000 0016304 0002032  12.4%  init_main
       5     5     4   0 100 RR       Task      - Running            
0000000000000000 0016304 0003216  19.7%  sh
   ```
   
   ### checkpatch
   
   ```text
   $ tools/checkpatch.sh -c -u -m -g ed122374690..HEAD
   Used config files:
       1: .codespellrc
   ✔️ All checks pass.
   ```
   
   ## Honest notes
   
   - **`MPU_REGION_ENTRY` currently has no call sites in this tree.** A 
repo-wide
     search for the identifier finds only the `#define` itself:
   
     ```text
     $ grep -rn "MPU_REGION_ENTRY" --include=*.c --include=*.h .
     ./arch/arm64/src/common/arm64_mpu.h:321:#define MPU_REGION_ENTRY(...)
     ```
   
     The same holds for `struct arm64_mpu_config` and the `REGION_*_ATTR` 
helpers,
     which exist but are unused. So the Coverity findings this fixes come from
     downstream code that expands the macro, not from anything in this 
repository,
     and the build verification above proves "nothing breaks" rather than
     "the reported path was exercised".
   - **No runtime verification on the MPU path.** QEMU has no ARMv8-R machine 
and
     the Arm FVP models are not available here, so `fvp-armv8r` was 
build-verified
     only. The `qemu-armv8a` run above exercises the unrelated MMU path purely 
as a
     regression check.
   - **Rule 20.7 normally expects *all* macro parameters to be parenthesized.** 
With
     `_attr` left bare, the finding will persist for that one parameter. If the
     project's policy is to have zero deviations, the alternative is to change
     call sites to pass a compound literal or a scalar and build the struct 
inside
     the macro — a much larger change than this one and out of scope here.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to