Skip to contentSkip to conversation

x86: x86_emulate: address violations of MISRA C Rule 19.1

PATCH In review [email protected] 4 participants 8 messages May 2, 2025 – May 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 x86: x86_emulate: address violations of MISRA C Rule 19.1

From: Nicola Vetrini <[email protected]>

Rule 19.1 states: "An object shall not be assigned or copied
to an overlapping object". In the function like macro "get_rep_prefix", one member of a union is assigned the value of another member. Reading from one member and writing to the other violates the rule, while not causing Undefined Behavior due to their relative sizes. Instead, use casts combined with exactly overlapping accesses to address violations.

No functional change.

---
Changes in v2:
- Use casts combined with exactly overlapping accesses to address violations
- fix commit message
---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monné <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +3 -3
index 8e14ebb35b..d678855238 100644
@@ -527,8 +527,8 @@ static inline void put_loop_count(
         if ( !amd_like(ctxt) && mode_64bit() && ad_bytes == 4 )         \
         {                                                               \
             _regs.r(cx) = 0;                                            \
-            if ( extend_si ) _regs.r(si) = _regs.esi;                   \
-            if ( extend_di ) _regs.r(di) = _regs.edi;                   \
+            if ( extend_si ) _regs.r(si) = (uint32_t)_regs.r(si);        \
+            if ( extend_di ) _regs.r(di) = (uint32_t)_regs.r(di);        \
         }                                                               \
         goto complete_insn;                                             \
     }                                                                   \
