1 // Copyright (c) 2012, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 #if defined(__x86_64__)
31 #include <asm/sigcontext.h>
34 #include <sys/ucontext.h>
36 #include "breakpad_googletest_includes.h"
37 #include "common/android/ucontext_constants.h"
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];
47 #define COMPILE_ASSERT_EQ(left, right, tag) \
48 CompileAssertEquals<left, right> tag;
50 TEST(AndroidUContext, GRegsOffset) {
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]))
95 ASSERT_EQ(static_cast<size_t>(UCONTEXT_FPREGS_OFFSET),
96 offsetof(ucontext_t,uc_mcontext.fpregs));
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));
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));
108 ASSERT_EQ(static_cast<size_t>(MCONTEXT_FPREGS_OFFSET),
109 offsetof(ucontext_t,uc_mcontext.fpregs));
111 ASSERT_EQ(static_cast<size_t>(MCONTEXT_FPC_CSR),
112 offsetof(ucontext_t,uc_mcontext.fpc_csr));
113 #elif defined(__x86_64__)
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)
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
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);
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),
178 ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
179 offsetof(ucontext_t,uc_mcontext.gregs));
183 TEST(AndroidUContext, SigmakOffset) {
184 ASSERT_EQ(static_cast<size_t>(UCONTEXT_SIGMASK_OFFSET),
185 offsetof(ucontext_t,uc_sigmask));