Skip to content

Commit 2f58b9a

Browse files
authored
[lldb] Unify CalculateMD5 return types (#90921)
# Overview In my previous PR: #88812, @JDevlieghere suggested to match return types of the various calculate md5 functions. This PR achieves that by changing the various calculate md5 functions to return `llvm::ErrorOr<llvm::MD5::MD5Result>`.   The suggestion was to go for `std::optional<>` but I opted for `llvm::ErrorOr<>` because local calculate md5 was already possibly returning `ErrorOr`. To make sure I didn't break the md5 calculation functionality, I ran some tests for the gdb remote client, and things seem to work. # Testing 1. Remote file doesn't exist ![image](https://github.com/llvm/llvm-project/assets/1326275/b26859e2-18c3-4685-be8f-c6b6a5a4bc77) 1. Remote file differs ![image](https://github.com/llvm/llvm-project/assets/1326275/cbdb3c58-555a-401b-9444-c5ff4c04c491) 1. Remote file matches ![image](https://github.com/llvm/llvm-project/assets/1326275/07561572-22d1-4e0a-988f-bc91b5c2ffce) ## Test gaps Unfortunately, I had to modify `lldb/source/Plugins/Platform/MacOSX/PlatformDarwinDevice.cpp` and I can't test the changes there. Hopefully, the existing test suite / code review from whomever is reading this will catch any issues. Co-authored-by: Anthony Ha <[email protected]>
1 parent f8a9973 commit 2f58b9a

File tree

9 files changed

+60
-52
lines changed

9 files changed

+60
-52
lines changed

lldb/include/lldb/Target/Platform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,8 @@ class Platform : public PluginInterface {
649649

650650
virtual std::string GetPlatformSpecificConnectionInformation() { return ""; }
651651

652-
virtual bool CalculateMD5(const FileSpec &file_spec, uint64_t &low,
653-
uint64_t &high);
652+
virtual llvm::ErrorOr<llvm::MD5::MD5Result>
653+
CalculateMD5(const FileSpec &file_spec);
654654

655655
virtual uint32_t GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) {
656656
return 1;

lldb/include/lldb/Target/RemoteAwarePlatform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class RemoteAwarePlatform : public Platform {
5858
Status SetFilePermissions(const FileSpec &file_spec,
5959
uint32_t file_permissions) override;
6060

61-
bool CalculateMD5(const FileSpec &file_spec, uint64_t &low,
62-
uint64_t &high) override;
61+
llvm::ErrorOr<llvm::MD5::MD5Result>
62+
CalculateMD5(const FileSpec &file_spec) override;
6363

6464
Status GetFileWithUUID(const FileSpec &platform_file, const UUID *uuid,
6565
FileSpec &local_file) override;

lldb/source/Plugins/Platform/MacOSX/PlatformDarwinDevice.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,21 @@ lldb_private::Status PlatformDarwinDevice::GetSharedModuleWithLocalCache(
405405
// when going over the *slow* GDB remote transfer mechanism we first
406406
// check the hashes of the files - and only do the actual transfer if
407407
// they differ
408-
uint64_t high_local, high_remote, low_local, low_remote;
409408
auto MD5 = llvm::sys::fs::md5_contents(module_cache_spec.GetPath());
410409
if (!MD5)
411410
return Status(MD5.getError());
412-
std::tie(high_local, low_local) = MD5->words();
413411

414-
m_remote_platform_sp->CalculateMD5(module_spec.GetFileSpec(),
415-
low_remote, high_remote);
416-
if (low_local != low_remote || high_local != high_remote) {
412+
Log *log = GetLog(LLDBLog::Platform);
413+
bool requires_transfer = true;
414+
llvm::ErrorOr<llvm::MD5::MD5Result> remote_md5 =
415+
m_remote_platform_sp->CalculateMD5(module_spec.GetFileSpec());
416+
if (std::error_code ec = remote_md5.getError())
417+
LLDB_LOG(log, "couldn't get md5 sum from remote: {0}",
418+
ec.message());
419+
else
420+
requires_transfer = *MD5 != *remote_md5;
421+
if (requires_transfer) {
417422
// bring in the remote file
418-
Log *log = GetLog(LLDBLog::Platform);
419423
LLDB_LOGF(log,
420424
"[%s] module %s/%s needs to be replaced from remote copy",
421425
(IsHost() ? "host" : "remote"),

lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,12 +684,12 @@ Status PlatformRemoteGDBServer::RunShellCommand(
684684
signo_ptr, command_output, timeout);
685685
}
686686

687-
bool PlatformRemoteGDBServer::CalculateMD5(const FileSpec &file_spec,
688-
uint64_t &low, uint64_t &high) {
687+
llvm::ErrorOr<llvm::MD5::MD5Result>
688+
PlatformRemoteGDBServer::CalculateMD5(const FileSpec &file_spec) {
689689
if (!IsConnected())
690-
return false;
690+
return std::make_error_code(std::errc::not_connected);
691691

692-
return m_gdb_client_up->CalculateMD5(file_spec, low, high);
692+
return m_gdb_client_up->CalculateMD5(file_spec);
693693
}
694694

695695
void PlatformRemoteGDBServer::CalculateTrapHandlerSymbolNames() {

lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ class PlatformRemoteGDBServer : public Platform, private UserIDResolver {
146146

147147
void CalculateTrapHandlerSymbolNames() override;
148148

149-
bool CalculateMD5(const FileSpec &file_spec, uint64_t &low,
150-
uint64_t &high) override;
149+
llvm::ErrorOr<llvm::MD5::MD5Result>
150+
CalculateMD5(const FileSpec &file_spec) override;
151151

152152
const lldb::UnixSignalsSP &GetRemoteUnixSignals() override;
153153

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,8 +3418,8 @@ bool GDBRemoteCommunicationClient::GetFileExists(
34183418
return true;
34193419
}
34203420

3421-
bool GDBRemoteCommunicationClient::CalculateMD5(
3422-
const lldb_private::FileSpec &file_spec, uint64_t &low, uint64_t &high) {
3421+
llvm::ErrorOr<llvm::MD5::MD5Result> GDBRemoteCommunicationClient::CalculateMD5(
3422+
const lldb_private::FileSpec &file_spec) {
34233423
std::string path(file_spec.GetPath(false));
34243424
lldb_private::StreamString stream;
34253425
stream.PutCString("vFile:MD5:");
@@ -3428,11 +3428,11 @@ bool GDBRemoteCommunicationClient::CalculateMD5(
34283428
if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
34293429
PacketResult::Success) {
34303430
if (response.GetChar() != 'F')
3431-
return false;
3431+
return std::make_error_code(std::errc::illegal_byte_sequence);
34323432
if (response.GetChar() != ',')
3433-
return false;
3433+
return std::make_error_code(std::errc::illegal_byte_sequence);
34343434
if (response.Peek() && *response.Peek() == 'x')
3435-
return false;
3435+
return std::make_error_code(std::errc::no_such_file_or_directory);
34363436

34373437
// GDBRemoteCommunicationServerCommon::Handle_vFile_MD5 concatenates low and
34383438
// high hex strings. We can't use response.GetHexMaxU64 because that can't
@@ -3455,25 +3455,33 @@ bool GDBRemoteCommunicationClient::CalculateMD5(
34553455
auto part =
34563456
response.GetStringRef().substr(response.GetFilePos(), MD5_HALF_LENGTH);
34573457
if (part.size() != MD5_HALF_LENGTH)
3458-
return false;
3458+
return std::make_error_code(std::errc::illegal_byte_sequence);
34593459
response.SetFilePos(response.GetFilePos() + part.size());
34603460

3461+
uint64_t low;
34613462
if (part.getAsInteger(/*radix=*/16, low))
3462-
return false;
3463+
return std::make_error_code(std::errc::illegal_byte_sequence);
34633464

34643465
// Get high part
34653466
part =
34663467
response.GetStringRef().substr(response.GetFilePos(), MD5_HALF_LENGTH);
34673468
if (part.size() != MD5_HALF_LENGTH)
3468-
return false;
3469+
return std::make_error_code(std::errc::illegal_byte_sequence);
34693470
response.SetFilePos(response.GetFilePos() + part.size());
34703471

3472+
uint64_t high;
34713473
if (part.getAsInteger(/*radix=*/16, high))
3472-
return false;
3474+
return std::make_error_code(std::errc::illegal_byte_sequence);
34733475

3474-
return true;
3476+
llvm::MD5::MD5Result result;
3477+
llvm::support::endian::write<uint64_t, llvm::endianness::little>(
3478+
result.data(), low);
3479+
llvm::support::endian::write<uint64_t, llvm::endianness::little>(
3480+
result.data() + 8, high);
3481+
3482+
return result;
34753483
}
3476-
return false;
3484+
return std::make_error_code(std::errc::operation_canceled);
34773485
}
34783486

34793487
bool GDBRemoteCommunicationClient::AvoidGPackets(ProcessGDBRemote *process) {

lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class GDBRemoteCommunicationClient : public GDBRemoteClientBase {
392392
*command_output, // Pass nullptr if you don't want the command output
393393
const Timeout<std::micro> &timeout);
394394

395-
bool CalculateMD5(const FileSpec &file_spec, uint64_t &low, uint64_t &high);
395+
llvm::ErrorOr<llvm::MD5::MD5Result> CalculateMD5(const FileSpec &file_spec);
396396

397397
lldb::DataBufferSP ReadRegister(
398398
lldb::tid_t tid,

lldb/source/Target/Platform.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,22 +1199,22 @@ Status Platform::PutFile(const FileSpec &source, const FileSpec &destination,
11991199
Status error;
12001200

12011201
bool requires_upload = true;
1202-
uint64_t dest_md5_low, dest_md5_high;
1203-
bool success = CalculateMD5(destination, dest_md5_low, dest_md5_high);
1204-
if (!success) {
1205-
LLDB_LOGF(log, "[PutFile] couldn't get md5 sum of destination");
1202+
llvm::ErrorOr<llvm::MD5::MD5Result> remote_md5 = CalculateMD5(destination);
1203+
if (std::error_code ec = remote_md5.getError()) {
1204+
LLDB_LOG(log, "[PutFile] couldn't get md5 sum of destination: {0}",
1205+
ec.message());
12061206
} else {
1207-
auto local_md5 = llvm::sys::fs::md5_contents(source.GetPath());
1208-
if (!local_md5) {
1209-
LLDB_LOGF(log, "[PutFile] couldn't get md5 sum of source");
1207+
llvm::ErrorOr<llvm::MD5::MD5Result> local_md5 =
1208+
llvm::sys::fs::md5_contents(source.GetPath());
1209+
if (std::error_code ec = local_md5.getError()) {
1210+
LLDB_LOG(log, "[PutFile] couldn't get md5 sum of source: {0}",
1211+
ec.message());
12101212
} else {
1211-
const auto [local_md5_high, local_md5_low] = local_md5->words();
12121213
LLDB_LOGF(log, "[PutFile] destination md5: %016" PRIx64 "%016" PRIx64,
1213-
dest_md5_high, dest_md5_low);
1214+
remote_md5->high(), remote_md5->low());
12141215
LLDB_LOGF(log, "[PutFile] local md5: %016" PRIx64 "%016" PRIx64,
1215-
local_md5_high, local_md5_low);
1216-
requires_upload =
1217-
local_md5_high != dest_md5_high || local_md5_low != dest_md5_low;
1216+
local_md5->high(), local_md5->low());
1217+
requires_upload = *remote_md5 != *local_md5;
12181218
}
12191219
}
12201220

@@ -1339,15 +1339,11 @@ lldb_private::Status Platform::RunShellCommand(
13391339
return Status("unable to run a remote command without a platform");
13401340
}
13411341

1342-
bool Platform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
1343-
uint64_t &high) {
1342+
llvm::ErrorOr<llvm::MD5::MD5Result>
1343+
Platform::CalculateMD5(const FileSpec &file_spec) {
13441344
if (!IsHost())
1345-
return false;
1346-
auto Result = llvm::sys::fs::md5_contents(file_spec.GetPath());
1347-
if (!Result)
1348-
return false;
1349-
std::tie(high, low) = Result->words();
1350-
return true;
1345+
return std::make_error_code(std::errc::not_supported);
1346+
return llvm::sys::fs::md5_contents(file_spec.GetPath());
13511347
}
13521348

13531349
void Platform::SetLocalCacheDirectory(const char *local) {

lldb/source/Target/RemoteAwarePlatform.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ Status RemoteAwarePlatform::Unlink(const FileSpec &file_spec) {
266266
return Platform::Unlink(file_spec);
267267
}
268268

269-
bool RemoteAwarePlatform::CalculateMD5(const FileSpec &file_spec, uint64_t &low,
270-
uint64_t &high) {
269+
llvm::ErrorOr<llvm::MD5::MD5Result>
270+
RemoteAwarePlatform::CalculateMD5(const FileSpec &file_spec) {
271271
if (m_remote_platform_sp)
272-
return m_remote_platform_sp->CalculateMD5(file_spec, low, high);
273-
return Platform::CalculateMD5(file_spec, low, high);
272+
return m_remote_platform_sp->CalculateMD5(file_spec);
273+
return Platform::CalculateMD5(file_spec);
274274
}
275275

276276
FileSpec RemoteAwarePlatform::GetRemoteWorkingDirectory() {

0 commit comments

Comments
 (0)