From 76f8916b904db14facf811ae44e1265261349702 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 26 Jan 2015 13:34:58 -0800 Subject: [PATCH] Clean up slightly. Interestingly, this mostly involves cleaning up our implementation of various functions. Change-Id: Ifaef49b5cb997134f7bc0cc31bdac844bdb9e089 --- libc/Android.mk | 2 +- libc/arch-arm/arm.mk | 1 - libc/arch-arm/cortex-a15/cortex-a15.mk | 4 +- libc/arch-arm/cortex-a9/cortex-a9.mk | 4 +- libc/arch-arm/generic/generic.mk | 4 +- libc/arch-arm/krait/krait.mk | 4 +- libc/arch-mips/mips.mk | 5 +- libc/arch-mips64/mips64.mk | 4 +- libc/bionic/error.cpp | 1 + libc/bionic/memcpy.cpp | 38 ------- libc/bionic/memmove.c | 30 ------ libc/bionic/ndk_cruft.cpp | 6 ++ libc/bionic/scandir.cpp | 1 + libc/bionic/syslog.cpp | 1 + libc/dns/resolv/res_state.c | 1 + libc/include/stdlib.h | 13 +-- libc/include/string.h | 4 + .../upstream-openbsd/lib/libc/string/memcpy.c | 99 +++++++++++++++++++ .../lib/libc/string/{bcopy.c => memmove.c} | 18 +--- .../lib/libc/string/wmemcpy.c | 14 +-- 20 files changed, 143 insertions(+), 111 deletions(-) delete mode 100644 libc/bionic/memcpy.cpp delete mode 100644 libc/bionic/memmove.c create mode 100644 libc/upstream-openbsd/lib/libc/string/memcpy.c rename libc/upstream-openbsd/lib/libc/string/{bcopy.c => memmove.c} (89%) rename libc/{upstream-freebsd => upstream-openbsd}/lib/libc/string/wmemcpy.c (84%) diff --git a/libc/Android.mk b/libc/Android.mk index a2a310865..691017a8b 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -271,7 +271,6 @@ libc_upstream_freebsd_src_files := \ upstream-freebsd/lib/libc/string/wcsspn.c \ upstream-freebsd/lib/libc/string/wcstok.c \ upstream-freebsd/lib/libc/string/wmemchr.c \ - upstream-freebsd/lib/libc/string/wmemcpy.c \ upstream-freebsd/lib/libc/string/wmemset.c \ libc_upstream_netbsd_src_files := \ @@ -506,6 +505,7 @@ libc_upstream_openbsd_src_files := \ upstream-openbsd/lib/libc/string/strspn.c \ upstream-openbsd/lib/libc/string/strstr.c \ upstream-openbsd/lib/libc/string/strtok.c \ + upstream-openbsd/lib/libc/string/wmemcpy.c \ upstream-openbsd/lib/libc/string/wcslcpy.c \ upstream-openbsd/lib/libc/string/wcsstr.c \ upstream-openbsd/lib/libc/string/wcswidth.c \ diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk index 5f0b1b344..f712c4c69 100644 --- a/libc/arch-arm/arm.mk +++ b/libc/arch-arm/arm.mk @@ -40,7 +40,6 @@ libc_freebsd_src_files_arm += \ upstream-freebsd/lib/libc/string/wmemmove.c \ libc_openbsd_src_files_arm += \ - upstream-openbsd/lib/libc/string/bcopy.c \ upstream-openbsd/lib/libc/string/stpncpy.c \ upstream-openbsd/lib/libc/string/strlcat.c \ upstream-openbsd/lib/libc/string/strlcpy.c \ diff --git a/libc/arch-arm/cortex-a15/cortex-a15.mk b/libc/arch-arm/cortex-a15/cortex-a15.mk index ec423c5be..cc502cf29 100644 --- a/libc/arch-arm/cortex-a15/cortex-a15.mk +++ b/libc/arch-arm/cortex-a15/cortex-a15.mk @@ -9,4 +9,6 @@ libc_bionic_src_files_arm += \ arch-arm/cortex-a15/bionic/strcpy.S \ arch-arm/cortex-a15/bionic/__strcpy_chk.S \ arch-arm/cortex-a15/bionic/strlen.S \ - bionic/memmove.c \ + +libc_openbsd_src_files_arm += \ + upstream-openbsd/lib/libc/string/memmove.c \ diff --git a/libc/arch-arm/cortex-a9/cortex-a9.mk b/libc/arch-arm/cortex-a9/cortex-a9.mk index e38c40ae9..7570567a8 100644 --- a/libc/arch-arm/cortex-a9/cortex-a9.mk +++ b/libc/arch-arm/cortex-a9/cortex-a9.mk @@ -9,4 +9,6 @@ libc_bionic_src_files_arm += \ arch-arm/cortex-a9/bionic/strcpy.S \ arch-arm/cortex-a9/bionic/__strcpy_chk.S \ arch-arm/cortex-a9/bionic/strlen.S \ - bionic/memmove.c \ + +libc_openbsd_src_files_arm += \ + upstream-openbsd/lib/libc/string/memmove.c \ diff --git a/libc/arch-arm/generic/generic.mk b/libc/arch-arm/generic/generic.mk index 96ed949ec..e49d6d200 100644 --- a/libc/arch-arm/generic/generic.mk +++ b/libc/arch-arm/generic/generic.mk @@ -5,8 +5,10 @@ libc_bionic_src_files_arm += \ arch-arm/generic/bionic/strcmp.S \ arch-arm/generic/bionic/strcpy.S \ arch-arm/generic/bionic/strlen.c \ - bionic/memmove.c \ bionic/__strcat_chk.cpp \ bionic/__strcpy_chk.cpp \ + +libc_openbsd_src_files_arm += \ + upstream-openbsd/lib/libc/string/memmove.c \ upstream-openbsd/lib/libc/string/stpcpy.c \ upstream-openbsd/lib/libc/string/strcat.c \ diff --git a/libc/arch-arm/krait/krait.mk b/libc/arch-arm/krait/krait.mk index 450f4723e..8bd5e8bff 100644 --- a/libc/arch-arm/krait/krait.mk +++ b/libc/arch-arm/krait/krait.mk @@ -12,4 +12,6 @@ libc_bionic_src_files_arm += \ arch-arm/cortex-a15/bionic/strcat.S \ arch-arm/cortex-a15/bionic/strcpy.S \ arch-arm/cortex-a15/bionic/strlen.S \ - bionic/memmove.c \ + +libc_openbsd_src_files_arm += \ + upstream-openbsd/lib/libc/string/memmove.c \ diff --git a/libc/arch-mips/mips.mk b/libc/arch-mips/mips.mk index d558baa00..034633e95 100644 --- a/libc/arch-mips/mips.mk +++ b/libc/arch-mips/mips.mk @@ -29,7 +29,6 @@ libc_bionic_src_files_mips += \ bionic/__strcat_chk.cpp \ bionic/memchr.c \ bionic/memcmp.c \ - bionic/memmove.c \ bionic/memrchr.c \ bionic/strchr.cpp \ bionic/strnlen.c \ @@ -46,7 +45,7 @@ libc_freebsd_src_files_mips += \ upstream-freebsd/lib/libc/string/wmemmove.c \ libc_openbsd_src_files_mips += \ - upstream-openbsd/lib/libc/string/bcopy.c \ + upstream-openbsd/lib/libc/string/memmove.c \ upstream-openbsd/lib/libc/string/stpcpy.c \ upstream-openbsd/lib/libc/string/stpncpy.c \ upstream-openbsd/lib/libc/string/strcat.c \ @@ -79,10 +78,10 @@ libc_bionic_src_files_mips += \ else libc_bionic_src_files_mips += \ - bionic/memcpy.cpp \ bionic/memset.c \ libc_openbsd_src_files_mips += \ + upstream-openbsd/lib/libc/string/memcpy.c \ upstream-openbsd/lib/libc/string/strlen.c \ endif diff --git a/libc/arch-mips64/mips64.mk b/libc/arch-mips64/mips64.mk index 6fa7d3354..26733cb49 100644 --- a/libc/arch-mips64/mips64.mk +++ b/libc/arch-mips64/mips64.mk @@ -11,8 +11,6 @@ libc_bionic_src_files_mips64 += \ bionic/__strcat_chk.cpp \ bionic/memchr.c \ bionic/memcmp.c \ - bionic/memcpy.cpp \ - bionic/memmove.c \ bionic/memrchr.c \ bionic/memset.c \ bionic/strchr.cpp \ @@ -30,6 +28,8 @@ libc_freebsd_src_files_mips64 += \ upstream-freebsd/lib/libc/string/wmemmove.c \ libc_openbsd_src_files_mips64 += \ + upstream-openbsd/lib/libc/string/memcpy.c \ + upstream-openbsd/lib/libc/string/memmove.c \ upstream-openbsd/lib/libc/string/stpcpy.c \ upstream-openbsd/lib/libc/string/stpncpy.c \ upstream-openbsd/lib/libc/string/strcat.c \ diff --git a/libc/bionic/error.cpp b/libc/bionic/error.cpp index 9f4d0eef8..e8df0e0e3 100644 --- a/libc/bionic/error.cpp +++ b/libc/bionic/error.cpp @@ -31,6 +31,7 @@ #include #include #include +#include unsigned int error_message_count = 0; void (*error_print_progname)(void) = NULL; diff --git a/libc/bionic/memcpy.cpp b/libc/bionic/memcpy.cpp deleted file mode 100644 index d527e85a3..000000000 --- a/libc/bionic/memcpy.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -#undef _FORTIFY_SOURCE -#include -#include - -// Our unoptimized memcpy just calls the best bcopy available. -// (It's this way round rather than the opposite because we're based on BSD source.) -void* memcpy(void* dst, const void* src, size_t n) { - bcopy(src, dst, n); - return dst; -} diff --git a/libc/bionic/memmove.c b/libc/bionic/memmove.c deleted file mode 100644 index 39c766e2f..000000000 --- a/libc/bionic/memmove.c +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2008 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. - */ - -#define MEMMOVE -#include "upstream-openbsd/lib/libc/string/bcopy.c" diff --git a/libc/bionic/ndk_cruft.cpp b/libc/bionic/ndk_cruft.cpp index 18a4a148a..341ba015a 100644 --- a/libc/bionic/ndk_cruft.cpp +++ b/libc/bionic/ndk_cruft.cpp @@ -235,6 +235,12 @@ extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) { return signal(signum, handler); } +// This was removed from POSIX 2008. +#undef bcopy +extern "C" void bcopy(const void* src, void* dst, size_t n) { + memcpy(dst, src, n); +} + // sysv_signal() was never in POSIX. extern sighandler_t _signal(int signum, sighandler_t handler, int flags); extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) { diff --git a/libc/bionic/scandir.cpp b/libc/bionic/scandir.cpp index 28b4ed0aa..ee62feee4 100644 --- a/libc/bionic/scandir.cpp +++ b/libc/bionic/scandir.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "private/bionic_macros.h" #include "private/ScopedReaddir.h" diff --git a/libc/bionic/syslog.cpp b/libc/bionic/syslog.cpp index d8b8b190d..8e3f34f9a 100644 --- a/libc/bionic/syslog.cpp +++ b/libc/bionic/syslog.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include "private/libc_logging.h" diff --git a/libc/dns/resolv/res_state.c b/libc/dns/resolv/res_state.c index 75bff9775..7533d1957 100644 --- a/libc/dns/resolv/res_state.c +++ b/libc/dns/resolv/res_state.c @@ -34,6 +34,7 @@ #include "resolv_cache.h" #include #include +#include #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ #include diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 0060ea3f0..4cb288d12 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -25,15 +25,16 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -#ifndef _STDLIB_H_ -#define _STDLIB_H_ + +#ifndef _STDLIB_H +#define _STDLIB_H #include +#include -#include #include -#include -#include +#include +#include __BEGIN_DECLS @@ -173,4 +174,4 @@ extern size_t __ctype_get_mb_cur_max(void); __END_DECLS -#endif /* _STDLIB_H_ */ +#endif /* _STDLIB_H */ diff --git a/libc/include/string.h b/libc/include/string.h index f0bed100c..4ca77ae0f 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -36,6 +36,10 @@ __BEGIN_DECLS +#if defined(__USE_BSD) +#include +#endif + extern void* memccpy(void* __restrict, const void* __restrict, int, size_t); extern void* memchr(const void *, int, size_t) __purefunc; extern void* memrchr(const void *, int, size_t) __purefunc; diff --git a/libc/upstream-openbsd/lib/libc/string/memcpy.c b/libc/upstream-openbsd/lib/libc/string/memcpy.c new file mode 100644 index 000000000..1b9715e47 --- /dev/null +++ b/libc/upstream-openbsd/lib/libc/string/memcpy.c @@ -0,0 +1,99 @@ +/* $OpenBSD: memcpy.c,v 1.1 2014/11/30 19:43:56 deraadt Exp $ */ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 +#include + +/* + * sizeof(word) MUST BE A POWER OF TWO + * SO THAT wmask BELOW IS ALL ONES + */ +typedef long word; /* "word" used for optimal copy speed */ + +#define wsize sizeof(word) +#define wmask (wsize - 1) + +/* + * Copy a block of memory, not handling overlap. + */ +void * +memcpy(void *dst0, const void *src0, size_t length) +{ + char *dst = dst0; + const char *src = src0; + size_t t; + + if (length == 0 || dst == src) /* nothing to do */ + goto done; + + if ((dst < src && dst + length > src) || + (src < dst && src + length > dst)) { + struct syslog_data sdata = SYSLOG_DATA_INIT; + + syslog_r(LOG_CRIT, &sdata, "backwards memcpy"); + abort(); + } + + /* + * Macros: loop-t-times; and loop-t-times, t>0 + */ +#define TLOOP(s) if (t) TLOOP1(s) +#define TLOOP1(s) do { s; } while (--t) + + /* + * Copy forward. + */ + t = (long)src; /* only need low bits */ + if ((t | (long)dst) & wmask) { + /* + * Try to align operands. This cannot be done + * unless the low bits match. + */ + if ((t ^ (long)dst) & wmask || length < wsize) + t = length; + else + t = wsize - (t & wmask); + length -= t; + TLOOP1(*dst++ = *src++); + } + /* + * Copy whole words, then mop up any trailing bytes. + */ + t = length / wsize; + TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); + t = length & wmask; + TLOOP(*dst++ = *src++); +done: + return (dst0); +} diff --git a/libc/upstream-openbsd/lib/libc/string/bcopy.c b/libc/upstream-openbsd/lib/libc/string/memmove.c similarity index 89% rename from libc/upstream-openbsd/lib/libc/string/bcopy.c rename to libc/upstream-openbsd/lib/libc/string/memmove.c index 4308c6484..1baad5354 100644 --- a/libc/upstream-openbsd/lib/libc/string/bcopy.c +++ b/libc/upstream-openbsd/lib/libc/string/memmove.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bcopy.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ +/* $OpenBSD: memmove.c,v 1.1 2014/11/30 19:43:56 deraadt Exp $ */ /*- * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. @@ -44,21 +44,9 @@ typedef long word; /* "word" used for optimal copy speed */ /* * Copy a block of memory, handling overlap. - * This is the routine that actually implements - * (the portable versions of) bcopy, memcpy, and memmove. */ -#ifdef MEMCOPY -void * -memcpy(void *dst0, const void *src0, size_t length) -#else -#ifdef MEMMOVE void * memmove(void *dst0, const void *src0, size_t length) -#else -void -bcopy(const void *src0, void *dst0, size_t length) -#endif -#endif { char *dst = dst0; const char *src = src0; @@ -120,9 +108,5 @@ bcopy(const void *src0, void *dst0, size_t length) TLOOP(*--dst = *--src); } done: -#if defined(MEMCOPY) || defined(MEMMOVE) return (dst0); -#else - return; -#endif } diff --git a/libc/upstream-freebsd/lib/libc/string/wmemcpy.c b/libc/upstream-openbsd/lib/libc/string/wmemcpy.c similarity index 84% rename from libc/upstream-freebsd/lib/libc/string/wmemcpy.c rename to libc/upstream-openbsd/lib/libc/string/wmemcpy.c index c10770ce4..9bbd83648 100644 --- a/libc/upstream-freebsd/lib/libc/string/wmemcpy.c +++ b/libc/upstream-openbsd/lib/libc/string/wmemcpy.c @@ -1,3 +1,6 @@ +/* $OpenBSD: wmemcpy.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ +/* $NetBSD: wmemcpy.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ + /*- * Copyright (c)1999 Citrus Project, * All rights reserved. @@ -26,19 +29,12 @@ * citrus Id: wmemcpy.c,v 1.2 2000/12/20 14:08:31 itojun Exp */ -#include -#if 0 -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: wmemcpy.c,v 1.1 2000/12/23 23:14:37 itojun Exp $"); -#endif /* LIBC_SCCS and not lint */ -#endif -__FBSDID("$FreeBSD$"); - #include #include wchar_t * -wmemcpy(wchar_t * __restrict d, const wchar_t * __restrict s, size_t n) +wmemcpy(wchar_t *d, const wchar_t *s, size_t n) { + return (wchar_t *)memcpy(d, s, n * sizeof(wchar_t)); }