小编kam*_*shi的帖子

AddressSanitizer 并在运行时加载动态库 -> (<未知模块>)

我在所有项目中都使用 AddressSanitizer,以便检测内存泄漏、堆损坏等。但是,当通过 dlopen 在运行时加载动态库时,AddressSanitizer 的输出还有很多不足之处。我写了一个简单的测试程序来说明这个问题。代码本身并不有趣,只是两个库,一个在编译时通过 -l 链接,另一个在运行时使用 dlopen 加载。为了完整起见,这里是我用于测试的代码:

// ----------------------------------------------------------------------------
// dllHelper.hpp
#pragma once

#include <string>
#include <sstream>
#include <iostream>

#include <errno.h>
#include <dlfcn.h>

// Generic helper definitions for shared library support
#if defined WIN32
#define MY_DLL_EXPORT __declspec(dllexport)
#define MY_DLL_IMPORT __declspec(dllimport)
#define MY_DLL_LOCAL
#define MY_DLL_INTERNAL
#else
#if __GNUC__ >= 4
#define MY_DLL_EXPORT __attribute__ ((visibility ("default")))
#define MY_DLL_IMPORT __attribute__ ((visibility ("default")))
#define MY_DLL_LOCAL  __attribute__ ((visibility ("hidden")))
#define MY_DLL_INTERNAL __attribute__ ((visibility ("internal")))
#else
#define MY_DLL_IMPORT
#define MY_DLL_EXPORT
#define MY_DLL_LOCAL
#define MY_DLL_INTERNAL …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-library address-sanitizer

5
推荐指数
1
解决办法
7238
查看次数

可变参数模板参数包扩展失去了限定符

假设我有一个可变参数函数模板,它将函数指针指向具有所述可变参数的函数。以下代码不能在 gcc (11.2) 下编译,但可以在 clang 和 msvc 下编译(https://godbolt.org/z/TWbEKWb9f)。

#include <type_traits>

void dummyFunc(const int);

template<typename... Args>
void callFunc(void(*)(Args...), Args&&...); // <- this one is problematic
// see /sf/ask/4695719031/
template<typename... Args>
void callFunc2(void(*)(std::conditional_t<std::is_const_v<Args>, const Args, Args>...), Args&&...); // <- this one works


int main()
{
    // fails on gcc, works on clang and msvc
    callFunc<const int>(&dummyFunc, 2);
    // this works
    //callFunc(&dummyFunc, 2);
    // this works as well
    //callFunc2<const int>(&dummyFunc, 2);
}
Run Code Online (Sandbox Code Playgroud)

将函数参数“Args...”显式指定为“const int”可防止 gcc 编译代码。显然,模板参数扩展在 gcc 下失去了 cv 限定符,而在 clang …

c++ gcc variadic-functions variadic-templates

5
推荐指数
1
解决办法
298
查看次数