Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 3037ea4

Browse files
committed
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
1 parent d824421 commit 3037ea4

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

libc/include/bits/struct_file.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2016 The Android Open Source Project
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26+
* SUCH DAMAGE.
27+
*/
28+
29+
#ifndef BITS_FILE_H
30+
#define BITS_FILE_H
31+
32+
#include <sys/cdefs.h>
33+
34+
__BEGIN_DECLS
35+
36+
struct __sFILE {
37+
#if defined(__LP64__)
38+
char __private[152];
39+
#else
40+
char __private[84];
41+
#endif
42+
} __attribute__((aligned(sizeof(void*))));
43+
44+
__END_DECLS
45+
46+
#endif /* BITS_FILE_H */

libc/include/stdio.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949

5050
#include <bits/seek_constants.h>
5151

52+
#if __ANDROID_API__ <= 23
53+
#include <bits/struct_file.h>
54+
#endif
55+
5256
__BEGIN_DECLS
5357

5458
#if defined(__clang__)
@@ -73,11 +77,11 @@ extern FILE* stderr __INTRODUCED_IN(23);
7377
#define stderr stderr
7478
#else
7579
/* Before M the actual symbols for stdin and friends had different names. */
76-
extern FILE* __sF[] __REMOVED_IN(23);
80+
extern FILE __sF[] __REMOVED_IN(23);
7781

78-
#define stdin __sF[0]
79-
#define stdout __sF[1]
80-
#define stderr __sF[2]
82+
#define stdin (&__sF[0])
83+
#define stdout (&__sF[1])
84+
#define stderr (&__sF[2])
8185
#endif
8286

8387
/*

libc/stdio/stdio.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,16 @@ int wprintf(const wchar_t* fmt, ...) {
848848
int wscanf(const wchar_t* fmt, ...) {
849849
PRINTF_IMPL(vfwscanf(stdin, fmt, ap));
850850
}
851+
852+
namespace {
853+
854+
namespace phony {
855+
#include <bits/struct_file.h>
856+
}
857+
858+
static_assert(sizeof(::__sFILE) == sizeof(phony::__sFILE),
859+
"size mismatch between `struct __sFILE` implementation and public stub");
860+
static_assert(alignof(::__sFILE) == alignof(phony::__sFILE),
861+
"alignment mismatch between `struct __sFILE` implementation and public stub");
862+
863+
}

0 commit comments

Comments
 (0)