From ad03c82d65aa076d3a7e97ad58ceb3f8e63861f6 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 22 Apr 2019 12:46:36 -0700 Subject: [PATCH] Implement reallocarray. reallocarray is non-standard but present in GLIBC and BSDs. --- Makefile | 9 ++++----- basics/{libc => crt}/crt1.c | 0 basics/include/__functions_malloc.h | 4 ++++ basics/sources/reallocarray.c | 14 ++++++++++++++ basics/{libc => sources}/string.c | 0 expected/wasm32-wasi/defined-symbols.txt | 2 ++ 6 files changed, 24 insertions(+), 5 deletions(-) rename basics/{libc => crt}/crt1.c (100%) create mode 100644 basics/sources/reallocarray.c rename basics/{libc => sources}/string.c (100%) diff --git a/Makefile b/Makefile index 800e6b9..a6f84c9 100644 --- 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/libc/crt1.c b/basics/crt/crt1.c similarity index 100% rename from basics/libc/crt1.c rename to basics/crt/crt1.c diff --git a/basics/include/__functions_malloc.h b/basics/include/__functions_malloc.h index 803bc11..9d4676b 100644 --- a/basics/include/__functions_malloc.h +++ b/basics/include/__functions_malloc.h @@ -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/sources/reallocarray.c b/basics/sources/reallocarray.c new file mode 100644 index 0000000..3e828cc --- /dev/null +++ b/basics/sources/reallocarray.c @@ -0,0 +1,14 @@ +#include +#include + +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/libc/string.c b/basics/sources/string.c similarity index 100% rename from basics/libc/string.c rename to basics/sources/string.c diff --git a/expected/wasm32-wasi/defined-symbols.txt b/expected/wasm32-wasi/defined-symbols.txt index ae840ca..63a413e 100644 --- a/expected/wasm32-wasi/defined-symbols.txt +++ b/expected/wasm32-wasi/defined-symbols.txt @@ -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 -- 2.39.2