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 // asm/sigcontext.h can't be included with signal.h on glibc or
31 // musl, so only compare _libc_fpstate and _fpstate on Android.
32 #if defined(__ANDROID__) && defined(__x86_64__)
33 #include <asm/sigcontext.h>
34 #endif
35
36 #include <sys/ucontext.h>
37
38 #include <type_traits>
39
40 #include "breakpad_googletest_includes.h"
41 #include "common/linux/ucontext_constants.h"
42
43 template <int left, int right>
44 struct CompileAssertEquals {
45 // a compilation error here indicates left and right are not equal.
46 char left_too_large[right - left];
47 // a compilation error here indicates left and right are not equal.
48 char right_too_large[left - right];
49 };
50
51 #define COMPILE_ASSERT_EQ(left, right, tag) \
52 CompileAssertEquals<left, right> tag;
53
TEST(AndroidUContext,GRegsOffset)54 TEST(AndroidUContext, GRegsOffset) {
55 #if defined(__arm__)
56 // There is no gregs[] array on ARM, so compare to the offset of
57 // first register fields, since they're stored in order.
58 ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
59 offsetof(ucontext_t,uc_mcontext.arm_r0));
60 #elif defined(__aarch64__)
61 // There is no gregs[] array on ARM, so compare to the offset of
62 // first register fields, since they're stored in order.
63 ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
64 offsetof(ucontext_t,uc_mcontext.regs[0]));
65 ASSERT_EQ(static_cast<size_t>(MCONTEXT_SP_OFFSET),
66 offsetof(ucontext_t,uc_mcontext.sp));
67 ASSERT_EQ(static_cast<size_t>(MCONTEXT_PC_OFFSET),
68 offsetof(ucontext_t,uc_mcontext.pc));
69 ASSERT_EQ(static_cast<size_t>(MCONTEXT_PSTATE_OFFSET),
70 offsetof(ucontext_t,uc_mcontext.pstate));
71 ASSERT_EQ(static_cast<size_t>(MCONTEXT_EXTENSION_OFFSET),
72 offsetof(ucontext_t,uc_mcontext.__reserved));
73 #elif defined(__i386__)
74 ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
75 offsetof(ucontext_t,uc_mcontext.gregs));
76 #define CHECK_REG(x) \
77 ASSERT_EQ(static_cast<size_t>(MCONTEXT_##x##_OFFSET), \
78 offsetof(ucontext_t,uc_mcontext.gregs[REG_##x]))
79 CHECK_REG(GS);
80 CHECK_REG(FS);
81 CHECK_REG(ES);
82 CHECK_REG(DS);
83 CHECK_REG(EDI);
84 CHECK_REG(ESI);
85 CHECK_REG(EBP);
86 CHECK_REG(ESP);
87 CHECK_REG(EBX);
88 CHECK_REG(EDX);
89 CHECK_REG(ECX);
90 CHECK_REG(EAX);
91 CHECK_REG(TRAPNO);
92 CHECK_REG(ERR);
93 CHECK_REG(EIP);
94 CHECK_REG(CS);
95 CHECK_REG(EFL);
96 CHECK_REG(UESP);
97 CHECK_REG(SS);
98
99 ASSERT_EQ(static_cast<size_t>(UCONTEXT_FPREGS_OFFSET),
100 offsetof(ucontext_t,uc_mcontext.fpregs));
101
102 ASSERT_EQ(static_cast<size_t>(UCONTEXT_FPREGS_MEM_OFFSET),
103 offsetof(ucontext_t,__fpregs_mem));
104 #elif defined(__mips__)
105 ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
106 offsetof(ucontext_t,uc_mcontext.gregs));
107
108 // PC for mips is not part of gregs.
109 ASSERT_EQ(static_cast<size_t>(MCONTEXT_PC_OFFSET),
110 offsetof(ucontext_t,uc_mcontext.pc));
111
112 ASSERT_EQ(static_cast<size_t>(MCONTEXT_FPREGS_OFFSET),
113 offsetof(ucontext_t,uc_mcontext.fpregs));
114
115 ASSERT_EQ(static_cast<size_t>(MCONTEXT_FPC_CSR),
116 offsetof(ucontext_t,uc_mcontext.fpc_csr));
117 #elif defined(__x86_64__)
118
119 COMPILE_ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
120 offsetof(ucontext_t,uc_mcontext.gregs),
121 mcontext_gregs_offset);
122 #define CHECK_REG(x) \
123 COMPILE_ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_##x), \
124 offsetof(ucontext_t,uc_mcontext.gregs[REG_##x]), reg_##x)
125 CHECK_REG(R8);
126 CHECK_REG(R9);
127 CHECK_REG(R10);
128 CHECK_REG(R11);
129 CHECK_REG(R12);
130 CHECK_REG(R13);
131 CHECK_REG(R14);
132 CHECK_REG(R15);
133 CHECK_REG(RDI);
134 CHECK_REG(RSI);
135 CHECK_REG(RBP);
136 CHECK_REG(RBX);
137 CHECK_REG(RDX);
138 CHECK_REG(RAX);
139 CHECK_REG(RCX);
140 CHECK_REG(RSP);
141 CHECK_REG(RIP);
142
143 // sigcontext is an analog to mcontext_t. The layout should be the same.
144 COMPILE_ASSERT_EQ(offsetof(mcontext_t,fpregs),
145 offsetof(sigcontext,fpstate), sigcontext_fpstate);
146
147 #if defined(__ANDROID__)
148 // Check that _fpstate from asm/sigcontext.h is essentially the same
149 // as _libc_fpstate.
150 COMPILE_ASSERT_EQ(sizeof(_libc_fpstate), sizeof(_fpstate),
151 sigcontext_fpstate_size);
152 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,cwd),offsetof(_fpstate,cwd),
153 sigcontext_fpstate_cwd);
154 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,swd),offsetof(_fpstate,swd),
155 sigcontext_fpstate_swd);
156 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,ftw),offsetof(_fpstate,twd),
157 sigcontext_fpstate_twd);
158 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,fop),offsetof(_fpstate,fop),
159 sigcontext_fpstate_fop);
160 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,rip),offsetof(_fpstate,rip),
161 sigcontext_fpstate_rip);
162 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,rdp),offsetof(_fpstate,rdp),
163 sigcontext_fpstate_rdp);
164 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,mxcsr),offsetof(_fpstate,mxcsr),
165 sigcontext_fpstate_mxcsr);
166 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,mxcr_mask),
167 offsetof(_fpstate,mxcsr_mask),
168 sigcontext_fpstate_mxcsr_mask);
169 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,_st), offsetof(_fpstate,st_space),
170 sigcontext_fpstate_stspace);
171 COMPILE_ASSERT_EQ(offsetof(_libc_fpstate,_xmm), offsetof(_fpstate,xmm_space),
172 sigcontext_fpstate_xmm_space);
173 #endif
174
175 COMPILE_ASSERT_EQ(MCONTEXT_FPREGS_PTR,
176 offsetof(ucontext_t,uc_mcontext.fpregs),
177 mcontext_fpregs_ptr);
178 COMPILE_ASSERT_EQ(MCONTEXT_FPREGS_MEM, offsetof(ucontext_t,__fpregs_mem),
179 mcontext_fpregs_mem);
180 COMPILE_ASSERT_EQ(FPREGS_OFFSET_MXCSR,
181 offsetof(std::remove_pointer<fpregset_t>::type,mxcsr),
182 fpregs_offset_mxcsr);
183 COMPILE_ASSERT_EQ(UCONTEXT_SIGMASK_OFFSET, offsetof(ucontext_t, uc_sigmask),
184 ucontext_sigmask);
185 #else
186 ASSERT_EQ(static_cast<size_t>(MCONTEXT_GREGS_OFFSET),
187 offsetof(ucontext_t,uc_mcontext.gregs));
188 #endif
189 }
190
TEST(AndroidUContext,SigmakOffset)191 TEST(AndroidUContext, SigmakOffset) {
192 ASSERT_EQ(static_cast<size_t>(UCONTEXT_SIGMASK_OFFSET),
193 offsetof(ucontext_t,uc_sigmask));
194 }
195