| 1 | //===-- PlatformSiginfoTest.cpp -------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "gtest/gtest.h" |
| 10 | |
| 11 | #include <initializer_list> |
| 12 | #include <optional> |
| 13 | #include <tuple> |
| 14 | |
| 15 | #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h" |
| 16 | #include "Plugins/Platform/Linux/PlatformLinux.h" |
| 17 | #include "Plugins/Platform/NetBSD/PlatformNetBSD.h" |
| 18 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
| 19 | |
| 20 | #include "TestingSupport/SubsystemRAII.h" |
| 21 | #include "lldb/Core/Debugger.h" |
| 22 | #include "lldb/Host/FileSystem.h" |
| 23 | #include "lldb/Host/HostInfo.h" |
| 24 | #include "lldb/Utility/ArchSpec.h" |
| 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
| 29 | namespace { |
| 30 | class PlatformSiginfoTest : public ::testing::Test { |
| 31 | SubsystemRAII<FileSystem, HostInfo, TypeSystemClang> subsystems; |
| 32 | PlatformSP platform_sp; |
| 33 | DebuggerSP debugger_sp; |
| 34 | TargetSP target_sp; |
| 35 | |
| 36 | public: |
| 37 | CompilerType siginfo_type; |
| 38 | |
| 39 | void SetUp() override { |
| 40 | platform_freebsd::PlatformFreeBSD::Initialize(); |
| 41 | platform_linux::PlatformLinux::Initialize(); |
| 42 | platform_netbsd::PlatformNetBSD::Initialize(); |
| 43 | } |
| 44 | |
| 45 | void TearDown() override { |
| 46 | platform_netbsd::PlatformNetBSD::Terminate(); |
| 47 | platform_linux::PlatformLinux::Terminate(); |
| 48 | platform_freebsd::PlatformFreeBSD::Terminate(); |
| 49 | } |
| 50 | |
| 51 | typedef std::tuple<const char *, uint64_t, uint64_t> field_tuple; |
| 52 | |
| 53 | void ExpectField(const CompilerType &siginfo_type, field_tuple field) { |
| 54 | const char *path; |
| 55 | uint64_t offset, size; |
| 56 | std::tie(args&: path, args&: offset, args&: size) = field; |
| 57 | |
| 58 | SCOPED_TRACE(path); |
| 59 | CompilerType field_type = siginfo_type; |
| 60 | uint64_t total_offset = 0; |
| 61 | for (auto field_name : llvm::split(Str: path, Separator: '.')) { |
| 62 | uint64_t bit_offset; |
| 63 | std::string name; |
| 64 | auto index_or_err = field_type.GetIndexOfChildWithName(name: field_name, omit_empty_base_classes: false); |
| 65 | ASSERT_FALSE(!index_or_err); |
| 66 | field_type = field_type.GetFieldAtIndex(idx: *index_or_err, name, bit_offset_ptr: &bit_offset, |
| 67 | bitfield_bit_size_ptr: nullptr, is_bitfield_ptr: nullptr); |
| 68 | ASSERT_TRUE(field_type); |
| 69 | total_offset += bit_offset; |
| 70 | } |
| 71 | |
| 72 | EXPECT_EQ(total_offset, offset * 8); |
| 73 | EXPECT_EQ(llvm::expectedToOptional(field_type.GetByteSize(nullptr)), |
| 74 | std::optional<uint64_t>(size)); |
| 75 | } |
| 76 | |
| 77 | void ExpectFields(const CompilerType &container, |
| 78 | std::initializer_list<field_tuple> fields) { |
| 79 | for (auto x : fields) |
| 80 | ExpectField(siginfo_type: container, field: x); |
| 81 | } |
| 82 | |
| 83 | void InitializeSiginfo(const std::string &triple) { |
| 84 | ArchSpec arch(triple); |
| 85 | |
| 86 | switch (arch.GetTriple().getOS()) { |
| 87 | case llvm::Triple::FreeBSD: |
| 88 | platform_sp = |
| 89 | platform_freebsd::PlatformFreeBSD::CreateInstance(force: true, arch: &arch); |
| 90 | break; |
| 91 | case llvm::Triple::Linux: |
| 92 | platform_sp = platform_linux::PlatformLinux::CreateInstance(force: true, arch: &arch); |
| 93 | break; |
| 94 | case llvm::Triple::NetBSD: |
| 95 | platform_sp = |
| 96 | platform_netbsd::PlatformNetBSD::CreateInstance(force: true, arch: &arch); |
| 97 | break; |
| 98 | default: |
| 99 | llvm_unreachable("unknown ostype in triple" ); |
| 100 | } |
| 101 | Platform::SetHostPlatform(platform_sp); |
| 102 | |
| 103 | debugger_sp = Debugger::CreateInstance(); |
| 104 | ASSERT_TRUE(debugger_sp); |
| 105 | |
| 106 | debugger_sp->GetTargetList().CreateTarget( |
| 107 | debugger&: *debugger_sp, user_exe_path: "" , arch, get_dependent_modules: eLoadDependentsNo, platform_sp, target_sp); |
| 108 | ASSERT_TRUE(target_sp); |
| 109 | |
| 110 | siginfo_type = platform_sp->GetSiginfoType(triple: arch.GetTriple()); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | } // namespace |
| 115 | |
| 116 | TEST_F(PlatformSiginfoTest, TestLinux_64bit) { |
| 117 | for (std::string arch : {"x86_64" , "aarch64" , "powerpc64le" }) { |
| 118 | SCOPED_TRACE(arch); |
| 119 | InitializeSiginfo(triple: arch + "-pc-linux-gnu" ); |
| 120 | ASSERT_TRUE(siginfo_type); |
| 121 | |
| 122 | ExpectFields(container: siginfo_type, |
| 123 | fields: { |
| 124 | {"si_signo" , 0, 4}, |
| 125 | {"si_errno" , 4, 4}, |
| 126 | {"si_code" , 8, 4}, |
| 127 | {"_sifields._kill.si_pid" , 16, 4}, |
| 128 | {"_sifields._kill.si_uid" , 20, 4}, |
| 129 | {"_sifields._timer.si_tid" , 16, 4}, |
| 130 | {"_sifields._timer.si_overrun" , 20, 4}, |
| 131 | {"_sifields._timer.si_sigval" , 24, 8}, |
| 132 | {"_sifields._rt.si_pid" , 16, 4}, |
| 133 | {"_sifields._rt.si_uid" , 20, 4}, |
| 134 | {"_sifields._rt.si_sigval" , 24, 8}, |
| 135 | {"_sifields._sigchld.si_pid" , 16, 4}, |
| 136 | {"_sifields._sigchld.si_uid" , 20, 4}, |
| 137 | {"_sifields._sigchld.si_status" , 24, 4}, |
| 138 | {"_sifields._sigchld.si_utime" , 32, 8}, |
| 139 | {"_sifields._sigchld.si_stime" , 40, 8}, |
| 140 | {"_sifields._sigfault.si_addr" , 16, 8}, |
| 141 | {"_sifields._sigfault.si_addr_lsb" , 24, 2}, |
| 142 | {"_sifields._sigfault._bounds._addr_bnd._lower" , 32, 8}, |
| 143 | {"_sifields._sigfault._bounds._addr_bnd._upper" , 40, 8}, |
| 144 | {"_sifields._sigfault._bounds._pkey" , 32, 4}, |
| 145 | {"_sifields._sigpoll.si_band" , 16, 8}, |
| 146 | {"_sifields._sigpoll.si_fd" , 24, 4}, |
| 147 | {"_sifields._sigsys._call_addr" , 16, 8}, |
| 148 | {"_sifields._sigsys._syscall" , 24, 4}, |
| 149 | {"_sifields._sigsys._arch" , 28, 4}, |
| 150 | }); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | TEST_F(PlatformSiginfoTest, TestLinux_32bit) { |
| 155 | for (std::string arch : {"i386" , "armv7" }) { |
| 156 | SCOPED_TRACE(arch); |
| 157 | InitializeSiginfo(triple: arch + "-pc-linux" ); |
| 158 | ASSERT_TRUE(siginfo_type); |
| 159 | |
| 160 | ExpectFields(container: siginfo_type, |
| 161 | fields: { |
| 162 | {"si_signo" , 0, 4}, |
| 163 | {"si_errno" , 4, 4}, |
| 164 | {"si_code" , 8, 4}, |
| 165 | {"_sifields._kill.si_pid" , 12, 4}, |
| 166 | {"_sifields._kill.si_uid" , 16, 4}, |
| 167 | {"_sifields._timer.si_tid" , 12, 4}, |
| 168 | {"_sifields._timer.si_overrun" , 16, 4}, |
| 169 | {"_sifields._timer.si_sigval" , 20, 4}, |
| 170 | {"_sifields._rt.si_pid" , 12, 4}, |
| 171 | {"_sifields._rt.si_uid" , 16, 4}, |
| 172 | {"_sifields._rt.si_sigval" , 20, 4}, |
| 173 | {"_sifields._sigchld.si_pid" , 12, 4}, |
| 174 | {"_sifields._sigchld.si_uid" , 16, 4}, |
| 175 | {"_sifields._sigchld.si_status" , 20, 4}, |
| 176 | {"_sifields._sigchld.si_utime" , 24, 4}, |
| 177 | {"_sifields._sigchld.si_stime" , 28, 4}, |
| 178 | {"_sifields._sigfault.si_addr" , 12, 4}, |
| 179 | {"_sifields._sigfault.si_addr_lsb" , 16, 2}, |
| 180 | {"_sifields._sigfault._bounds._addr_bnd._lower" , 20, 4}, |
| 181 | {"_sifields._sigfault._bounds._addr_bnd._upper" , 24, 4}, |
| 182 | {"_sifields._sigfault._bounds._pkey" , 20, 4}, |
| 183 | {"_sifields._sigpoll.si_band" , 12, 4}, |
| 184 | {"_sifields._sigpoll.si_fd" , 16, 4}, |
| 185 | {"_sifields._sigsys._call_addr" , 12, 4}, |
| 186 | {"_sifields._sigsys._syscall" , 16, 4}, |
| 187 | {"_sifields._sigsys._arch" , 20, 4}, |
| 188 | }); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | TEST_F(PlatformSiginfoTest, TestFreeBSD_64bit) { |
| 193 | for (std::string arch : {"x86_64" , "aarch64" }) { |
| 194 | SCOPED_TRACE(arch); |
| 195 | InitializeSiginfo(triple: "x86_64-unknown-freebsd13.0" ); |
| 196 | ASSERT_TRUE(siginfo_type); |
| 197 | |
| 198 | ExpectFields(container: siginfo_type, fields: { |
| 199 | {"si_signo" , 0, 4}, |
| 200 | {"si_errno" , 4, 4}, |
| 201 | {"si_code" , 8, 4}, |
| 202 | {"si_pid" , 12, 4}, |
| 203 | {"si_uid" , 16, 4}, |
| 204 | {"si_status" , 20, 4}, |
| 205 | {"si_addr" , 24, 8}, |
| 206 | {"si_value" , 32, 8}, |
| 207 | {"_reason._fault._trapno" , 40, 4}, |
| 208 | {"_reason._timer._timerid" , 40, 4}, |
| 209 | {"_reason._timer._overrun" , 44, 4}, |
| 210 | {"_reason._mesgq._mqd" , 40, 4}, |
| 211 | {"_reason._poll._band" , 40, 8}, |
| 212 | }); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | TEST_F(PlatformSiginfoTest, TestFreeBSD_32bit) { |
| 217 | for (std::string arch : {"i386" }) { |
| 218 | SCOPED_TRACE(arch); |
| 219 | InitializeSiginfo(triple: arch + "-unknown-freebsd13.0" ); |
| 220 | ASSERT_TRUE(siginfo_type); |
| 221 | |
| 222 | ExpectFields(container: siginfo_type, fields: { |
| 223 | {"si_signo" , 0, 4}, |
| 224 | {"si_errno" , 4, 4}, |
| 225 | {"si_code" , 8, 4}, |
| 226 | {"si_pid" , 12, 4}, |
| 227 | {"si_uid" , 16, 4}, |
| 228 | {"si_status" , 20, 4}, |
| 229 | {"si_addr" , 24, 4}, |
| 230 | {"si_value" , 28, 4}, |
| 231 | {"_reason._fault._trapno" , 32, 4}, |
| 232 | {"_reason._timer._timerid" , 32, 4}, |
| 233 | {"_reason._timer._overrun" , 36, 4}, |
| 234 | {"_reason._mesgq._mqd" , 32, 4}, |
| 235 | {"_reason._poll._band" , 32, 4}, |
| 236 | }); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | TEST_F(PlatformSiginfoTest, TestNetBSD_64bit) { |
| 241 | for (std::string arch : {"x86_64" }) { |
| 242 | SCOPED_TRACE(arch); |
| 243 | InitializeSiginfo(triple: arch + "-unknown-netbsd9.0" ); |
| 244 | ASSERT_TRUE(siginfo_type); |
| 245 | |
| 246 | ExpectFields( |
| 247 | container: siginfo_type, |
| 248 | fields: { |
| 249 | {"_info._signo" , 0, 4}, |
| 250 | {"_info._code" , 4, 4}, |
| 251 | {"_info._errno" , 8, 4}, |
| 252 | {"_info._reason._rt._pid" , 16, 4}, |
| 253 | {"_info._reason._rt._uid" , 20, 4}, |
| 254 | {"_info._reason._rt._value" , 24, 8}, |
| 255 | {"_info._reason._child._pid" , 16, 4}, |
| 256 | {"_info._reason._child._uid" , 20, 4}, |
| 257 | {"_info._reason._child._status" , 24, 4}, |
| 258 | {"_info._reason._child._utime" , 28, 4}, |
| 259 | {"_info._reason._child._stime" , 32, 4}, |
| 260 | {"_info._reason._fault._addr" , 16, 8}, |
| 261 | {"_info._reason._fault._trap" , 24, 4}, |
| 262 | {"_info._reason._fault._trap2" , 28, 4}, |
| 263 | {"_info._reason._fault._trap3" , 32, 4}, |
| 264 | {"_info._reason._poll._band" , 16, 8}, |
| 265 | {"_info._reason._poll._fd" , 24, 4}, |
| 266 | {"_info._reason._syscall._sysnum" , 16, 4}, |
| 267 | {"_info._reason._syscall._retval" , 20, 8}, |
| 268 | {"_info._reason._syscall._error" , 28, 4}, |
| 269 | {"_info._reason._syscall._args" , 32, 64}, |
| 270 | {"_info._reason._ptrace_state._pe_report_event" , 16, 4}, |
| 271 | {"_info._reason._ptrace_state._option._pe_other_pid" , 20, 4}, |
| 272 | {"_info._reason._ptrace_state._option._pe_lwp" , 20, 4}, |
| 273 | }); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | TEST_F(PlatformSiginfoTest, TestNetBSD_32bit) { |
| 278 | for (std::string arch : {"i386" }) { |
| 279 | SCOPED_TRACE(arch); |
| 280 | InitializeSiginfo(triple: arch + "-unknown-netbsd9.0" ); |
| 281 | ASSERT_TRUE(siginfo_type); |
| 282 | |
| 283 | ExpectFields( |
| 284 | container: siginfo_type, |
| 285 | fields: { |
| 286 | {"_info._signo" , 0, 4}, |
| 287 | {"_info._code" , 4, 4}, |
| 288 | {"_info._errno" , 8, 4}, |
| 289 | {"_info._reason._rt._pid" , 12, 4}, |
| 290 | {"_info._reason._rt._uid" , 16, 4}, |
| 291 | {"_info._reason._rt._value" , 20, 4}, |
| 292 | {"_info._reason._child._pid" , 12, 4}, |
| 293 | {"_info._reason._child._uid" , 16, 4}, |
| 294 | {"_info._reason._child._status" , 20, 4}, |
| 295 | {"_info._reason._child._utime" , 24, 4}, |
| 296 | {"_info._reason._child._stime" , 28, 4}, |
| 297 | {"_info._reason._fault._addr" , 12, 4}, |
| 298 | {"_info._reason._fault._trap" , 16, 4}, |
| 299 | {"_info._reason._fault._trap2" , 20, 4}, |
| 300 | {"_info._reason._fault._trap3" , 24, 4}, |
| 301 | {"_info._reason._poll._band" , 12, 4}, |
| 302 | {"_info._reason._poll._fd" , 16, 4}, |
| 303 | {"_info._reason._syscall._sysnum" , 12, 4}, |
| 304 | {"_info._reason._syscall._retval" , 16, 8}, |
| 305 | {"_info._reason._syscall._error" , 24, 4}, |
| 306 | {"_info._reason._syscall._args" , 28, 64}, |
| 307 | {"_info._reason._ptrace_state._pe_report_event" , 12, 4}, |
| 308 | {"_info._reason._ptrace_state._option._pe_other_pid" , 16, 4}, |
| 309 | {"_info._reason._ptrace_state._option._pe_lwp" , 16, 4}, |
| 310 | }); |
| 311 | } |
| 312 | } |
| 313 | |