Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c    |    Meh, in C you gotta define EVERYTHING    |    243,242 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 242,733 of 243,242    |
|    highcrew to David Brown    |
|    Re: On Undefined Behavior    |
|    03 Jan 26 14:42:59    |
   
   From: high.crew3868@fastmail.com   
      
   On 1/3/26 1:42 PM, David Brown wrote:   
   >> Yes, I'm aware of this instruments, but I'm not very knowledgeable about   
   >> it. I'd like to learn more, and I'll need to spend time doing so.   
   >>   
   >   
   > The tools here can be useful. Of course it is best when you can find   
   > bugs earlier, at the static analysis stage (I am a big fan of lots of   
   > compiler warnings), but the "-fsanatize" options are the next step for a   
   > lot of development. They are of limited value in my own work (small   
   > embedded systems - there's often no console for log messages, and much   
   > less possibility of "hardware accelerated" error detection such as   
   > creative use of a processor's MMU), but for PC programming they can be a   
   > great help.   
      
   Agreed.   
      
   I happen to work with embedded systems as well, and while I came late to   
   the party (all the possible checks are already employed by colleagues   
   who came before me. They took the fun part!), I can tell the value of   
   sanitizers even if the code will later run on embedded systems.   
   That's why I say I'd like to learn more: I'm merely a user of it.   
      
   Simulations use that code. Then the same code is compiled for the target   
   platform.   
      
   On the light of what I learned on this thread, the value of such testing   
   is not just mere "let's see if it crashes", but effectively bound to UB!   
   Or to the attempt to rule out the existing of UB, I should rather say.   
      
   Sanitizer to the rescue. Assuming UB is not in the code, we saddle the   
   (cross)compiler of the correctness of the output.   
      
   Then - I'm reasoning as I write - the devil is in the details.   
   Let's say that the compiler is unable to catch the issues, and so are   
   the sanitizers. Then the UB-tainted source code goes to the target.   
   Assuming the cross compiler is unable to catch it either, we have   
   a garbage-in-garbage-out situation, affecting the product.   
      
   Following this thoughts, I started to wonder: the code I reported in   
   the beginning of the thread, built with -O2, is effectively coping with   
   UB by replacing the function with the equivalent of `return 1`.   
   What if I build it with -O2 and -fsanitize=address?   
   Will the instrumentation be able to catch it, given that there's nothing   
   inherently bad around a `return 1` (minus the fact that it's not what   
   the developer intended?).   
      
    $ cat x.c   
    int table[4] = {0};   
    int exists_in_table(int v)   
    {   
    // return true in one of the first 4 iterations   
    // or UB due to out-of-bounds access   
    for (int i = 0; i <= 4; i++) {   
    if (table[i] == v) return 1;   
    }   
    return 0;   
    }   
    $ gcc -c -O2 x.c   
    $ objdump --disassemble=exists_in_table x.o   
      
    x.o: file format elf64-x86-64   
      
      
    Disassembly of section .text:   
      
    0000000000000000
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca