Skip to content

Undefined behaviour tests ubsan (1313) #767

Open
@GalacticEmperor1

Description

@GalacticEmperor1

Issue №1313 opened by illume at 2019-09-17 09:39:56

UndefinedBehaviorSanitizer (UBSan) is a fast undefined behavior detector. UBSan modifies the program at compile-time to catch various kinds of undefined behavior during program execution, for example:

  • Using misaligned or null pointer
  • Signed integer overflow
  • Conversion to, from, or between floating-point types which would overflow the destination

Here is the gcc documentation for Instrumentation Options and UBSAN (https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html).

From https://www.sqlite.org/testing.html

To help ensure that SQLite does not make use of undefined or implementation defined behavior, the test suites are rerun using instrumented builds that try to detect undefined behavior. For example, test suites are run using the "-ftrapv" option of GCC. And they are run again using the "-fsanitize=undefined" option on Clang. And again using the "/RTC1" option in MSVC

To compile a python C extension with a UBSAN with clang on Mac do:

LDFLAGS="-g -fsanitize=undefined" CFLAGS="-g -fsanitize=undefined -fno-omit-frame-pointer" python3 setup.py install

The Microsoft Visual Studio compiler can use the Run Time Error Checks feature to find some issues. /RTC (Run-Time Error Checks) (https://docs.microsoft.com/en-us/cpp/build/reference/rtc-run-time-error-checks?view=vs-2019)


Comments

# # illume commented at 2019-10-05 10:08:14

Here are two issues the UBSAN found on my 64bit Mac whilst running the unit tests.

LDFLAGS="-g -fsanitize=undefined" CFLAGS="-g -fsanitize=undefined -fno-omit-frame-pointer" python3 setup.py install
python3 -m pygame.tests
src_c/draw.c:1273:13: runtime error: store to misaligned address 0x7fa14046e1b3 for type 'Uint16' (aka 'unsigned short'), which requires 2 byte alignment
0x7fa14046e1b3: note: pointer points here
 00  00 00 ff 00 00 00 00 00  ff 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00
              ^ 
src_c/freetype/ft_cache.c:103:43: runtime error: index -1 out of bounds for type 'FT_UInt32 const[8]'

# # illume commented at 2019-10-05 10:16:56

I tested with both clang and gcc-9 with the -ftrapv command line arg, and ran the tests with no issues.

CC="gcc-9" LDFLAGS="-g -ftrapv" CFLAGS="-g -ftrapv -fno-omit-frame-pointer" python3 setup.py install
LDFLAGS="-g -ftrapv" CFLAGS="-g -ftrapv -fno-omit-frame-pointer" python3 setup.py install

# # illume commented at 2019-10-05 10:28:44

I ran ubsan under gcc, and only got one issue: the draw.c one that clang found.

CC="gcc-9" LDFLAGS="-g -fsanitize=undefined" CFLAGS="-g -fsanitize=undefined -fno-omit-frame-pointer" python3 setup.py install

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions