2014-12-03 23:39:20 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-06-10 01:36:28 +02:00
|
|
|
#include <pty.h>
|
|
|
|
|
2014-12-03 23:39:20 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-06-10 01:36:28 +02:00
|
|
|
#include <pthread.h>
|
2014-12-03 23:39:20 +01:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2016-06-10 01:36:28 +02:00
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
|
2016-01-26 22:04:57 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-12-03 23:39:20 +01:00
|
|
|
TEST(pty, openpty) {
|
2020-07-28 21:06:23 +02:00
|
|
|
int pty, tty;
|
2014-12-03 23:39:20 +01:00
|
|
|
char name[32];
|
|
|
|
struct winsize w = { 123, 456, 9999, 999 };
|
2020-07-28 21:06:23 +02:00
|
|
|
ASSERT_EQ(0, openpty(&pty, &tty, name, nullptr, &w));
|
|
|
|
ASSERT_NE(-1, pty);
|
|
|
|
ASSERT_NE(-1, tty);
|
|
|
|
ASSERT_NE(pty, tty);
|
2014-12-03 23:39:20 +01:00
|
|
|
|
|
|
|
char tty_name[32];
|
2020-07-28 21:06:23 +02:00
|
|
|
ASSERT_EQ(0, ttyname_r(tty, tty_name, sizeof(tty_name)));
|
2014-12-03 23:39:20 +01:00
|
|
|
ASSERT_STREQ(tty_name, name);
|
|
|
|
|
|
|
|
struct winsize w_actual;
|
2020-07-28 21:06:23 +02:00
|
|
|
ASSERT_EQ(0, ioctl(tty, TIOCGWINSZ, &w_actual));
|
2014-12-03 23:39:20 +01:00
|
|
|
ASSERT_EQ(w_actual.ws_row, w.ws_row);
|
|
|
|
ASSERT_EQ(w_actual.ws_col, w.ws_col);
|
|
|
|
ASSERT_EQ(w_actual.ws_xpixel, w.ws_xpixel);
|
|
|
|
ASSERT_EQ(w_actual.ws_ypixel, w.ws_ypixel);
|
|
|
|
|
2020-07-28 21:06:23 +02:00
|
|
|
close(pty);
|
|
|
|
close(tty);
|
2014-12-03 23:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pty, forkpty) {
|
|
|
|
pid_t sid = getsid(0);
|
|
|
|
|
2020-07-28 21:06:23 +02:00
|
|
|
int pty;
|
|
|
|
pid_t pid = forkpty(&pty, nullptr, nullptr, nullptr);
|
2014-12-03 23:39:20 +01:00
|
|
|
ASSERT_NE(-1, pid);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
// We're the child.
|
|
|
|
ASSERT_NE(sid, getsid(0));
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(sid, getsid(0));
|
|
|
|
|
2016-01-26 22:04:57 +01:00
|
|
|
AssertChildExited(pid, 0);
|
2014-12-03 23:39:20 +01:00
|
|
|
|
2020-07-28 21:06:23 +02:00
|
|
|
close(pty);
|
2014-12-03 23:39:20 +01:00
|
|
|
}
|
2016-06-10 01:36:28 +02:00
|
|
|
|
|
|
|
struct PtyReader_28979140_Arg {
|
2016-07-12 02:26:35 +02:00
|
|
|
int main_cpu_id;
|
2020-07-28 21:06:23 +02:00
|
|
|
int fd;
|
2016-07-12 02:26:35 +02:00
|
|
|
uint32_t data_count;
|
|
|
|
bool finished;
|
|
|
|
std::atomic<bool> matched;
|
2016-06-10 01:36:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void PtyReader_28979140(PtyReader_28979140_Arg* arg) {
|
|
|
|
arg->finished = false;
|
|
|
|
cpu_set_t cpus;
|
|
|
|
ASSERT_EQ(0, sched_getaffinity(0, sizeof(cpu_set_t), &cpus));
|
2016-07-12 02:26:35 +02:00
|
|
|
CPU_CLR(arg->main_cpu_id, &cpus);
|
2016-06-10 01:36:28 +02:00
|
|
|
ASSERT_EQ(0, sched_setaffinity(0, sizeof(cpu_set_t), &cpus));
|
|
|
|
|
|
|
|
uint32_t counter = 0;
|
|
|
|
while (counter <= arg->data_count) {
|
|
|
|
char buf[4096]; // Use big buffer to read to hit the bug more easily.
|
|
|
|
size_t to_read = std::min(sizeof(buf), (arg->data_count + 1 - counter) * sizeof(uint32_t));
|
2020-07-28 21:06:23 +02:00
|
|
|
ASSERT_TRUE(android::base::ReadFully(arg->fd, buf, to_read));
|
2016-06-10 01:36:28 +02:00
|
|
|
size_t num_of_value = to_read / sizeof(uint32_t);
|
|
|
|
uint32_t* p = reinterpret_cast<uint32_t*>(buf);
|
|
|
|
while (num_of_value-- > 0) {
|
|
|
|
if (*p++ != counter++) {
|
|
|
|
arg->matched = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 21:06:23 +02:00
|
|
|
close(arg->fd);
|
2016-06-10 01:36:28 +02:00
|
|
|
arg->finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(pty, bug_28979140) {
|
|
|
|
// This test is to test a kernel bug, which uses a lock free ring-buffer to
|
|
|
|
// pass data through a raw pty, but missing necessary memory barriers.
|
2016-07-12 02:26:35 +02:00
|
|
|
cpu_set_t cpus;
|
|
|
|
ASSERT_EQ(0, sched_getaffinity(0, sizeof(cpu_set_t), &cpus));
|
|
|
|
if (CPU_COUNT(&cpus) < 2) {
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "This bug only happens on multiprocessors";
|
2016-06-10 01:36:28 +02:00
|
|
|
}
|
2017-11-17 00:25:12 +01:00
|
|
|
constexpr uint32_t TEST_DATA_COUNT = 2000000;
|
2016-06-10 01:36:28 +02:00
|
|
|
|
|
|
|
// 1. Open raw pty.
|
2020-07-28 21:06:23 +02:00
|
|
|
int pty;
|
|
|
|
int tty;
|
|
|
|
ASSERT_EQ(0, openpty(&pty, &tty, nullptr, nullptr, nullptr));
|
2016-06-10 01:36:28 +02:00
|
|
|
termios tattr;
|
2020-07-28 21:06:23 +02:00
|
|
|
ASSERT_EQ(0, tcgetattr(tty, &tattr));
|
2016-06-10 01:36:28 +02:00
|
|
|
cfmakeraw(&tattr);
|
2020-07-28 21:06:23 +02:00
|
|
|
ASSERT_EQ(0, tcsetattr(tty, TCSADRAIN, &tattr));
|
2016-06-10 01:36:28 +02:00
|
|
|
|
2020-07-28 21:06:23 +02:00
|
|
|
// 2. Make two threads running on different cpus:
|
|
|
|
// pty thread uses first available cpu, and tty thread uses other cpus.
|
2016-06-10 01:36:28 +02:00
|
|
|
PtyReader_28979140_Arg arg;
|
2016-07-12 02:26:35 +02:00
|
|
|
arg.main_cpu_id = -1;
|
|
|
|
for (int i = 0; i < CPU_SETSIZE; i++) {
|
|
|
|
if (CPU_ISSET(i, &cpus)) {
|
|
|
|
arg.main_cpu_id = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_GE(arg.main_cpu_id, 0);
|
|
|
|
|
2020-07-28 21:06:23 +02:00
|
|
|
// 3. Create thread for tty reader.
|
2016-07-12 02:26:35 +02:00
|
|
|
pthread_t thread;
|
2020-07-28 21:06:23 +02:00
|
|
|
arg.fd = tty;
|
2016-06-10 01:36:28 +02:00
|
|
|
arg.data_count = TEST_DATA_COUNT;
|
|
|
|
arg.matched = true;
|
|
|
|
ASSERT_EQ(0, pthread_create(&thread, nullptr,
|
|
|
|
reinterpret_cast<void*(*)(void*)>(PtyReader_28979140),
|
|
|
|
&arg));
|
|
|
|
|
|
|
|
CPU_ZERO(&cpus);
|
2016-07-12 02:26:35 +02:00
|
|
|
CPU_SET(arg.main_cpu_id, &cpus);
|
2016-06-10 01:36:28 +02:00
|
|
|
ASSERT_EQ(0, sched_setaffinity(0, sizeof(cpu_set_t), &cpus));
|
|
|
|
|
2020-07-28 21:06:23 +02:00
|
|
|
// 4. Send data to tty reader.
|
2017-11-17 00:25:12 +01:00
|
|
|
// Send a bunch of data at a time, so it is easier to catch the bug that some data isn't seen
|
|
|
|
// by the reader thread on another cpu.
|
|
|
|
uint32_t counter_buf[100];
|
2016-06-10 01:36:28 +02:00
|
|
|
uint32_t counter = 0;
|
|
|
|
while (counter <= TEST_DATA_COUNT) {
|
2017-11-17 00:25:12 +01:00
|
|
|
for (size_t i = 0; i < sizeof(counter_buf) / sizeof(counter_buf[0]); ++i) {
|
|
|
|
counter_buf[i] = counter++;
|
|
|
|
}
|
2020-07-28 21:06:23 +02:00
|
|
|
ASSERT_TRUE(android::base::WriteFully(pty, &counter_buf, sizeof(counter_buf)));
|
2016-06-10 01:36:28 +02:00
|
|
|
ASSERT_TRUE(arg.matched) << "failed at count = " << counter;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0, pthread_join(thread, nullptr));
|
|
|
|
ASSERT_TRUE(arg.finished);
|
|
|
|
ASSERT_TRUE(arg.matched);
|
2020-07-28 21:06:23 +02:00
|
|
|
close(pty);
|
2016-06-10 01:36:28 +02:00
|
|
|
}
|