nukeykt/Nuked-OPL3

Negative value is being left-shifted (undefined behavior)

kcgen opened this issue · 6 comments

kcgen commented

Flagged by GCC 10.2 when code is instrumented with UBSAN:

../../src/libs/nuked/opl3.c:478:40: runtime error: left shift of negative value -512
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../src/libs/nuked/opl3.c:478:40 in

2021-03-11_07-13

Please check it again with GCC 10.3.

Also, the last one is GCC 11.1 (as today).

does 85d96d1 resolve this issue?

kcgen commented

Confirmed under GCC 10.3

../../src/libs/nuked/opl3.c:478:40: runtime error: left shift of negative value -512
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../src/libs/nuked/opl3.c:478:40 in 
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 10.3.0-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-gDeRY6/gcc-10-10.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-gDeRY6/gcc-10-10.3.0/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.3.0 (Ubuntu 10.3.0-1ubuntu1) 

Thanks @nukeykt - will retry w/ 85d96d1!

kcgen commented

UBSAN is still reporting the problem with 85d96d1 applied.

../../src/libs/nuked/opl3.c:478:40: runtime error: left shift of negative value -512
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../src/libs/nuked/opl3.c:478:40 in 

In this line:

eg_inc = ((~slot->eg_rout) << shift) >> 4;

The (~slot->eg_rout) variable holds -512, which is then left-shifted by the shift value (whatever it might be).

got rid of left shift in d9d6eea altogether

kcgen commented

Confirmed fixed. Tested:

  • Blues Bros
  • Kult
  • Adventures of Moktar
  • Lord of the Realm
  • Super Cauldron
  • Wolf 3D

Thank you @nukeykt!