xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Wei Liu <wei.liu2@citrix.com>, Ian Jackson <Ian.Jackson@eu.citrix.com>
Subject: [PATCH 2/2] libs/foreignmemory: do not use alloca(3)
Date: Wed, 17 Aug 2016 15:33:02 +0100	[thread overview]
Message-ID: <1471444382-30409-3-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1471444382-30409-1-git-send-email-wei.liu2@citrix.com>

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@citrix.com>
---
 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@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2016-08-17 14:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-17 14:33 [PATCH 0/2] tools/libs: don't use alloca(3) Wei Liu
2016-08-17 14:33 ` [PATCH 1/2] libs/gnttab: do not " Wei Liu
2016-08-22  9:46   ` David Vrabel
2016-08-22 10:10     ` Wei Liu
2016-08-22 10:24       ` David Vrabel
2016-08-22 10:44         ` Wei Liu
2016-08-17 14:33 ` Wei Liu [this message]
2016-08-17 23:39 ` [PATCH 0/2] tools/libs: don't " Dario Faggioli
2016-08-18  9:29   ` Wei Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1471444382-30409-3-git-send-email-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).