Skip to content

Commit b313411

Browse files
committed
Fix crash in uncaught exceptions thrown by the code. Fix #998
1 parent b8b213a commit b313411

3 files changed

Lines changed: 119 additions & 39 deletions

File tree

core/adapters/corelliumadapter.cpp

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ bool CorelliumAdapter::LoadRegisterInfo()
136136
}
137137

138138
if (architecture.empty())
139-
throw std::runtime_error("failed to find architecture");
139+
{
140+
LogWarn("failed to find architecture");
141+
return false;
142+
}
140143

141144
// Store the original architecture for endianness detection before stripping the prefix
142145
std::string fullArchitecture = architecture;
@@ -336,8 +339,10 @@ std::vector<DebugThread> CorelliumAdapter::GetThreadList()
336339

337340
auto reply = this->m_rspConnector->TransmitAndReceive(RspData("qfThreadInfo"));
338341
while(reply.m_data[0] != 'l') {
339-
if (reply.m_data[0] != 'm')
340-
throw std::runtime_error("thread list failed?");
342+
if (reply.m_data[0] != 'm') {
343+
LogWarn("thread list failed");
344+
return threads;
345+
}
341346

342347
const auto shortened_string =
343348
reply.AsString().substr(1);
@@ -376,13 +381,22 @@ bool CorelliumAdapter::SetActiveThreadId(std::uint32_t tid)
376381
return false;
377382

378383
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("T{:x}"), tid)).AsString() != "OK" )
379-
throw std::runtime_error("thread does not exist!");
384+
{
385+
LogWarn("thread does not exist");
386+
return false;
387+
}
380388

381389
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("Hc{:x}"), tid)).AsString() != "OK")
382-
throw std::runtime_error("failed to set thread");
390+
{
391+
LogWarn("failed to set thread");
392+
return false;
393+
}
383394

384395
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("Hg{:x}"), tid)).AsString() != "OK")
385-
throw std::runtime_error("failed to set thread");
396+
{
397+
LogWarn("failed to set thread");
398+
return false;
399+
}
386400

387401
this->m_lastActiveThreadId = tid;
388402

@@ -430,7 +444,10 @@ bool CorelliumAdapter::RemoveBreakpoint(const DebugBreakpoint& breakpoint)
430444
kind = 4;
431445

432446
if (this->m_rspConnector->TransmitAndReceive(RspData("z0,{:x},{}", breakpoint.m_address, kind)).AsString() != "OK" )
433-
throw std::runtime_error("rsp reply failure on remove breakpoint");
447+
{
448+
LogWarn("rsp reply failure on remove breakpoint");
449+
return false;
450+
}
434451

435452
if (auto location = std::find(this->m_debugBreakpoints.begin(), this->m_debugBreakpoints.end(), breakpoint);
436453
location != this->m_debugBreakpoints.end())
@@ -512,7 +529,10 @@ std::unordered_map<std::string, DebugRegister> CorelliumAdapter::ReadAllRegister
512529
return m_regCache.value();
513530

514531
if ( this->m_registerInfo.empty() )
515-
throw std::runtime_error("register info empty");
532+
{
533+
LogWarn("register info empty");
534+
return {};
535+
}
516536

517537
// Sort the registers according to their index, as the g reply packet will provide values in the same order
518538
std::vector<register_pair> register_info_vec{};
@@ -528,7 +548,10 @@ std::unordered_map<std::string, DebugRegister> CorelliumAdapter::ReadAllRegister
528548
const auto register_info_reply = this->m_rspConnector->TransmitAndReceive(RspData(&request, sizeof(request)));
529549
auto register_info_reply_string = register_info_reply.AsString();
530550
if ( register_info_reply_string.empty() )
531-
throw std::runtime_error("register request reply empty");
551+
{
552+
LogWarn("register request reply empty");
553+
return {};
554+
}
532555