@@ -2029,7 +2029,7 @@ x86_emulate(
         switch ( op_bytes )
         {
         case 2: _regs.ax = (int8_t)_regs.ax; break; /* cbw */
-        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.ax; break; /* cwde */
+        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.r(ax); break; /* cwde */
         case 8: _regs.r(ax) = (int32_t)_regs.r(ax); break; /* cdqe */
         }
         break;

--
2.25.1

#1 lore raw

PATCH v2 2/2 automation/eclair: tag Rule 19.1 as clean

From: Federico Serafini <[email protected]>

Tag MISRA C Rule 19.1 as clean to avoid regressions.

---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monné <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>

---
 automation/eclair_analysis/ECLAIR/tagging.ecl | 1 +
 1 file changed, 1 insertion(+)
automation/eclair_analysis/ECLAIR/tagging.ecl +1 -0
index 1d078d8905..dab3c51faa 100644
@@ -78,6 +78,7 @@ MC3A2.R17.5||
 MC3A2.R17.6||
 MC3A2.R18.6||
 MC3A2.R18.8||
+MC3A2.R19.1||
 MC3A2.R20.2||
 MC3A2.R20.3||
 MC3A2.R20.4||

--
2.25.1

#2 lore raw

Re: [PATCH v2 2/2] automation/eclair: tag Rule 19.1 as clean

Sorry, the content transfer encoding was wrong so the patch might not apply, I'll re-send this.

Victor

#3 lore raw

PATCH v2 1/2 x86: x86_emulate: address violations of MISRA C Rule 19.1

From: Nicola Vetrini <[email protected]>

Rule 19.1 states: "An object shall not be assigned or copied
to an overlapping object". In the function like macro "get_rep_prefix", one member of a union is assigned the value of another member. Reading from one member and writing to the other violates the rule, while not causing Undefined Behavior due to their relative sizes. Instead, use casts combined with exactly overlapping accesses to address violations.

No functional change.

---
Changes in v2:
- Use casts combined with exactly overlapping accesses to address violations
- fix commit message
---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monné <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +3 -3
index 8e14ebb35b..d678855238 100644
@@ -527,8 +527,8 @@ static inline void put_loop_count(
         if ( !amd_like(ctxt) && mode_64bit() && ad_bytes == 4 )         \
         {                                                               \
             _regs.r(cx) = 0;                                            \
-            if ( extend_si ) _regs.r(si) = _regs.esi;                   \
-            if ( extend_di ) _regs.r(di) = _regs.edi;                   \
+            if ( extend_si ) _regs.r(si) = (uint32_t)_regs.r(si);        \
+            if ( extend_di ) _regs.r(di) = (uint32_t)_regs.r(di);        \
         }                                                               \
         goto complete_insn;                                             \
     }                                                                   \
@@ -2029,7 +2029,7 @@ x86_emulate(
         switch ( op_bytes )
         {
         case 2: _regs.ax = (int8_t)_regs.ax; break; /* cbw */
-        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.ax; break; /* cwde */
+        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.r(ax); break; /* cwde */
         case 8: _regs.r(ax) = (int32_t)_regs.r(ax); break; /* cdqe */
         }
         break;

--
2.25.1

#4 lore raw

PATCH v2 2/2 automation/eclair: tag Rule 19.1 as clean

From: Federico Serafini <[email protected]>

Tag MISRA C Rule 19.1 as clean to avoid regressions.

---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monné <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>

---
 automation/eclair_analysis/ECLAIR/tagging.ecl | 1 +
 1 file changed, 1 insertion(+)
automation/eclair_analysis/ECLAIR/tagging.ecl +1 -0
index 1d078d8905..dab3c51faa 100644
@@ -78,6 +78,7 @@ MC3A2.R17.5||
 MC3A2.R17.6||
 MC3A2.R18.6||
 MC3A2.R18.8||
+MC3A2.R19.1||
 MC3A2.R20.2||
 MC3A2.R20.3||
 MC3A2.R20.4||

--
2.25.1

Reviewed-by on v2 1/2
#5 lore raw
In reply to [email protected]
Quoted 9 lines · On 03.05.2025 01:49, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Rule 19.1 states: "An object shall not be assigned or copied
to an overlapping object". In the function like macro "get_rep_prefix", one member of a union is assigned the value of another member. Reading from one member and writing to the other violates the rule, while not causing Undefined Behavior due to their relative sizes. Instead, use casts combined with exactly overlapping accesses to address violations.

Quoted 5 lines

No functional change.

albeit strictly speaking the description covers only ...

xen/arch/x86/x86_emulate/x86_emulate.c static inline void put_loop_count( · line 534

Quoted 13 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -2
@@ -527,8 +527,8 @@ static inline void put_loop_count(
         if ( !amd_like(ctxt) && mode_64bit() && ad_bytes == 4 )         \
         {                                                               \
             _regs.r(cx) = 0;                                            \
-            if ( extend_si ) _regs.r(si) = _regs.esi;                   \
-            if ( extend_di ) _regs.r(di) = _regs.edi;                   \
+            if ( extend_si ) _regs.r(si) = (uint32_t)_regs.r(si);        \
+            if ( extend_di ) _regs.r(di) = (uint32_t)_regs.r(di);        \
         }                                                               \
         goto complete_insn;                                             \
     }                                                                   \

... this hunk, but not ...

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 2035

Quoted 9 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -1
@@ -2029,7 +2029,7 @@ x86_emulate(
         switch ( op_bytes )
         {
         case 2: _regs.ax = (int8_t)_regs.ax; break; /* cbw */
-        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.ax; break; /* cwde */
+        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.r(ax); break; /* cwde */
         case 8: _regs.r(ax) = (int32_t)_regs.r(ax); break; /* cdqe */
         }
         break;

... this one.

Also the padding of the backslashes ought to be adjusted, which I guess I'll do while committing.

Jan

Reviewed-by on v2 1/2
#6 lore raw
In reply to [email protected]
Quoted 39 lines · On Fri, 2 May 2025, [email protected] wrote:

From: Nicola Vetrini <[email protected]>

Rule 19.1 states: "An object shall not be assigned or copied
to an overlapping object". In the function like macro "get_rep_prefix", one member of a union is assigned the value of another member. Reading from one member and writing to the other violates the rule, while not causing Undefined Behavior due to their relative sizes. Instead, use casts combined with exactly overlapping accesses to address violations.

No functional change.

---
Changes in v2:
- Use casts combined with exactly overlapping accesses to address violations
- fix commit message
---
Cc: Andrew Cooper <[email protected]>
Cc: Anthony PERARD <[email protected]>
Cc: Michal Orzel <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Roger Pau Monn?? <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Nicola Vetrini <[email protected]>
Cc: Federico Serafini <[email protected]>
Cc: Bertrand Marquis <[email protected]>

---
 xen/arch/x86/x86_emulate/x86_emulate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
index 8e14ebb35b..d678855238 100644

xen/arch/x86/x86_emulate/x86_emulate.c static inline void put_loop_count( · line 531

Quoted 7 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -2
@@ -527,8 +527,8 @@ static inline void put_loop_count(
         if ( !amd_like(ctxt) && mode_64bit() && ad_bytes == 4 )         \
         {                                                               \
             _regs.r(cx) = 0;                                            \
-            if ( extend_si ) _regs.r(si) = _regs.esi;                   \
-            if ( extend_di ) _regs.r(di) = _regs.edi;                   \
+            if ( extend_si ) _regs.r(si) = (uint32_t)_regs.r(si);        \
+            if ( extend_di ) _regs.r(di) = (uint32_t)_regs.r(di);        \

NIT: code style, alignment of the \

Can be fixed on commit.

Quoted 15 lines
xen/arch/x86/x86_emulate/x86_emulate.c +1 -2
         }                                                               \
         goto complete_insn;                                             \
     }                                                                   \
@@ -2029,7 +2029,7 @@ x86_emulate(
         switch ( op_bytes )
         {
         case 2: _regs.ax = (int8_t)_regs.ax; break; /* cbw */
-        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.ax; break; /* cwde */
+        case 4: _regs.r(ax) = (uint32_t)(int16_t)_regs.r(ax); break; /* cwde */
         case 8: _regs.r(ax) = (int32_t)_regs.r(ax); break; /* cdqe */
         }
         break;
--

2.25.1

Acked-by
#7 lore raw
Quoted 15 lines · Stefano Stabellini

xen/arch/x86/x86_emulate/x86_emulate.c static inline void put_loop_count( · line 531

Quoted 12 lines · On Fri, 2 May 2025, [email protected] wrote:
xen/arch/x86/x86_emulate/x86_emulate.c +2 -2
index 8e14ebb35b..d678855238 100644
@@ -527,8 +527,8 @@ static inline void put_loop_count(
         if ( !amd_like(ctxt) && mode_64bit() && ad_bytes == 4 )         \
         {                                                               \
             _regs.r(cx) = 0;                                            \
-            if ( extend_si ) _regs.r(si) = _regs.esi;                   \
-            if ( extend_di ) _regs.r(di) = _regs.edi;                   \
+            if ( extend_si ) _regs.r(si) = (uint32_t)_regs.r(si);        \
+            if ( extend_di ) _regs.r(di) = (uint32_t)_regs.r(di);        \

NIT: code style, alignment of the \

Quoted 3 lines

Can be fixed on commit.