Subject: bug#16492: Emacs won't bootstrap with clang 3.4 on Cygwin

From: Paul Eggert <[email-masked]>
Date: Sat, 18 Jan 2014 23:55:52 -0800

[This bug was originally reported by mirek.kaim in 
http://lists.gnu.org/archive/html/emacs-devel/2014-01/msg01702.html and 
I'm taking the liberty of copying its contents below.]

editfns.c gets miscompiled at -O > 0, causing emacs to crash.

backtrace:

(gdb) run
Starting program: /y/build/emacs/src/temacs --batch --load loadup bootstrap
[New Thread 3728.0x10dc]
[New Thread 3728.0x10c0]
Loading loadup.el (source)...
Using load-path (/y/emacs/lisp /y/emacs/lisp/emacs-lisp
/y/emacs/lisp/language /y/emacs/lisp/international
/y/emacs/lisp/textmodes /y/emacs/lisp/vc)
[New Thread 3728.0xdd4]
Loading emacs-lisp/byte-run (source)...
Loading emacs-lisp/backquote (source)...
Loading subr (source)...
Loading version (source)...

Program received signal SIGSEGV, Segmentation fault.
strftime_case_ (upcase=false, s=0x228f00 "\001\212\210",
     maxsize=<optimized out>, format=0x0, tp=0xa, ut=<optimized out>,
     ns=<optimized out>) at ../../../emacs/lib/strftime.c:507
507       for (f = format; *f != '\0'; ++f)
(gdb) backtrace
#0  strftime_case_ (upcase=false, s=0x228f00 "\001\212\210",
     maxsize=<optimized out>, format=0x0, tp=0xa, ut=<optimized out>,
     ns=<optimized out>) at ../../../emacs/lib/strftime.c:507
#1  0x005a80c2 in nstrftime (s=0x228f00 "\001\212\210", maxsize=0, 
format=0x0,
     tp=0x61256e80 <tm>, ut=1, ns=484375000)
     at ../../../emacs/lib/strftime.c:1449
#2  0x00513ef0 in format_time_string (format=0x0, formatlen=0, t=...,
     ut=<optimized out>, tmp=<optimized out>)
     at ../../../emacs/src/editfns.c:1672
#3  0x00513d44 in Fformat_time_string (format_string=<optimized out>,
     timeval=2268888, universal=<optimized out>)
     at ../../../emacs/src/editfns.c:1756
#4  0x0051d61c in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2182
#5  0x0051d429 in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2140
#6  0x0051e6e1 in Flet (args=<optimized out>) at 
../../../emacs/src/eval.c:937
#7  0x0051d397 in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2124
#8  0x0052175b in funcall_lambda (fun=<optimized out>, nargs=<optimized 
out>,
     arg_vector=<optimized out>) at ../../../emacs/src/eval.c:459
#9  0x00520346 in apply_lambda (fun=9104334, args=<optimized out>)
     at ../../../emacs/src/eval.c:2915
---Type <return> to continue, or q <return> to quit---
#10 0x0051d53d in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2221
#11 0x0052030b in apply_lambda (fun=9361006, args=<optimized out>)
     at ../../../emacs/src/eval.c:2906
#12 0x0051d53d in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2221
#13 0x0051d429 in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2140
#14 0x0051d4d9 in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2161
#15 0x0053e568 in readevalloop (readcharfun=8496874,
     stream=0x864080 <bss_sbrk_buffer+671560>, sourcename=<optimized out>,
     printflag=<optimized out>, unibyte=<optimized out>, readfun=8130560,
     start=<optimized out>, end=<optimized out>)
     at ../../../emacs/src/lread.c:1934
#16 0x0053d3f4 in Fload (file=<optimized out>, noerror=<optimized out>,
     nomessage=<optimized out>, nosuffix=<optimized out>,
     must_suffix=<optimized out>) at ../../../emacs/src/lread.c:1370
#17 0x0051d675 in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2190
#18 0x0053e568 in readevalloop (readcharfun=8496874,
     stream=0x864010 <bss_sbrk_buffer+671448>, sourcename=<optimized out>,
     printflag=<optimized out>, unibyte=<optimized out>, readfun=8130560,
---Type <return> to continue, or q <return> to quit---
     start=<optimized out>, end=<optimized out>)
     at ../../../emacs/src/lread.c:1934
#19 0x0053d3f4 in Fload (file=<optimized out>, noerror=<optimized out>,
     nomessage=<optimized out>, nosuffix=<optimized out>,
     must_suffix=<optimized out>) at ../../../emacs/src/lread.c:1370
#20 0x0051d675 in eval_sub (form=<optimized out>)
     at ../../../emacs/src/eval.c:2190
#21 0x005200d7 in Feval (form=8430502, lexical=<optimized out>)
     at ../../../emacs/src/eval.c:1994
#22 0x004bd1bd in top_level_2 () at ../../../emacs/src/keyboard.c:1179
#23 0x0051f4af in internal_condition_case (bfun=0x77e170 <pbm_format+676>,
     handlers=<optimized out>, hfun=<optimized out>)
     at ../../../emacs/src/eval.c:1345
#24 0x004bd183 in top_level_1 (ignore=8450074)
     at ../../../emacs/src/keyboard.c:1187
#25 0x0051ee24 in internal_catch (tag=<optimized out>, func=0x0, 
arg=7856496)
     at ../../../emacs/src/eval.c:1109
#26 0x004ac41a in recursive_edit_1 () at ../../../emacs/src/keyboard.c:1148
#27 0x004ac5d5 in Frecursive_edit () at ../../../emacs/src/keyboard.c:841
#28 0x004ab10a in main (
     argc=<error reading variable: Cannot access memory at address 0x0>,
     argv=<optimized out>) at ../../../emacs/src/emacs.c:1637
(gdb)

exact steps to reproduce:
http://aurora.irc.arloria.net/~unic0rn/emacs_clang_optimization_bug.html

my workaround is kinda silly, but at least it works. i've tried to
modify editfns.c to trick llvm's optimizers into generating something
that will work, to no avail. it seems like the first param doesn't end
up on the stack somehow. since llvm/clang 3.4 is a current release
version, i guess it'll land on osx and freebsd sooner than later, and
if they won't fix that until then - well, it may cause some problems.
unfortunately, i can't test it with higher -march settings, because my
cpu doesn't even support SSE2.

i've filled a bug for llvm, but i'm not sure how far it'll go:
http://llvm.org/bugs/show_bug.cgi?id=18537