533556
std::unordered_map<std::string, DebugRegister> all_regs{};
534557
for ( const auto& [register_name, register_info] : register_info_vec ) {
@@ -552,7 +575,10 @@ DebugRegister CorelliumAdapter::ReadRegister(const std::string& reg)
552575
return DebugRegister{};
553576

554577
if ( this->m_registerInfo.find(reg) == this->m_registerInfo.end() )
555-
throw std::runtime_error(fmt::format("register {} does not exist in target", reg));
578+
{
579+
LogWarn("register %s does not exist in target", reg.c_str());
580+
return DebugRegister{};
581+
}
556582

557583
return this->ReadAllRegisters()[reg];
558584
}
@@ -651,7 +677,7 @@ DataBuffer CorelliumAdapter::ReadMemory(std::uintptr_t address, std::size_t size
651677
return input - 'A' + 10;
652678
if(input >= 'a' && input <= 'f')
653679
return input - 'a' + 10;
654-
throw std::invalid_argument("Invalid input string");
680+
return 0;
655681
};
656682

657683
while(*src && src[1]) {
@@ -697,7 +723,10 @@ std::string CorelliumAdapter::GetRemoteFile(const std::string& path)
697723
int32_t error;
698724
int32_t ret = this->m_rspConnector->HostFileIO(RspData("vFile:setfs:0"), output, error);
699725
if (ret < 0)
700-
throw runtime_error("Could not set remote filesystem");
726+
{
727+
LogWarn("Could not set remote filesystem");
728+
return "";
729+
}
701730

702731
std::string path_hex_string{};
703732
for ( const auto& ch : path )
@@ -706,7 +735,10 @@ std::string CorelliumAdapter::GetRemoteFile(const std::string& path)
706735
ret = this->m_rspConnector->HostFileIO(
707736
RspData("vFile:open:{},{:X},{:X}", path_hex_string.c_str(), 0, 0), output, error);
708737
if (ret < 0)
709-
throw runtime_error("Unable to open file with host I/O");
738+
{
739+
LogWarn("Unable to open file with host I/O");
740+
return "";
741+
}
710742

711743
int32_t fd = ret;
712744

@@ -719,21 +751,27 @@ std::string CorelliumAdapter::GetRemoteFile(const std::string& path)
719751
ret = this->m_rspConnector->HostFileIO(
720752
RspData("vFile:pread:{:X},{:X},{:X}", fd, blockSize, offset), output, error);
721753
if (ret < 0)
722-
throw runtime_error(fmt::format("host i/o pread() failed, result=%d, errno=%d", ret, error));
754+
{
755+
LogWarn("host i/o pread() failed, result=%d, errno=%d", ret, error);
756+
return data;
757+
}
723758
if (ret == 0)
724759
// EOF
725760
break;
726761
if (ret != (int32_t)output.AsString().length())
727-
throw runtime_error(fmt::format("host i/o pread() returned {:X} but decoded binary attachment is size {:X}",
728-
ret, output.AsString().length()));
762+
{
763+
LogWarn("host i/o pread() returned %X but decoded binary attachment is size %zX",
764+
ret, output.AsString().length());
765+
return data;
766+
}
729767

730768
data += output.AsString();
731769
offset += output.AsString().length();
732770
}
733771

734772
ret = this->m_rspConnector->HostFileIO(RspData(fmt::format("vFile:close:{:X}", fd)), output, error);
735773
if (ret)
736-
throw runtime_error(fmt::format("host i/o close() failed, result={}, errno={}", ret, error));
774+
LogWarn("host i/o close() failed, result=%d, errno=%d", ret, error);
737775

738776
return data;
739777
}
@@ -852,7 +890,7 @@ DebugStopReason CorelliumAdapter::ResponseHandler()
852890
return input - 'A' + 10;
853891
if(input >= 'a' && input <= 'f')
854892
return input - 'a' + 10;
855-
throw std::invalid_argument("Invalid input string");
893+
return 0;
856894
};
857895

858896
while(*src && src[1]) {
@@ -1240,7 +1278,7 @@ void CorelliumAdapter::HandleAsyncPacket(const RspData& data)
12401278
return input - 'A' + 10;
12411279
if(input >= 'a' && input <= 'f')
12421280
return input - 'a' + 10;
1243-
throw std::invalid_argument("Invalid input string");
1281+
return 0;
12441282
};
12451283

12461284
while(*src && src[1]) {

core/adapters/esrevenadapter.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ bool EsrevenAdapter::LoadRegisterInfo()
160160
}
161161

