359 lines · cpp
1//===-- RegisterValueTest.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Utility/RegisterValue.h"10#include "lldb/Utility/DataExtractor.h"11#include "lldb/lldb-private-types.h"12#include "gtest/gtest.h"13#include <optional>14 15using namespace lldb_private;16using llvm::APInt;17 18TEST(RegisterValueTest, GetSet8) {19 RegisterValue R8(uint8_t(47));20 EXPECT_EQ(47u, R8.GetAsUInt8());21 R8 = uint8_t(42);22 EXPECT_EQ(42u, R8.GetAsUInt8());23 EXPECT_EQ(42u, R8.GetAsUInt16());24 EXPECT_EQ(42u, R8.GetAsUInt32());25 EXPECT_EQ(42u, R8.GetAsUInt64());26}27 28TEST(RegisterValueTest, GetScalarValue) {29 using RV = RegisterValue;30 const auto &Get = [](const RV &V) -> std::optional<Scalar> {31 Scalar S;32 if (V.GetScalarValue(S))33 return S;34 return std::nullopt;35 };36 EXPECT_EQ(Get(RV(uint8_t(47))), Scalar(47));37 EXPECT_EQ(Get(RV(uint16_t(4747))), Scalar(4747));38 EXPECT_EQ(Get(RV(uint32_t(47474242))), Scalar(47474242));39 EXPECT_EQ(Get(RV(uint64_t(4747424247474242))), Scalar(4747424247474242));40 EXPECT_EQ(Get(RV(APInt::getMaxValue(128))), Scalar(APInt::getMaxValue(128)));41 EXPECT_EQ(Get(RV(47.5f)), Scalar(47.5f));42 EXPECT_EQ(Get(RV(47.5)), Scalar(47.5));43 EXPECT_EQ(Get(RV(47.5L)), Scalar(47.5L));44 EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc}, lldb::eByteOrderLittle)),45 Scalar(0xccddeeff));46 EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc}, lldb::eByteOrderBig)),47 Scalar(0xffeeddcc));48 EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66,49 0x55, 0x44, 0x33, 0x22, 0x11, 0x00},50 lldb::eByteOrderLittle)),51 Scalar((APInt(128, 0x0011223344556677ull) << 64) |52 APInt(128, 0x8899aabbccddeeff)));53 EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66,54 0x55, 0x44, 0x33, 0x22, 0x11, 0x00},55 lldb::eByteOrderBig)),56 Scalar((APInt(128, 0xffeeddccbbaa9988ull) << 64) |57 APInt(128, 0x7766554433221100)));58}59 60void TestSetValueFromData(const Scalar &etalon, void *src, size_t src_byte_size,61 const lldb::ByteOrder endianness,62 const RegisterValue::Type register_value_type) {63 RegisterInfo ri{"test",64 nullptr,65 static_cast<uint32_t>(src_byte_size),66 0,67 lldb::Encoding::eEncodingUint,68 lldb::Format::eFormatDefault,69 {0, 0, 0, LLDB_INVALID_REGNUM, 0},70 nullptr,71 nullptr,72 nullptr};73 DataExtractor src_extractor(src, src_byte_size, endianness, 8);74 RegisterValue rv;75 EXPECT_TRUE(rv.SetValueFromData(ri, src_extractor, 0, false).Success());76 Scalar s;77 EXPECT_TRUE(rv.GetScalarValue(s));78 EXPECT_EQ(rv.GetType(), register_value_type);79 EXPECT_EQ(s, etalon);80}81 82static const Scalar etalon7(APInt(32, 0x0000007F));83 84TEST(RegisterValueTest, SetValueFromData_7_le) {85 uint8_t src[] = {0x7F};86 TestSetValueFromData(etalon7, src, 1, lldb::ByteOrder::eByteOrderLittle,87 RegisterValue::eTypeUInt8);88}89 90TEST(RegisterValueTest, SetValueFromData_7_be) {91 uint8_t src[] = {0x7F};92 TestSetValueFromData(etalon7, src, 1, lldb::ByteOrder::eByteOrderBig,93 RegisterValue::eTypeUInt8);94}95 96static const Scalar etalon8(APInt(32, 0x000000FE));97 98TEST(RegisterValueTest, SetValueFromData_8_le) {99 uint8_t src[] = {0xFE};100 TestSetValueFromData(etalon8, src, 1, lldb::ByteOrder::eByteOrderLittle,101 RegisterValue::eTypeUInt8);102}103 104TEST(RegisterValueTest, SetValueFromData_8_be) {105 uint8_t src[] = {0xFE};106 TestSetValueFromData(etalon8, src, 1, lldb::ByteOrder::eByteOrderBig,107 RegisterValue::eTypeUInt8);108}109 110static const Scalar etalon9(APInt(32, 0x000001FE));111 112TEST(RegisterValueTest, SetValueFromData_9_le) {113 uint8_t src[] = {0xFE, 0x01};114 TestSetValueFromData(etalon9, src, 2, lldb::ByteOrder::eByteOrderLittle,115 RegisterValue::eTypeUInt16);116}117 118TEST(RegisterValueTest, SetValueFromData_9_be) {119 uint8_t src[] = {0x01, 0xFE};120 TestSetValueFromData(etalon9, src, 2, lldb::ByteOrder::eByteOrderBig,121 RegisterValue::eTypeUInt16);122}123 124static const Scalar etalon15(APInt(32, 0x00007FED));125 126TEST(RegisterValueTest, SetValueFromData_15_le) {127 uint8_t src[] = {0xED, 0x7F};128 TestSetValueFromData(etalon15, src, 2, lldb::ByteOrder::eByteOrderLittle,129 RegisterValue::eTypeUInt16);130}131 132TEST(RegisterValueTest, SetValueFromData_15_be) {133 uint8_t src[] = {0x7F, 0xED};134 TestSetValueFromData(etalon15, src, 2, lldb::ByteOrder::eByteOrderBig,135 RegisterValue::eTypeUInt16);136}137 138static const Scalar etalon16(APInt(32, 0x0000FEDC));139 140TEST(RegisterValueTest, SetValueFromData_16_le) {141 uint8_t src[] = {0xDC, 0xFE};142 TestSetValueFromData(etalon16, src, 2, lldb::ByteOrder::eByteOrderLittle,143 RegisterValue::eTypeUInt16);144}145 146TEST(RegisterValueTest, SetValueFromData_16_be) {147 uint8_t src[] = {0xFE, 0xDC};148 TestSetValueFromData(etalon16, src, 2, lldb::ByteOrder::eByteOrderBig,149 RegisterValue::eTypeUInt16);150}151 152static const Scalar etalon17(APInt(32, 0x0001FEDC));153 154TEST(RegisterValueTest, SetValueFromData_17_le) {155 uint8_t src[] = {0xDC, 0xFE, 0x01};156 TestSetValueFromData(etalon17, src, 3, lldb::ByteOrder::eByteOrderLittle,157 RegisterValue::eTypeUInt32);158}159 160TEST(RegisterValueTest, SetValueFromData_17_be) {161 uint8_t src[] = {0x01, 0xFE, 0xDC};162 TestSetValueFromData(etalon17, src, 3, lldb::ByteOrder::eByteOrderBig,163 RegisterValue::eTypeUInt32);164}165 166static const Scalar etalon24(APInt(32, 0x00FEDCBA));167 168TEST(RegisterValueTest, SetValueFromData_24_le) {169 uint8_t src[] = {0xBA, 0xDC, 0xFE};170 TestSetValueFromData(etalon24, src, 3, lldb::ByteOrder::eByteOrderLittle,171 RegisterValue::eTypeUInt32);172}173 174TEST(RegisterValueTest, SetValueFromData_24_be) {175 uint8_t src[] = {0xFE, 0xDC, 0xBA};176 TestSetValueFromData(etalon24, src, 3, lldb::ByteOrder::eByteOrderBig,177 RegisterValue::eTypeUInt32);178}179 180static const Scalar etalon31(APInt(32, 0x7EDCBA98));181 182TEST(RegisterValueTest, SetValueFromData_31_le) {183 uint8_t src[] = {0x98, 0xBA, 0xDC, 0x7E};184 TestSetValueFromData(etalon31, src, 4, lldb::ByteOrder::eByteOrderLittle,185 RegisterValue::eTypeUInt32);186}187 188TEST(RegisterValueTest, SetValueFromData_31_be) {189 uint8_t src[] = {0x7E, 0xDC, 0xBA, 0x98};190 TestSetValueFromData(etalon31, src, 4, lldb::ByteOrder::eByteOrderBig,191 RegisterValue::eTypeUInt32);192}193 194static const Scalar etalon32(APInt(32, 0xFEDCBA98));195 196TEST(RegisterValueTest, SetValueFromData_32_le) {197 uint8_t src[] = {0x98, 0xBA, 0xDC, 0xFE};198 TestSetValueFromData(etalon32, src, 4, lldb::ByteOrder::eByteOrderLittle,199 RegisterValue::eTypeUInt32);200}201 202TEST(RegisterValueTest, SetValueFromData_32_be) {203 uint8_t src[] = {0xFE, 0xDC, 0xBA, 0x98};204 TestSetValueFromData(etalon32, src, 4, lldb::ByteOrder::eByteOrderBig,205 RegisterValue::eTypeUInt32);206}207 208static const Scalar etalon33(APInt(64, 0x00000001FEDCBA98));209 210TEST(RegisterValueTest, SetValueFromData_33_le) {211 uint8_t src[] = {0x98, 0xBA, 0xDC, 0xFE, 0x01};212 TestSetValueFromData(etalon33, src, 5, lldb::ByteOrder::eByteOrderLittle,213 RegisterValue::eTypeUInt64);214}215 216TEST(RegisterValueTest, SetValueFromData_33_be) {217 uint8_t src[] = {0x01, 0xFE, 0xDC, 0xBA, 0x98};218 TestSetValueFromData(etalon33, src, 5, lldb::ByteOrder::eByteOrderBig,219 RegisterValue::eTypeUInt64);220}221 222static const Scalar etalon40(APInt(64, 0x000000FEDCBA9876));223 224TEST(RegisterValueTest, SetValueFromData_40_le) {225 uint8_t src[] = {0x76, 0x98, 0xBA, 0xDC, 0xFE};226 TestSetValueFromData(etalon40, src, 5, lldb::ByteOrder::eByteOrderLittle,227 RegisterValue::eTypeUInt64);228}229 230TEST(RegisterValueTest, SetValueFromData_40_be) {231 uint8_t src[] = {0xFE, 0xDC, 0xBA, 0x98, 0x76};232 TestSetValueFromData(etalon40, src, 5, lldb::ByteOrder::eByteOrderBig,233 RegisterValue::eTypeUInt64);234}235 236static const Scalar etalon63(APInt(64, 0x7EDCBA9876543210));237 238TEST(RegisterValueTest, SetValueFromData_63_le) {239 uint8_t src[] = {0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0x7E};240 TestSetValueFromData(etalon63, src, 8, lldb::ByteOrder::eByteOrderLittle,241 RegisterValue::eTypeUInt64);242}243 244TEST(RegisterValueTest, SetValueFromData_63_be) {245 uint8_t src[] = {0x7E, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};246 TestSetValueFromData(etalon63, src, 8, lldb::ByteOrder::eByteOrderBig,247 RegisterValue::eTypeUInt64);248}249 250static const Scalar etalon64(APInt(64, 0xFEDCBA9876543210));251 252TEST(RegisterValueTest, SetValueFromData_64_le) {253 uint8_t src[] = {0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE};254 TestSetValueFromData(etalon64, src, 8, lldb::ByteOrder::eByteOrderLittle,255 RegisterValue::eTypeUInt64);256}257 258TEST(RegisterValueTest, SetValueFromData_64_be) {259 uint8_t src[] = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};260 TestSetValueFromData(etalon64, src, 8, lldb::ByteOrder::eByteOrderBig,261 RegisterValue::eTypeUInt64);262}263 264static const Scalar etalon65(APInt(72, 0x0000000000000001ull) << 1 * 64 |265 APInt(72, 0x0706050403020100ull) << 0 * 64);266 267TEST(RegisterValueTest, SetValueFromData_65_le) {268 uint8_t src[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x01};269 TestSetValueFromData(etalon65, src, 9, lldb::ByteOrder::eByteOrderLittle,270 RegisterValue::eTypeUIntN);271}272 273TEST(RegisterValueTest, SetValueFromData_65_be) {274 uint8_t src[] = {0x01, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};275 TestSetValueFromData(etalon65, src, 9, lldb::ByteOrder::eByteOrderBig,276 RegisterValue::eTypeUIntN);277}278 279static const Scalar etalon127(APInt(128, 0x7f0e0d0c0b0a0908ull) << 1 * 64 |280 APInt(128, 0x0706050403020100ull) << 0 * 64);281 282TEST(RegisterValueTest, SetValueFromData_127_le) {283 uint8_t src[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,284 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x7f};285 TestSetValueFromData(etalon127, src, 16, lldb::ByteOrder::eByteOrderLittle,286 RegisterValue::eTypeUIntN);287}288 289TEST(RegisterValueTest, SetValueFromData_127_be) {290 uint8_t src[] = {0x7f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,291 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};292 TestSetValueFromData(etalon127, src, 16, lldb::ByteOrder::eByteOrderBig,293 RegisterValue::eTypeUIntN);294}295 296static const Scalar etalon128(APInt(128, 0x0f0e0d0c0b0a0908ull) << 1 * 64 |297 APInt(128, 0x0706050403020100ull) << 0 * 64);298 299TEST(RegisterValueTest, SetValueFromData_128_le) {300 uint8_t src[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,301 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};302 TestSetValueFromData(etalon128, src, 16, lldb::ByteOrder::eByteOrderLittle,303 RegisterValue::eTypeUIntN);304}305 306TEST(RegisterValueTest, SetValueFromData_128_be) {307 uint8_t src[] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,308 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};309 TestSetValueFromData(etalon128, src, 16, lldb::ByteOrder::eByteOrderBig,310 RegisterValue::eTypeUIntN);311}312 313static const Scalar etalon256(APInt(256, 0x1f1e1d1c1b1a1918ull) << 3 * 64 |314 APInt(256, 0x1716151413121110ull) << 2 * 64 |315 APInt(256, 0x0f0e0d0c0b0a0908ull) << 1 * 64 |316 APInt(256, 0x0706050403020100ull) << 0 * 64);317 318TEST(RegisterValueTest, SetValueFromData_256_le) {319 uint8_t src[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,320 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,321 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,322 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};323 TestSetValueFromData(etalon256, src, 32, lldb::ByteOrder::eByteOrderLittle,324 RegisterValue::eTypeUIntN);325}326 327TEST(RegisterValueTest, SetValueFromData_256_be) {328 uint8_t src[] = {0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,329 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,330 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,331 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};332 TestSetValueFromData(etalon256, src, 32, lldb::ByteOrder::eByteOrderBig,333 RegisterValue::eTypeUIntN);334}335 336static const Scalar etalon257(APInt(512, 0x0000000000000001ull) << 4 * 64 |337 APInt(512, 0x1f1e1d1c1b1a1918ull) << 3 * 64 |338 APInt(512, 0x1716151413121110ull) << 2 * 64 |339 APInt(512, 0x0f0e0d0c0b0a0908ull) << 1 * 64 |340 APInt(512, 0x0706050403020100ull) << 0 * 64);341 342TEST(RegisterValueTest, SetValueFromData_257_le) {343 uint8_t src[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,344 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,345 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a,346 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x01};347 TestSetValueFromData(etalon257, src, 33, lldb::ByteOrder::eByteOrderLittle,348 RegisterValue::eTypeUIntN);349}350 351TEST(RegisterValueTest, SetValueFromData_257_be) {352 uint8_t src[] = {0x01, 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,353 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f,354 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06,355 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};356 TestSetValueFromData(etalon257, src, 33, lldb::ByteOrder::eByteOrderBig,357 RegisterValue::eTypeUIntN);358}359