C++ in Kernel Drivers (c++, boost, std)

Recently i was looking for another approaches how to use c++ features in kernel mode drivers. I found some references, but no one will fullfill my needs & desires to use also boost & std (at least partially).

Some time ago my friend show me a way how to add mentioned libraries to kernel code, so i decided to do it from scratch, do some minimalistic approach with some kind of ‘manual’ and PoC, and maybe it can be for someone, except myself, usefull.

 

  • Part I. – force c++ to cooperate

Firstly, it is no big deal to use c++ in kernel mode. For example, some of references that i was able to found :

What is needed, is well known, and it is to provide minimalistic c++ enviroment, including :

  • malloc
  • free
  • realloc
  • operator new
  • operator delete

it is easy doable :

another essential think, is to add implementation of calling ctors & dtors, at loading / unloading drivers (allow creating singletons, …) :

Ok, now it is good enough to use c++ in Kernel mode, and also usage of nice feature of std::unique_ptr. But when you attempt to use some boost::intrusive or std::shared_ptr you may encounter some probems! And this problemes are c++ E X C E P T I O N S , on code project resides very good article for reading about c++ exceptions vs kernel.

problem makers (vs 2013, c++ 11) :

  • _CxxThrowException
  • _wassert
  • __CxxFrameHandler3
  • some std friends (_Xbad_alloc, _Xlength_error, _Xout_of_range, _Syserror_map, _Winerror_map)

so for first two, to avoid redefinition, is dummy solution in .asm :

for others can it be done in similliar dummy way but in c++ :

But remember, those are really dumb implementations, wich allows you to use c++ and some of nice c++ libraries (like std, boost), but you probably want to implement this methods correctly

Alltogether you can find .lib source codes (projects, etc. … vs2013) on my github

  • Part II. – Set up enviroment to cooperate (Visual Studio 2013 & WDM driver)

Things what we need to do :

  1. link libc.lib to project
  2. add additional .lib to project (to cover f.e. functions like _hypot, .. )
  3. Set code generation, entry point, runtime  .. c++ and linker options mainly

so lets finalize it :

in libc.git project, i use $(DDK_LIB_PATH)\libcntpr.lib; for including some additional functions (_hypot and friends…). And in next version of vs can be needed also another functions, and for this you should firstly find in .lib in $(DDK_LIB_PATH) and try to link it to project, or after that if nothing was found implement it by yourself …

But important here, is to avoid linking usermode .dll into your kernel mode driver

I also pushed on github KernelProject PoC of usage of libc.git and new WDM driver for visual studio 2013 – (shared_ptr, unique_ptr, boost::intrusive::avltree). (i used also Common repo because of demonstrating on Vads, and usage of CCppDriver class as “main” of driver) – Windows 8.1 Release is setup-ed configuration – platform, so you can try to set-up Windows 7.1 release for sucessfully running also Vad test (because in undoc is setuped constans just for win7sp1 and win7, both x64)

 

Leave a comment

1 Comments.

  1. Well done, I tried doing this before. And wanted to implement a base library base on COM likes. Whant I recommend is how about all class inherit base class, which contain object reference. In future, we can implement ourself GC

Leave a Reply


[ Ctrl + Enter ]


Go To Top
Follow

Get every new post delivered to your Inbox

Join other followers: