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

[Xen-devel] [PATCH 2/2] libs/foreignmemory: do not use alloca(3)



The semantics of alloca(3) is not very nice. If the stack overflows,
program behaviour is undefined.

Remove the use of alloca(3) and always use mmap.

Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx>
---
 tools/libs/foreignmemory/linux.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/tools/libs/foreignmemory/linux.c b/tools/libs/foreignmemory/linux.c
index 423c744..316d988 100644
--- a/tools/libs/foreignmemory/linux.c
+++ b/tools/libs/foreignmemory/linux.c
@@ -17,7 +17,6 @@
  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  */
 
-#include <alloca.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -187,18 +186,13 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle 
*fmem,
         xen_pfn_t *pfn;
         unsigned int pfn_arr_size = ROUNDUP((num * sizeof(*pfn)), PAGE_SHIFT);
 
-        if ( pfn_arr_size <= PAGE_SIZE )
-            pfn = alloca(num * sizeof(*pfn));
-        else
+        pfn = mmap(NULL, pfn_arr_size, PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
+        if ( pfn == MAP_FAILED )
         {
-            pfn = mmap(NULL, pfn_arr_size, PROT_READ | PROT_WRITE,
-                       MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
-            if ( pfn == MAP_FAILED )
-            {
-                PERROR("mmap of pfn array failed");
-                (void)munmap(addr, num << PAGE_SHIFT);
-                return NULL;
-            }
+            PERROR("mmap of pfn array failed");
+            (void)munmap(addr, num << PAGE_SHIFT);
+            return NULL;
         }
 
         memcpy(pfn, arr, num * sizeof(*arr));
@@ -241,8 +235,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle 
*fmem,
             break;
         }
 
-        if ( pfn_arr_size > PAGE_SIZE )
-            munmap(pfn, pfn_arr_size);
+        munmap(pfn, pfn_arr_size);
 
         if ( rc == -ENOENT && i == num )
             rc = 0;
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
https://lists.xen.org/xen-devel

 


Rackspace

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