- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个动态链接库的程序。该程序将函数指针传递给该库以执行。
但是 ubsan(Undefined Behavior Sanitizer)指定指针位于错误的函数类型上。那只会发生
我使用 clang 来编译我的项目。
这是 clang 未定义行为 sanitizer 中的错误吗?
以下代码简化为一个简单的测试用例。检查评论,看看我们可以在哪里删除一些警告
应用程序代码:
主.cxx
#include "Caller.h"
#include "Param.h"
static void FctVoid()
{
}
static void FctInt(int _param)
{
static_cast<void>(&_param);
}
static void FctCaller(Caller &_caller)
{
static_cast<void>(&_caller);
}
static void FctParam(Param const &_param)
{
static_cast<void>(&_param);
}
int main()
{
Param param;
Caller::CallVoid(&FctVoid);
Caller::CallInt(&FctInt);
Caller::CallThis(&FctCaller);
Caller::CallParam(&FctParam, param);
return 0;
}
库文件的代码是:
调用者.cxx:
#include "Caller.h"
// To uncomment to fix one warning
//#include "Param.h"
void Caller::CallVoid(FctVoidT _fct)
{
_fct();
}
void Caller::CallInt(FctIntT _fct)
{
_fct(32);
}
void Caller::CallThis(FctThisT _fct)
{
Caller caller;
_fct(caller);
}
void Caller::CallParam(FctParamT const &_fct, Param const &_param)
{
_fct(_param);
}
调用者.h
#ifndef __Caller_h_
#define __Caller_h_
#include "owExport.h"
class Param;
class EXPORT_Library Caller
{
public:
typedef void(*FctVoidT)();
static void CallVoid(FctVoidT _fct);
typedef void(*FctIntT)(int);
static void CallInt(FctIntT _fct);
typedef void(*FctThisT)(Caller &);
static void CallThis(FctThisT _fct);
typedef void(*FctParamT)(Param const &);
static void CallParam(FctParamT const &_fct, Param const &_param);
};
#endif
参数.h
#ifndef __Param_h_
#define __Param_h_
#include "owExport.h"
class EXPORT_Library Param
{
public:
};
#endif
owExport.h
#ifndef __owExport_h_
#define __owExport_h_
#define OW_EXPORT __attribute__ ((visibility("default")))
#define OW_IMPORT
// Use this one to fix one warning
#define OW_IMPORT __attribute__ ((visibility("default")))
#ifdef Library_EXPORTS
# define EXPORT_Library OW_EXPORT
#else
# define EXPORT_Library OW_IMPORT
#endif
#endif
配置项目的CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(TestFunction)
set(BUILD_SHARED_LIBS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fsanitize=undefined ")
# Act here to for the call of function through pointer to incorrect function type
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
add_library(Library Caller.cxx Param.cxx)
add_executable(TestWithLib Main.cxx)
target_link_libraries(TestWithLib Library)
最佳答案
首先:如果您已经编辑问题以添加修复程序,那就不好了。这让人很难回答。
对于您的问题:您基本上有 2 个问题:第一个是 Ssymbol Caller,第二个是
Param`,两者基本相同。
对于问题的来源:UBSAN 将指针的类型信息与预期的类型信息进行比较。如果类型信息不同,则会显示错误。 typeinfo
比较是通过指针比较来完成的。这对提高速度非常有用,但引入了一个微妙的问题:即使实际类型完全相同,它们也可能不会共享相同的 typeinfo
。当您从共享库中抛出一个类型并希望在可执行文件中捕获它(反之亦然)时,这也很重要:捕获是通过类型信息比较完成的,如果两个类型不完全相同(共享相同的类型信息)你不会捕获它。
所以您的第一个问题是 class EXPORT_Library Caller
:您有条件地将 EXPORT_Library
定义为“导出”或不“导出”。如果它是从多个 DSO 导出的,那么类型信息将被合并。在您的情况下,您将其导出到共享库中,而不是导出到可执行文件中,这会阻止合并它们。您可以为此使用 BOOST_SYMBOL_EXPORT
或 OW_EXPORT
。
第二个问题是相反的(假设 EXPORT_Library==OW_EXPORT
):Param
在包含 Param.h header 时被导出,这仅由可执行文件完成不是共享库。同样,类型信息未合并 -> 不同类型到 RTTI 系统。
底线:导出您要在 DSO 边界上使用的所有类。
关于c++ - 通过指向错误函数类型的指针调用函数(未知),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220926/
有人能给我解释一下我下面程序中的一点吗我编写的以下程序是为了理解 int * const * var 的含义; #include "iostream" using namespace std ; in
我正在摆弄 C 中的指针,但仍然不确定一些非常基础的知识。我想出了以下示例代码: #include int main(void) { int num = 42; /
以下代码产生警告: const char * mystr = "\r\nHello"; void send_str(char * str); void main(void){ send_str
我正在尝试在我的 Linux 计算机上升级 cmake。这是我使用的命令。 删除旧的 cmake: sudo apt purge --auto-remove cmake 从 https://cmake
我试图借助 char 指针来追踪 union 第一个字节的值。我假设 byte1 的输出应该是 ff,但实际上是 ffffffff。如果我错了请纠正我。 #include #include #in
我正在使用微 Controller 进行一些 ADC 测量。当我尝试使用 -O2 优化编译以下代码时遇到问题,当代码中存在 PrintVal() 函数时,MCU 卡住。我做了一些调试,结果发现,当我添
#include #include using namespace std; int main () { vector qwerty; qwerty.push_back(5);
我有我的 woking setup.py 文件;其中包含以下行: home = os.environ["HOME"] # home = /home/dr in my machine; distu
这段代码 #include #include static_assert(std::is_same_v::value_type, volatile int>); 在最新的 GCC 和 clang
我对 C 中的前向声明有疑问。 代码 typedef struct yhash_s t_yhash;// forward declaration struct yhash_s { size_t
我想提取成员指针指向的成员的类型。 template void demo(myClass& instance, void* ptr) { instance.*member = *reinter
我正在尝试使用指针将一段 C 代码转换为 Swift 3。这是 C 代码中的相关部分。 Float32 sampleArray[256] = { // Array is 256 Float value
您可能知道,VLA's haves pros and cons 和它们在 C11 中是可选的。 我想使 VLA 成为可选项的主要原因是:“堆栈可能会爆炸”: int arr[n]; /* where
这段代码有什么错误?为什么我不能按照我尝试的方式取消引用该元素。 #include typedef struct { int value; struct node * left;
有什么方法可以在 JavaScript 中创建\返回指向变量的指针吗? 比如,在 PHP 中: function func() { ..... return &$result; } 我
如果您想使用方法的指针作为参数,则需要将该方法键入作为对象的函数,就像这样好 : type TAcceptor = function(filename:string):boolean of objec
很简单的问题: 我对 C++ 中的智能指针有点陌生。我想我得到了所有权的东西,但我不知道如何访问他们实际指向的内容。当我尝试使用对象的成员函数/变量时,我只是得到了 unique_ptr 类的函数,这
我得到了一个点的方位 Angular 、指南针方向和一个可以将箭头设置到某个方向的 api(0 是顶部,90 是右侧,180 是底部,360 是顶部) 如果我希望箭头指向我采用方位 Angular 形
我正在尝试找到一种方法,从单元格中获取位于当前工作表左侧(托盘下方)的工作表单元格中的数据。 我知道如何通过调用其他工作表 =Sheet1!A1 但现在我需要一些最好的解释 =Sheet[-1]!A1
所以我在 MATLAB 中有一个 for 循环,其中向量 x 将通过一个函数,比如 cos(x).^2,或者不同的选择,比如 sin(x).^2 + 9.*x。用户将在 for 循环之前选择他想使用的
我是一名优秀的程序员,十分优秀!