Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions include/Config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "ArgumentParser/ArgumentManager.hpp"
#include "ArgumentParser/ArgumentParserFactory.hpp"
#include "ctrace_tools/strings.hpp"
#include "Process/Ipc/IpcStrategy.hpp"
#include "ctrace_defs/types.hpp"

static void printHelp(void)
{
Expand All @@ -29,6 +31,9 @@ static void printHelp(void)
--invoke <tools> Invokes specific tools (comma-separated).
Available tools: flawfinder, ikos, cppcheck, tscancode.
--input <files> Specifies the source files to analyse (comma-separated).
--ipc <method> Specifies the IPC method to use (e.g., fifo, socket).
--ipc-path <path> Specifies the IPC path (default: /tmp/coretrace_ipc).
--async Enables asynchronous execution.

Examples:
ctrace --input main.cpp,util.cpp --static --invoke=cppcheck,flawfinder
Expand Down Expand Up @@ -67,11 +72,13 @@ namespace ctrace
struct GlobalConfig
{
bool verbose = false; ///< Enables verbose output.
std:: launch hasAsync = std::launch::deferred; ///< Enables asynchronous execution.
std::launch hasAsync = std::launch::deferred; ///< Enables asynchronous execution.
bool hasSarifFormat = false; ///< Indicates if SARIF format is enabled.
bool hasStaticAnalysis = false; ///< Indicates if static analysis is enabled.
bool hasDynamicAnalysis = false; ///< Indicates if dynamic analysis is enabled.
bool hasInvokedSpecificTools = false; ///< Indicates if specific tools are invoked.
std::string ipc = ctrace_defs::IPC_TYPES.front(); ///< IPC method to use (e.g., fifo, socket).
std::string ipcPath = "/tmp/coretrace_ipc"; ///< Path for IPC communication.

std::vector<std::string> specificTools; ///< List of specific tools to invoke.

Expand Down Expand Up @@ -180,6 +187,25 @@ namespace ctrace
{
config.global.entry_points = value;
};
commands["--ipc"] = [this](const std::string& value)
{
auto ipc_list = ctrace_defs::IPC_TYPES;

if (std::find(ipc_list.begin(), ipc_list.end(), value) == ipc_list.end())
{
std::cerr << "Invalid IPC type: '" << value << "'\n"
<< "Available IPC types: ";
for (const auto& ipc : ipc_list)
std::cerr << ipc << " ";
std::cerr << std::endl;
std::exit(EXIT_FAILURE);
}
config.global.ipc = value;
};
commands["--ipc-path"] = [this](const std::string& value)
{
config.global.ipcPath = value;
};
// commands["--output"] = [this](const std::string& value) {
// if (!config.files.empty()) config.files.back().output_file = value;
// };
Expand Down Expand Up @@ -207,7 +233,7 @@ namespace ctrace
*
* @param argManager The argument manager containing parsed options.
*/
void process(ArgumentManager& argManager)
void process(ArgumentManager& argManager)
{
for (const auto& [option, command] : commands)
{
Expand Down
75 changes: 75 additions & 0 deletions include/Process/Ipc/IpcStrategy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef IPC_STRATEGY_HPP
#define IPC_STRATEGY_HPP

#include <regex>

#include "../Tools/IAnalysisTools.hpp"
#include "../ProcessFactory.hpp"
#include "../ThreadProcess.hpp"

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>

class IpcStrategy
{
public:
virtual ~IpcStrategy() = default;
virtual void write(const std::string& data) = 0;
virtual void close() = 0; // Pour cleanup RAII
};

namespace ctrace::ipc
{
class SocketError : public std::runtime_error
{
public:
explicit SocketError(const std::string& msg)
: std::runtime_error("[IPC::SocketError] " + msg) {}
};
}

class UnixSocketStrategy : public IpcStrategy
{
private:
int sock;
std::string path;
public:
UnixSocketStrategy(const std::string& socketPath) : path(socketPath), sock(-1) {
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
throw std::runtime_error("Error socket creation: " + std::string(strerror(errno)));
}
sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);
if (connect(sock, (sockaddr*)&addr, sizeof(addr)) == -1) {
throw std::runtime_error("Error socket connexion: " + std::string(strerror(errno)));
}
}
~UnixSocketStrategy() { close(); }
void write(const std::string& data) override
{
if (sock == -1)
{
throw ctrace::ipc::SocketError(std::string("Socket is not connected: ") + strerror(errno));
}
if (send(sock, data.c_str(), data.size(), 0) == -1)
{
throw ctrace::ipc::SocketError(std::string("Connection failed: ") + strerror(errno));
}
}
void close() override {
if (sock != -1) {
::close(sock);
sock = -1;
unlink(path.c_str()); // Optionnel
}
}
};

#endif // IPC_STRATEGY_HPP
33 changes: 22 additions & 11 deletions include/Process/Tools/AnalysisTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#include <regex>

#include "IAnalysisTools.hpp"
// #include "IAnalysisTools.hpp"
#include "AnalysisToolsBase.hpp"
#include "ctrace_tools/languageType.hpp"
#include "ctrace_tools/mangle.hpp"
#include "../ProcessFactory.hpp"
Expand All @@ -12,7 +13,8 @@

using json = nlohmann::json;

class EntryPoint {
class EntryPoint
{
public:
EntryPoint(const std::string& entryPointName, const std::vector<std::string>& paramTypes)
: name(entryPointName), paramTypes(paramTypes)
Expand Down Expand Up @@ -74,7 +76,7 @@ namespace ctrace
{

// Outils statiques
class IkosToolImplementation : public IAnalysisTool
class IkosToolImplementation : public AnalysisToolBase
{
public:
void execute(const std::string& file, ctrace::ProgramConfig config) const override
Expand Down Expand Up @@ -137,7 +139,7 @@ class IkosToolImplementation : public IAnalysisTool
}
};

class FlawfinderToolImplementation : public IAnalysisTool
class FlawfinderToolImplementation : public AnalysisToolBase
{
public:
void execute(const std::string& file, ctrace::ProgramConfig config) const override
Expand All @@ -161,9 +163,18 @@ class FlawfinderToolImplementation : public IAnalysisTool
argsProcess.push_back("--sarif");
}
argsProcess.push_back(src_file);
auto process = ProcessFactory::createProcess("python3", argsProcess); // ou "cmd.exe" pour Windows
auto process = ProcessFactory::createProcess("python3", argsProcess); // or "cmd.exe" for Windows
process->execute();
ctrace::Thread::Output::cout(process->logOutput);

if (config.global.ipc == "standardIO")
{
ctrace::Thread::Output::cout(process->logOutput);
}
else
{
ipc->write(process->logOutput);
}

} catch (const std::exception& e) {
ctrace::Thread::Output::cout("Error: " + std::string(e.what()));
return;
Expand All @@ -175,7 +186,7 @@ class FlawfinderToolImplementation : public IAnalysisTool
}
};

class TscancodeToolImplementation : public IAnalysisTool
class TscancodeToolImplementation : public AnalysisToolBase
{
public:
void execute(const std::string& file, ProgramConfig config) const override;
Expand All @@ -187,7 +198,7 @@ class TscancodeToolImplementation : public IAnalysisTool
};


class CppCheckToolImplementation : public IAnalysisTool
class CppCheckToolImplementation : public AnalysisToolBase
{
public:
void execute(const std::string& file, ctrace::ProgramConfig config) const override
Expand Down Expand Up @@ -222,7 +233,7 @@ class CppCheckToolImplementation : public IAnalysisTool
};

// Outils dynamiques
class DynTool1 : public IAnalysisTool
class DynTool1 : public AnalysisToolBase
{
public:
void execute(const std::string& file, ctrace::ProgramConfig config) const override
Expand All @@ -235,7 +246,7 @@ class DynTool1 : public IAnalysisTool
}
};

class DynTool2 : public IAnalysisTool
class DynTool2 : public AnalysisToolBase
{
public:
void execute(const std::string& file, ctrace::ProgramConfig config) const override
Expand All @@ -248,7 +259,7 @@ class DynTool2 : public IAnalysisTool
}
};

class DynTool3 : public IAnalysisTool
class DynTool3 : public AnalysisToolBase
{
public:
void execute(const std::string& file, ctrace::ProgramConfig config) const override
Expand Down
16 changes: 16 additions & 0 deletions include/Process/Tools/AnalysisToolsBase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "IAnalysisTools.hpp"

namespace ctrace
{

class AnalysisToolBase : public IAnalysisTool
{
protected:
std::shared_ptr<IpcStrategy> ipc;
public:
void setIpcStrategy(std::shared_ptr<IpcStrategy> strategy) override {
ipc = std::move(strategy);
}
};

}
9 changes: 9 additions & 0 deletions include/Process/Tools/IAnalysisTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <iostream>
#include "Config/config.hpp"
#include "../Ipc/IpcStrategy.hpp"

namespace ctrace {

Expand Down Expand Up @@ -39,6 +40,14 @@ namespace ctrace {
* @return A `std::string` representing the name of the tool.
*/
virtual std::string name() const = 0;

/**
* @brief Sets the IPC strategy for the analysis tool.
* This method allows the tool to communicate results or data
* through the specified IPC mechanism.
* @param ipc A shared pointer to an `IpcStrategy` instance.
*/
virtual void setIpcStrategy(std::shared_ptr<IpcStrategy> ipc) = 0;
};

}
Expand Down
26 changes: 20 additions & 6 deletions include/Process/Tools/ToolsInvoker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,24 @@ class ToolInvoker {
tools["dyn_tools_2"] = std::make_unique<DynTool2>();
tools["dyn_tools_3"] = std::make_unique<DynTool3>();


// Groupes prédéfinis
static_tools = {"cppcheck", "flawfinder", "tscancode", "ikos"};
dynamic_tools = {"dyn_tools_1", "dyn_tools_2", "dyn_tools_3"};

if (m_config.global.ipc == "standardIO")
{
m_ipc = nullptr; // Use std::cout directely
std::cout << "\033[36mUsing standardIO for IPC.\033[0m\n";
}
else
{
m_ipc = std::make_shared<UnixSocketStrategy>(m_config.global.ipcPath);

for (auto& [_, tool] : tools)
tool->setIpcStrategy(m_ipc);
}
}
// Exécute tous les outils statiques

// execute all static analysis tools
void runStaticTools(const std::string& file) const
{
std::vector<std::future<void>> results;
Expand All @@ -123,9 +134,11 @@ class ToolInvoker {
}
}

// Exécute tous les outils dynamiques
void runDynamicTools(const std::string& file) const {
for (const auto& tool_name : dynamic_tools) {
// execute all dynamic analysis tools
void runDynamicTools(const std::string& file) const
{
for (const auto& tool_name : dynamic_tools)
{
tools.at(tool_name)->execute(file, m_config);
}
}
Expand Down Expand Up @@ -153,6 +166,7 @@ class ToolInvoker {
ctrace::ProgramConfig m_config;
uint8_t m_nbThreadPool;
std::launch m_policy;
std::shared_ptr<IpcStrategy> m_ipc;
};
}

Expand Down
10 changes: 10 additions & 0 deletions include/ctrace_defs/types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef TYPES_HPP
#define TYPES_HPP

#include <vector>
#include <string>

namespace ctrace_defs
{
/**
Expand All @@ -14,6 +17,13 @@ namespace ctrace_defs
C = 0,
CPP = 1,
};

inline const std::vector<std::string> IPC_TYPES = {
"standardIO", // default
"socket",
// "pipe" // future implementation
};

}

#endif // TYPES_HPP
Loading