162162
if (architecture.empty())
163-
throw std::runtime_error("failed to find architecture");
163+
{
164+
LogWarn("failed to find architecture");
165+
return false;
166+
}
164167

165168
// Store the original architecture for endianness detection before stripping the prefix
166169
std::string fullArchitecture = architecture;
@@ -409,13 +412,22 @@ bool EsrevenAdapter::SetActiveThreadId(std::uint32_t tid)
409412
return false;
410413

411414
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("T{:x}"), tid)).AsString() != "OK" )
412-
throw std::runtime_error("thread does not exist!");
415+
{
416+
LogWarn("thread does not exist");
417+
return false;
418+
}
413419

414420
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("Hc{:x}"), tid)).AsString() != "OK")
415-
throw std::runtime_error("failed to set thread");
421+
{
422+
LogWarn("failed to set thread");
423+
return false;
424+
}
416425

417426
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("Hg{:x}"), tid)).AsString() != "OK")
418-
throw std::runtime_error("failed to set thread");
427+
{
428+
LogWarn("failed to set thread");
429+
return false;
430+
}
419431

420432
this->m_lastActiveThreadId = tid;
421433

@@ -551,7 +563,10 @@ std::unordered_map<std::string, DebugRegister> EsrevenAdapter::ReadAllRegisters(
551563
return m_regCache.value();
552564

553565
if ( this->m_registerInfo.empty() )
554-
throw std::runtime_error("register info empty");
566+
{
567+
LogWarn("register info empty");
568+
return {};
569+
}
555570

556571
// Sort the registers according to their index, as the g reply packet will provide values in the same order
557572
std::vector<register_pair> register_info_vec{};
@@ -567,7 +582,10 @@ std::unordered_map<std::string, DebugRegister> EsrevenAdapter::ReadAllRegisters(
567582
const auto register_info_reply = this->m_rspConnector->TransmitAndReceive(RspData(&request, sizeof(request)));
568583
auto register_info_reply_string = register_info_reply.AsString();
569584
if ( register_info_reply_string.empty() )
570-
throw std::runtime_error("register request reply empty");
585+
{
586+
LogWarn("register request reply empty");
587+
return {};
588+
}
571589

572590
std::unordered_map<std::string, DebugRegister> all_regs{};
573591
for ( const auto& [register_name, register_info] : register_info_vec ) {
@@ -591,7 +609,10 @@ DebugRegister EsrevenAdapter::ReadRegister(const std::string& reg)
591609
return DebugRegister{};
592610

593611
if ( this->m_registerInfo.find(reg) == this->m_registerInfo.end() )
594-
throw std::runtime_error(fmt::format("register {} does not exist in target", reg));
612+
{
613+
LogWarn("register %s does not exist in target", reg.c_str());
614+
return DebugRegister{};
615+
}
595616

596617
return this->ReadAllRegisters()[reg];
597618
}
@@ -692,7 +713,7 @@ DataBuffer EsrevenAdapter::ReadMemory(std::uintptr_t address, std::size_t size)
692713
return input - 'A' + 10;
693714
if(input >= 'a' && input <= 'f')
694715
return input - 'a' + 10;
695-
throw std::invalid_argument("Invalid input string");
716+
return 0;
696717
};
697718

698719
while(*src && src[1]) {
@@ -1143,7 +1164,7 @@ DebugStopReason EsrevenAdapter::ResponseHandler(bool notifyStopped)
11431164
return input - 'A' + 10;
11441165
if(input >= 'a' && input <= 'f')
11451166
return input - 'a' + 10;
1146-
throw std::invalid_argument("Invalid input string");
1167+
return 0;
11471168
};
11481169

11491170
while(*src && src[1]) {
@@ -1743,7 +1764,7 @@ void EsrevenAdapter::HandleAsyncPacket(const RspData& data)
17431764
return input - 'A' + 10;
17441765
if(input >= 'a' && input <= 'f')
17451766
return input - 'a' + 10;
1746-
throw std::invalid_argument("Invalid input string");
1767+
return 0;
17471768
};
17481769

17491770
while(*src && src[1]) {

core/adapters/gdbadapter.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ bool GdbAdapter::LoadRegisterInfo()
160160
}
161161

162162
if (architecture.empty())
163-
throw std::runtime_error("failed to find architecture");
163+
{
164+
LogWarn("failed to find architecture");
165+
return false;
166+
}
164167

165168
// Store the original architecture for endianness detection before stripping the prefix
166169
std::string fullArchitecture = architecture;
@@ -407,13 +410,22 @@ bool GdbAdapter::SetActiveThreadId(std::uint32_t tid)
407410
return false;
408411

409412
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("T{:x}"), tid)).AsString() != "OK" )
410-
throw std::runtime_error("thread does not exist!");
413+
{
414+
LogWarn("thread does not exist");
415+
return false;
416+
}
411417

