通过指向错误函数类型的指针来调用函数(未知)

Gal*_*ixe 5 c++ pointers undefined-behavior ubsan

我有一个可以动态链接到库的程序。该程序将函数指针传递到该库以执行。

但是ubsan(未定义的行为清理器)指定指针位于错误的函数类型上。那只会发生

  • 如果回调函数具有一个类作为参数
  • 如果回调函数具有一个类作为参数,但仅向前声明
  • 如果指定编译标志:-fvisibility = hidden。

我用clang编译我的项目。

它是clang未定义行为消毒器中的错误吗?

以下代码简化为一个简单的测试用例。查看评论,看看我们可以采取行动删除一些警告

该应用程序的代码:

Main.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;
}
Run Code Online (Sandbox Code Playgroud)

库文件的代码为:

Caller.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);
}
Run Code Online (Sandbox Code Playgroud)

来电者

#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
Run Code Online (Sandbox Code Playgroud)

参数

#ifndef __Param_h_
#define __Param_h_
#include "owExport.h"
class EXPORT_Library Param
{
public:
};
#endif
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

Fla*_*ire 2

第一:如果您编辑问题以添加修复程序,那就不好了。这让人很难回答。

对于您的问题:您基本上有两个问题:首先是 Ssymbol Caller, second withParam`,两者基本相同。

对于问题的根源:UBSAN 将指针的类型信息与预期的类型信息进行比较。如果类型信息不同,则会显示错误。typeinfo比较是通过指针比较来完成的。这对于速度来说非常有用,但引入了一个微妙的问题:即使实际类型实际上相同,它们也可能不会共享相同的typeinfo. 当您从共享库中抛出类型并希望在可执行文件中捕获它时(反之亦然),这一点也很重要:捕获是通过类型信息比较来完成的,并且如果两种类型不完全相同(共享相同的类型信息)你不会抓住它的。

所以你的第一个问题是class EXPORT_Library Caller:你有条件地定义EXPORT_Library是否“导出”。如果它是从多个 DSO 导出的,则类型信息将被合并。在您的情况下,您将其导出到共享库中,而不是导出到可执行文件中,这会阻止合并它们。为此,您可以使用BOOST_SYMBOL_EXPORT或。OW_EXPORT

第二个问题是相反的(假设EXPORT_Library==OW_EXPORT):Param当包含 Param.h 标头时导出,这仅由可执行文件完成,而不是由共享库完成。同样,类型信息未合并 -> RTTI 系统的不同类型。

底线:导出您想要在 DSO 边界上使用的所有类。