]> git.proxmox.com Git - wasi-libc.git/commitdiff
Implement reallocarray.
authorDan Gohman <sunfish@mozilla.com>
Mon, 22 Apr 2019 19:46:36 +0000 (12:46 -0700)
committerDan Gohman <sunfish@mozilla.com>
Tue, 23 Apr 2019 22:00:07 +0000 (15:00 -0700)
reallocarray is non-standard but present in GLIBC and BSDs.

Makefile
basics/crt/crt1.c [new file with mode: 0644]
basics/include/__functions_malloc.h
basics/libc/crt1.c [deleted file]
basics/libc/string.c [deleted file]
basics/sources/reallocarray.c [new file with mode: 0644]
basics/sources/string.c [new file with mode: 0644]
expected/wasm32-wasi/defined-symbols.txt

index 800e6b970ab2a88ad93deadfb06bd71fb06efad9..a6f84c96c4e4ab02de20ba115bd3bc39c0aa64b5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -33,9 +33,8 @@ endif
 # directories in the source tree.
 BASICS_DIR = $(CURDIR)/basics
 BASICS_INC = $(BASICS_DIR)/include
-BASICS_LIBC_DIR = $(BASICS_DIR)/libc
-BASICS_CRT_SOURCES = $(wildcard $(BASICS_LIBC_DIR)/crt*.c)
-BASICS_LIBC_SOURCES = $(BASICS_LIBC_DIR)/string.c
+BASICS_CRT_SOURCES = $(wildcard $(BASICS_DIR)/crt/*.c)
+BASICS_SOURCES = $(wildcard $(BASICS_DIR)/sources/*.c)
 DLMALLOC_DIR = $(CURDIR)/dlmalloc
 DLMALLOC_SRC_DIR = $(DLMALLOC_DIR)/src
 DLMALLOC_SOURCES = $(DLMALLOC_SRC_DIR)/dlmalloc.c
@@ -184,11 +183,11 @@ endif
 override WASM_CFLAGS += --sysroot="$(SYSROOT)"
 
 objs = $(patsubst $(CURDIR)/%.c,$(OBJDIR)/%.o,$(1))
-override BASICS_LIBC_OBJS = $(call objs,$(BASICS_LIBC_SOURCES))
+override BASICS_OBJS = $(call objs,$(BASICS_SOURCES))
 override DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES))
 override LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
 override LIBC_TOP_HALF_ALL_OBJS = $(call objs,$(LIBC_TOP_HALF_ALL_SOURCES))
-override LIBC_OBJS := $(BASICS_LIBC_OBJS)
+override LIBC_OBJS := $(BASICS_OBJS)
 ifeq ($(BUILD_DLMALLOC),yes)
 override LIBC_OBJS += $(DLMALLOC_OBJS)
 endif
diff --git a/basics/crt/crt1.c b/basics/crt/crt1.c
new file mode 100644 (file)
index 0000000..d63d107
--- /dev/null
@@ -0,0 +1,20 @@
+extern void __wasm_call_ctors(void);
+extern int main(int, char *[]);
+extern void __prepare_for_exit(void);
+void _Exit(int) __attribute__((noreturn));
+
+void _start(void) {
+    /* The linker synthesizes this to call constructors. */
+    __wasm_call_ctors();
+
+    /* Call main with no arguments. */
+    int r = main(0, 0);
+
+    /* Call atexit functions, destructors, stdio cleanup, etc. */
+    __prepare_for_exit();
+
+    /* If main exited successfully, just return, otherwise call _Exit. */
+    if (r != 0) {
+        _Exit(r);
+    }
+}
index 803bc1138f205e90ef86281f9848051214dd72d5..9d4676b784a25ddfa6becc23aa15d6782fc4ceb7 100644 (file)
@@ -15,6 +15,10 @@ void free(void *ptr);
 void *calloc(size_t nmemb, size_t size) __attribute__((__malloc__, __warn_unused_result__));
 void *realloc(void *ptr, size_t size) __attribute__((__warn_unused_result__));
 
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
+void *reallocarray(void *, size_t, size_t) __attribute__((__warn_unused_result__));
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/basics/libc/crt1.c b/basics/libc/crt1.c
deleted file mode 100644 (file)
index d63d107..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-extern void __wasm_call_ctors(void);
-extern int main(int, char *[]);
-extern void __prepare_for_exit(void);
-void _Exit(int) __attribute__((noreturn));
-
-void _start(void) {
-    /* The linker synthesizes this to call constructors. */
-    __wasm_call_ctors();
-
-    /* Call main with no arguments. */
-    int r = main(0, 0);
-
-    /* Call atexit functions, destructors, stdio cleanup, etc. */
-    __prepare_for_exit();
-
-    /* If main exited successfully, just return, otherwise call _Exit. */
-    if (r != 0) {
-        _Exit(r);
-    }
-}
diff --git a/basics/libc/string.c b/basics/libc/string.c
deleted file mode 100644 (file)
index 77d7201..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-#include <string.h>
-#include <stdint.h>
-
-static void *copy_forward(void *restrict dst, const void *restrict src, size_t n) {
-    char *d = (char *)dst;
-    const char *s = (const char *)src;
-    while (n-- != 0) {
-        *d++ = *s++;
-    }
-    return dst;
-}
-
-static void *copy_backward(void *restrict dst, const void *restrict src, size_t n) {
-    char *d = (char *)dst;
-    const char *s = (const char *)src;
-    d += n;
-    s += n;
-    while (n-- != 0) {
-        *--d = *--s;
-    }
-    return dst;
-}
-
-void *memcpy(void *restrict dst, const void *restrict src, size_t n) {
-    return copy_forward(dst, src, n);
-}
-
-void *memmove(void *dst, const void *src, size_t n) {
-    if ((uintptr_t)dst - (uintptr_t)src >= n) {
-        return copy_forward(dst, src, n);
-    }
-    return copy_backward(dst, src, n);
-}
-
-void *memset(void *restrict dst, int c, size_t n) {
-    char *d = (char *)dst;
-    while (n-- != 0) {
-        *d++ = c;
-    }
-    return dst;
-}
diff --git a/basics/sources/reallocarray.c b/basics/sources/reallocarray.c
new file mode 100644 (file)
index 0000000..3e828cc
--- /dev/null
@@ -0,0 +1,14 @@
+#include <stdlib.h>
+#include <errno.h>
+
+void *__reallocarray(void *ptr, size_t nmemb, size_t size) {
+    size_t bytes;
+    if (__builtin_umull_overflow(nmemb, size, &bytes)) {
+        errno = ENOMEM;
+        return NULL;
+    }
+    return realloc(ptr, bytes);
+}
+
+void *reallocarray(void *ptr, size_t nmemb, size_t size)
+    __attribute__((__weak__, __alias__("__reallocarray")));
diff --git a/basics/sources/string.c b/basics/sources/string.c
new file mode 100644 (file)
index 0000000..77d7201
--- /dev/null
@@ -0,0 +1,41 @@
+#include <string.h>
+#include <stdint.h>
+
+static void *copy_forward(void *restrict dst, const void *restrict src, size_t n) {
+    char *d = (char *)dst;
+    const char *s = (const char *)src;
+    while (n-- != 0) {
+        *d++ = *s++;
+    }
+    return dst;
+}
+
+static void *copy_backward(void *restrict dst, const void *restrict src, size_t n) {
+    char *d = (char *)dst;
+    const char *s = (const char *)src;
+    d += n;
+    s += n;
+    while (n-- != 0) {
+        *--d = *--s;
+    }
+    return dst;
+}
+
+void *memcpy(void *restrict dst, const void *restrict src, size_t n) {
+    return copy_forward(dst, src, n);
+}
+
+void *memmove(void *dst, const void *src, size_t n) {
+    if ((uintptr_t)dst - (uintptr_t)src >= n) {
+        return copy_forward(dst, src, n);
+    }
+    return copy_backward(dst, src, n);
+}
+
+void *memset(void *restrict dst, int c, size_t n) {
+    char *d = (char *)dst;
+    while (n-- != 0) {
+        *d++ = c;
+    }
+    return dst;
+}
index ae840cacd3bdceb646314e027ab0fabf5179be5e..63a413e418046b084115f447b054ff25721e7418 100644 (file)
@@ -163,6 +163,7 @@ __progname
 __progname_full
 __putenv
 __rand48_step
+__reallocarray
 __rem_pio2
 __rem_pio2_large
 __rem_pio2f
@@ -825,6 +826,7 @@ readlink
 readlinkat
 readv
 realloc
+reallocarray
 recv
 regcomp
 regerror