Skip to content

Commit

Permalink
Show a warning dialog when the user tries to launch a file for the fi…
Browse files Browse the repository at this point in the history
…rst time. Fix Vector35#452
  • Loading branch information
xusheng6 committed Apr 11, 2023
1 parent d4df7e4 commit dfbcc29
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 14 deletions.
1 change: 1 addition & 0 deletions api/debuggerapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ namespace BinaryNinjaDebuggerAPI {
bool ActivateDebugAdapter();

std::string GetAddressInformation(uint64_t address);
bool IsFirstLaunch();

void PostDebuggerEvent(const DebuggerEvent& event);
};
Expand Down
6 changes: 6 additions & 0 deletions api/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,12 @@ std::string DebuggerController::GetAddressInformation(uint64_t address)
}


bool DebuggerController::IsFirstLaunch()
{
return BNDebuggerIsFirstLaunch(m_object);
}


void DebuggerController::PostDebuggerEvent(const DebuggerEvent &event)
{
BNDebuggerEvent* evt = new BNDebuggerEvent;
Expand Down
1 change: 1 addition & 0 deletions api/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ extern "C"
DEBUGGER_FFI_API bool BNDebuggerActivateDebugAdapter(BNDebuggerController* controller);

DEBUGGER_FFI_API char* BNDebuggerGetAddressInformation(BNDebuggerController* controller, uint64_t address);
DEBUGGER_FFI_API bool BNDebuggerIsFirstLaunch(BNDebuggerController* controller);

DEBUGGER_FFI_API void BNDebuggerPostDebuggerEvent(BNDebuggerController* controller, BNDebuggerEvent* event);

Expand Down
4 changes: 4 additions & 0 deletions api/python/debuggercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,10 @@ def set_adapter_property(self, name: str, value: binaryninja.metadata.MetadataVa
def get_addr_info(self, addr: int):
return dbgcore.BNDebuggerGetAddressInformation(self.handle, addr)

@property
def is_first_launch(self):
return dbgcore.BNDebuggerIsFirstLaunch(self.handle)

def __del__(self):
if dbgcore is not None:
dbgcore.BNDebuggerFreeController(self.handle)
Expand Down
9 changes: 9 additions & 0 deletions core/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ static void RegisterSettings()
"description" : "When enabled, this prevents the debugger from launching any file.",
"ignore" : ["SettingsProjectScope", "SettingsResourceScope"]
})");

settings->RegisterSetting("debugger.confirmFirstLaunch",
R"({
"title" : "Confirm on first launch",
"type" : "boolean",
"default" : true,
"description" : "Asks the user to confirm the operation when the target is launched for the first time.",
"ignore" : ["SettingsProjectScope", "SettingsResourceScope"]
})");
}

extern "C"
Expand Down
9 changes: 9 additions & 0 deletions core/debuggercontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ DebugStopReason DebuggerController::LaunchAndWaitInternal()
return InternalError;
}

if (m_firstLaunch)
m_firstLaunch = false;

DebuggerEvent event;
event.type = LaunchEventType;
PostDebuggerEvent(event);
Expand Down Expand Up @@ -2090,3 +2093,9 @@ std::string DebuggerController::GetAddressInformation(uint64_t address)

return "";
}


bool DebuggerController::IsFirstLaunch()
{
return m_firstLaunch;
}
4 changes: 4 additions & 0 deletions core/debuggercontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace BinaryNinjaDebugger {
bool m_inputFileLoaded = false;
bool m_initialBreakpointSeen = false;

bool m_firstLaunch = true;

void EventHandler(const DebuggerEvent& event);
void UpdateStackVariables();
void AddRegisterValuesToExpressionParser();
Expand Down Expand Up @@ -268,5 +270,7 @@ namespace BinaryNinjaDebugger {

// Dereference an address and check for printable strings, functions, symbols, etc
std::string GetAddressInformation(uint64_t address);

bool IsFirstLaunch();
};
}; // namespace BinaryNinjaDebugger
6 changes: 6 additions & 0 deletions core/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,12 @@ char* BNDebuggerGetAddressInformation(BNDebuggerController* controller, uint64_t
}


bool BNDebuggerIsFirstLaunch(BNDebuggerController* controller)
{
return controller->object->IsFirstLaunch();
}


void BNDebuggerPostDebuggerEvent(BNDebuggerController* controller, BNDebuggerEvent* event)
{
DebuggerEvent evt;
Expand Down
36 changes: 22 additions & 14 deletions ui/controlswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#include "adaptersettings.h"
#include <QPixmap>
#include <QInputDialog>
#include <QMessageBox>
#include "binaryninjaapi.h"
#include "disassemblyview.h"
#include "theme.h"
Expand Down Expand Up @@ -128,20 +129,27 @@ QString DebugControlsWidget::getToolTip(const QString& name)

void DebugControlsWidget::performLaunch()
{
// QString text = QString(
// "The debugger is %1 the target and preparing the debugger binary view. \n"
// "This might take a while.").arg("launching");
// ProgressTask* task =
// new ProgressTask(this, "Launching", text, "", [=](std::function<bool(size_t, size_t)> progress) {
// m_controller->Launch();
//
// // For now, this cant be canceled, as the Debugger model wasn't
// // designed with that in mind. This function below can return false if canceling is enabled
// progress(1, 1);
// return;
// });
// task->wait();
std::thread([&]() { m_controller->Launch(); }).detach();
if (m_controller->IsFirstLaunch() && Settings::Instance()->Get<bool>("debugger.confirmFirstLaunch"))
{
auto prompt = QString("You are about to launch \n\n%1\n\non your machine. "
"This may harm your machine. Are you sure to continue?").arg(QString::fromStdString(m_controller->GetExecutablePath()));
if (QMessageBox::question(this, "Launch Target", prompt) != QMessageBox::Yes)
return;
}

QString text = QString(
"The debugger is %1 the target and preparing the debugger binary view. \n"
"This might take a while.").arg("launching");
ProgressTask* task =
new ProgressTask(this, "Launching", text, "", [=](std::function<bool(size_t, size_t)> progress) {
m_controller->Launch();

// For now, this cant be canceled, as the Debugger model wasn't
// designed with that in mind. This function below can return false if canceling is enabled
progress(1, 1);
return;
});
task->wait();
}


Expand Down
8 changes: 8 additions & 0 deletions ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ void GlobalDebuggerUI::SetupMenu(UIContext* context)
auto controller = DebuggerController::GetController(ctxt.binaryView);
if (!controller)
return;
if (controller->IsFirstLaunch() && Settings::Instance()->Get<bool>("debugger.confirmFirstLaunch"))
{
auto prompt = QString("You are about to launch \n\n%1\n\non your machine. "
"This may harm your machine. Are you sure to continue?").
arg(QString::fromStdString(controller->GetExecutablePath()));
if (QMessageBox::question(ctxt.context->mainWindow(), "Launch Target", prompt) != QMessageBox::Yes)
return;
}
QString text = QString(
"The debugger is launching the target and preparing the debugger binary view. \n"
"This might take a while.");
Expand Down

0 comments on commit dfbcc29

Please sign in to comment.