Skip to content

Commit

Permalink
Remove musl stdio from libandroid_support.
Browse files Browse the repository at this point in the history
Historically this is where the libandroid_support bugs are. The only
things we lose by dropping this is support for %a in printf and
functioning versions of *wprintf functions.

Parts of libc++ that rely on the former are just being marked XFAIL
for the moment (it's only needed for serialization of RNG state to
stringstreams, something that doesn't seem all that important). We'll
pick up Bionic's *sprintf family later to fix this.

We've actually had definitions for the latter forever, but they just
return -1 and set errno to ENOTSUP. The functions existing but not
working actually causes std::to_wstring to OOM (it allocates an
increasingly large wstring until it finds one that doesn't fail, and
never does), which in turn causes OOM killer to kill the device.

We can avoid this by implementing swprintf in terms of snprintf, using
wcsrtombs and mbsrtowcs to translate. The only thing that doesn't work
in this case is %ls. Since we're delegating to the device's snprintf,
that will not work on old devices, but will work on new devices, so
this only improves functionality.

We need to pull in part of bionic's wide character implementation for
this, because musl's isn't correct.

Also disable a %a test for pre-L. We didn't have support for this
until L, and it's temporarily being removed from libandroid_support.

Test: ./checkbuild.py && ./run_tests.py
Bug: android/ndk#300
Bug: android/ndk#437
Change-Id: Ic70c0d3f6f439de2e1c19027ec4fd3d37b766f77
  • Loading branch information
DanAlbert committed Jun 30, 2017
1 parent 355d3d6 commit 38a9713
Show file tree
Hide file tree
Showing 57 changed files with 411 additions and 2,175 deletions.
60 changes: 30 additions & 30 deletions sources/android/support/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ LOCAL_PATH := $(call my-dir)
android_support_c_includes := $(LOCAL_PATH)/include

ifneq ($(filter $(NDK_KNOWN_DEVICE_ABI64S),$(TARGET_ARCH_ABI)),)
is_lp64 := true
else
is_lp64 :=
endif

ifeq ($(is_lp64),true)
# 64-bit ABIs
android_support_sources := \
src/locale_support.c \
Expand Down Expand Up @@ -42,36 +48,9 @@ android_support_sources := \
src/musl-math/frexp.c \
src/musl-math/frexpf.c \
src/musl-math/frexpl.c \
src/musl-multibyte/btowc.c \
src/musl-multibyte/internal.c \
src/musl-multibyte/mblen.c \
src/musl-multibyte/mbrlen.c \
src/musl-multibyte/mbrtowc.c \
src/musl-multibyte/mbsinit.c \
src/musl-multibyte/mbsnrtowcs.c \
src/musl-multibyte/mbsrtowcs.c \
src/musl-multibyte/mbstowcs.c \
src/musl-multibyte/mbtowc.c \
src/musl-multibyte/wcrtomb.c \
src/musl-multibyte/wcsnrtombs.c \
src/musl-multibyte/wcsrtombs.c \
src/musl-multibyte/wcstombs.c \
src/musl-multibyte/wctob.c \
src/musl-multibyte/wctomb.c \
src/musl-stdio/printf.c \
src/musl-stdio/snprintf.c \
src/musl-stdio/sprintf.c \
src/musl-stdio/swprintf.c \
src/musl-stdio/vprintf.c \
src/musl-stdio/vsprintf.c \
src/musl-stdio/vwprintf.c \
src/musl-stdio/wprintf.c \
src/posix_memalign.cpp \
src/stdio/stdio_impl.c \
src/stdio/strtod.c \
src/stdio/vfprintf.c \
src/stdio/vfwprintf.c \
src/stdlib_support.c \
src/swprintf.cpp \
src/wchar_support.c \
src/wcstox/floatscan.c \
src/wcstox/intscan.c \
Expand Down Expand Up @@ -103,13 +82,22 @@ include $(PREBUILT_STATIC_LIBRARY)

else # Building

BIONIC_PATH := ../../../../bionic

# This is only available as a static library for now.
include $(CLEAR_VARS)
LOCAL_MODULE := android_support
LOCAL_SRC_FILES := $(android_support_sources)
LOCAL_C_INCLUDES := $(android_support_c_includes)
LOCAL_CFLAGS += -Drestrict=__restrict__ -ffunction-sections -fdata-sections -fvisibility=hidden
LOCAL_CPPFLAGS += -fvisibility-inlines-hidden
LOCAL_CFLAGS := \
-Drestrict=__restrict__ \
-ffunction-sections \
-fdata-sections \
-fvisibility=hidden \

LOCAL_CPPFLAGS := \
-fvisibility-inlines-hidden \
-std=c++11 \

# These Clang warnings are triggered by the Musl sources. The code is fine,
# but we don't want to modify it. TODO(digit): This is potentially dangerous,
Expand All @@ -125,6 +113,18 @@ LOCAL_CFLAGS += \
endif

LOCAL_EXPORT_C_INCLUDES := $(android_support_c_includes)

ifneq ($(is_lp64),true)
LOCAL_SRC_FILES += \
$(BIONIC_PATH)/libc/bionic/c32rtomb.cpp \
$(BIONIC_PATH)/libc/bionic/mbstate.cpp \
$(BIONIC_PATH)/libc/bionic/mbrtoc32.cpp \
$(BIONIC_PATH)/libc/bionic/wchar.cpp \
$(BIONIC_PATH)/libc/upstream-openbsd/lib/libc/locale/mbtowc.c \

LOCAL_C_INCLUDES += $(BIONIC_PATH)/libc
endif

include $(BUILD_STATIC_LIBRARY)

endif # Prebuilt/building
93 changes: 6 additions & 87 deletions sources/android/support/include/langinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,97 +28,16 @@
#ifndef NDK_ANDROID_SUPPORT_LANGINFO_H
#define NDK_ANDROID_SUPPORT_LANGINFO_H

// __LP64__
#include_next <langinfo.h>

#include <nl_types.h>
__BEGIN_DECLS

#define _NL_ITEM(category,index) (((category) << 10) | (index))
#if __ANDROID_API__ < __ANDROID_API_O__

#define _NL_ITEM_CATEGORY(nl) ((nl) >> 10)
#define _NL_ITEM_INDEX(nl) ((nl) & 0x3ff)
char* nl_langinfo(nl_item);

#define CODESET _NL_ITEM(LC_CTYPE, 0)

/* Abbreviated days of the week */
#define ABDAY_1 _NL_ITEM(LC_TIME,1)
#define ABDAY_2 _NL_ITEM(LC_TIME,2)
#define ABDAY_3 _NL_ITEM(LC_TIME,3)
#define ABDAY_4 _NL_ITEM(LC_TIME,4)
#define ABDAY_5 _NL_ITEM(LC_TIME,5)
#define ABDAY_6 _NL_ITEM(LC_TIME,6)
#define ABDAY_7 _NL_ITEM(LC_TIME,7)

/* Long names of the week */
#define DAY_1 _NL_ITEM(LC_TIME,11)
#define DAY_2 _NL_ITEM(LC_TIME,12)
#define DAY_3 _NL_ITEM(LC_TIME,13)
#define DAY_4 _NL_ITEM(LC_TIME,14)
#define DAY_5 _NL_ITEM(LC_TIME,15)
#define DAY_6 _NL_ITEM(LC_TIME,16)
#define DAY_7 _NL_ITEM(LC_TIME,17)

/* Abbreviated month names */
#define ABMON_1 _NL_ITEM(LC_TIME,21)
#define ABMON_2 _NL_ITEM(LC_TIME,22)
#define ABMON_3 _NL_ITEM(LC_TIME,23)
#define ABMON_4 _NL_ITEM(LC_TIME,24)
#define ABMON_5 _NL_ITEM(LC_TIME,25)
#define ABMON_6 _NL_ITEM(LC_TIME,26)
#define ABMON_7 _NL_ITEM(LC_TIME,27)
#define ABMON_8 _NL_ITEM(LC_TIME,28)
#define ABMON_9 _NL_ITEM(LC_TIME,29)
#define ABMON_10 _NL_ITEM(LC_TIME,30)
#define ABMON_11 _NL_ITEM(LC_TIME,31)
#define ABMON_12 _NL_ITEM(LC_TIME,32)

/* Long month names */
#define MON_1 _NL_ITEM(LC_TIME,41)
#define MON_2 _NL_ITEM(LC_TIME,42)
#define MON_3 _NL_ITEM(LC_TIME,43)
#define MON_4 _NL_ITEM(LC_TIME,44)
#define MON_5 _NL_ITEM(LC_TIME,45)
#define MON_6 _NL_ITEM(LC_TIME,46)
#define MON_7 _NL_ITEM(LC_TIME,47)
#define MON_8 _NL_ITEM(LC_TIME,48)
#define MON_9 _NL_ITEM(LC_TIME,49)
#define MON_10 _NL_ITEM(LC_TIME,50)
#define MON_11 _NL_ITEM(LC_TIME,51)
#define MON_12 _NL_ITEM(LC_TIME,52)

#define AM_STR _NL_ITEM(LC_TIME,53)
#define PM_STR _NL_ITEM(LC_TIME,54)
#define D_T_FMT _NL_ITEM(LC_TIME,55)
#define D_FMT _NL_ITEM(LC_TIME,56)
#define T_FMT _NL_ITEM(LC_TIME,57)
#define T_FMT_AMPM _NL_ITEM(LC_TIME,58)
#define ERA _NL_ITEM(LC_TIME,59)
#define ERA_D_FMT _NL_ITEM(LC_TIME,60)
#define ERA_D_T_FMT _NL_ITEM(LC_TIME,61)
#define ERA_T_FMT _NL_ITEM(LC_TIME,62)
#define ALT_DIGITS _NL_ITEM(LC_TIME,70)

#define INT_CURRENCY_SYMBOL _NL_ITEM(LC_MONETARY,0)
#define CURRENCY_SYMBOL _NL_ITEM(LC_MONETARY,1)
#define MON_DECIMAL_POINT _NL_ITEM(LC_MONETARY,2)
#define MON_THOUSANDS_SEP _NL_ITEM(LC_MONETARY,3)
#define MON_GROUPING _NL_ITEM(LC_MONETARY,4)
#define POSITIVE_SIGN _NL_ITEM(LC_MONETARY,5)
#define NEGATIVE_SIGN _NL_ITEM(LC_MONETARY,6)
#define INT_FRAC_DIGITS _NL_ITEM(LC_MONETARY,7)
#define FRAC_DIGITS _NL_ITEM(LC_MONETARY,8)

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(__LP64__)
char *nl_langinfo(nl_item);
char *nl_langinfo_l(nl_item, locale_t);
#endif // !__LP64__

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* NDK_ANDROID_SUPPORT_LANGINFO_H */
__END_DECLS

#endif /* NDK_ANDROID_SUPPORT_LANGINFO_H */
19 changes: 17 additions & 2 deletions sources/android/support/include/uchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,23 @@
#ifndef NDK_ANDROID_SUPPORT_UCHAR_H
#define NDK_ANDROID_SUPPORT_UCHAR_H

// __LP64__
#include_next <uchar.h>

#warning Not implemented
__BEGIN_DECLS

#if __ANDROID_API__ < __ANDROID_API_L__

size_t c16rtomb(char* __restrict, char16_t, mbstate_t* __restrict)
__INTRODUCED_IN(21);
size_t c32rtomb(char* __restrict, char32_t, mbstate_t* __restrict)
__INTRODUCED_IN(21);
size_t mbrtoc16(char16_t* __restrict, const char* __restrict, size_t,
mbstate_t* __restrict) __INTRODUCED_IN(21);
size_t mbrtoc32(char32_t* __restrict, const char* __restrict, size_t,
mbstate_t* __restrict) __INTRODUCED_IN(21);

#endif /* __ANDROID_API__ < __ANDROID_API_L__ */

__END_DECLS

#endif // NDK_ANDROID_SUPPORT_UCHAR_H
Loading

0 comments on commit 38a9713

Please sign in to comment.