C compilation bug

35 views
Skip to first unread message

juergen...@gmail.com

unread,
Nov 10, 2020, 6:12:11 PM11/10/20
to emscripten-discuss
I just refactored some old C code of mine. Surprisingly the updated code failed in a very unexpected manner that I still don't understand. I narrowed the problem down and here is what the relevant code looks like:

static uint8_t _x, _y; // ... _x and _y get both initialized and updated elsewhere 
void setOutput(uint8_t val) { ...}void someFunction() {
   static uint8_t tmp;
   ...
   swich(..) {
      case foo: {
         // tmp = _x & _y;
         setOutput( _x & _y); 
      }
      ...
   }
}

The code fails because the _x & _y expression suddenly passes total garbage 
to the setOutput() function. Originally I had used the commented tmp assignment
and then used tmp as an argument for that call - and that functions correctly.

However what I find most puzzling is this: simply uncommenting the "tmp" assignment - without even using tmp later) makes the problem disappear...WTF?!

I am still using an older Emscripten  version 1.38 and the effect is there regardless if I compile to WASM or asm.js and whether or not I am using different optimizer options or the closure compiler.

Any ideas what is going on here? Is this some kind of known bug or some C feature that I am not aware of..

Thomas Lively

unread,
Nov 10, 2020, 8:23:19 PM11/10/20
to emscripte...@googlegroups.com
Does the problem reproduce on a newer Emscripten? It sounds like either a bug in LLVM (which would probably have been fixed by now) or perhaps undefined behavior in your program. If you can reproduce the issue with the latest Emscripten, would you be able to provide source code that demonstrates the problem?

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/636d73be-ce53-4a55-bdba-66b7ab938887n%40googlegroups.com.

Dan Stromberg

unread,
Nov 10, 2020, 11:52:06 PM11/10/20
to emscripte...@googlegroups.com

It's about the sort of things that can happen if you reference undefined regions of memory.  I tuses FORTRAN to make its points, but they apply even more to C.

Floh

unread,
Nov 12, 2020, 11:05:50 AM11/12/20
to emscripten-discuss
I would try with ASAN and probably also UBSAN first (see https://emscripten.org/docs/debugging/Sanitizers.html), and see if they trigger somewhere. I'd guess that some unrelated code goes haywire and overwrites the memory locations where _x and _y are stored with garbage. Strange that this doesn't happen with tmp, but maybe since tmp is also a static variable it might change the layout of the static variable storage area in a way that the memory corruption plays out differently.

Are you able to compile and test the code natively? Memory corruption problems are sometimes easier to debug there.
Reply all
Reply to author
Forward
0 new messages