Skip to contentSkip to conversation

qemu-user mmap bug

PATCH New Paul Brook 1 participant 1 message Jan 4, 2006

lore
Share & tools
lore

Paul Brook

author
#0 lore raw

PATCH 1/1 qemu-user mmap bug

Under some circumstances target_mmap will return -EINVAL. However its callers expect it behave like normal mmap. ie. return -1 and ser errno.

Discovered when testing qemu with some malformed ELF executables. It segfaulted instead of displaying an error.
The patch below changes target_map to have the expected error behavior.

Paul

Index: linux-user/mmap.c
===================================================================
RCS file: /sources/qemu/qemu/linux-user/mmap.c,v
retrieving revision 1.8

mmap.c +12 -6
diff -u -p -r1.8 mmap.c
@@ -183,8 +183,10 @@ long target_mmap(unsigned long start, un
     }
 #endif
 
-    if (offset & ~TARGET_PAGE_MASK)
-        return -EINVAL;
+    if (offset & ~TARGET_PAGE_MASK) {
+        errno = EINVAL;
+        return -1;
+    }
 
     len = TARGET_PAGE_ALIGN(len);
     if (len == 0)
@@ -232,8 +234,10 @@ long target_mmap(unsigned long start, un
         }
     }
     
-    if (start & ~TARGET_PAGE_MASK)
-        return -EINVAL;
+    if (start & ~TARGET_PAGE_MASK) {
+        errno = EINVAL;
+        return -1;
+    }
     end = start + len;
     host_end = HOST_PAGE_ALIGN(end);
 
@@ -244,8 +248,10 @@ long target_mmap(unsigned long start, un
         /* msync() won't work here, so we return an error if write is
            possible while it is a shared mapping */
         if ((flags & MAP_TYPE) == MAP_SHARED &&
-            (prot & PROT_WRITE))
-            return -EINVAL;
+            (prot & PROT_WRITE)) {
+            errno = EINVAL;
+            return -1;
+        }
         retaddr = target_mmap(start, len, prot | PROT_WRITE, 
                               MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, 
                               -1, 0);