#include "sandbox/linux/services/thread_helpers.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include "base/files/scoped_file.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "sandbox/linux/services/proc_util.h"
namespace sandbox {
namespace {
const char kAssertSingleThreadedError[] =
"Current process is not mono-threaded!";
const char kAssertThreadDoesNotAppearInProcFS[] =
"Started thread does not appear in /proc";
const char kAssertThreadDoesNotDisappearInProcFS[] =
"Stopped thread does not disappear in /proc";
bool IsSingleThreadedImpl(int proc_fd) {
CHECK_LE(0, proc_fd);
struct stat task_stat;
int fstat_ret = fstatat(proc_fd, "self/task/", &task_stat, 0);
PCHECK(0 == fstat_ret);
CHECK_LE(3UL, task_stat.st_nlink);
return task_stat.st_nlink == 3;
}
bool IsThreadPresentInProcFS(int proc_fd,
const std::string& thread_id_dir_str) {
struct stat task_stat;
const int fstat_ret =
fstatat(proc_fd, thread_id_dir_str.c_str(), &task_stat, 0);
if (fstat_ret < 0) {
PCHECK(ENOENT == errno);
return false;
}
return true;
}
bool IsNotThreadPresentInProcFS(int proc_fd,
const std::string& thread_id_dir_str) {
return !IsThreadPresentInProcFS(proc_fd, thread_id_dir_str);
}
void RunWhileTrue(const base::RepeatingCallback<bool(void)>& cb,
const char* message) {
#if defined(NDEBUG)
const unsigned int kMaxIterations = 30U;
#else
const unsigned int kMaxIterations = 25U;
#endif
for (unsigned int i = 0; i < kMaxIterations; ++i) {
if (!cb.Run()) {
return;
}
struct timespec ts = {0, 1L << i };
PCHECK(0 == HANDLE_EINTR(nanosleep(&ts, &ts)));
}
LOG(FATAL) << message << " (iterations: " << kMaxIterations << ")";
}
bool IsMultiThreaded(int proc_fd) {
return !ThreadHelpers::IsSingleThreaded(proc_fd);
}
enum class ThreadAction { Start, Stop };
bool ChangeThreadStateAndWatchProcFS(
int proc_fd, base::Thread* thread, ThreadAction action) {
DCHECK_LE(0, proc_fd);
DCHECK(thread);
DCHECK(action == ThreadAction::Start || action == ThreadAction::Stop);
base::RepeatingCallback<bool(void)> cb;
const char* message;
if (action == ThreadAction::Start) {
if (!thread->Start())
return false;
}
const base::PlatformThreadId thread_id = thread->GetThreadId();
const std::string thread_id_dir_str =
"self/task/" + base::NumberToString(thread_id.raw()) + "/";
if (action == ThreadAction::Stop) {
DCHECK(IsThreadPresentInProcFS(proc_fd, thread_id_dir_str));
thread->Stop();
}
if (action == ThreadAction::Start) {
cb = base::BindRepeating(&IsNotThreadPresentInProcFS, proc_fd,
thread_id_dir_str);
message = kAssertThreadDoesNotAppearInProcFS;
} else {
cb = base::BindRepeating(&IsThreadPresentInProcFS, proc_fd,
thread_id_dir_str);
message = kAssertThreadDoesNotDisappearInProcFS;
}
RunWhileTrue(cb, message);
DCHECK_EQ(action == ThreadAction::Start,
IsThreadPresentInProcFS(proc_fd, thread_id_dir_str));
return true;
}
}
bool ThreadHelpers::IsSingleThreaded(int proc_fd) {
DCHECK_LE(0, proc_fd);
return IsSingleThreadedImpl(proc_fd);
}
bool ThreadHelpers::IsSingleThreaded() {
base::ScopedFD task_fd(ProcUtil::OpenProc());
return IsSingleThreaded(task_fd.get());
}
void ThreadHelpers::AssertSingleThreaded(int proc_fd) {
DCHECK_LE(0, proc_fd);
const base::RepeatingCallback<bool(void)> cb =
base::BindRepeating(&IsMultiThreaded, proc_fd);
RunWhileTrue(cb, kAssertSingleThreadedError);
}
void ThreadHelpers::AssertSingleThreaded() {
base::ScopedFD task_fd(ProcUtil::OpenProc());
AssertSingleThreaded(task_fd.get());
}
bool ThreadHelpers::StartThreadAndWatchProcFS(int proc_fd,
base::Thread* thread) {
return ChangeThreadStateAndWatchProcFS(proc_fd, thread, ThreadAction::Start);
}
bool ThreadHelpers::StopThreadAndWatchProcFS(int proc_fd,
base::Thread* thread) {
return ChangeThreadStateAndWatchProcFS(proc_fd, thread, ThreadAction::Stop);
}
const char* ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests() {
return kAssertSingleThreadedError;
}
}