summaryrefslogtreecommitdiff
path: root/view/kernelcache/ui/KernelCacheUINotifications.cpp
diff options
context:
space:
mode:
authorkat <kat@vector35.com>2025-07-06 15:05:01 -0400
committerkat <kat@vector35.com>2025-07-07 07:37:23 -0400
commit768f7c78465fb93936e5ca50a0ca712664fe54e7 (patch)
tree78c73e0022d6006882e365a257595a5b55a37432 /view/kernelcache/ui/KernelCacheUINotifications.cpp
parent8f3e251c42169fb4fe8db9403d900571434d88ff (diff)
KernelCache rewrite
Diffstat (limited to 'view/kernelcache/ui/KernelCacheUINotifications.cpp')
-rw-r--r--view/kernelcache/ui/KernelCacheUINotifications.cpp133
1 files changed, 77 insertions, 56 deletions
diff --git a/view/kernelcache/ui/KernelCacheUINotifications.cpp b/view/kernelcache/ui/KernelCacheUINotifications.cpp
index 6bd8fdf0..9d7e17a9 100644
--- a/view/kernelcache/ui/KernelCacheUINotifications.cpp
+++ b/view/kernelcache/ui/KernelCacheUINotifications.cpp
@@ -3,13 +3,15 @@
//
#include "KernelCacheUINotifications.h"
-#include <QLayout>
#include <kernelcacheapi.h>
#include "ui/sidebar.h"
#include "ui/linearview.h"
#include "ui/viewframe.h"
#include "progresstask.h"
+using namespace BinaryNinja;
+using namespace KernelCacheAPI;
+
UINotifications* UINotifications::m_instance = nullptr;
void UINotifications::init()
@@ -18,75 +20,94 @@ void UINotifications::init()
UIContext::registerNotification(m_instance);
}
-
void UINotifications::OnViewChange(UIContext* context, ViewFrame* frame, const QString& type)
{
if (!frame)
return;
- // FIXME there is a bv func for this
- static std::function<bool(Ref<BinaryView>, uint64_t)> isAddrMapped = [](Ref<BinaryView> view, uint64_t addr) {
- if (view && view->GetTypeName() == KC_VIEW_NAME)
+ auto view = frame->getCurrentBinaryView();
+ if (!view || view->GetTypeName() != KC_VIEW_NAME)
+ return;
+
+ auto viewInt = frame->getCurrentViewInterface();
+ if (!viewInt)
+ return;
+
+ auto ah = viewInt->actionHandler();
+ // Check to see if we have already bound these actions.
+ if (ah->isBoundAction("Load Image by Name"))
+ return;
+
+ static auto loadImageAtAddr = [](BinaryView& view, uint64_t addr) {
+ auto controller = KernelCacheController::GetController(view);
+ if (!controller)
+ return;
+ if (auto foundImage = controller->GetImageContaining(addr))
{
- for (const auto& seg : view->GetSegments())
- {
- if (seg->GetStart() <= addr && seg->GetEnd() > addr)
- return true;
- }
+ // If we did not load the image, then we don't need to run analysis.
+ if (!controller->ApplyImage(view, *foundImage))
+ return;
+ view.AddAnalysisOption("linearsweep");
+ view.UpdateAnalysis();
}
- return false;
};
- auto view = frame->getCurrentBinaryView();
- if (view && view->GetTypeName() == KC_VIEW_NAME)
- {
- if (auto viewInt = frame->getCurrentViewInterface())
+ auto loadImageAddrAction = [](const UIActionContext& ctx) {
+ uint64_t addr = 0;
+ if (GetAddressInput(addr, "Address", "Address"))
{
- auto ah = viewInt->actionHandler();
- if (!ah->isBoundAction("KC Load IMGHERE"))
- {
- ah->bindAction("KC Load IMGHERE",
- UIAction(
- [](const UIActionContext& ctx) {
- Ref<BinaryView> view = ctx.binaryView;
- Ref<KernelCacheAPI::KernelCache> cache = new KernelCacheAPI::KernelCache(view);
- uint64_t addr = ctx.token.token.value;
- if (addr)
- {
- BackgroundThread::create(ctx.context->mainWindow())->thenBackground(
- [cache=cache, addr=addr]() {
- cache->LoadImageContainingAddress(addr);
- })->start();
- }
- },
- [](const UIActionContext& ctx) {
- Ref<KernelCacheAPI::KernelCache> cache = new KernelCacheAPI::KernelCache(ctx.binaryView);
- uint64_t addr = ctx.token.token.value;
- if (isAddrMapped(ctx.binaryView, addr))
- return false;
- return addr && cache->GetImageNameForAddress(addr) != ""; // bool
- }));
- ah->setActionDisplayName("KC Load IMGHERE", [](const UIActionContext& ctx) {
- Ref<KernelCacheAPI::KernelCache> cache = new KernelCacheAPI::KernelCache(ctx.binaryView);
- uint64_t addr = ctx.token.token.value;
- if (addr)
- return QString("Load ") + cache->GetImageNameForAddress(addr).c_str();
- return QString("Error");
- });
- if (auto linearView = qobject_cast<LinearView*>(viewInt->widget()))
- {
- linearView->contextMenu().addAction("KC Load IMGHERE", KC_VIEW_NAME);
- linearView->contextMenu().setGroupOrdering(KC_VIEW_NAME, 0);
- }
- }
+ BackgroundThread::create(ctx.context->mainWindow())
+ ->thenBackground([ctx, addr]() {
+ loadImageAtAddr(*ctx.binaryView, addr);
+ })->start();
}
+ };
+
+ auto loadImageTokenAction = [](const UIActionContext& ctx) {
+ BackgroundThread::create(ctx.context->mainWindow())
+ ->thenBackground([ctx](){ loadImageAtAddr(*ctx.binaryView, ctx.token.token.value); })
+ ->start();
+ };
+
+ auto isValidUnloadedImageAction = [](const UIActionContext& ctx) {
+ uint64_t addr = ctx.token.token.value;
+ // Check if the image is already loaded in the view.
+ if (!ctx.binaryView->GetSectionsAt(addr).empty())
+ return false;
+ auto controller = KernelCacheController::GetController(*ctx.binaryView);
+ if (!controller)
+ return false;
+ return controller->GetImageContaining(addr).has_value();
+ };
+
+ ah->bindAction("Load Image by Address", UIAction(loadImageAddrAction));
+
+ ah->bindAction("Load IMGHERE", UIAction(loadImageTokenAction, isValidUnloadedImageAction));
+
+ ah->setActionDisplayName("Load IMGHERE", [](const UIActionContext& ctx) {
+ auto controller = KernelCacheController::GetController(*ctx.binaryView);
+ if (!controller)
+ return QString("NO CONTROLLER");
+ uint64_t addr = ctx.token.token.value;
+ auto image = controller->GetImageContaining(addr);
+ if (!image)
+ return QString("NO IMAGE");
+ return QString("Load ") + image->name.c_str();
+ });
+
+ // Finally add the actions to the context menu.
+ if (auto linearView = qobject_cast<LinearView*>(viewInt->widget()))
+ {
+ constexpr auto groupOneName = KC_VIEW_NAME;
+ constexpr auto groupTwoName = KC_VIEW_NAME "2";
+ linearView->contextMenu().addAction("Load IMGHERE", groupOneName);
+ linearView->contextMenu().addAction("Load Image by Address", groupTwoName);
+ linearView->contextMenu().setGroupOrdering(groupOneName, 0);
+ linearView->contextMenu().setGroupOrdering(groupTwoName, 1);
}
}
+
void UINotifications::OnAfterOpenFile(UIContext* context, FileContext* file, ViewFrame* frame)
{
- if (frame->getCurrentBinaryView())
- {
- // Register BD notifications. We dont use them right now.
- }
UIContextNotification::OnAfterOpenFile(context, file, frame);
}