From 4200e260d266fd0c176e71fbd720d0bab04b02db Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 3 Nov 2015 05:14:08 -0500 Subject: [PATCH] fix the mremap signature The mremap definition was incorrect (unsigned long instead of int) and it was missing the optional new_address parameter. Change-Id: Ib9d0675aaa098c21617cedc9b2b8cf267be3aec4 --- libc/Android.mk | 3 +- libc/SYSCALLS.TXT | 2 +- libc/arch-arm/syscalls/___mremap.S | 23 ++++++++++ libc/arch-arm/syscalls/mremap.S | 14 ------ .../syscalls/{mremap.S => ___mremap.S} | 5 ++- .../syscalls/{mremap.S => ___mremap.S} | 5 ++- .../syscalls/{mremap.S => ___mremap.S} | 5 ++- .../syscalls/{mremap.S => ___mremap.S} | 18 +++++--- .../syscalls/{mremap.S => ___mremap.S} | 5 ++- libc/bionic/{ioctl.c => ioctl.cpp} | 21 ++++----- libc/bionic/mremap.cpp | 45 +++++++++++++++++++ libc/include/sys/mman.h | 2 +- tests/sys_mman_test.cpp | 4 ++ 13 files changed, 109 insertions(+), 43 deletions(-) create mode 100644 libc/arch-arm/syscalls/___mremap.S delete mode 100644 libc/arch-arm/syscalls/mremap.S rename libc/arch-arm64/syscalls/{mremap.S => ___mremap.S} (81%) rename libc/arch-mips/syscalls/{mremap.S => ___mremap.S} (84%) rename libc/arch-mips64/syscalls/{mremap.S => ___mremap.S} (87%) rename libc/arch-x86/syscalls/{mremap.S => ___mremap.S} (69%) rename libc/arch-x86_64/syscalls/{mremap.S => ___mremap.S} (84%) rename libc/bionic/{ioctl.c => ioctl.cpp} (86%) create mode 100644 libc/bionic/mremap.cpp diff --git a/libc/Android.mk b/libc/Android.mk index 2ea96d44b..4ffdf5768 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -47,7 +47,6 @@ libc_common_src_files := \ bionic/if_indextoname.c \ bionic/if_nametoindex.c \ bionic/initgroups.c \ - bionic/ioctl.c \ bionic/isatty.c \ bionic/memmem.c \ bionic/pututline.c \ @@ -151,6 +150,7 @@ libc_bionic_ndk_src_files := \ bionic/gettid.cpp \ bionic/__gnu_basename.cpp \ bionic/inotify_init.cpp \ + bionic/ioctl.cpp \ bionic/lchown.cpp \ bionic/lfs64_support.cpp \ bionic/__libc_current_sigrtmax.cpp \ @@ -170,6 +170,7 @@ libc_bionic_ndk_src_files := \ bionic/mkfifo.cpp \ bionic/mknod.cpp \ bionic/mntent.cpp \ + bionic/mremap.cpp \ bionic/NetdClientDispatch.cpp \ bionic/open.cpp \ bionic/pathconf.cpp \ diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT index 4188c1518..526b6d090 100644 --- a/libc/SYSCALLS.TXT +++ b/libc/SYSCALLS.TXT @@ -107,7 +107,7 @@ ssize_t pwritev|pwritev64(int, const struct iovec*, int, off_t) arm64,mips64 int ___close:close(int) all pid_t __getpid:getpid() all int munmap(void*, size_t) all -void* mremap(void*, size_t, size_t, unsigned long) all +void* ___mremap:mremap(void*, size_t, size_t, int, void*) all int msync(const void*, size_t, int) all int mprotect(const void*, size_t, int) all int madvise(void*, size_t, int) all diff --git a/libc/arch-arm/syscalls/___mremap.S b/libc/arch-arm/syscalls/___mremap.S new file mode 100644 index 000000000..ade8d7897 --- /dev/null +++ b/libc/arch-arm/syscalls/___mremap.S @@ -0,0 +1,23 @@ +/* Generated by gensyscalls.py. Do not edit. */ + +#include + +ENTRY(___mremap) + mov ip, sp + stmfd sp!, {r4, r5, r6, r7} + .cfi_def_cfa_offset 16 + .cfi_rel_offset r4, 0 + .cfi_rel_offset r5, 4 + .cfi_rel_offset r6, 8 + .cfi_rel_offset r7, 12 + ldmfd ip, {r4, r5, r6} + ldr r7, =__NR_mremap + swi #0 + ldmfd sp!, {r4, r5, r6, r7} + .cfi_def_cfa_offset 0 + cmn r0, #(MAX_ERRNO + 1) + bxls lr + neg r0, r0 + b __set_errno_internal +END(___mremap) +.hidden ___mremap diff --git a/libc/arch-arm/syscalls/mremap.S b/libc/arch-arm/syscalls/mremap.S deleted file mode 100644 index 505ea3c5d..000000000 --- a/libc/arch-arm/syscalls/mremap.S +++ /dev/null @@ -1,14 +0,0 @@ -/* Generated by gensyscalls.py. Do not edit. */ - -#include - -ENTRY(mremap) - mov ip, r7 - ldr r7, =__NR_mremap - swi #0 - mov r7, ip - cmn r0, #(MAX_ERRNO + 1) - bxls lr - neg r0, r0 - b __set_errno_internal -END(mremap) diff --git a/libc/arch-arm64/syscalls/mremap.S b/libc/arch-arm64/syscalls/___mremap.S similarity index 81% rename from libc/arch-arm64/syscalls/mremap.S rename to libc/arch-arm64/syscalls/___mremap.S index 69b91d6bc..aeb93bcef 100644 --- a/libc/arch-arm64/syscalls/mremap.S +++ b/libc/arch-arm64/syscalls/___mremap.S @@ -2,7 +2,7 @@ #include -ENTRY(mremap) +ENTRY(___mremap) mov x8, __NR_mremap svc #0 @@ -11,4 +11,5 @@ ENTRY(mremap) b.hi __set_errno_internal ret -END(mremap) +END(___mremap) +.hidden ___mremap diff --git a/libc/arch-mips/syscalls/mremap.S b/libc/arch-mips/syscalls/___mremap.S similarity index 84% rename from libc/arch-mips/syscalls/mremap.S rename to libc/arch-mips/syscalls/___mremap.S index 7cbb94e7e..82e2eb366 100644 --- a/libc/arch-mips/syscalls/mremap.S +++ b/libc/arch-mips/syscalls/___mremap.S @@ -2,7 +2,7 @@ #include -ENTRY(mremap) +ENTRY(___mremap) .set noreorder .cpload t9 li v0, __NR_mremap @@ -16,4 +16,5 @@ ENTRY(mremap) j t9 nop .set reorder -END(mremap) +END(___mremap) +.hidden ___mremap diff --git a/libc/arch-mips64/syscalls/mremap.S b/libc/arch-mips64/syscalls/___mremap.S similarity index 87% rename from libc/arch-mips64/syscalls/mremap.S rename to libc/arch-mips64/syscalls/___mremap.S index cf7f1ded8..5004d505a 100644 --- a/libc/arch-mips64/syscalls/mremap.S +++ b/libc/arch-mips64/syscalls/___mremap.S @@ -2,7 +2,7 @@ #include -ENTRY(mremap) +ENTRY(___mremap) .set push .set noreorder li v0, __NR_mremap @@ -22,4 +22,5 @@ ENTRY(mremap) j t9 move ra, t0 .set pop -END(mremap) +END(___mremap) +.hidden ___mremap diff --git a/libc/arch-x86/syscalls/mremap.S b/libc/arch-x86/syscalls/___mremap.S similarity index 69% rename from libc/arch-x86/syscalls/mremap.S rename to libc/arch-x86/syscalls/___mremap.S index 869ef5de9..e5e9c5467 100644 --- a/libc/arch-x86/syscalls/mremap.S +++ b/libc/arch-x86/syscalls/___mremap.S @@ -2,7 +2,7 @@ #include -ENTRY(mremap) +ENTRY(___mremap) pushl %ebx .cfi_def_cfa_offset 8 .cfi_rel_offset ebx, 0 @@ -15,10 +15,14 @@ ENTRY(mremap) pushl %esi .cfi_adjust_cfa_offset 4 .cfi_rel_offset esi, 0 - mov 20(%esp), %ebx - mov 24(%esp), %ecx - mov 28(%esp), %edx - mov 32(%esp), %esi + pushl %edi + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset edi, 0 + mov 24(%esp), %ebx + mov 28(%esp), %ecx + mov 32(%esp), %edx + mov 36(%esp), %esi + mov 40(%esp), %edi movl $__NR_mremap, %eax int $0x80 cmpl $-MAX_ERRNO, %eax @@ -28,9 +32,11 @@ ENTRY(mremap) call __set_errno_internal addl $4, %esp 1: + popl %edi popl %esi popl %edx popl %ecx popl %ebx ret -END(mremap) +END(___mremap) +.hidden ___mremap diff --git a/libc/arch-x86_64/syscalls/mremap.S b/libc/arch-x86_64/syscalls/___mremap.S similarity index 84% rename from libc/arch-x86_64/syscalls/mremap.S rename to libc/arch-x86_64/syscalls/___mremap.S index a6042cb6c..cc6dee9c0 100644 --- a/libc/arch-x86_64/syscalls/mremap.S +++ b/libc/arch-x86_64/syscalls/___mremap.S @@ -2,7 +2,7 @@ #include -ENTRY(mremap) +ENTRY(___mremap) movq %rcx, %r10 movl $__NR_mremap, %eax syscall @@ -13,4 +13,5 @@ ENTRY(mremap) call __set_errno_internal 1: ret -END(mremap) +END(___mremap) +.hidden ___mremap diff --git a/libc/bionic/ioctl.c b/libc/bionic/ioctl.cpp similarity index 86% rename from libc/bionic/ioctl.c rename to libc/bionic/ioctl.cpp index 6dd95d0a9..db851329b 100644 --- a/libc/bionic/ioctl.c +++ b/libc/bionic/ioctl.cpp @@ -25,19 +25,16 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ + +#include #include -extern int __ioctl(int, int, void *); +extern "C" int __ioctl(int, int, void *); -int ioctl(int fd, int request, ...) -{ - va_list ap; - void * arg; - - va_start(ap, request); - arg = va_arg(ap, void *); - va_end(ap); - - return __ioctl(fd, request, arg); +int ioctl(int fd, int request, ...) { + va_list ap; + va_start(ap, request); + void* arg = va_arg(ap, void*); + va_end(ap); + return __ioctl(fd, request, arg); } - diff --git a/libc/bionic/mremap.cpp b/libc/bionic/mremap.cpp new file mode 100644 index 000000000..4892b1dd9 --- /dev/null +++ b/libc/bionic/mremap.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +extern "C" void* ___mremap(void*, size_t, size_t, int, void*); + +void* mremap(void* old_address, size_t old_size, size_t new_size, int flags, ...) { + void* new_address = nullptr; + // The optional argument is only valid if the MREMAP_FIXED flag is set, + // so we assume it's not present otherwise. + if ((flags & MREMAP_FIXED) != 0) { + va_list ap; + va_start(ap, flags); + new_address = va_arg(ap, void*); + va_end(ap); + } + return ___mremap(old_address, old_size, new_size, flags, new_address); +} diff --git a/libc/include/sys/mman.h b/libc/include/sys/mman.h index 6857f60fa..a19ceb58b 100644 --- a/libc/include/sys/mman.h +++ b/libc/include/sys/mman.h @@ -59,7 +59,7 @@ extern void* mmap64(void*, size_t, int, int, int, off64_t); extern int munmap(void*, size_t); extern int msync(const void*, size_t, int); extern int mprotect(const void*, size_t, int); -extern void* mremap(void*, size_t, size_t, unsigned long); +extern void* mremap(void*, size_t, size_t, int, ...); extern int mlockall(int); extern int munlockall(void); diff --git a/tests/sys_mman_test.cpp b/tests/sys_mman_test.cpp index b0e40fdd6..dffb646a5 100644 --- a/tests/sys_mman_test.cpp +++ b/tests/sys_mman_test.cpp @@ -215,3 +215,7 @@ TEST(sys_mman, posix_madvise_POSIX_MADV_DONTNEED) { ASSERT_EQ(0, munmap(map, pagesize)); } + +TEST(sys_mman, mremap) { + ASSERT_EQ(MAP_FAILED, mremap(nullptr, 0, 0, 0)); +}