All good stuff here, mind if I take us on an academic exercise? The author
wanted to know if class design was possible in the kernel. Keeping things
very basic here, is this an acceptable usage of class design in the kernel?
What other operator new / delete (I know I missed new and delete)
prototypes should be implemented, and are these correct? Should or can we
leverage the crt/new.h? I’ve also yet to see a microsoft reference driver
example in their github that uses c++ classes. I might only want to use
classes to improve doxygen documentation 
#include <ntddk.h>
void * __cdecl operator new(size_t size) {
return ExAllocatePoolWithTag(NonPagedPool, size, ‘sslC’);
}
void__cdecl operator delete(void * object) {
ExFreePoolWithTag(object, ‘sslC’);
}
// placement new
void * __cdecl operator new(size_t size, void * place) {
return place;
}
// placement delete
void__cdecl operator delete(void *, void *) {
return;
}
#ifdef _KERNEL_MODE
#define NONPAGESECTION __declspec(code_seg(“$kerneltext$”))
#else
#define NONPAGESECTION
#endif
class NONPAGESECTION TestClass {
public:
explicit TestClass() { };
~TestClass() {};
NTSTATUS Setup(
In PDRIVER_OBJECT pDriverObject,
In PUNICODE_STRING pUniRegistryPath)
{
UNREFERENCED_PARAMETER(pDriverObject);
UNREFERENCED_PARAMETER(pUniRegistryPath);
return STATUS_SUCCESS;
}
VOID Teardown() { };
private:
// member objects here, paged or nonpaged, can we page anything?
}
NTSTATUS DriverEntry(
In PDRIVER_OBJECT pDriverObject,
In PUNICODE_STRING pUniRegistryPath)
{
auto testClass = new TestClass();
if (!testClass) {
return STATUS_INSUFFICIENT_RESOURCES;
}
auto ntStatus = testClass->Setup();
if (NT_SUCCESS(ntStatus)) {
testClass->Teardown();
delete testClass;
}
return ntStatus;
}
On Wed, Oct 12, 2016 at 4:30 PM, Jan Bottorff
wrote:
> On 10/12/16, 9:45 AM, “xxxxx@lists.osr.com on behalf of
> Robert Ammerman” > xxxxx@ramsystems.biz> wrote:
>
> Does RTTI work!?!
> -----------------------------------------------------
>
> Things link dynamic_cast do not work, which is one of the uses of RTTI, as
> if the type is wrong you can’t just throw an exception.
>
> You can EASILY add you own class metadata, like with virtual member
> functions for things like asString.
>
> I think the hardest thing for people who use C++ in user mode, is NONE of
> the typical C++ class libraries are usable in kernel mode, you will have to
> roll you own or find C++ code that’s explicitly designed for kernel use.
>
> I’ve written a ton of C++ kernel driver code over the years, and mostly I
> no longer do so. The primary reason is many kernel developers are not
> comfortable using C++. I got tired of having to convince team members and
> managers over and over, that C++ had lots of valuable capabilities. My
> experience also is the value of C++ is diminished if you only use a little,
> and it works best if almost everything in a driver is C++. Since there are
> no standard C++ kernel compatible libraries, you tend to have to do upfront
> framework development, before you start using the framework for your
> driver. Writing a framework + a single specific driver is not more
> production that just writing the single specific driver. On the other hand,
> if you had multiple complex driver products, taking the time to create the
> framework would likely give big gains in efficiently for the sum of the
> projects.
>
> Another factor making kernel C++ hard is Linux. Often a driver will be
> needed on multiple platforms, and C++ might have advantages on Windows, but
> getting the Linux kernel team to accept your C++ containing driver into the
> mainline tree is difficult (impossible?).
>
> I personally think C++ can improve the quality of kernel code. The way the
> compiler can optimize and inline inherited code is just vastly better than
> what you can do simulating OOP in C with function pointers. You can, using
> C++ syntax, teach the compiler a lot more about the semantic meaning of
> things, and the compiler than then automatically generate the correct to
> enforce that semantic meaning. For example, using scoping and
> constructors/destructors to create automatic reference counting. Once you
> taught the compile that some blob of bits is more than just an scalar
> value, but is a reference to something else, the compiler can do operations
> to make sure those references are always correct. That same reference
> counting class also can have a debug version which keeps a list of all
> references, which is incredibly useful during debugging. Another example,
> the kernel spinlock concept is easily turned into a class that acquires a
> spinlock on scope entry and releases it on scope exit.
>
> A lot of people are critical of C++ because you can do things like
> override the assignment operator, but doing so is critical in teaching the
> compiler that an assignment is not just a memory copy, but is a change in
> the reference count to some other object, and if you just destroyed the
> last reference to an object, that object needs to be cleaned up or deleted.
> In C, you the programmer has to remember, and write code, every time you
> overwrite that reference. I personally much prefer to write the code once,
> and let the compiler insert that code every time it’s needed, which I can’t
> do in C.
>
> On the downside, I feel C++ is a language created by a committee, and has
> a ton of really ugly constructs. I’m not really a fan of C++ specifically,
> but am a fan of getting languages to automatically keep us from doing silly
> things. Even though C++ has many ways to shoot your foot off, it also has
> ways to make your foot self-protecting. I’d love to see some new language
> (or implementation of a current language) that took the best of what we
> know about language design and used that to help solve the problems kernel
> developers have. For example, why as a kernel developer can’t I just
> declare an attribute on a pointer that means it points to pageable memory,
> and get the compiler to verify I never access that referenced memory in an
> inappropriate context. I’m not especially optimistic this will happen
> anytime soon, with issues like the above platform inertia problem a
> contributing factor.
>
> I think the best hope at this point would be universities teach good
> computer science, including the area of computer languages, so in 30 years,
> new kernel developers would just be repulsed if they had to use a language
> that allowed them to accidentally corrupt memory. Today, the number of
> developers who are so entrenched as this being normal and acceptable is
> huge. It’s a bit like as a kid, the concept of wearing a helmet while
> riding a bicycle was non-existent, and today, it’s well know that your risk
> of serious injury is high is you don’t wear a helmet, and vast numbers of
> people do. It’s not that the risk of crashing a bike have changed, it’s
> that we learned better ways to prevent the nasty consequences.
>
> I’m a former computer language implementer of exotic pure object oriented
> languages, and it’s really painful to see problems not being solved, that
> we know how to solve. I suspect the crisis in security issues may force
> things to evolve. For a while, Windows was getting blamed for being much
> less secure, but then we had really major security vulneraries in some open
> source and Linux projects, which I think was a wakeup call that Windows was
> not the problem, the C language was.
>
> Don’t get me started about computer languages…
>
> Jan
>
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer>
></http:></http:></ntddk.h>