6

In order to have a better protection against UB cases like:

#include <stdio.h>
int f(){
  int x;
  return x;
}
int main()
{
   f();
   while(1);
   return 0;
}

I've updated my GCC today so I could use ubsan. My current version is 5.3.0 according to gcc --version. I thought ubsan will be added by this update, but it seems that it was not because after compiling with C:\Users\my_name\Desktop>gcc -fsanitize=undefined a.c this is what I get:

c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/ld.exe: cannot fin d -lubsan

Now, I've seen this post but the OS is Ubuntu 15.04 and I use Win 8.1 so that didn't help me. One of the comments here says:

You will need to install the libubsan package.

but I don't know if it was meant for windows/ubuntu, and even if it was for windows users, I don't understand how to do it.

edit: I tried also compiling gcc -fno-sanitize=all a.c (there are a lot of options in here) and this compiled with no warning, so I guess GCC recognizes the sanitizer somehow (because it compiled OK) but rejects my original compilation try for some reason

underscore_d
  • 6,309
  • 3
  • 38
  • 64
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • How do you compile your source file? – Mohammed Bakr Sikal May 16 '17 at 09:13
  • @CIsForCookies: You're probably new to programming? And not yet familiar with libraries and functions? There' not much magic involved here: libubsan provides functions to check for UB, and GCC can insert calls to these functions. If you tell GCC _not_ to insert these calls, then the library which provides the functions obviously won't be needed. – MSalters May 16 '17 at 09:32
  • @MSalters I thought that any call to _sanitize_ would cause problems if GCC is not familiar with the library, even if the call is _ignore_. But anyway, how can I _insert_ these calls? – CIsForCookies May 16 '17 at 09:35
  • 2
    @CIsForCookies: Well, that pretty much confirms my assumptions. The compiler knows how to insert the calls, and you already know how: -`fsanitize=`. But the compiler is not the problem. You have a linker error. It needs to insert the called functions matching the calls. Those functions need to come from a library. You apparently do not have the library. – MSalters May 16 '17 at 09:39
  • 2
    @MSalters thx, but that was the question all along. I said I'm not sure I have the library. I said I don't how to install. – CIsForCookies May 16 '17 at 09:40
  • 1
    You need to specify how you are using this stuff on Windows, e.g. MSYS2 or something else. MSYS2 for example uses the [`MinGW-Packages`](https://github.com/Alexpux/MINGW-packages) repository, and `ubsan` does not appear to be among them. In that case, you would want to check for existing, open issue(s) requesting the package, and express interest in using it, if there isn't enough already. The alternative is to build and install it yourself (unless someone else already did, and their version is compatible with your environment, but mixing random packages like that isn't usually a good idea). – underscore_d Oct 13 '18 at 13:42

1 Answers1

1

For GCC on x86_64 sanitizer runtime libraries (including -lubsan) are only supported for Linux, OSX, FreeBSD and Solaris (see https://github.com/gcc-mirror/gcc/blob/master/libsanitizer/configure.tg for details). So unfortunately you can not yet use UBsan on Windows.

yugr
  • 19,769
  • 3
  • 51
  • 96