Skip to content

Commit

Permalink
Linux sandbox: add warning if running tests multithreaded.
Browse files Browse the repository at this point in the history
BUG=169416
NOTRY=true

Review URL: https://chromiumcodereview.appspot.com/12207004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@180747 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jln@chromium.org committed Feb 5, 2013
1 parent dd3df00 commit 054584c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 0 additions & 1 deletion sandbox/linux/tests/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "testing/gtest/include/gtest/gtest.h"


int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
// Always go through re-execution for death tests.
Expand Down
26 changes: 22 additions & 4 deletions sandbox/linux/tests/unit_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ int GetSubProcessTimeoutTimeInSeconds() {
return 10;
}

// Returns the number of threads of the current process or -1.
int CountThreads() {
struct stat task_stat;
int task_d = stat("/proc/self/task", &task_stat);
// task_stat.st_nlink should be the number of tasks + 2 (accounting for
// "." and "..".
if (task_d != 0 || task_stat.st_nlink < 3)
return -1;
return task_stat.st_nlink - 2;
}

} // namespace

namespace sandbox {

static const int kExpectedValue = 42;
Expand Down Expand Up @@ -66,12 +77,19 @@ static void SetProcessTimeout(int time_in_seconds) {
// alarm.
}

// Runs a test in a sub-process. This is necessary for most of the code
// in the BPF sandbox, as it potentially makes global state changes and as
// it also tends to raise fatal errors, if the code has been used in an
// insecure manner.
void UnitTests::RunTestInProcess(UnitTests::Test test, void *arg,
DeathCheck death, const void *death_aux) {
// Runs a test in a sub-process. This is necessary for most of the code
// in the BPF sandbox, as it potentially makes global state changes and as
// it also tends to raise fatal errors, if the code has been used in an
// insecure manner.
if (CountThreads() != 1) {
// We need to use fork(), so we can't be multi-threaded.
// TODO(jln): change this to a fatal error once we can launch
// Android tests with --exe.
fprintf(stderr, "WARNING: running sandbox tests with multiple threads"
" is not supported and will make the tests flaky.\n");
}
int fds[2];
ASSERT_EQ(0, pipe(fds));
// Check that our pipe is not on one of the standard file descriptor.
Expand Down

0 comments on commit 054584c

Please sign in to comment.