bionic/tests/stdio_ext_test.cpp
Yabin Cui 76144aaa63 Change _stdio_handles_locking into _caller_handles_locking.
It is reported by tsan that funlockfile() can unlock an unlocked mutex.
It happens when printf() is called before fopen() or other stdio stuff.
As FLOCKFILE(fp) is called before __sinit(), _stdio_handles_locking is false,
and _FLOCK(fp) will not be locked. But then cantwrite(fp) in __vfprintf()
calls__sinit(), which makes _stdio_handles_locking become true, and
FUNLOCKFILE(fp) unlocks _FLOCK(fp).

Change _stdio_handles_locking into _caller_handles_locking,
so __sinit() won't change its value. Add test due to my previous fault.

Bug: 25392375
Change-Id: I483e3c3cdb28da65e62f1fd9615bf58c5403b4dd
2015-11-20 17:44:26 -08:00

165 lines
4.0 KiB
C++

/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio_ext.h>
#include <gtest/gtest.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wchar.h>
#include <locale.h>
#include "TemporaryFile.h"
#include "utils.h"
TEST(stdio_ext, __fbufsize) {
FILE* fp = fopen("/proc/version", "r");
// Initially, there's no buffer in case the first thing you do is disable buffering.
ASSERT_EQ(0U, __fbufsize(fp));
// A read forces a buffer to be created.
char buf[128];
fgets(buf, sizeof(buf), fp);
ASSERT_EQ(1024U, __fbufsize(fp));
ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 1));
ASSERT_EQ(1U, __fbufsize(fp));
ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 8));
ASSERT_EQ(8U, __fbufsize(fp));
fclose(fp);
}
TEST(stdio_ext, __flbf) {
FILE* fp = fopen("/proc/version", "r");
ASSERT_FALSE(__flbf(fp));
char buf[128];
ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
ASSERT_TRUE(__flbf(fp));
fclose(fp);
}
TEST(stdio_ext, __fpending) {
FILE* fp = fopen("/dev/null", "w");
ASSERT_EQ(0U, __fpending(fp));
ASSERT_EQ('x', fputc('x', fp));
ASSERT_EQ(1U, __fpending(fp));
ASSERT_EQ('y', fputc('y', fp));
ASSERT_EQ(2U, __fpending(fp));
fflush(fp);
ASSERT_EQ(0U, __fpending(fp));
fclose(fp);
}
TEST(stdio_ext, __fpurge) {
FILE* fp = tmpfile();
ASSERT_EQ('a', fputc('a', fp));
ASSERT_EQ(1U, __fpending(fp));
__fpurge(fp);
ASSERT_EQ(0U, __fpending(fp));
ASSERT_EQ('b', fputc('b', fp));
ASSERT_EQ('\n', fputc('\n', fp));
ASSERT_EQ(2U, __fpending(fp));
rewind(fp);
char buf[16];
char* s = fgets(buf, sizeof(buf), fp);
ASSERT_TRUE(s != NULL);
ASSERT_STREQ("b\n", s);
fclose(fp);
}
TEST(stdio_ext, _flushlbf) {
FILE* fp = fopen("/dev/null", "w");
char buf[128];
ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
ASSERT_EQ('a', fputc('a', fp));
ASSERT_EQ(1U, __fpending(fp));
_flushlbf();
ASSERT_EQ(0U, __fpending(fp));
fclose(fp);
}
TEST(stdio_ext, __freadable__fwritable) {
FILE* fp = fopen("/dev/null", "r");
ASSERT_TRUE(__freadable(fp));
ASSERT_FALSE(__fwritable(fp));
fclose(fp);
fp = fopen("/dev/null", "w");
ASSERT_FALSE(__freadable(fp));
ASSERT_TRUE(__fwritable(fp));
fclose(fp);
fp = fopen("/dev/null", "w+");
ASSERT_TRUE(__freadable(fp));
ASSERT_TRUE(__fwritable(fp));
fclose(fp);
}
TEST(stdio_ext, __fsetlocking) {
FILE* fp = fopen("/proc/version", "r");
ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_BYCALLER));
ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_QUERY));
ASSERT_EQ(FSETLOCKING_BYCALLER, __fsetlocking(fp, FSETLOCKING_INTERNAL));
ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
fclose(fp);
}
static void LockingByCallerHelper(std::atomic<pid_t>* pid) {
*pid = gettid();
flockfile(stdout);
funlockfile(stdout);
}
TEST(stdio_ext, __fsetlocking_BYCALLER) {
// Check if users can use flockfile/funlockfile to protect stdio operations.
int old_state = __fsetlocking(stdout, FSETLOCKING_BYCALLER);
flockfile(stdout);
pthread_t thread;
std::atomic<pid_t> pid(0);
ASSERT_EQ(0, pthread_create(&thread, nullptr,
reinterpret_cast<void* (*)(void*)>(LockingByCallerHelper), &pid));
WaitUntilThreadSleep(pid);
funlockfile(stdout);
ASSERT_EQ(0, pthread_join(thread, nullptr));
__fsetlocking(stdout, old_state);
}