diff options
author | Dan Albert <danalbert@google.com> | 2016-10-06 15:46:45 -0700 |
---|---|---|
committer | Dan Albert <danalbert@google.com> | 2016-10-20 10:10:45 -0700 |
commit | 3037ea43fccf5ec64537c3ee024bc726ee723c01 (patch) | |
tree | 830fda3221eb1207bb837f78298032be2d2c1706 | |
parent | d8244214751f9b48e60e69910c4e7175f8fab1ac (diff) |
Fix stdin/stdout/stderr for pre-M.
This wasn't an array of pointers, it was an array of structs.
Unfortunately we need a complete type to index into the struct for
stdin/stdout/stderr, so add a phony struct that matches the size and
alignment of `struct __sFILE`. This property is guaranteed by the
static_asserts in libc/bionic/struct_file_test.cpp.
Test: mma
Bug: http://b/30465923
Change-Id: I8ce851dd64a261703bb44f9b5cd23b7caff4dd68
-rw-r--r-- | libc/include/bits/struct_file.h | 46 | ||||
-rw-r--r-- | libc/include/stdio.h | 12 | ||||
-rw-r--r-- | libc/stdio/stdio.cpp | 13 |
3 files changed, 67 insertions, 4 deletions
diff --git a/libc/include/bits/struct_file.h b/libc/include/bits/struct_file.h new file mode 100644 index 000000000..08e18a13a --- /dev/null +++ b/libc/include/bits/struct_file.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 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. + */ + +#ifndef BITS_FILE_H +#define BITS_FILE_H + +#include <sys/cdefs.h> + +__BEGIN_DECLS + +struct __sFILE { +#if defined(__LP64__) + char __private[152]; +#else + char __private[84]; +#endif +} __attribute__((aligned(sizeof(void*)))); + +__END_DECLS + +#endif /* BITS_FILE_H */ diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 38021efe9..816bd282a 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -49,6 +49,10 @@ #include <bits/seek_constants.h> +#if __ANDROID_API__ <= 23 +#include <bits/struct_file.h> +#endif + __BEGIN_DECLS #if defined(__clang__) @@ -73,11 +77,11 @@ extern FILE* stderr __INTRODUCED_IN(23); #define stderr stderr #else /* Before M the actual symbols for stdin and friends had different names. */ -extern FILE* __sF[] __REMOVED_IN(23); +extern FILE __sF[] __REMOVED_IN(23); -#define stdin __sF[0] -#define stdout __sF[1] -#define stderr __sF[2] +#define stdin (&__sF[0]) +#define stdout (&__sF[1]) +#define stderr (&__sF[2]) #endif /* diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp index b709b40c8..b0f5c607f 100644 --- a/libc/stdio/stdio.cpp +++ b/libc/stdio/stdio.cpp @@ -848,3 +848,16 @@ int wprintf(const wchar_t* fmt, ...) { int wscanf(const wchar_t* fmt, ...) { PRINTF_IMPL(vfwscanf(stdin, fmt, ap)); } + +namespace { + +namespace phony { +#include <bits/struct_file.h> +} + +static_assert(sizeof(::__sFILE) == sizeof(phony::__sFILE), + "size mismatch between `struct __sFILE` implementation and public stub"); +static_assert(alignof(::__sFILE) == alignof(phony::__sFILE), + "alignment mismatch between `struct __sFILE` implementation and public stub"); + +} |