Skip to content

Commit

Permalink
Various fixes in CFB::Driver (#26)
Browse files Browse the repository at this point in the history
- New constructors for `KUnicodeString`
- Better logging & overall logging structure
- Fixed a weird bug causing an endless loop when generic callback interceptor is called
- Only log kernel stuff at passive & apc (because of Rtl* functions dependencies)
- Add a `critical` level of logging
  • Loading branch information
hugsy authored Mar 23, 2024
1 parent c95be06 commit 792252f
Show file tree
Hide file tree
Showing 26 changed files with 2,744 additions and 2,553 deletions.
242 changes: 108 additions & 134 deletions Broker/Headers/ManagerBase.hpp
Original file line number Diff line number Diff line change
@@ -1,134 +1,108 @@
#pragma once

#include <wil/resource.h>

#include "Error.hpp"
#include "States.hpp"

#define xerr(fmt, ...) \
{ \
warn("[%s] " fmt, Name().c_str(), __VA_ARGS__); \
}

#define xwarn(fmt, ...) \
{ \
warn("[%s] " fmt, Name().c_str(), __VA_ARGS__); \
}

#define xok(fmt, ...) \
{ \
ok("[%s] " fmt, Name().c_str(), __VA_ARGS__); \
}

#define xinfo(fmt, ...) \
{ \
info("[%s] " fmt, Name().c_str(), __VA_ARGS__); \
}

#define xdbg(fmt, ...) \
{ \
dbg("[%s] " fmt, Name().c_str(), __VA_ARGS__); \
}


namespace CFB::Broker
{
class ManagerBase
{
public:
///
/// @brief Construct a new Manager Base object
///
///
ManagerBase();

///
/// @brief Destroy the Manager Base object
///
///
~ManagerBase();

///
/// @brief Synchronizes on the Global state semaphore to execute code only when in a specific state
///
/// @param NewState
/// @return true
/// @return false
///
bool
WaitForState(CFB::Broker::State WantedState);

///
/// @brief Simple wrapper of `Globals.NotifyNewState`
///
/// @param NewState
/// @return true
/// @return false
///
bool
SetState(CFB::Broker::State NewState);

///
/// @brief
///
/// @return true
/// @return false
///
bool
NotifyStateChange();

///
/// @brief
///
/// @return true
/// @return false
///
bool
NotifyTermination();

///
/// @brief
///
///
virtual Result<bool>
Setup() = 0;


///
/// @brief
///
///
virtual void
Run() = 0;

///
/// @brief
///
/// @return std::string const&
///
virtual std::string const
Name() = 0;


protected:
///
/// @brief
///
///
wil::unique_handle m_hChangedStateEvent;

///
/// @brief
///
///
wil::unique_handle m_hTerminationEvent;

///
/// @brief
///
///
bool m_bIsShuttingDown;
};


} // namespace CFB::Broker
#pragma once

#include <wil/resource.h>

#include "Error.hpp"
#include "States.hpp"

namespace CFB::Broker
{
class ManagerBase
{
public:
///
/// @brief Construct a new Manager Base object
///
///
ManagerBase();

///
/// @brief Destroy the Manager Base object
///
///
~ManagerBase();

///
/// @brief Synchronizes on the Global state semaphore to execute code only when in a specific state
///
/// @param NewState
/// @return true
/// @return false
///
bool
WaitForState(CFB::Broker::State WantedState);

///
/// @brief Simple wrapper of `Globals.NotifyNewState`
///
/// @param NewState
/// @return true
/// @return false
///
bool
SetState(CFB::Broker::State NewState);

///
/// @brief
///
/// @return true
/// @return false
///
bool
NotifyStateChange();

///
/// @brief
///
/// @return true
/// @return false
///
bool
NotifyTermination();

///
/// @brief
///
///
virtual Result<bool>
Setup() = 0;


///
/// @brief
///
///
virtual void
Run() = 0;

///
/// @brief
///
/// @return std::string const&
///
virtual std::string const
Name() = 0;


protected:
///
/// @brief
///
///
wil::unique_handle m_hChangedStateEvent;

///
/// @brief
///
///
wil::unique_handle m_hTerminationEvent;

///
/// @brief
///
///
bool m_bIsShuttingDown;
};


} // namespace CFB::Broker
16 changes: 8 additions & 8 deletions Broker/Source/ConnectorManager.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define CFB_NS "[CFB::ConnectorManager::CallbackDispatcher]"

// clang-format off
#include "ConnectorManager.hpp"

Expand All @@ -8,7 +10,6 @@
#include "Connectors/JsonQueue.hpp"
// clang-format on


namespace CFB::Broker
{

Expand All @@ -28,13 +29,12 @@ bool
CallbackDispatcher(CFB::Comms::CapturedIrp const& Irp)
{
const usize nbTotal = g_Connectors.size();
dbg("[ConnectorManager::CallbackDispatcher] Dispatching IRP @ %llu to %u connector%s",
dbg("[ConnectorManager::CallbackDispatcher] Dispatching IRP received at TS=%llu to %u connector%s",
Irp.Header.TimeStamp,
nbTotal,
PLURAL_IF(nbTotal > 1),
&g_Connectors);


usize nbSuccess = 0;

for ( auto& Connector : g_Connectors )
Expand Down Expand Up @@ -74,10 +74,10 @@ ConnectorManager::Setup()
//
// Register the callback dispatcher
//
xdbg("Register the connector dispatcher to the IRP manager");
dbg("Register the connector dispatcher to the IRP manager");
if ( Globals.IrpManager()->SetCallback(&CallbackDispatcher) )
{
xinfo("Connector dispatcher successfully registered");
info("Connector dispatcher successfully registered");

//
// Add new connector to that list
Expand All @@ -94,11 +94,11 @@ ConnectorManager::Setup()
g_Connectors.push_back(conn);
}

xdbg("%u connector%s registered to %p", g_Connectors.size(), PLURAL_IF(g_Connectors.size() > 1), &g_Connectors);
dbg("%u connector%s registered to %p", g_Connectors.size(), PLURAL_IF(g_Connectors.size() > 1), &g_Connectors);
}
else
{
xinfo("Failed to register the connector dispatcher");
info("Failed to register the connector dispatcher");
return Err(ErrorCode::InitializationError);
}

Expand Down Expand Up @@ -141,7 +141,7 @@ ConnectorManager::Run()
// Wait for termination event
//
::WaitForSingleObject(m_hTerminationEvent.get(), INFINITE);
xdbg("TerminationEvent received");
dbg("TerminationEvent received");

//
// Propagate the notification to the other managers
Expand Down
44 changes: 22 additions & 22 deletions Broker/Source/Connectors/Dummy.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define CFB_NS "[CFB::Broker::Connectors::Dummy]"

#include "Connectors/Dummy.hpp"

#include <iostream>
Expand All @@ -7,6 +9,7 @@

#define MAX_HEXDUMP_BYTES 256


namespace CFB::Broker::Connectors
{

Expand Down Expand Up @@ -44,29 +47,26 @@ Dummy::IrpCallback(CFB::Comms::CapturedIrp const& Irp)
info("%s", info.str().c_str());
}

std::ostringstream details;
details << "Details:" << std::endl;
details << " - Driver: " << CFB::Utils::ToString(Irp.Header.DriverName) << std::endl;
details << " - Device: " << CFB::Utils::ToString(Irp.Header.DeviceName) << std::endl;
details << " - Process: " << CFB::Utils::ToString(Irp.Header.ProcessName) << " (PID:" << Irp.Header.Pid
<< ", TID:" << Irp.Header.Tid << ")" << std::endl;
if ( Irp.Header.MajorFunction == 0xe || Irp.Header.MajorFunction == 0xf )
{
details << " - IOCTL code: " << CFB::Utils::ToString(CFB::Comms::Ioctl {Irp.Header.IoctlCode}) << std::endl;
}
details << std::hex;
details << " - Major: " << CFB::Utils::IrpMajorToString((u32)Irp.Header.MajorFunction) << std::endl;
details << " - Minor: " << (u32)Irp.Header.MinorFunction << std::endl;
details << " - InLen: " << Irp.Header.InputBufferLength << std::endl;
details << " - OutLen: " << Irp.Header.OutputBufferLength << std::endl;
details << " - Status: " << Irp.Header.Status << std::endl;
dbg("%s", details.str().c_str());
if ( Irp.Header.Status )
{
std::ostringstream details;
details << "Details:" << std::endl;
details << " - Driver: " << CFB::Utils::ToString(Irp.Header.DriverName) << std::endl;
details << " - Device: " << CFB::Utils::ToString(Irp.Header.DeviceName) << std::endl;
details << " - Process: " << CFB::Utils::ToString(Irp.Header.ProcessName) << " (PID:" << Irp.Header.Pid
<< ", TID:" << Irp.Header.Tid << ")" << std::endl;
if ( Irp.Header.MajorFunction == 0xe || Irp.Header.MajorFunction == 0xf )
{
details << " - IOCTL code: " << CFB::Utils::ToString(CFB::Comms::Ioctl {Irp.Header.IoctlCode})
<< std::endl;
}
details << std::hex;
details << " - Major: " << CFB::Utils::IrpMajorToString((u32)Irp.Header.MajorFunction) << std::endl;
details << " - Minor: " << (u32)Irp.Header.MinorFunction << std::endl;
details << " - InLen: " << Irp.Header.InputBufferLength << std::endl;
details << " - OutLen: " << Irp.Header.OutputBufferLength << std::endl;
details << " - Status: " << Irp.Header.Status << std::endl;
dbg("%s", details.str().c_str());
if ( Irp.Header.Status )
{
CFB::Log::ntperror(" - NTSTATUS", Irp.Header.Status);
}
CFB::Log::ntperror(" - NTSTATUS", Irp.Header.Status);
}

if ( Irp.Header.InputBufferLength )
Expand Down
Loading

0 comments on commit 792252f

Please sign in to comment.