Xen: Remove the of variable length arrays
cover Xen: Remove the of variable length arrays
Variable length arrays, while convenient in some cases, are problematic with small stacks, and can turn logical bugs into stack overflows. Furthermore, the code generation behind them is very poor.
Xen has two uses of VLAs which can be fixed easily, and one case where the VLA logic triggers on a type, which can be worked around.
Andrew Cooper (5):
x86/PoD: Simplify handling of the quick check
x86/PoD: Avoid using variable length arrays in p2m_pod_zero_check() x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla ARM/dom0: Avoid using a variable length array in make_memory_node()
xen: Disallow variable length arrays
xen/Rules.mk | 1 + xen/arch/arm/domain_build.c | 4 +++- xen/arch/x86/hvm/emulate.c | 6 +++--- xen/arch/x86/mm/p2m-pod.c | 26 ++++++++++---------------- 4 files changed, 17 insertions(+), 20 deletions(-)
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 1/5 x86/PoD: Simplify handling of the quick check
There is no need to duplicate the contents of the skip block.
While cleaning up this function, change 4 ints to be unsigned.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: George Dunlap <[email protected]>
--- xen/arch/x86/mm/p2m-pod.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-)
xen/arch/x86/mm/p2m-pod.c +3 -11
@@ -863,15 +863,14 @@ p2m_pod_zero_check_superpage(struct p2m_domain *p2m, gfn_t gfn) }static void -p2m_pod_zero_check (struct p2m_domain *p2m,const gfn_t *gfns,int count) +p2m_pod_zero_check (struct p2m_domain *p2m,const gfn_t *gfns,unsigned int count) {mfn_t mfns[count];p2m_type_t types[count];unsigned long *map[count];struct domain *d = p2m->domain; +unsigned int i, j, max_ref =1 ; -int i, j; -int max_ref =1 ;/* Allow an extra refcount for one shadow pt mapping in shadowed domains */ if (paging_mode_shadow (d) )
@@ -911,14 +910,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t *gfns, int count)/* Quick zero-check */ for ( j =0 ; j <16 ; j++ )if ( *(map[i] + j) !=0 ) -break ; - -if ( j <16 ) - { -unmap_domain_page (map[i]); - map[i] =NULL ; -continue ; - } + goto skip;/* Try to remove the page, restoring old mapping if it fails. */ if ( p2m_set_entry(p2m, gfns[i], INVALID_MFN, PAGE_ORDER_4K,
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/5] x86/PoD: Simplify handling of the quick check
Quoted 5 lines · Andrew Cooper
There is no need to duplicate the contents of the skip block.
While cleaning up this function, change 4 ints to be unsigned.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 1/5] x86/PoD: Simplify handling of the quick check
In reply to Andrew CooperQuoted 5 lines · Andrew Cooper
There is no need to duplicate the contents of the skip block.
While cleaning up this function, change 4 ints to be unsigned.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 2/5 x86/PoD: Avoid using variable length arrays in p2m_pod_zero_check()
In reply to Andrew CooperCallers of p2m_pod_zero_check() pass a count of up to POD_SWEEP_STRIDE. Move the definition of POD_SWEEP_STRIDE and give the arrays a fixed bound.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: George Dunlap <[email protected]>
--- xen/arch/x86/mm/p2m-pod.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
xen/arch/x86/mm/p2m-pod.c +7 -5
@@ -862,15 +862,19 @@ p2m_pod_zero_check_superpage(struct p2m_domain *p2m, gfn_t gfn)return ret; } +#define POD_SWEEP_LIMIT 1024 +#define POD_SWEEP_STRIDE 16 +static void p2m_pod_zero_check (struct p2m_domain *p2m,const gfn_t *gfns,unsigned int count) { -mfn_t mfns[count]; -p2m_type_t types[count]; -unsigned long *map[count]; +mfn_t mfns[POD_SWEEP_STRIDE ]; +p2m_type_t types[POD_SWEEP_STRIDE ]; +unsigned long *map[POD_SWEEP_STRIDE ];struct domain *d = p2m->domain;unsigned int i, j, max_ref =1 ; +BUG_ON (count >POD_SWEEP_STRIDE );/* Allow an extra refcount for one shadow pt mapping in shadowed domains */ if (paging_mode_shadow (d) )
@@ -1012,8 +1016,6 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t *gfns, unsigned int countunmap_domain_page (map[i]); } -#define POD_SWEEP_LIMIT 1024 -#define POD_SWEEP_STRIDE 16static void p2m_pod_emergency_sweep (struct p2m_domain *p2m) {
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 2/5] x86/PoD: Avoid using variable length arrays in p2m_pod_zero_check()
Quoted 5 lines · Andrew Cooper
Callers of
p2m_pod_zero_check()pass a count of up toPOD_SWEEP_STRIDE. Move the definition ofPOD_SWEEP_STRIDEand give the arrays a fixed bound.Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 2/5] x86/PoD: Avoid using variable length arrays in p2m_pod_zero_check()
In reply to Andrew CooperQuoted 5 lines · Andrew Cooper
Callers of
p2m_pod_zero_check()pass a count of up toPOD_SWEEP_STRIDE. Move the definition ofPOD_SWEEP_STRIDEand give the arrays a fixed bound.Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 3/5 x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla
In reply to Andrew CooperFor at least "gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609", the typecast of buf triggers a Variable Length Array warning.
Alter the asm expression to avoid the typecast, which necessitates the introduction of a memory clobber as the compiler can no longer identify the total quantity of written memory.
Despite the memory clobber, there is no change to the generated.
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Paul Durrant <[email protected]>
--- xen/arch/x86/hvm/emulate.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/emulate.c +3 -3
@@ -1859,7 +1859,7 @@ static int hvmemul_rep_stos(switch ( p2mt ) {unsigned long bytes; -void *buf; +char *buf;default :/* Allocate temporary buffer. */
@@ -1882,10 +1882,10 @@ static int hvmemul_rep_stos(#define CASE (bits, suffix) \ case (bits) / 8: \ asm ( "rep stos" #suffix \ - : "=m" (*(char (*)[bytes])buf), \ + : "=m" (*buf), \ "=D" (dummy), "=c" (dummy) \ : "a" (*(const uint##bits##_t *)p_data), \ - "1" (buf), "2" (*reps) ); \ + "1" (buf), "2" (*reps) : "memory" ); \ breakCASE (8 , b);CASE (16 , w);
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/5] x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla
Quoted 13 lines
-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 24 September 2018 14:00
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Jan Beulich <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 3/5] x86/hvm: Adjusthvmemul_rep_stos()to compile with - WvlaFor at least "gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609", the typecast of buf triggers a Variable Length Array warning.
Quoted 7 lines
Alter the asm expression to avoid the typecast, which necessitates the introduction of a memory clobber as the compiler can no longer identify the total quantity of written memory.
Despite the memory clobber, there is no change to the generated.
Signed-off-by Andrew Cooper <[email protected]>
Quoted 35 lines
---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Paul Durrant <[email protected]>--- xen/arch/x86/hvm/emulate.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)xen/arch/x86/hvm/emulate.c +3
-4@@ -1859,7 +1859,7 @@ static int hvmemul_rep_stos(switch ( p2mt ) {unsigned long bytes; -void *buf; +char *buf;default :/* Allocate temporary buffer. */ @@ -1882,10 +1882,10 @@ static int hvmemul_rep_stos(#define CASE (bits, suffix) \ case (bits) / 8: \ asm ( "rep stos" #suffix \ - : "=m" (*(char (*)[bytes])buf), \ + : "=m" (*buf), \ "=D" (dummy), "=c" (dummy) \ : "a" (*(const uint##bits##_t *)p_data), \ - "1" (buf), "2" (*reps) ); \ + "1" (buf), "2" (*reps) : "memory" ); \ breakCASE (8 , b);CASE (16 , w); --
Quoted 1 line
2.1.4
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/5] x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla
In reply to Andrew CooperQuoted 8 lines · Andrew Cooper
For at least "gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609", the typecast of buf triggers a Variable Length Array warning.
Alter the asm expression to avoid the typecast, which necessitates the introduction of a memory clobber as the compiler can no longer identify the total quantity of written memory.
Despite the memory clobber, there is no change to the generated.
generated assembly code?
Quoted 2 lines
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/5] x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla
Quoted 10 lines · Wei Liu
Quoted 8 lines · Andrew Cooper
For at least "gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609", the typecast of buf triggers a Variable Length Array warning.
Alter the asm expression to avoid the typecast, which necessitates the introduction of a memory clobber as the compiler can no longer identify the total quantity of written memory.
Despite the memory clobber, there is no change to the generated.
generated assembly code?
Yeah - I already noticed and fixed it up to "generated asm."
Serves me right for last minute edits to my commit message.
Quoted 3 lines
Quoted 2 lines
Signed-off-by Andrew Cooper <[email protected]>Reviewed-by Wei Liu <[email protected]>
Thanks,
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/5] x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla
In reply to Andrew CooperQuoted 7 lines
Quoted 1 line
Quoted 1 line · On 24.09.18 at 15:00, <[email protected]> wrote:
For at least "gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609", the typecast of buf triggers a Variable Length Array warning.
Alter the asm expression to avoid the typecast, which necessitates the introduction of a memory clobber as the compiler can no longer identify the total quantity of written memory.
I dislike this, but what do you do. Would be quite helpful though to have a comment there, or else I might be tempted to undo the memory clobber in a couple of months/years time.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/5] x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla
Quoted 10 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 24.09.18 at 15:00, <[email protected]> wrote:
For at least "gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609", the typecast of buf triggers a Variable Length Array warning.
Alter the asm expression to avoid the typecast, which necessitates the introduction of a memory clobber as the compiler can no longer identify the total quantity of written memory.
I dislike this, but what do you do. Would be quite helpful though to have a comment there, or else I might be tempted to undo the memory clobber in a couple of months/years time.
On further investigation, its all versions of GCC and Clang when using -Wvla. I'll tweak the commit message so it doesn't read as if it might be a quirk in that specific compiler.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/5] x86/hvm: Adjust hvmemul_rep_stos() to compile with -Wvla
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 25.09.18 at 12:31, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 24.09.18 at 15:00, <[email protected]> wrote:
For at least "gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609", the typecast of buf triggers a Variable Length Array warning.
Alter the asm expression to avoid the typecast, which necessitates the introduction of a memory clobber as the compiler can no longer identify the total quantity of written memory.
Quoted 7 lines
Quoted 3 lines
I dislike this, but what do you do. Would be quite helpful though to have a comment there, or else I might be tempted to undo the memory clobber in a couple of months/years time.
On further investigation, its all versions of GCC and Clang when using -Wvla. I'll tweak the commit message so it doesn't read as if it might be a quirk in that specific compiler.
Well, it's a quirk in all compilers then. That type cast certainly is not an _instantiation_ of a VLA, yet only those are what we want to prevent (and what I understand the warning is to point out).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 4/5 ARM/dom0: Avoid using a variable length array in make_memory_node()
In reply to Andrew CooperThe reg[] array can have a maximum size of 8 in practice, so use the worst case calculation rather than making it variable length.
---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
Only compile tested.
--- xen/arch/arm/domain_build.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
xen/arch/arm/domain_build.c +3 -1
@@ -539,9 +539,11 @@ static int __init make_memory_node(const struct domain *d,int res, i;int reg_size = addrcells + sizecells;int nr_cells = reg_size*kinfo->mem.nr_banks; -__be32 reg[nr_cells]; +__be32 reg[NR_MEM_BANKS *4 /* Worst case addrcells + sizecells */ ];__be32 *cells; +BUG_ON (nr_cells >=ARRAY_SIZE (reg)); +dt_dprintk ("Create memory node (reg size %d, nr cells %d)\n" , reg_size, nr_cells);
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 4/5] ARM/dom0: Avoid using a variable length array in make_memory_node()
Hi Andrew,
Quoted 4 lines · Andrew Cooper
The reg[] array can have a maximum size of 8 in practice, so use the worst case calculation rather than making it variable length.
Signed-off-by Andrew Cooper <[email protected]>
Cheers,
Quoted 27 lines
---
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>Only compile tested.
--- xen/arch/arm/domain_build.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)xen/arch/arm/domain_build.c +3
-1@@ -539,9 +539,11 @@ static int __init make_memory_node(const struct domain *d,int res, i;int reg_size = addrcells + sizecells;int nr_cells = reg_size*kinfo->mem.nr_banks; -__be32 reg[nr_cells]; +__be32 reg[NR_MEM_BANKS *4 /* Worst case addrcells + sizecells */ ];__be32 *cells; +BUG_ON (nr_cells >=ARRAY_SIZE (reg)); +dt_dprintk ("Create memory node (reg size %d, nr cells %d)\n" , reg_size, nr_cells);
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 5/5 xen: Disallow variable length arrays
In reply to Andrew CooperVariable length arrays result in excess stack utilisation, with a risk of stack overflow if the length is too large. It also results in fairly poor asm generation, because of requiring a divide as part of the space calcuation.
Xen no longer has any variable length arrays, so take the opportunity to formally disallow them.
---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
--- xen/Rules.mk | 1 + 1 file changed, 1 insertion(+)
xen/Rules.mk +1 -0
@@ -58,6 +58,7 @@ CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith CFLAGS += -pipe -D__XEN__ -include $(BASEDIR)/include/xen/config.h CFLAGS-$(CONFIG_DEBUG_INFO) += -g CFLAGS += '-D__OBJECT_FILE__="$@"' +$(call cc-option-add,CFLAGS,CC,-Wvla) ifneq ($(clang),y) # Clang doesn't understand this command line argument, and doesn't appear to
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 5/5] xen: Disallow variable length arrays
Quoted 9 lines · Andrew Cooper
Variable length arrays result in excess stack utilisation, with a risk of stack overflow if the length is too large. It also results in fairly poor asm generation, because of requiring a divide as part of the space calcuation.
Xen no longer has any variable length arrays, so take the opportunity to formally disallow them.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 5/5] xen: Disallow variable length arrays
In reply to Andrew CooperQuoted 9 lines · Andrew Cooper
Variable length arrays result in excess stack utilisation, with a risk of stack overflow if the length is too large. It also results in fairly poor asm generation, because of requiring a divide as part of the space calcuation.
Xen no longer has any variable length arrays, so take the opportunity to formally disallow them.
Signed-off-by Andrew Cooper <[email protected]>
Quoted 25 lines
---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>--- xen/Rules.mk | 1 + 1 file changed, 1 insertion(+)xen/Rules.mk +1
-0@@ -58,6 +58,7 @@ CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith CFLAGS += -pipe -D__XEN__ -include $(BASEDIR)/include/xen/config.h CFLAGS-$(CONFIG_DEBUG_INFO) += -g CFLAGS += '-D__OBJECT_FILE__="$@"' +$(call cc-option-add,CFLAGS,CC,-Wvla) ifneq ($(clang),y) # Clang doesn't understand this command line argument, and doesn't appear to
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 5/5] xen: Disallow variable length arrays
In reply to Andrew Cooperxen/Rules.mk CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith · line 61
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 24.09.18 at 15:00, <[email protected]> wrote:
xen/Rules.mk +1
-0@@ -58,6 +58,7 @@ CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith CFLAGS += -pipe -D__XEN__ -include $(BASEDIR)/include/xen/config.h CFLAGS-$(CONFIG_DEBUG_INFO) += -g CFLAGS += '-D__OBJECT_FILE__="$@"' +$(call cc-option-add,CFLAGS,CC,-Wvla)
Could I talk you into moving this up a few lines, next to the other -W settings (just out of context)? In any event
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel