Issue with UBSAN

Plug-in and third party software discussion.
Post Reply
Gisle Vanem
Posts: 37
Joined: Mon May 04, 2015 10:30 am

Issue with UBSAN

Post by Gisle Vanem »

Hello list.

Compiling my program using clang-cl ver. 20.1 and UBSAN (Undefined Sanitizer, option -fsanitize=undefined) triggers
several UBSAN events at runtime. E.g. when storing into a DWORD from an address not a multiple of 4 bytes.
Like in this report:

Code: Select all

Everything.c:2395:15: runtime error: load of misaligned address 0x1230caaa0719 for type 'DWORD' (aka 'unsigned long'), which requires 
4 byte alignment
0x1230caaa0719: note: pointer points here
 64 6c 6c  00 1e 00 00 00 46 3a 5c  67 76 5c 57 69 6e 4b 69  74 5c 44 65 62 75 67 67  65 72 73 5c 78
              ^
    #0 0x7ff6f8a9dda0 in _Everything_GetRequestData F:\gv\VC_project\EnvTool\src\Everything.c:2395
    #1 0x7ff6f8aa38e5 in Everything_GetResultFullPathNameA F:\gv\VC_project\EnvTool\src\Everything.c:2045
    #2 0x7ff6f8a1a082 in do_check_evry F:\gv\VC_project\EnvTool\src\envtool.c:2211
  ...
I.e. code such as:

Code: Select all

        if (dwRequestType == EVERYTHING_REQUEST_FILE_NAME)
        {
            return p;
        }

        len = *(DWORD *) p;  // << !! here
        p += sizeof(DWORD);
My fix was simply this macro:

Code: Select all

#ifdef USE_UBSAN
  #define UNALIGNED_DWORD_STORE(dst, src)    memcpy (&dst, src, sizeof(dst))
#else
  #define UNALIGNED_DWORD_STORE(dst, src)    dst = *(DWORD*)(src)
#endif
And use it like this:

Code: Select all

        UNALIGNED_DWORD_STORE (len, p);
        p += sizeof(DWORD);
I'm not sure if such an unaligned is so bad, but though I just let you know.
void
Developer
Posts: 19870
Joined: Fri Oct 16, 2009 11:31 pm

Re: Issue with UBSAN

Post by void »

Thank you for the issue report Gisle Vanem,

Everything IPC was originally written for x86/x64 and doesn't really have issues with unaligned access.

Since IPC is slow, using memcpy to align the data is the best option.
Gisle Vanem
Posts: 37
Joined: Mon May 04, 2015 10:30 am

Re: Issue with UBSAN

Post by Gisle Vanem »

> Since IPC is slow,

Is it really so slow? How can I measure it's speed?
void
Developer
Posts: 19870
Joined: Fri Oct 16, 2009 11:31 pm

Re: Issue with UBSAN

Post by void »

Everything IPCv2 uses WM_COPYDATA.

While WM_COPYDATA is instant, it takes Everything roughly 500 milliseconds per 1million files to build the reply.

The less data Everything has to allocate and fill, the faster this will be..



To check the timing information:
Enable Debug Logging from Tools -> Debug -> Start Debug Logging.
Perform the IPC query.
Stop Debug Logging from Tools -> Debug -> Stop Debug Logging.
---This will open your %TEMP%\Everything Debug Log.txt in Notepad.
Examine the time difference between the two lines:

Code: Select all

finished sort, time taken 0.000094 seconds
...
IPC: query complete: 4294967295 max results, offset 0, reply hwnd 0000000000123456
Gisle Vanem
Posts: 37
Joined: Mon May 04, 2015 10:30 am

Re: Issue with UBSAN

Post by Gisle Vanem »

> To check the timing information:

Thanks. I'll try it.
Post Reply