Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++20 modules #29

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ env:
REPO: hugsy/pwn--

on:
pull_request:
workflow_dispatch:
pull_request:
push:
branches:
- main
Expand Down Expand Up @@ -65,7 +65,6 @@ jobs:

- name: Prepare common environment
run: |
mkdir build
mkdir artifact

- name: Prepare Windows environment
Expand Down
31 changes: 17 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
# pwn++ is NOT a top dir (i.e. build as dependency)
option(PWN_BUILD_TOOLKIT "Compile the executables of pwn++ toolkit" OFF)
option(PWN_BUILD_TESTING "Compile the test suite" OFF)
option(PWN_BUILD_MODULE_CRYPTO "Compile the crypto module (requires m4)" OFF)
option(PWN_BUILD_DOCS "Generate the Doxygen API files" OFF)
else()
# pwn++ is a top dir
Expand All @@ -51,21 +52,21 @@ if(WIN32)
# Listed by dependency order
Common

Crypto
Network
Symbols
System
Security
Registry
Service
Shellcode
# Crypto
# Network
# Symbols
# System
# Security
# Registry
# Service
# Shellcode

FileSystem
Process
Remote
# FileSystem
# Process
# Remote

Binary
CTF
# Binary
# CTF

# TODO : ideas for future modules
# - WTS
Expand Down Expand Up @@ -97,7 +98,9 @@ else()
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION True)
endif()

find_package(MkCLib REQUIRED)
if(PWN_BUILD_MODULE_CRYPTO)
find_package(MkCLib REQUIRED)
endif(PWN_BUILD_MODULE_CRYPTO)

if(PWN_DISASSEMBLE_X86)
find_package(Zydis REQUIRED)
Expand Down
21 changes: 14 additions & 7 deletions Modules/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ set(HEADER_DIR ${SOURCE_DIR}/Include)

set(SOURCE_FILES

${SOURCE_DIR}/Architecture.cpp
${SOURCE_DIR}/Context.cpp
${SOURCE_DIR}/Error.cpp
${SOURCE_DIR}/Log.cpp
${SOURCE_DIR}/Utils.cpp
# ${SOURCE_DIR}/Architecture.cpp
# ${SOURCE_DIR}/Context.cpp
# ${SOURCE_DIR}/Error.cpp
# ${SOURCE_DIR}/Log.cpp
# ${SOURCE_DIR}/Utils.cpp
)

#
# Create and build the target static library
#
add_library(${PROJECT_NAME} STATIC)
add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES})
add_library(PWN::Common ALIAS ${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_FILES})
target_sources(${PROJECT_NAME}
PRIVATE
FILE_SET CXX_MODULES FILES

# ${INTERFACE_DIR}/Common.ixx
${SOURCE_DIR}/Common.cxx
)

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_23)
target_include_directories(${PROJECT_NAME} PUBLIC ${INTERFACE_DIR} PRIVATE ${HEADER_DIR})

Expand Down
25 changes: 25 additions & 0 deletions Modules/Common/Source/Common.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module;
#include <iostream>
export module pwn.common;

export using u8 = std::uint8_t;
export using u16 = std::uint16_t;
export using u32 = std::uint32_t;
export using u64 = std::uint64_t;
export using i8 = std::int8_t;
export using i16 = std::int16_t;
export using i32 = std::int32_t;
export using i64 = std::int64_t;
#ifdef _M_IX86
export using usize = unsigned long;
#else
export using usize = std::size_t;
#endif
export using ssize = std::intptr_t;
export using uptr = std::uintptr_t;

export void
test()
{
std::cout << "test modules\n";
}
6 changes: 4 additions & 2 deletions pwn++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(PWN_BUILD_OS ${CMAKE_SYSTEM_NAME})

set(PWN_MODULES_AS_STRING "")
foreach(MODULE ${PWN_MODULES})
set(PWN_MODULES_AS_STRING "L\"${MODULE}\",${PWN_MODULES_AS_STRING}")
set(PWN_MODULES_AS_STRING "\"${MODULE}\",${PWN_MODULES_AS_STRING}")
endforeach()

set(INTERFACE_DIR ${PROJECT_SOURCE_DIR}/Include)
Expand All @@ -30,10 +30,12 @@ endif()
message(STATUS "Generating pwn++ main header")
list(TRANSFORM PWN_MODULES PREPEND PWN:: OUTPUT_VARIABLE MODULE_NAMESPACES)
list(LENGTH MODULE_NAMESPACES PWN_MODULES_LENGTH)
configure_file(${PROJECT_SOURCE_DIR}/pwn.hpp.in ${INTERFACE_DIR}/pwn NEWLINE_STYLE WIN32)
# configure_file(${PROJECT_SOURCE_DIR}/pwn.hpp.in ${INTERFACE_DIR}/pwn NEWLINE_STYLE WIN32)
configure_file(${PROJECT_SOURCE_DIR}/pwn.hpp.in ${INTERFACE_DIR}/pwn.ixx NEWLINE_STYLE WIN32)

