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
This commit is contained in:
Dan Albert 2016-10-06 15:46:45 -07:00
parent d824421475
commit 3037ea43fc
3 changed files with 67 additions and 4 deletions

View file

@ -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 */

View file

@ -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
/*

View file

@ -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");
}