412418
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("Hc{:x}"), tid)).AsString() != "OK")
413-
throw std::runtime_error("failed to set thread");
419+
{
420+
LogWarn("failed to set thread");
421+
return false;
422+
}
414423

415424
if ( this->m_rspConnector->TransmitAndReceive(RspData(string("Hg{:x}"), tid)).AsString() != "OK")
416-
throw std::runtime_error("failed to set thread");
425+
{
426+
LogWarn("failed to set thread");
427+
return false;
428+
}
417429

418430
this->m_lastActiveThreadId = tid;
419431

@@ -549,7 +561,10 @@ std::unordered_map<std::string, DebugRegister> GdbAdapter::ReadAllRegisters()
549561
return m_regCache.value();
550562

551563
if ( this->m_registerInfo.empty() )
552-
throw std::runtime_error("register info empty");
564+
{
565+
LogWarn("register info empty");
566+
return {};
567+
}
553568

554569
// Sort the registers according to their index, as the g reply packet will provide values in the same order
555570
std::vector<register_pair> register_info_vec{};
@@ -565,7 +580,10 @@ std::unordered_map<std::string, DebugRegister> GdbAdapter::ReadAllRegisters()
565580
const auto register_info_reply = this->m_rspConnector->TransmitAndReceive(RspData(&request, sizeof(request)));
566581
auto register_info_reply_string = register_info_reply.AsString();
567582
if ( register_info_reply_string.empty() )
568-
throw std::runtime_error("register request reply empty");
583+
{
584+
LogWarn("register request reply empty");
585+
return {};
586+
}
569587

570588
std::unordered_map<std::string, DebugRegister> all_regs{};
571589
for ( const auto& [register_name, register_info] : register_info_vec ) {
@@ -589,7 +607,10 @@ DebugRegister GdbAdapter::ReadRegister(const std::string& reg)
589607
return DebugRegister{};
590608

591609
if ( this->m_registerInfo.find(reg) == this->m_registerInfo.end() )
592-
throw std::runtime_error(fmt::format("register {} does not exist in target", reg));
610+
{
611+
LogWarn("register %s does not exist in target", reg.c_str());
612+
return DebugRegister{};
613+
}
593614

594615
return this->ReadAllRegisters()[reg];
595616
}
@@ -689,7 +710,7 @@ DataBuffer GdbAdapter::ReadMemory(std::uintptr_t address, std::size_t size)
689710
return input - 'A' + 10;
690711
if(input >= 'a' && input <= 'f')
691712
return input - 'a' + 10;
692-
throw std::invalid_argument("Invalid input string");
713+
return 0;
693714
};
694715

695716
while(*src && src[1]) {
@@ -997,7 +1018,7 @@ DebugStopReason GdbAdapter::ResponseHandler(bool notifyStopped)
9971018
return input - 'A' + 10;
9981019
if(input >= 'a' && input <= 'f')
9991020
return input - 'a' + 10;
1000-
throw std::invalid_argument("Invalid input string");
1021+
return 0;
10011022
};
10021023

10031024
while(*src && src[1]) {
@@ -1542,7 +1563,7 @@ void GdbAdapter::HandleAsyncPacket(const RspData& data)
15421563
return input - 'A' + 10;
15431564
if(input >= 'a' && input <= 'f')
15441565
return input - 'a' + 10;
1545-
throw std::invalid_argument("Invalid input string");
1566+
return 0;
15461567
};
15471568

15481569
while(*src && src[1]) {

0 commit comments

Comments
 (0)