if(MSVC)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_DIR}/Win32/dllmain.cpp)
target_sources(${PROJECT_NAME} PRIVATE FILE_SET CXX_MODULES FILES ${INTERFACE_DIR}/pwn.ixx)

target_link_options(
${PROJECT_NAME}
Expand Down
163 changes: 163 additions & 0 deletions pwn++/Include/pwn.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
module;

/**
* Include non-module, locally only
*/
#include <array>
#include <string_view>

/**
* Export module `pwn`
*/
export module pwn;

/**
* Re-export submodules
*/
export import pwn.common;

/**
* Additional imports
*/
import std;

// // clang-format off
// #include "Common.hpp"
// #include "Architecture.hpp"
// #include "Log.hpp"
// #include "Literals.hpp"
// #include "Formatters.hpp"
// #include "Utils.hpp"
// #include "Handle.hpp"
// #include "Context.hpp"

// #include "Crypto.hpp"

// #ifdef PWN_INCLUDE_DISASSEMBLER
// #include "Disassembler.hpp"
// #endif // PWN_INCLUDE_DISASSEMBLER

// #if defined(PWN_BUILD_FOR_WINDOWS)
// #include "Win32/Network.hpp"
// #include "Win32/FileSystem.hpp"
// #include "Win32/System.hpp"
// #include "Win32/PE.hpp"
// #include "Win32/Network.hpp"
// #include "Win32/Job.hpp"
// #include "Win32/Process.hpp"
// #include "Win32/Thread.hpp"
// #include "Win32/Token.hpp"
// #include "Win32/ObjectManager.hpp"
// #include "Win32/System.hpp"
// #include "Win32/Service.hpp"
// #include "Win32/ALPC.hpp"
// #include "Win32/RPC.hpp"
// #include "Win32/API.hpp"
// #include "Win32/Symbols.hpp"
// #include "CTF/Win32/Remote.hpp"
// #include "CTF/Win32/Process.hpp"

// #elif defined(PWN_BUILD_FOR_LINUX)

// #include "CTF/Linux/Remote.hpp"
// #include "CTF/Linux/Process.hpp"

// #else

// #error "Unsupported OS"

// #endif // PWN_BUILD_FOR_WINDOWS
// clang-format on

namespace pwn
{
// clang-format off
///
///@brief
///
export constexpr std::string_view LibraryName = "pwn++";

///
///@brief
///
export constexpr std::string_view LibraryAuthor = "hugsy";

///
///@brief
///
export constexpr std::string_view LibraryLicense = "MIT";

///
///@brief
///
export constexpr std::string_view LibraryBanner = "pwn++" " v" "0.1.3" " - " "Standalone";
// clang-format on

///
///@brief pwn++ version information
///
constexpr struct VersionType
{
///
///@brief pwn++ major version
///
const u8 Major;

///
///@brief pwn++ minor version
///
const u8 Minor;

///
///@brief pwn++ patch information
///
const u16 Patch;

///
///@brief pwn++ release information
///
const std::string_view Release;

///
///@brief pwn++ complete version information as wstring
///
const std::string_view VersionString;
} Version = {
// clang-format off
0,
1,
3,
"Standalone",
"0.1.3",
// clang-format on
};

///
///@brief
///
constexpr struct HostInfo
{
///
///@brief The host architecture pwn++ was built against
///
const std::string_view Architecture;

///
///@brief The host OS pwn++ was built against
///
const std::string_view System;
} Host {
// clang-format off
"AMD64",
"Windows"
// clang-format on
};
// clang-format off

///
///@brief A list of all modules built with pwn++
///
export constexpr std::array<std::string_view, 1> ModuleNames = {"Common",};
// clang-format on

} // namespace pwn
17 changes: 11 additions & 6 deletions pwn++/Source/Win32/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#include <pwn>
#include <windows.h>
import pwn;
import std;

using namespace pwn;

// using namespace pwn;

void
OnAttachRoutine()
{
//
// Initialize the RNG
//
Utils::Random::Seed();
// Utils::Random::Seed();
std::println("loading library {}, {}", pwn::LibraryName, pwn::LibraryBanner);
test();
}


Expand All @@ -19,10 +24,10 @@ OnDetachRoutine()


BOOL APIENTRY
DllMain(_In_ HMODULE hModule, _In_ DWORD ul_reason_for_call, _In_ LPVOID lpReserved)
DllMain(_In_ HMODULE /* hModule */, _In_ DWORD ul_reason_for_call, _In_ LPVOID /* lpReserved */)
{
UnusedParameter(hModule);
UnusedParameter(lpReserved);
// UnusedParameter(hModule);
// UnusedParameter(lpReserved);

switch ( ul_reason_for_call )
{
Expand Down
Loading