| Index: sandbox/linux/services/ucontext_unittest.cc
|
| diff --git a/sandbox/linux/services/ucontext_unittest.cc b/sandbox/linux/services/ucontext_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a3050700ef314670805b1e188c062c71280c4cc7
|
| --- /dev/null
|
| +++ b/sandbox/linux/services/ucontext_unittest.cc
|
| @@ -0,0 +1,87 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include <setjmp.h>
|
| +#include <signal.h>
|
| +#include "sandbox/linux/seccomp-bpf/sandbox_bpf.h"
|
| +#include "sandbox/linux/services/android_ucontext.h"
|
| +#include "sandbox/linux/tests/unit_tests.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace sandbox {
|
| +const int kExpectedValueParm2 = 0xec;
|
| +const int kExpectedValueParm3 = 0xed;
|
| +const int kExpectedValueParm4 = 0xee;
|
| +const int kExpectedValueParm5 = 0x00;
|
| +
|
| +void SigAction(int n, siginfo_t *SigInfo, void* SigContext) {
|
| + const ucontext_t *Context = static_cast<ucontext_t*>(SigContext);
|
| + sigset_t set = 0;
|
| +
|
| + sigaddset(&set, SIGPIPE);
|
| + sigaddset(&set, SIGFPE);
|
| +
|
| + // ARM define registers array as unsigned while x86 define it as signed.
|
| + SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM2(Context)) ==
|
| + static_cast<int>(kExpectedValueParm2));
|
| + SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM3(Context)) ==
|
| + static_cast<int>(kExpectedValueParm3));
|
| + SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM4(Context)) ==
|
| + static_cast<int>(kExpectedValueParm4));
|
| + SANDBOX_ASSERT(static_cast<int>(SECCOMP_PARM5(Context)) ==
|
| + static_cast<int>(kExpectedValueParm5));
|
| + SANDBOX_ASSERT(Context->uc_sigmask == set);
|
| +}
|
| +
|
| +SANDBOX_DEATH_TEST(UcontextTest, TestUcontext, DEATH_BY_SIGNAL(SIGSEGV)) {
|
| + int ret;
|
| + struct sigaction NewAct;
|
| + struct sigaction OldAct;
|
| + sigset_t NewSet, OldSet;
|
| +
|
| + memset(&NewAct, 0, sizeof(NewAct));
|
| + NewAct.sa_sigaction = SigAction;
|
| + // We need SA_RESETHAND here to make sure the process is terminated
|
| + // after return from signal handler. Otherwise, there will be dead loop.
|
| + NewAct.sa_flags = SA_RESETHAND | SA_SIGINFO;
|
| + sigemptyset(&NewAct.sa_mask);
|
| +
|
| + sigemptyset(&NewSet);
|
| + sigaddset(&NewSet, SIGPIPE);
|
| + sigaddset(&NewSet, SIGFPE);
|
| +
|
| + sigprocmask(SIG_SETMASK, &NewSet, &OldSet);
|
| + ret = sigaction(SIGSEGV, &NewAct, &OldAct);
|
| + SANDBOX_ASSERT(ret == 0);
|
| +
|
| + // We don't use *((int*) 0x0) = 0xdeadbeaf here because toolchain
|
| + // may allocate registers (Android ARM gcc uses r2/r3). Which makes
|
| + // registers checked in signal handler are not easy to choice.
|
| + // The following code is suppose to make process terminated. So we don't
|
| + // care about the which reigsters are clobbered here.
|
| +#if defined(__i386__)
|
| + asm __volatile__ (
|
| + "movl $0xec, %ecx\n\t"
|
| + "movl $0xed, %edx\n\t"
|
| + "movl $0xee, %esi\n\t"
|
| + "movl $0x00, %edi\n\t"
|
| + "movl $0x00, (%edi)\n\t"
|
| + );
|
| +#elif defined(__arm__)
|
| + asm __volatile__ (
|
| + "mov r1, #0xec\n\t"
|
| + "mov r2, #0xed\n\t"
|
| + "mov r3, #0xee\n\t"
|
| + "mov r4, #0x00\n\t"
|
| + "str r3, [r4]\n\t"
|
| + );
|
| +#endif
|
| +
|
| + /* Shouldn't be here */
|
| + SANDBOX_ASSERT(1 == 0);
|
| + sigprocmask(SIG_SETMASK, &OldSet, NULL);
|
| + sigaction(SIGSEGV, &OldAct, NULL);
|
| +}
|
| +
|
| +} // namespace sandbox
|
|
|