【问题标题】:WDK (Windows Driver Kit) issues with utility library (std::unique_ptr)实用程序库 (std::unique_ptr) 的 WDK(Windows 驱动程序工具包)问题
【发布时间】:2022-01-10 16:32:59
【问题描述】:

我目前正在使用 WDK,并希望将 std::unique_ptr 与自定义删除器一起使用,以避免调用 ObDereferenceObject, ExFreePoolWithTag。但似乎当包含<utility> 时,项目只是中断并且无法正常工作,即显示没有重载匹配(没有T*)我唯一ptr 的输入。 在“常规”项目中使用相同的代码时,一切正常。

#include <memory>
struct PoolDeleter
{
    void operator( )(void* arg) const
    {
        if (arg)
            ExFreePoolWithTag(arg, 0);
    }
};

using PoolBuffer = std::unique_ptr<void, PoolDeleter>;

// Example inside functions

PoolBuffer buffer{ ExAllocatePoolZero(NonPagedPool, 0x2000, POOL_TAG) };

这会产生

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0289   no instance of constructor "std::unique_ptr<_Ty, _Dx>::unique_ptr [with _Ty=void, _Dx=PoolDeleter]" matches the argument list

表示T* 没有过载。此外,当添加&lt;memory&gt; 时,整个解决方案会出现多个错误。


Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0282   the global scope has no "_Mbstatet" Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdio   38  
Error (active)  E0282   the global scope has no "snprintf"  Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdio   84  
Error (active)  E0282   the global scope has no "vfscanf"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdio   86  
Error (active)  E0282   the global scope has no "vscanf"    Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdio   87  
Error (active)  E0282   the global scope has no "vsscanf"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdio   88  
Error (active)  E0282   the global scope has no "lldiv_t"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  66  
Error (active)  E0282   the global scope has no "atoll" Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  73  
Error (active)  E0282   the global scope has no "llabs" Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  74  
Error (active)  E0282   the global scope has no "lldiv" Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  75  
Error (active)  E0282   the global scope has no "strtof"    Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  76  
Error (active)  E0282   the global scope has no "strtold"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  77  
Error (active)  E0282   the global scope has no "strtoll"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  78  
Error (active)  E0282   the global scope has no "strtoull"  Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  79  
Error (active)  E0282   the global scope has no "_Exit" Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  81  
Error (active)  E0282   the global scope has no "at_quick_exit" Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  82  
Error (active)  E0282   the global scope has no "quick_exit"    Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cstdlib  83  
Error (active)  E0282   the global scope has no "vfwscanf"  Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cwchar   88  
Error (active)  E0282   the global scope has no "vswscanf"  Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cwchar   89  
Error (active)  E0282   the global scope has no "vwscanf"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cwchar   90  
Error (active)  E0282   the global scope has no "wcstof"    Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cwchar   91  
Error (active)  E0282   the global scope has no "wcstold"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cwchar   92  
Error (active)  E0282   the global scope has no "wcstoll"   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cwchar   93  
Error (active)  E0282   the global scope has no "wcstoull"  Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\cwchar   94  
Error (active)  E0864   enable_if_t is not a template   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\limits   1094    
Error (active)  E0864   enable_if_t is not a template   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\memory   3578    
Error (active)  E0864   enable_if_t is not a template   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\memory   3583    
Error (active)  E0864   enable_if_t is not a template   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\memory   3589    
Error (active)  E0864   enable_if_t is not a template   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\memory   3610    
Error (active)  E0864   enable_if_t is not a template   Driver.sys (Driver\Driver.sys) - x64-Debug  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\memory   3717    
F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\vcruntime.h(61): error C2220: the following warning is treated as an error
F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\vcruntime.h(61): warning C4083: expected ')'; found identifier '_VCRUNTIME_DISABLED_WARNINGS'
F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\vcruntime.h(111): warning C4005: '_CRT_STRINGIZE': macro redefinition
  F:\Program Files\Windows Kits\10\Include\10.0.22000.0\km\crt\crtdefs.h(85): note: see previous definition of '_CRT_STRINGIZE'
