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

[Xen-devel] [PATCH v11 8/9] xen: use DECLARE_BOUNDS in alternative.c



Use DECLARE_BOUNDS and the two static inline functions that come with it
for comparisons and subtractions of:

__alt_instructions, __alt_instructions_end

M3CM: Rule-18.2: Subtraction between pointers shall only be applied to
pointers that address elements of the same array

https://wiki.sei.cmu.edu/confluence/display/c/ARR36-C.+Do+not+subtract+or+compare+two+pointers+that+do+not+refer+to+the+same+array

QAVerify: 2761
Signed-off-by: Stefano Stabellini <stefanos@xxxxxxxxxx>
CC: JBeulich@xxxxxxxx
CC: andrew.cooper3@xxxxxxxxxx

---
Changes in v11:
- new patch
---
 xen/arch/arm/alternative.c        | 12 +++++++-----
 xen/arch/x86/alternative.c        | 10 +++++-----
 xen/common/livepatch.c            |  5 +++--
 xen/include/asm-arm/alternative.h |  6 +++++-
 xen/include/asm-x86/alternative.h |  7 ++++++-
 5 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/xen/arch/arm/alternative.c b/xen/arch/arm/alternative.c
index 6013110..32a03d6 100644
--- a/xen/arch/arm/alternative.c
+++ b/xen/arch/arm/alternative.c
@@ -38,11 +38,9 @@
 #undef virt_to_mfn
 #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
 
-extern const struct alt_instr __alt_instructions[], __alt_instructions_end[];
-
 struct alt_region {
     const struct alt_instr *begin;
-    const struct alt_instr *end;
+    const struct abstract_alt_instr *end;
 };
 
 /*
@@ -131,7 +129,10 @@ static int __apply_alternatives(const struct alt_region 
*region,
     printk(XENLOG_INFO "alternatives: Patching with alt table %p -> %p\n",
            region->begin, region->end);
 
-    for ( alt = region->begin; alt < region->end; alt++ )
+    /* region->begin and region->end might point to different objects. */
+    for ( alt = region->begin;
+          alt_instr_lt(alt, region->end);
+          alt++ )
     {
         int nr_inst;
 
@@ -236,7 +237,8 @@ void __init apply_alternatives_all(void)
     BUG_ON(ret);
 }
 
-int apply_alternatives(const struct alt_instr *start, const struct alt_instr 
*end)
+int apply_alternatives(const struct alt_instr *start,
+                       const struct abstract_alt_instr *end)
 {
     const struct alt_region region = {
         .begin = start,
diff --git a/xen/arch/x86/alternative.c b/xen/arch/x86/alternative.c
index b8c819a..30abf4e 100644
--- a/xen/arch/x86/alternative.c
+++ b/xen/arch/x86/alternative.c
@@ -29,8 +29,6 @@
 
 #define MAX_PATCH_LEN (255-1)
 
-extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
-
 #ifdef K8_NOP1
 static const unsigned char k8nops[] init_or_livepatch_const = {
     K8_NOP1,
@@ -178,8 +176,8 @@ text_poke(void *addr, const void *opcode, size_t len)
  * APs have less capabilities than the boot processor are not handled.
  * Tough. Make sure you disable such features by hand.
  */
-void init_or_livepatch apply_alternatives(struct alt_instr *start,
-                                          struct alt_instr *end)
+void init_or_livepatch apply_alternatives(const struct alt_instr *start,
+                                          const struct abstract_alt_instr *end)
 {
     struct alt_instr *a, *base;
 
@@ -193,8 +191,10 @@ void init_or_livepatch apply_alternatives(struct alt_instr 
*start,
      *
      * So be careful if you want to change the scan order to any other
      * order.
+     *
+     * start and end could be pointers to different objects.
      */
-    for ( a = base = start; a < end; a++ )
+    for ( a = base = (struct alt_instr *)start; alt_instr_lt(a, end); a++ )
     {
         uint8_t *orig = ALT_ORIG_PTR(a);
         uint8_t *repl = ALT_REPL_PTR(a);
diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c
index d6eaae6..e02f95d 100644
--- a/xen/common/livepatch.c
+++ b/xen/common/livepatch.c
@@ -661,7 +661,8 @@ static int prepare_payload(struct payload *payload,
     if ( sec )
     {
 #ifdef CONFIG_HAS_ALTERNATIVE
-        struct alt_instr *a, *start, *end;
+        struct alt_instr *a, *start;
+        struct abstract_alt_instr *end;
 
         if ( !section_ok(elf, sec, sizeof(*a)) )
             return -EINVAL;
@@ -669,7 +670,7 @@ static int prepare_payload(struct payload *payload,
         start = sec->load_addr;
         end = sec->load_addr + sec->sec->sh_size;
 
-        for ( a = start; a < end; a++ )
+        for ( a = start; alt_instr_lt(a, end); a++ )
         {
             const void *instr = ALT_ORIG_PTR(a);
             const void *replacement = ALT_REPL_PTR(a);
diff --git a/xen/include/asm-arm/alternative.h 
b/xen/include/asm-arm/alternative.h
index dedb6dd..68f848e 100644
--- a/xen/include/asm-arm/alternative.h
+++ b/xen/include/asm-arm/alternative.h
@@ -7,6 +7,7 @@
 
 #ifndef __ASSEMBLY__
 
+#include <xen/lib.h>
 #include <xen/types.h>
 #include <xen/stringify.h>
 
@@ -28,7 +29,10 @@ typedef void (*alternative_cb_t)(const struct alt_instr *alt,
                                 int nr_inst);
 
 void apply_alternatives_all(void);
-int apply_alternatives(const struct alt_instr *start, const struct alt_instr 
*end);
+typedef struct alt_instr alt_instr_t;
+DECLARE_BOUNDS(alt_instr, __alt_instructions, __alt_instructions_end);
+int apply_alternatives(const struct alt_instr *start,
+                              const struct abstract_alt_instr *end);
 
 #define ALTINSTR_ENTRY(feature, cb)                                          \
        " .word 661b - .\n"                             /* label           */ \
diff --git a/xen/include/asm-x86/alternative.h 
b/xen/include/asm-x86/alternative.h
index d96411f..c0d65ab 100644
--- a/xen/include/asm-x86/alternative.h
+++ b/xen/include/asm-x86/alternative.h
@@ -4,6 +4,7 @@
 #ifdef __ASSEMBLY__
 #include <asm/alternative-asm.h>
 #else
+#include <xen/lib.h>
 #include <xen/stringify.h>
 #include <xen/types.h>
 #include <asm/asm-macros.h>
@@ -23,8 +24,12 @@ struct __packed alt_instr {
 #define ALT_REPL_PTR(a)     __ALT_PTR(a, repl_offset)
 
 extern void add_nops(void *insns, unsigned int len);
+
+typedef struct alt_instr alt_instr_t;
+DECLARE_BOUNDS(alt_instr, __alt_instructions, __alt_instructions_end);
 /* Similar to alternative_instructions except it can be run with IRQs enabled. 
*/
-extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
+void apply_alternatives(const struct alt_instr *start,
+                               const struct abstract_alt_instr *end);
 extern void alternative_instructions(void);
 
 #define alt_orig_len       "(.LXEN%=_orig_e - .LXEN%=_orig_s)"
-- 
1.9.1


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.