4

I enabled ubsan test (-fsanitize=undefined) in my project and found some ubsan runtime errors. Can anyone help me find why it failed here? How to fix this issue on GCC and Clang?

Here is the lib.so module which includes lib.h and lib.cpp.

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()
{

}

Here is the executable module that will call lib.so module:

main.cpp

#include "lib.h"

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

    return 0;
}

Build the modules (lib.so and main) and run main, there are runtime errors:

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

Errors:

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'
  • For further questions, you need to extract and provide a [mcve] inline in your question. No links to external sites. As a new user here, please take the [tour] and read [ask]. – Ulrich Eckhardt Dec 09 '19 at 07:59
  • This github example is my minimal reproducible example to demo this issue. – Hongliang Du Dec 09 '19 at 09:24
  • Please make questions *self contained*, which means that you need to present all relevant code and other information inside the question itself. Links can disappear or the information on the pages can change, which will make questions with such links worthless. Remember that questions here are not only for you right now, but for others who in the future might have the same or similar problems. – Some programmer dude Dec 09 '19 at 09:27
  • Thanks. I removed external link and refined my questions. – Hongliang Du Dec 09 '19 at 09:33
  • I could not replicate it with archlinux+gcc8.2. But I replicated it in docker ubuntu:latest with gcc7.4 as in the github actions. Could you please restore the links and add the gcc version output to the errors? – KamilCuk Dec 09 '19 at 09:52
  • Duplicate of https://stackoverflow.com/questions/57294792/c-ubsan-produces-false-positives-with-derived-objects ? – KamilCuk Dec 09 '19 at 09:57
  • Hi @KamilCuk, here is my mininal demo code: https://github.com/oneiric/TestUBSan You can see the results here: https://github.com/oneiric/TestUBSan/runs/339546431 Click the "Build and Run", then see the results in github actions. I am also using gcc 7.4 in the github actions. – Hongliang Du Dec 09 '19 at 12:18
  • @KamilCuk, why do you think it is duplicate issue with another? Our expported macro usage is different from that one. Do you think we should use the consistent macro export for both internal shared so file and external application caller? – Hongliang Du Dec 09 '19 at 12:23
  • Accroding to this https://stackoverflow.com/questions/2164827/explicitly-exporting-shared-library-functions-in-linux EXPORT and IMPORT should use different macro before export class. So my issue is different from https://stackoverflow.com/questions/57294792/c-ubsan-produces-false-positives-with-derived-objects In that question https://stackoverflow.com/questions/57294792/c-ubsan-produces-false-positives-with-derived-objects , they just use the same EXPORT macor for both .so and main modules. – Hongliang Du Dec 09 '19 at 12:40

0 Answers0