Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / breakpad / src / common / android / breakpad_getcontext_unittest.cc
1 // Copyright (c) 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #if defined(__x86_64__)
31 #include <asm/sigcontext.h>
32 #endif
33
34 #include <sys/ucontext.h>
35
36 #include "breakpad_googletest_includes.h"
37 #include "common/android/ucontext_constants.h"
38
39 template <int left, int right>
40 struct CompileAssertEquals {
41   // a compilation error here indicates left and right are not equal.
42   char left_too_large[right - left];
43   // a compilation error here indicates left and right are not equal.
44   char right_too_large[left - right];
45 };
46
47 #define COMPILE_ASSERT_EQ(left, right, tag) \
48   CompileAssertEquals<left, right> tag;
49
50 TEST(AndroidUContext, GRegsOffset) {
51 #if defined(__arm__)
52   // There is no gregs[] array on ARM, so compare to the offset of
53   // first register fields, since they're stored in order.
54   ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
55             offsetof(ucontext_t,uc_mcontext.arm_r0));
56 #elif defined(__aarch64__)
57   // There is no gregs[] array on ARM, so compare to the offset of
58   // first register fields, since they're stored in order.
59   ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
60             offsetof(ucontext_t,uc_mcontext.regs[0]));
61   ASSERT_EQ(static_cast<size_t>(MCONTEXT_SP_OFFSET),
62             offsetof(ucontext_t,uc_mcontext.sp));
63   ASSERT_EQ(static_cast<size_t>(MCONTEXT_PC_OFFSET),
64             offsetof(ucontext_t,uc_mcontext.pc));
65   ASSERT_EQ(static_cast<size_t>(MCONTEXT_PSTATE_OFFSET),
66             offsetof(ucontext_t,uc_mcontext.pstate));
67   ASSERT_EQ(static_cast<size_t>(MCONTEXT_EXTENSION_OFFSET),
68             offsetof(ucontext_t,uc_mcontext.__reserved));
69 #elif defined(__i386__)
70   ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
71             offsetof(ucontext_t,uc_mcontext.gregs));
72 #define CHECK_REG(x) \
73   ASSERT_EQ(static_cast<size_t>(MCONTEXT_##x##_OFFSET),         \
74             offsetof(ucontext_t,uc_mcontext.gregs[REG_##x]))
75   CHECK_REG(GS);
76   CHECK_REG(FS);
77   CHECK_REG(ES);
78   CHECK_REG(DS);
79   CHECK_REG(EDI);
80   CHECK_REG(ESI);
81   CHECK_REG(EBP);
82   CHECK_REG(ESP);
83   CHECK_REG(EBX);
84   CHECK_REG(EDX);
85   CHECK_REG(ECX);
86   CHECK_REG(EAX);
87   CHECK_REG(TRAPNO);
88   CHECK_REG(ERR);
89   CHECK_REG(EIP);
90   CHECK_REG(CS);
91   CHECK_REG(EFL);
92   CHECK_REG(UESP);
93   CHECK_REG(SS);
94
95   ASSERT_EQ(static_cast<size_t>(UCONTEXT_FPREGS_OFFSET),
96             offsetof(ucontext_t,uc_mcontext.fpregs));
97
98   ASSERT_EQ(static_cast<size_t>(UCONTEXT_FPREGS_MEM_OFFSET),
99             offsetof(ucontext_t,__fpregs_mem));
100 #elif defined(__mips__)
101   ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
102             offsetof(ucontext_t,uc_mcontext.gregs));
103
104   // PC for mips is not part of gregs.
105   ASSERT_EQ(static_cast<size_t>(MCONTEXT_PC_OFFSET),
106             offsetof(ucontext_t,uc_mcontext.pc));
107
108   ASSERT_EQ(static_cast<size_t>(MCONTEXT_FPREGS_OFFSET),
109             offsetof(ucontext_t,uc_mcontext.fpregs));
110
111   ASSERT_EQ(static_cast<size_t>(MCONTEXT_FPC_CSR),
112             offsetof(ucontext_t,uc_mcontext.fpc_csr));
113 #elif defined(__x86_64__)
114
115   COMPILE_ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
116                     offsetof(ucontext_t,uc_mcontext.gregs),
117                     mcontext_gregs_offset);
118 #define CHECK_REG(x) \
119   COMPILE_ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_##x), \
120                     offsetof(ucontext_t,uc_mcontext.gregs[REG_##x]), reg_##x)
121   CHECK_REG(R8);
122   CHECK_REG(R9);
123   CHECK_REG(R10);
124   CHECK_REG(R11);
125   CHECK_REG(R12);
126   CHECK_REG(R13);
127   CHECK_REG(R14);
128   CHECK_REG(R15);
129   CHECK_REG(RDI);
130   CHECK_REG(RSI);
131   CHECK_REG(RBP);
132   CHECK_REG(RBX);
133   CHECK_REG(RDX);
134   CHECK_REG(RAX);
135   CHECK_REG(RCX);
136   CHECK_REG(RSP);
137   CHECK_REG(RIP);
138
139   // sigcontext is an analog to mcontext_t. The layout should be the same.
140   COMPILE_ASSERT_EQ(offsetof(mcontext_t,fpregs),
141                     offsetof(sigcontext,fpstate), sigcontext_fpstate);
142   // Check that _fpstate from asm/sigcontext.h is essentially the same
143   // as _libc_fpstate.
144   COMPILE_ASSERT_EQ(sizeof(_libc_fpstate), sizeof(_fpstate),
145                     sigcontext_fpstate_size);
146   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,cwd),offsetof(_fpstate,cwd),
147                     sigcontext_fpstate_cwd);
148   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,swd),offsetof(_fpstate,swd),
149                     sigcontext_fpstate_swd);
150   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,ftw),offsetof(_fpstate,twd),
151                     sigcontext_fpstate_twd);
152   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,fop),offsetof(_fpstate,fop),
153                     sigcontext_fpstate_fop);
154   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,rip),offsetof(_fpstate,rip),
155                     sigcontext_fpstate_rip);
156   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,rdp),offsetof(_fpstate,rdp),
157                     sigcontext_fpstate_rdp);
158   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,mxcsr),offsetof(_fpstate,mxcsr),
159                     sigcontext_fpstate_mxcsr);
160   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,mxcr_mask),
161                     offsetof(_fpstate,mxcsr_mask),
162                     sigcontext_fpstate_mxcsr_mask);
163   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,_st), offsetof(_fpstate,st_space),
164                     sigcontext_fpstate_stspace);
165   COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,_xmm), offsetof(_fpstate,xmm_space),
166                     sigcontext_fpstate_xmm_space);
167
168   COMPILE_ASSERT_EQ(MCONTEXT_FPREGS_PTR,
169                     offsetof(ucontext_t,uc_mcontext.fpregs),
170                     mcontext_fpregs_ptr);
171   COMPILE_ASSERT_EQ(MCONTEXT_FPREGS_MEM, offsetof(ucontext_t,__fpregs_mem),
172                     mcontext_fpregs_mem);
173   COMPILE_ASSERT_EQ(FPREGS_OFFSET_MXCSR, offsetof(_libc_fpstate,mxcsr),
174                     fpregs_offset_mxcsr);
175   COMPILE_ASSERT_EQ(UCONTEXT_SIGMASK_OFFSET, offsetof(ucontext_t, uc_sigmask),
176                     ucontext_sigmask);
177 #else
178   ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
179             offsetof(ucontext_t,uc_mcontext.gregs));
180 #endif
181 }
182
183 TEST(AndroidUContext, SigmakOffset) {
184   ASSERT_EQ(static_cast<size_t>(UCONTEXT_SIGMASK_OFFSET),
185             offsetof(ucontext_t,uc_sigmask));
186 }