F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\vcruntime.h(114): warning C4005: '_CRT_WIDE': macro redefinition
  F:\Program Files\Windows Kits\10\Include\10.0.22000.0\km\crt\crtdefs.h(88): note: see previous definition of '_CRT_WIDE'
F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\vcruntime.h(155): warning C4005: '__CRTDECL': macro redefinition
  F:\Program Files\Windows Kits\10\Include\10.0.22000.0\km\crt\crtdefs.h(640): note: see previous definition of '__CRTDECL'
F:\Program Files\Windows Kits\10\Include\10.0.22000.0\km\crt\yvals.h(35): warning C4005: '_STD': macro redefinition
  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\yvals_core.h(1281): note: see previous definition of '_STD'
F:\Program Files\Windows Kits\10\Include\10.0.22000.0\km\crt\yvals.h(37): warning C4005: '_STD_END': macro redefinition
  F:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include\yvals_core.h(1280): note: see previous definition of '_STD_END'
F:\Program Files\Windows Kits\10\Include\10.0.22000.0\km\crt\xstddef(26): fatal error C1189: #error:  "_HAS_EXCEPTIONS == 0 is not supported."
  ninja: build stopped: subcommand failed.

我使用的是最新的Windows 11 EWDK

是否有任何解决方法,或者无法在驱动程序中使用std::unique_ptr?否则我只会创建一个简单的包装类来为我处理清理工作,但使用智能指针会更好,因为这几乎是在重新发明轮子。

问候阿图尔

【问题讨论】:

  • 为什么包含&lt;utility&gt;std::unique_ptr&lt;memory&gt; 中?您最好发布完整的错误消息而不是您的描述。
  • @S.M.我用确切的代码和错误编辑了最初的问题。我不知何故在我的坏处感到困惑。 &lt;utility&gt; 代表std::pair
  • 错误在输出选项卡中。您发布的是错误列表。
  • 你不能在内核模式下使用 c/c++ 运行时
  • @S.M.添加了@RbMm 您是否知道创建包装类然后在 dtor 中调用 ExFreePoolWithTag 以不手动处理清理的任何其他更好的方法?

标签: c++ windows driver wdk


【解决方案1】:

NT 内核的 C++ 运行时不包括对 C++ 异常的支持。不幸的是,这破坏了 C++ STL 的大部分内容。官方 C++ 规范(尚未)不允许 STL 的无异常子集,并且 Microsoft STL 的维护者尚未(尚未)正式承诺支持此类子集。

非正式地,Microsoft STL 的维护者确实希望在 NT 内核上下文中可以使用一些基础标头,例如 &lt;type_traits&gt;。因此,您可能会发现一些标头确实在尽力而为的基础上在内核模式下工作。从the Microsoft STL is open-source 开始,您可以帮助他们的非官方项目,使他们的 STL 部分在 NT 内核中工作。

另外,有几个 C++ 库确实致力于提供类似 STL 的功能,而不需要 C++ 异常支持。例如,the wil library 包括一个wistd_memory.h,它提供与&lt;memory&gt; 大致相同的接口,除了在wistd:: 命名空间下,包括wistd::unique_ptr&lt;T&gt;。 wil 库由 Microsoft 的 Windows 团队维护,并在 Windows 代码库中广泛使用,包括在几个核心内核驱动程序中。

如果您搜索不引发异常的 C++ 库,您可能会找到其他几个提供合理的std::unique_ptr 替换的 C++ 库。如果 grep 使用关键字 throwcatch 显示某个库,您可以快速确定它不适合 NT 内核。

除了 C++ 异常之外,NT 内核中的 C++ 运行时还有另一个问题:运行时没有全局 operator newoperator delete。你需要provide your own operator new,无论是全局和/或作为所有课程的成员。幸运的是,这是一小部分样板文件,您可以将其放入任何 C++ 驱动程序项目中。

【讨论】:

  • 感谢您的精彩回复。我会检查 wil 库,看看这是否足够,因为我只是对自动清理池感兴趣,因为有时很难跟踪它。
猜你喜欢
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 2023-04-03
  • 2010-09-12
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
相关资源
最近更新 更多