Memory corruption with -O3, but not -O2 (and not with DMD)

James Blachly james.blachly at gmail.com
Sat Aug 17 20:33:08 UTC 2019


Hi all,

First , as always thanks for LDC2 without which we couldn't write high 
performance D software for our lab.

I've run in to an problem wherein after ~60,000 iterations of a loop we 
get memory corruption, but only when building with LDC2 and -O3; there 
are no problems AFAICT with -O2, or when building optimized versions 
with DMD. -enable-inlining does not make a difference. All of that being 
said, it does not rule out me making a pointer or memory error, but all 
seems well except with LDC2 -O3.

Debugging has been difficult because -O3 optimizes away a lot and thus 
lldb is not able to show me the tracking debugging variables I need to 
isolate the problematic code. Disassembling, I've found the register 
storing pointer to the corrupt string; interestingly, the correct string 
appears just slightly lower on the heap (maybe 32 bytes IIRC).

Manifestation slightly nondeterministic -- adding tracking variables and 
code makes the problem intermittent.

Indeed, I placed some simple guards (e.g.: if (pre_string != 
post_string) throw new Exception() ) near the place where the corrupt 
memory is manifest and sometimes the guard is triggered, while other 
times it is _not_, but the bad string shows up just a few statements 
later (inside a function).

Am at my wits' end, so help or next steps are greatly appreciated. I can 
provide disassembly of whatever combination of -O/-O2/-O3 and 
triggering/nontriggering code blocks if it would e helpful.

If this should move to github, let me know.

Kind regards


More information about the digitalmars-d-ldc mailing list