首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clang的UBSan &函数指针:这是非法的吗?

Clang的UBSan &函数指针:这是非法的吗?
EN

Stack Overflow用户
提问于 2015-01-16 03:05:52
回答 1查看 1.2K关注 0票数 10

我试图通过函数指针表调用一些C++函数,该表作为C符号从共享对象导出。代码实际上正在工作,但是Clang的未定义行为清除器(= UBSan)看到我发出的调用是非法的,如下所示:

代码语言:javascript
复制
==11410==WARNING: Trying to symbolize code, but external symbolizer is not initialized!
path/to/HelloWorld.cpp:25:13: runtime error: call to function (unknown) through pointer to incorrect function type 'foo::CBar &(*)()'
(./libFoo.so+0x20af0): note: (unknown) defined here

由于Clang的未定义行为清除器,间接调用函数是合法的,该函数通过函数指针返回C++标准类对象的引用,但对于用户定义的类是非法的。有人能告诉我它怎么了吗?

我一直试图用Clang-llvm3.4-1ubuntu3CMake 2.8.12.2Ubuntu14.04上构建这个项目。若要再现此现象,请将以下5文件放在同一个目录中并调用build.sh。它将创建一个makefile并构建项目,并运行可执行文件。

Foo.h

代码语言:javascript
复制
#ifndef FOO_H
#define FOO_H

#include <string>

//
#define EXPORT __attribute__ ((visibility ("default")))

namespace foo {
    class CBar
    {
        // empty
    };

    class CFoo
    {
    public:
        static CBar& GetUdClass();
        static std::string& GetStdString();
    };

    // function pointer table.
    typedef struct
    {
        CBar& (*GetUdClass)();
        std::string& (*GetStdString)();
    } fptr_t;

    //! function pointer table which is exported.
    extern "C" EXPORT const fptr_t FptrInFoo;
}

#endif

Foo.cpp

代码语言:javascript
复制
#include "Foo.h"
#include <iostream>

using namespace std;

namespace foo
{
    // returns reference of a static user-defined class object.
    CBar& CFoo::GetUdClass()
    {
        cout << "CFoo::GetUdClass" << endl;
        return *(new CBar);
    }

    // returns reference of a static C++ standard class object.
    std::string& CFoo::GetStdString()
    {
        cout << "CFoo::GetStdString" << endl;
        return *(new string("Hello"));
    }

    // function pointer table which is to be dynamically loaded.
    const fptr_t FptrInFoo = {
        CFoo::GetUdClass,
        CFoo::GetStdString,
    };
}

HelloWorld.cpp

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <dirent.h>
#include <dlfcn.h>
#include "Foo.h"

using namespace std;
using namespace foo;

int main()
{
    // Retrieve a shared object.
    const string LibName("./libFoo.so");
    void *pLibHandle = dlopen(LibName.c_str(), RTLD_LAZY);
    if (pLibHandle != 0) {
        cout << endl;
        cout << "Info: " << LibName << " found at " << pLibHandle << endl;
        // Try to bind a function pointer table:
        const string SymName("FptrInFoo");
        const fptr_t *DynLoadedFptr = static_cast<const fptr_t *>(dlsym(pLibHandle, SymName.c_str()));
        if (DynLoadedFptr != 0) {
            cout << "Info: " << SymName << " found at " << DynLoadedFptr << endl;
            cout << endl;
            // Do something with the functions in the function table pointer.
            DynLoadedFptr->GetUdClass();    // Q1. Why Clang UBSan find this is illegal??
            DynLoadedFptr->GetStdString();  // Q2. And why is this legal??
        } else {
            cout << "Warning: Not found symbol" << endl;
            cout << dlerror() << endl;
        }
    } else {
        cout << "Warning: Not found library" << endl;
        cout << dlerror() << endl;
    }
    cout << endl;
    return 0;
}

CMakeLists.txt

代码语言:javascript
复制
project (test)

if(COMMAND cmake_policy)
      cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-rpath,$ORIGIN")

add_library(Foo SHARED Foo.cpp)

add_executable(HelloWorld HelloWorld.cpp)
target_link_libraries (HelloWorld dl)

build.sh

代码语言:javascript
复制
#!/bin/bash

# 1. create a build directory.
if [ -d _build ]; then
    rm -rf _build
fi
mkdir _build
cd _build

# 2. generate a makefile.
CC=clang CXX=clang++ CXXFLAGS="-fvisibility=hidden -fsanitize=undefined -O0 -g3" cmake ..

# 3. build.
make

# 4. and run the executable.
./HelloWorld

我一直试图找到一个线索来挖掘这个问题,并意识到这个问题被消毒液(-fsanitize=function)的“功能”选项捕捉到了,但它并没有那么多的文档。我希望你们能给我一个合理的解释,这样的运行时错误信息,似乎来自另一个星球。谢谢。

在输出中Clang指出的“未知”是什么?

下面是addr2line的输出,用于检查消毒液的“未知”内容:

代码语言:javascript
复制
$ addr2line -Cfe _build/libFoo.so 0x20af0
foo::CFoo::GetUdClass()
path/to/Foo.cpp:12

嗯,看起来就像我想给我打电话的那个函数。你能猜到Clang看上去有什么不同吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-19 09:16:03

CBar的typeinfo需要对函数的类型具有默认的可见性,Clang在可执行文件和动态库中认为相同;将Foo.h更改为:

代码语言:javascript
复制
  class EXPORT CBar
  {
      ...
  }
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27976687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档