Commit b5a8273
committed
Fix a case of static initialization order debacle
When descent3 is built with g++ -flto=auto, there is a crash at startup:
```
Program received signal SIGSEGV, Segmentation fault.
0x00005555557b3b20 in pilot::initialize (
this=this@entry=0x555555fbd480 <Current_pilot>)
at Descent3/pilot_class.cpp:208
208 game_window_w = Video_res_list[Current_video_resolution_id].width;
Missing separate debuginfos, use: zypper install libcpp-httplib0_20-debuginfo-0.20.1-2.1.x86_64 libSDL3-0-debuginfo-3.2.16-1.1.x86_64 libzstd1-x86-64-v3-debuginfo-1.5.7-3.1.x86_64
(gdb) p Current_video_resolution_id
$1 = 0
(gdb) p Video_res_list
$2 = std::vector of length 0, capacity 0
(gdb) bt
f0 pilot::initialize (this=this@entry=0x555555fbd480 <Current_pilot>) at Descent3/pilot_class.cpp:208
f1 pilot::pilot (this=<optimized out>, this=<optimized out>) at Descent3/pilot_class.cpp:177
f2 _sub_I_65535_0.0 ()
f3 call_init (argc=1, argv=0x7fffffffdb68, env=<optimized out>) at ../csu/libc-start.c:145
f4 __libc_start_main_impl
f5 _start () at ../sysdeps/x86_64/start.S:115
```
When ASAN/UBSAN is enabled in conjunction with LTO:
```
/usr/include/c++/14/bits/stl_vector.h:1144:34: runtime error: reference binding to null pointer of type 'struct value_type'
Descent3/pilot_class.cpp:208:63: runtime error: member access within null pointer of type 'struct value_type'
AddressSanitizer:DEADLYSIGNAL
=================================================================
==58724==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
==58724==The signal is caused by a READ memory access.
==58724==Hint: address points to the zero page.
f0 pilot::initialize() Descent3/pilot_class.cpp:208
f1 pilot::pilot() Descent3/pilot_class.cpp:177
f2 _sub_I_65535_0.0 (b/build/Descent3+0x52ece3)
f3 call_init ../csu/libc-start.c:145
f4 __libc_start_main_impl ../csu/libc-start.c:347
f5 _start ../sysdeps/x86_64/start.S:115
```
``_sub_I_65535_0.0`` is indicative of a global constructor, so we
are looking at a case of Static Initialization Order Fiasco whereby
``pilot::initialize`` runs before ``Video_res_list`` gets initialized.
Static variables declared in functions are initialized on first use,
which can help steer global initialization short of placing the
global state in a class instance of its own.
Fixes: 40c7f0d1 parent e684a16 commit b5a8273
File tree
6 files changed
+50
-46
lines changed- Descent3
- renderer
6 files changed
+50
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
314 | 314 | | |
315 | 315 | | |
316 | 316 | | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
342 | 334 | | |
343 | 335 | | |
344 | 336 | | |
| |||
417 | 409 | | |
418 | 410 | | |
419 | 411 | | |
420 | | - | |
| 412 | + | |
| 413 | + | |
421 | 414 | | |
422 | 415 | | |
423 | 416 | | |
424 | | - | |
425 | | - | |
426 | | - | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
427 | 420 | | |
428 | | - | |
| 421 | + | |
429 | 422 | | |
430 | 423 | | |
431 | 424 | | |
| |||
434 | 427 | | |
435 | 428 | | |
436 | 429 | | |
437 | | - | |
438 | | - | |
| 430 | + | |
| 431 | + | |
439 | 432 | | |
440 | 433 | | |
441 | 434 | | |
| |||
782 | 775 | | |
783 | 776 | | |
784 | 777 | | |
785 | | - | |
| 778 | + | |
| 779 | + | |
786 | 780 | | |
787 | 781 | | |
788 | 782 | | |
| |||
856 | 850 | | |
857 | 851 | | |
858 | 852 | | |
859 | | - | |
860 | | - | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
861 | 856 | | |
862 | 857 | | |
863 | 858 | | |
| |||
886 | 881 | | |
887 | 882 | | |
888 | 883 | | |
889 | | - | |
| 884 | + | |
| 885 | + | |
890 | 886 | | |
891 | 887 | | |
892 | 888 | | |
| |||
901 | 897 | | |
902 | 898 | | |
903 | 899 | | |
904 | | - | |
| 900 | + | |
905 | 901 | | |
906 | 902 | | |
907 | | - | |
| 903 | + | |
908 | 904 | | |
909 | 905 | | |
910 | 906 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
937 | 937 | | |
938 | 938 | | |
939 | 939 | | |
940 | | - | |
941 | | - | |
| 940 | + | |
| 941 | + | |
942 | 942 | | |
943 | 943 | | |
944 | 944 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1305 | 1305 | | |
1306 | 1306 | | |
1307 | 1307 | | |
1308 | | - | |
| 1308 | + | |
1309 | 1309 | | |
1310 | 1310 | | |
1311 | 1311 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
205 | 205 | | |
206 | 206 | | |
207 | 207 | | |
208 | | - | |
209 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
210 | 217 | | |
211 | 218 | | |
212 | 219 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
351 | | - | |
352 | | - | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
353 | 354 | | |
354 | 355 | | |
355 | 356 | | |
| |||
0 commit comments