3

プロジェクトで ubsan テスト (-fsanitize=undefined) を有効にしましたが、いくつかの ubsan ランタイム エラーが見つかりました。ここで失敗した理由を見つけるのを手伝ってくれる人はいますか? GCC と Clang でこの問題を修正するにはどうすればよいですか?

これは lib.h と lib.cpp を含む lib.so モジュールです。

lib.h:

#ifndef LIB_H
#define LIB_H

#ifdef API_EXPORTS
   #define API __attribute__((visibility("default")))
#else
   #define API 
#endif

class API Exception
{
public:
     virtual ~ Exception() = 0;
     void SetReporter();
};

class API FileException : public Exception
{
public:
   ~FileException();
};

#endif

lib.cpp:

#include "lib.h"

Exception::~Exception() = default;

FileException::~FileException() = default;

void Exception::SetReporter()
{

}

lib.so モジュールを呼び出す実行可能モジュールは次のとおりです。

main.cpp

#include "lib.h"

int main(void) {
    FileException ex;
    ex.SetReporter();

    return 0;
}

モジュール (lib.so および main) をビルドして main を実行すると、実行時エラーが発生します。

build_run_gcc.sh

#!/bin/bash

# Test gcc version
gcc --version

# Build the API library
g++ -fPIC -D API_EXPORTS -o lib.so -shared lib.cpp -fvisibility=hidden -Wall -fsanitize=undefined -lubsan

# Build the main
g++ -o main main.cpp ./lib.so -fvisibility=hidden -Wall -fsanitize=undefined -lubsan

# Test main
./main

エラー:

main.cpp:5:19: runtime error: member call on address 0x7ffcb88a8c60 which does not point to an object of type 'Exception'
0x7ffcb88a8c60: note: object is of type 'FileException'
 14 56 00 00  48 cd 41 3d 14 56 00 00  00 fa 14 fd f4 29 3f 51  60 8d 8a b8 fc 7f 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              vptr for 'FileException'
4

0 に答える 0