ntoskrnl7 / ucxxrt

The Universal C++ RunTime library, supporting kernel-mode C++ exception-handler and STL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Universal C++ RunTime (UCXXRT)

Actions Status LICENSE LICENSE Windows Visual Studio

1. About

ucxxrt is a open source rutime library which based on MSVC.The highlight of this project is that it can be used in kernel-mode drivers.
It gives you the same experience as user-mode application development in C++ when developing kernel-mode drivers.

Before ucxxrt was born,in order to use C++ on kernel-mode drivers, I use (KTL、ustd、...).

But there are several problems,like it isn't support C++ exception and it cost much time on implementing new features which provided by the latest ISO,then ucxxrt was born.

1.1 Principle

  • In kernel-mode driver mode,forced disable kernel-mode flag by using property sheet ,it makes the compiler support C++ exceptions.
  • Implement the exception functions such as throwcatch. Simulated the exception dispatcher in throw.

1.2 Features

Kernel-mode:

  • support new/delete operators.
  • support C++ exception (/EHsc).
  • support SAFESEH、GS (Buffer Security Check).
  • support STL (not fully).
    • Thread Local Storage (TLS): thread_local、TlsAlloc ...
    • std::thread
    • std::filesystem
    • std::chrono
    • std::stacktrace_entry
    • std::locale
    • std::stream (std::fstream、std::iostream、std::cin、std::cout、std::cerr)
    • std::mutex
    • std::shared_mutex
    • std::packaged_task
    • std::promise
    • std::future
    • std::condition_variable
    • std::latch
    • std::semaphore (std::counting_semaphore、std::binary_semaphore)
    • ...
  • support static objects.

1.3 Example

See project unittest for more Infomation.

void Test$ThrowUnknow()
{
    try
    {
        try
        {
            try
            {
                throw std::wstring();
            }
            catch (int& e)
            {
                ASSERT(false);
                LOG(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "Catch Exception: %d\n", e);
            }
        }
        catch (std::string& e)
        {
            ASSERT(false);
            LOG(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "Catch Exception: %s\n", e.c_str());
        }
    }
    catch (...)
    {
        LOG(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, "Catch Exception: ...\n");
    }
}

void Test$HashMap()
{
    auto Rand = std::mt19937_64(::rand());
    auto Map = std::unordered_map<uint32_t, std::string>();
    for (auto i = 0u; i < 10; ++i)
    {
        Map[i] = std::to_string(Rand());
    }

    for (const auto& Item : Map)
    {
        LOG(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL,
            "map[%ld] = %s\n", Item.first, Item.second.c_str());
    }
}

void Test$Thread() {
    std::mutex mtx;
    std::condition_variable cv;
    std::thread t([&mtx, &cv]() {
        cv.notify_all();
        std::unique_lock<std::mutex> lk(mtx);
        });
    {
        std::unique_lock<std::mutex> lk(mtx);
        cv.wait(lk);
    }
    if (t.joinable())
        t.join();

    auto tid = std::this_thread::get_id();
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

2. Compile

IDE:Visual Studio 2017 or higher

  • git clone --recursive https://github.com/ntoskrnl7/ucxxrt.git
  • Open ucxxrt.sln and compile

3. How to use

  1. Copy ucxxrt/ucxxrt forder to you project directory.
  2. Add the property sheet ucxxrt.props to yor project.

!! kernel-mode:Rename DriverEntry to DriverMain

usage

4. Reference and Acknowledgement

Thanks to these excellent projects for help me on developing ucxxrt.

About

The Universal C++ RunTime library, supporting kernel-mode C++ exception-handler and STL.

License:MIT License


Languages

Language:C++ 87.3%Language:Assembly 8.2%Language:C 4.4%Language:Batchfile 0.1%