Skip to content

Commit 5ba3f9d

Browse files
committed
[lldb] Add Checksum class to lldbUtility
This commit adds an MD5 checksum (`Checksum`) class to LLDB. Its purpose is to store the MD5 hash added to the DWARF 5 line table.
1 parent b336d74 commit 5ba3f9d

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

lldb/include/lldb/Utility/Checksum.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- Checksum.h ----------------------------------------------*- C++ -*-===//
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+
#ifndef LLDB_UTILITY_CHECKSUM_H
10+
#define LLDB_UTILITY_CHECKSUM_H
11+
12+
#include "llvm/Support/MD5.h"
13+
14+
namespace lldb_private {
15+
class Checksum {
16+
public:
17+
static llvm::MD5::MD5Result sentinel;
18+
19+
Checksum(llvm::MD5::MD5Result md5 = sentinel);
20+
Checksum(const Checksum &checksum);
21+
Checksum &operator=(const Checksum &checksum);
22+
23+
explicit operator bool() const;
24+
bool operator==(const Checksum &checksum) const;
25+
bool operator!=(const Checksum &checksum) const;
26+
27+
std::string digest() const;
28+
29+
private:
30+
void SetMD5(llvm::MD5::MD5Result);
31+
32+
llvm::MD5::MD5Result m_checksum;
33+
};
34+
} // namespace lldb_private
35+
36+
#endif // LLDB_UTILITY_CHECKSUM_H

lldb/source/Utility/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_lldb_library(lldbUtility NO_INTERNAL_DEPENDENCIES
2929
Args.cpp
3030
Baton.cpp
3131
Broadcaster.cpp
32+
Checksum.cpp
3233
CompletionRequest.cpp
3334
Connection.cpp
3435
ConstString.cpp

lldb/source/Utility/Checksum.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===-- Checksum.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 "lldb/Utility/Checksum.h"
10+
#include "llvm/ADT/SmallString.h"
11+
12+
using namespace lldb_private;
13+
14+
Checksum::Checksum(llvm::MD5::MD5Result md5) { SetMD5(md5); }
15+
16+
Checksum::Checksum(const Checksum &checksum) { SetMD5(checksum.m_checksum); }
17+
18+
Checksum &Checksum::operator=(const Checksum &checksum) {
19+
SetMD5(checksum.m_checksum);
20+
return *this;
21+
}
22+
23+
void Checksum::SetMD5(llvm::MD5::MD5Result md5) {
24+
std::uninitialized_copy_n(md5.begin(), 16, m_checksum.begin());
25+
}
26+
27+
Checksum::operator bool() const {
28+
return !std::equal(m_checksum.begin(), m_checksum.end(), sentinel.begin());
29+
}
30+
31+
bool Checksum::operator==(const Checksum &checksum) const {
32+
return std::equal(m_checksum.begin(), m_checksum.end(),
33+
checksum.m_checksum.begin());
34+
}
35+
36+
bool Checksum::operator!=(const Checksum &checksum) const {
37+
return !std::equal(m_checksum.begin(), m_checksum.end(),
38+
checksum.m_checksum.begin());
39+
}
40+
41+
std::string Checksum::digest() const {
42+
return std::string(m_checksum.digest().str());
43+
}
44+
45+
llvm::MD5::MD5Result Checksum::sentinel = {0, 0, 0, 0, 0, 0, 0, 0,
46+
0, 0, 0, 0, 0, 0, 0, 0};

lldb/unittests/Utility/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_lldb_unittest(UtilityTests
44
OptionsWithRawTest.cpp
55
ArchSpecTest.cpp
66
BroadcasterTest.cpp
7+
ChecksumTest.cpp
78
ConstStringTest.cpp
89
CompletionRequestTest.cpp
910
DataBufferTest.cpp
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===-- ChecksumTest.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 "lldb/Utility/Checksum.h"
12+
13+
using namespace lldb_private;
14+
15+
static llvm::MD5::MD5Result hash1 = {0, 1, 2, 3, 4, 5, 6, 7,
16+
8, 9, 10, 11, 12, 13, 14, 15};
17+
18+
static llvm::MD5::MD5Result hash2 = {0, 1, 2, 3, 4, 5, 6, 7,
19+
8, 9, 10, 11, 12, 13, 14, 15};
20+
21+
static llvm::MD5::MD5Result hash3 = {8, 9, 10, 11, 12, 13, 14, 15,
22+
0, 1, 2, 3, 4, 5, 6, 7};
23+
24+
TEST(ChecksumTest, TestConstructor) {
25+
Checksum checksum1;
26+
EXPECT_FALSE(static_cast<bool>(checksum1));
27+
EXPECT_EQ(checksum1, Checksum());
28+
29+
Checksum checksum2 = Checksum(hash1);
30+
EXPECT_EQ(checksum2, Checksum(hash1));
31+
32+
Checksum checksum3(checksum2);
33+
EXPECT_EQ(checksum3, Checksum(hash1));
34+
}
35+
36+
TEST(ChecksumTest, TestCopyConstructor) {
37+
Checksum checksum1;
38+
EXPECT_FALSE(static_cast<bool>(checksum1));
39+
EXPECT_EQ(checksum1, Checksum());
40+
41+
Checksum checksum2 = checksum1;
42+
EXPECT_EQ(checksum2, checksum1);
43+
44+
Checksum checksum3(checksum1);
45+
EXPECT_EQ(checksum3, checksum1);
46+
}
47+
48+
TEST(ChecksumTest, TestMD5) {
49+
Checksum checksum1(hash1);
50+
EXPECT_TRUE(static_cast<bool>(checksum1));
51+
52+
// Make sure two checksums with the same underlying hashes are the same.
53+
EXPECT_EQ(Checksum(hash1), Checksum(hash2));
54+
55+
// Make sure two checksums with different underlying hashes are different.
56+
EXPECT_NE(Checksum(hash1), Checksum(hash3));
57+
}

0 commit comments

Comments
 (0)