This source file includes following definitions.
- sigsegv_reset_onstack_flag
- sigsegv_reset_onstack_flag
- sigsegv_reset_onstack_flag
- remember_stack_top
- sigsegv_handler
- sigsegv_handler
- install_for
- sigsegv_install_handler
- sigsegv_deinstall_handler
- sigsegv_leave_handler
- stackoverflow_install_handler
- stackoverflow_deinstall_handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22
23 #include "sigsegv.h"
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #if HAVE_GETRLIMIT
31 # include <sys/resource.h>
32 #endif
33
34 #ifdef __OpenBSD__
35 # include <sys/param.h>
36 #endif
37
38
39
40 int libsigsegv_version = LIBSIGSEGV_VERSION;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 #if defined __linux__ || defined __ANDROID__
65
66 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
67 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
68 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
69 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
70
71 # if defined __alpha__
72
73
74
75
76
77
78
79
80
81 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_regs[30]
82
83 # elif defined __arm64__
84
85
86
87
88
89
90
91 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sp
92
93 # elif defined __arm__ || defined __armhf__
94
95
96
97
98
99
100
101
102
103 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.arm_sp
104
105 # elif defined __cris__
106
107
108
109
110
111
112
113 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.usp
114
115 # elif defined __hppa__
116
117
118
119
120
121
122
123 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_gr[30]
124
125 # elif defined __x86_64__
126
127
128
129
130
131
132
133
134
135
136
137 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_RSP]
138
139 # elif defined __i386__
140
141
142
143
144
145
146
147
148
149 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_ESP]
150
151
152 # elif defined __ia64__
153
154
155
156
157
158
159
160
161
162
163
164 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.sc_gr[12]
165
166
167
168
169 # define SIGSEGV_FAULT_BSP_POINTER ((ucontext_t *) ucp)->uc_mcontext.sc_ar_bsp
170
171 # elif defined __m68k__
172
173
174
175
176
177
178
179
180
181 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[R_SP]
182
183 # elif defined __mips__ || defined __mipsn32__ || defined __mips64__
184
185
186
187
188
189
190
191
192
193
194
195 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[29]
196
197 # elif defined __nds32__
198
199
200
201
202
203
204
205 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.nds32_sp
206
207 # elif defined __powerpc__ || defined __powerpc64__ || defined __powerpc64_elfv2__
208
209
210
211
212
213
214
215
216
217
218 # if defined __powerpc64__ || defined __powerpc64_elfv2__
219 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gp_regs[1]
220 # else
221
222 # if 0
223 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.regs->gpr[1]
224 # else
225 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.uc_regs->gregs[1]
226 # endif
227 # endif
228
229 # elif defined __riscv32__ || __riscv64__
230
231
232
233
234
235
236
237
238
239
240 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.__gregs[REG_SP]
241
242 # elif defined __s390__ || defined __s390x__
243
244
245
246
247
248
249
250
251
252 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[15]
253
254 # elif defined __sh__
255
256
257
258
259
260
261
262
263
264 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[15]
265
266 # elif defined __sparc__ || defined __sparc64__
267
268
269
270
271
272
273
274
275
276
277
278 # if defined __sparc64__
279
280
281
282
283 # define SIGSEGV_FAULT_STACKPOINTER (((struct sigcontext *) ucp)->sigc_regs.u_regs[14] + 2047)
284 # else
285
286
287
288
289 # define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) ucp)->si_regs.u_regs[14]
290 # endif
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 # define BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW
310
311 # else
312
313
314
315
316
317
318
319
320
321 # endif
322
323 #endif
324
325 #if defined __GNU__
326
327 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
328 # define SIGSEGV_FAULT_ADDRESS (unsigned long) code
329 # define SIGSEGV_FAULT_CONTEXT scp
330
331 # if defined __i386__
332
333
334
335
336
337
338
339
340 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_uesp
341
342 # endif
343
344 #endif
345
346 #if defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__
347
348 # if defined __arm__ || defined __armhf__ || defined __arm64__
349
350 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
351 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
352 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
353
354 # if defined __arm64__
355
356
357
358 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.mc_gpregs.gp_sp
359
360 # elif defined __arm__ || defined __armhf__
361
362
363
364 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.__gregs[_REG_SP]
365
366 # endif
367
368 # else
369
370
371
372 # if 1
373
374
375 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp, void *addr
376 # define SIGSEGV_FAULT_ADDRESS addr
377 # define SIGSEGV_FAULT_CONTEXT scp
378
379
380
381 # if defined __x86_64__
382
383
384 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_rsp
385
386 # elif defined __i386__
387
388
389 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_esp
390
391 # endif
392
393 # else
394
395
396 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *scp
397 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
398 # define SIGSEGV_FAULT_CONTEXT ((struct sigcontext *) scp)
399 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
400
401
402
403 # if defined __x86_64__
404
405
406 # define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) scp)->sc_rsp
407
408 # elif defined __i386__
409
410
411 # define SIGSEGV_FAULT_STACKPOINTER ((struct sigcontext *) scp)->sc_esp
412
413 # endif
414
415 # endif
416
417 # endif
418
419 #endif
420
421 #if defined __NetBSD__
422
423 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
424 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
425 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
426 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
427
428
429
430
431
432
433
434
435 # ifdef _UC_MACHINE_SP
436 # define SIGSEGV_FAULT_STACKPOINTER _UC_MACHINE_SP ((ucontext_t *) ucp)
437 # endif
438
439 #endif
440
441 #if defined __OpenBSD__
442
443 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, struct sigcontext *scp
444 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
445 # define SIGSEGV_FAULT_CONTEXT scp
446 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
447
448 # if defined __alpha__
449
450
451
452
453 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[30]
454
455 # elif defined __arm__ || defined __armhf__
456
457
458
459
460 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_usr_sp
461
462 # elif defined __hppa__ || defined __hppa64__
463
464
465
466
467
468
469 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[30]
470
471 # elif defined __x86_64__
472
473
474
475
476
477 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_rsp
478
479 # elif defined __i386__
480
481
482
483
484
485 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_esp
486
487 # elif defined __m68k__
488
489
490
491
492 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp
493
494 # elif defined __m88k__
495
496
497
498
499
500
501 # if OpenBSD >= 201211
502 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[31]
503 # else
504 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs.r[31]
505 # endif
506
507 # elif defined __mips__ || defined __mipsn32__ || defined __mips64__
508
509
510
511
512 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[29]
513
514 # elif defined __powerpc64__
515
516
517
518
519 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp
520
521 # elif defined __powerpc__
522
523
524
525
526 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_frame.fixreg[1]
527
528 # elif defined __sh__
529
530
531
532
533
534
535 # if OpenBSD >= 201211
536 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_reg[20-15]
537 # else
538 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_reg.r_r15
539 # endif
540
541 # elif defined __sparc__ || defined __sparc64__
542
543
544
545
546
547
548 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp
549
550 # elif defined __vax__
551
552
553
554
555 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_sp
556
557 # endif
558
559 #endif
560
561 #if (defined __APPLE__ && defined __MACH__)
562
563 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
564 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
565 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
566 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
567
568 # if defined __x86_64__
569
570
571
572
573
574 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__rsp
575
576 # elif defined __i386__
577
578
579
580
581
582 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__esp
583
584 # elif defined __arm64__
585
586
587
588
589
590 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__sp
591
592 # elif defined __powerpc__
593
594
595
596
597
598 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext->__ss.__r1
599
600 # endif
601
602 #endif
603
604 #if defined _AIX
605
606 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
607 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
608 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
609 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
610
611 # if defined __powerpc__ || defined __powerpc64__
612 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.jmp_context.gpr[1]
613 # endif
614
615 #endif
616
617 #if defined __sgi
618
619 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, int code, struct sigcontext *scp
620 # define SIGSEGV_FAULT_ADDRESS (unsigned long) scp->sc_badvaddr
621 # define SIGSEGV_FAULT_CONTEXT scp
622
623 # if defined __mips__ || defined __mipsn32__ || defined __mips64__
624 # define SIGSEGV_FAULT_STACKPOINTER scp->sc_regs[29]
625 # endif
626
627 #endif
628
629 #if defined __sun
630
631 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
632 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
633 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
634 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
635
636 # if defined __x86_64__
637
638
639 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_RSP]
640
641 # elif defined __i386__
642
643
644 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[ESP]
645
646 # elif defined __sparc__ || defined __sparc64__
647
648 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.gregs[REG_O6]
649
650 # if SOLARIS11
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681 # define BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW
682
683 # endif
684
685 # endif
686
687 #endif
688
689 #if defined __CYGWIN__
690
691 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
692 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
693 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
694 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
695
696
697
698 # if defined __x86_64__
699
700 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.rsp
701 # elif defined __i386__
702
703 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.esp
704 # endif
705
706 #endif
707
708 #if defined __HAIKU__
709
710 # define SIGSEGV_FAULT_HANDLER_ARGLIST int sig, siginfo_t *sip, void *ucp
711 # define SIGSEGV_FAULT_ADDRESS sip->si_addr
712 # define SIGSEGV_FAULT_CONTEXT ((ucontext_t *) ucp)
713 # define SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
714
715 # if defined __x86_64__
716
717
718
719
720
721 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.rsp
722
723 # elif defined __i386__
724
725
726
727
728
729 # define SIGSEGV_FAULT_STACKPOINTER ((ucontext_t *) ucp)->uc_mcontext.esp
730
731 # endif
732
733 #endif
734
735
736
737
738
739 #if defined __GNU__ \
740 || defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ \
741 || defined __NetBSD__ || defined __OpenBSD__ \
742 || (defined __APPLE__ && defined __MACH__)
743 # define SIGSEGV_FOR_ALL_SIGNALS(var,body) \
744 { int var; var = SIGSEGV; { body } var = SIGBUS; { body } }
745 #else
746 # define SIGSEGV_FOR_ALL_SIGNALS(var,body) \
747 { int var; var = SIGSEGV; { body } }
748 #endif
749
750
751
752
753 #include "stackvma.h"
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791 #if HAVE_SIGSEGV_RECOVERY \
792 && !(defined SIGSEGV_FAULT_HANDLER_ARGLIST && defined SIGSEGV_FAULT_ADDRESS)
793 # error "You need to define SIGSEGV_FAULT_HANDLER_ARGLIST and SIGSEGV_FAULT_ADDRESS before you can define HAVE_SIGSEGV_RECOVERY."
794 #endif
795 #if !HAVE_SIGSEGV_RECOVERY \
796 && (defined SIGSEGV_FAULT_HANDLER_ARGLIST && defined SIGSEGV_FAULT_ADDRESS) \
797 && !(defined __FreeBSD__ && (defined __sparc__ || defined __sparc64__))
798 # if __GNUC__ || (__clang_major__ >= 4)
799 # warning "You can define HAVE_SIGSEGV_RECOVERY on this platform."
800 # else
801 # error "You can define HAVE_SIGSEGV_RECOVERY on this platform."
802 # endif
803 #endif
804
805 #if HAVE_STACK_OVERFLOW_RECOVERY \
806 && !(defined SIGSEGV_FAULT_ADDRESS + defined SIGSEGV_FAULT_STACKPOINTER + HAVE_STACKVMA >= 2)
807 # error "You need to define two of SIGSEGV_FAULT_ADDRESS, SIGSEGV_FAULT_STACKPOINTER, HAVE_STACKVMA, before you can define HAVE_STACK_OVERFLOW_RECOVERY."
808 #endif
809 #if !HAVE_STACK_OVERFLOW_RECOVERY \
810 && (defined SIGSEGV_FAULT_ADDRESS + defined SIGSEGV_FAULT_STACKPOINTER + HAVE_STACKVMA >= 2) \
811 && !(defined __FreeBSD__ && (defined __sparc__ || defined __sparc64__)) \
812 && !(defined __NetBSD__ && (defined __sparc__ || defined __sparc64__))
813 # if __GNUC__ || (__clang_major__ >= 4)
814 # warning "You can define HAVE_STACK_OVERFLOW_RECOVERY on this platform."
815 # else
816 # error "You can define HAVE_STACK_OVERFLOW_RECOVERY on this platform."
817 # endif
818 #endif
819
820
821
822 #if HAVE_STACK_OVERFLOW_RECOVERY
823
824
825
826
827
828 static void sigsegv_reset_onstack_flag (void);
829
830
831
832 # if defined __GNU__ \
833 || defined __FreeBSD_kernel__ || defined __FreeBSD__ || defined __DragonFly__ \
834 || defined __NetBSD__ || defined __OpenBSD__
835
836 static void
837 sigsegv_reset_onstack_flag (void)
838 {
839 stack_t ss;
840
841 if (sigaltstack (NULL, &ss) >= 0)
842 {
843 ss.ss_flags &= ~SS_ONSTACK;
844 sigaltstack (&ss, NULL);
845 }
846 }
847
848
849
850 # elif defined __sgi || defined __sun
851
852 # include <ucontext.h>
853
854 static void
855 sigsegv_reset_onstack_flag (void)
856 {
857 ucontext_t uc;
858
859 if (getcontext (&uc) >= 0)
860
861
862 if (uc.uc_stack.ss_flags & SS_ONSTACK)
863 {
864 uc.uc_stack.ss_flags &= ~SS_ONSTACK;
865
866
867
868
869 setcontext (&uc);
870 }
871 }
872
873
874
875 # else
876
877 static void
878 sigsegv_reset_onstack_flag (void)
879 {
880
881
882 }
883
884 # endif
885
886
887
888 # if HAVE_STACKVMA
889
890
891 static uintptr_t stack_top = 0;
892
893
894 static void
895 remember_stack_top (void *some_variable_on_stack)
896 {
897 struct vma_struct vma;
898
899 if (sigsegv_get_vma ((uintptr_t) some_variable_on_stack, &vma) >= 0)
900 stack_top = vma.end - 1;
901 }
902
903 # endif
904
905 static stackoverflow_handler_t stk_user_handler = (stackoverflow_handler_t)NULL;
906 static uintptr_t stk_extra_stack;
907 static size_t stk_extra_stack_size;
908
909 #endif
910
911 #if HAVE_SIGSEGV_RECOVERY
912
913
914 static sigsegv_handler_t user_handler = (sigsegv_handler_t)NULL;
915
916 #endif
917
918
919
920
921 #if HAVE_SIGSEGV_RECOVERY
922
923 static void
924 sigsegv_handler (SIGSEGV_FAULT_HANDLER_ARGLIST)
925 {
926 void *address = (void *) (SIGSEGV_FAULT_ADDRESS);
927
928 # if HAVE_STACK_OVERFLOW_RECOVERY
929 # if !(HAVE_STACKVMA || defined SIGSEGV_FAULT_STACKPOINTER)
930 #error "Insufficient heuristics for detecting a stack overflow. Either define CFG_STACKVMA and HAVE_STACKVMA correctly, or define SIGSEGV_FAULT_STACKPOINTER correctly, or undefine HAVE_STACK_OVERFLOW_RECOVERY!"
931 # endif
932
933
934 if (user_handler && (*user_handler) (address, 0))
935 {
936
937 }
938 else
939 {
940
941
942
943 if (stk_user_handler)
944 {
945
946 # ifdef SIGSEGV_FAULT_STACKPOINTER
947 uintptr_t old_sp = (uintptr_t) (SIGSEGV_FAULT_STACKPOINTER);
948 # ifdef __ia64
949 uintptr_t old_bsp = (uintptr_t) (SIGSEGV_FAULT_BSP_POINTER);
950 # endif
951 # endif
952
953 # if HAVE_STACKVMA
954
955 if (stack_top)
956 {
957
958 int saved_errno;
959 struct vma_struct vma;
960 int ret;
961
962 saved_errno = errno;
963 ret = sigsegv_get_vma (stack_top, &vma);
964 errno = saved_errno;
965 if (ret >= 0)
966 {
967 # ifndef BOGUS_FAULT_ADDRESS_UPON_STACK_OVERFLOW
968
969
970
971
972
973
974 uintptr_t addr = (uintptr_t) address;
975
976 # ifdef __ia64
977 if (addr >= vma.prev_end && addr <= vma.end - 1)
978 # else
979 # if STACK_DIRECTION < 0
980 if (addr >= vma.start
981 ? (addr <= vma.end - 1)
982 : vma.is_near_this (addr, &vma))
983 # else
984 if (addr <= vma.end - 1
985 ? (addr >= vma.start)
986 : vma.is_near_this (addr, &vma))
987 # endif
988 # endif
989 {
990 # else
991 # if HAVE_GETRLIMIT && defined RLIMIT_STACK
992
993
994
995 struct rlimit rl;
996
997 saved_errno = errno;
998 ret = getrlimit (RLIMIT_STACK, &rl);
999 errno = saved_errno;
1000 if (ret >= 0)
1001 {
1002 uintptr_t current_stack_size = vma.end - vma.start;
1003 uintptr_t max_stack_size = rl.rlim_cur;
1004 if (current_stack_size <= max_stack_size + 4096
1005 && max_stack_size <= current_stack_size + 4096
1006 # else
1007 {
1008 if (1
1009 # endif
1010 # ifdef SIGSEGV_FAULT_STACKPOINTER
1011
1012
1013
1014 && ((old_sp >= stk_extra_stack
1015 && old_sp <= stk_extra_stack + stk_extra_stack_size)
1016 # if STACK_DIRECTION < 0
1017 || (old_sp <= vma.start + 4096
1018 && vma.start <= old_sp + 4096))
1019 # else
1020 || (old_sp <= vma.end + 4096
1021 && vma.end <= old_sp + 4096))
1022 # endif
1023 # endif
1024 )
1025 # endif
1026 # else
1027
1028
1029 uintptr_t addr = (uintptr_t) address;
1030
1031 if ((addr <= old_sp + 4096 && old_sp <= addr + 4096)
1032 # ifdef __ia64
1033 || (addr <= old_bsp + 4096 && old_bsp <= addr + 4096)
1034 # endif
1035 )
1036 {
1037 {
1038 {
1039 # endif
1040 {
1041 # ifdef SIGSEGV_FAULT_STACKPOINTER
1042 int emergency =
1043 (old_sp >= stk_extra_stack
1044 && old_sp <= stk_extra_stack + stk_extra_stack_size);
1045 stackoverflow_context_t context = (SIGSEGV_FAULT_CONTEXT);
1046 # else
1047 int emergency = 0;
1048 stackoverflow_context_t context = (void *) 0;
1049 # endif
1050
1051 (*stk_user_handler) (emergency, context);
1052 }
1053 }
1054 }
1055 }
1056 }
1057 # endif
1058
1059 if (user_handler && (*user_handler) (address, 1))
1060 {
1061
1062 }
1063 else
1064 {
1065
1066
1067
1068 SIGSEGV_FOR_ALL_SIGNALS (signo, signal (signo, SIG_DFL);)
1069 }
1070
1071 # if HAVE_STACK_OVERFLOW_RECOVERY
1072 }
1073 # endif
1074 }
1075
1076 #elif HAVE_STACK_OVERFLOW_RECOVERY
1077
1078 static void
1079 # ifdef SIGSEGV_FAULT_STACKPOINTER
1080 sigsegv_handler (SIGSEGV_FAULT_HANDLER_ARGLIST)
1081 # else
1082 sigsegv_handler (int sig)
1083 # endif
1084 {
1085 # if !((HAVE_GETRLIMIT && defined RLIMIT_STACK) || defined SIGSEGV_FAULT_STACKPOINTER)
1086 # error "Insufficient heuristics for detecting a stack overflow. Either define SIGSEGV_FAULT_STACKPOINTER correctly, or undefine HAVE_STACK_OVERFLOW_RECOVERY!"
1087 # endif
1088
1089
1090 if (stk_user_handler)
1091 {
1092
1093 # ifdef SIGSEGV_FAULT_STACKPOINTER
1094 uintptr_t old_sp = (uintptr_t) (SIGSEGV_FAULT_STACKPOINTER);
1095 # endif
1096
1097
1098 if (stack_top)
1099 {
1100
1101 int saved_errno;
1102 struct vma_struct vma;
1103 int ret;
1104
1105 saved_errno = errno;
1106 ret = sigsegv_get_vma (stack_top, &vma);
1107 errno = saved_errno;
1108 if (ret >= 0)
1109 {
1110 # if HAVE_GETRLIMIT && defined RLIMIT_STACK
1111
1112
1113
1114 struct rlimit rl;
1115
1116 saved_errno = errno;
1117 ret = getrlimit (RLIMIT_STACK, &rl);
1118 errno = saved_errno;
1119 if (ret >= 0)
1120 {
1121 uintptr_t current_stack_size = vma.end - vma.start;
1122 uintptr_t max_stack_size = rl.rlim_cur;
1123 if (current_stack_size <= max_stack_size + 4096
1124 && max_stack_size <= current_stack_size + 4096
1125 # else
1126 {
1127 if (1
1128 # endif
1129 # ifdef SIGSEGV_FAULT_STACKPOINTER
1130
1131
1132
1133 && ((old_sp >= stk_extra_stack
1134 && old_sp <= stk_extra_stack + stk_extra_stack_size)
1135 # if STACK_DIRECTION < 0
1136 || (old_sp <= vma.start + 4096
1137 && vma.start <= old_sp + 4096))
1138 # else
1139 || (old_sp <= vma.end + 4096
1140 && vma.end <= old_sp + 4096))
1141 # endif
1142 # endif
1143 )
1144 {
1145 # ifdef SIGSEGV_FAULT_STACKPOINTER
1146 int emergency =
1147 (old_sp >= stk_extra_stack
1148 && old_sp <= stk_extra_stack + stk_extra_stack_size);
1149 stackoverflow_context_t context = (SIGSEGV_FAULT_CONTEXT);
1150 # else
1151 int emergency = 0;
1152 stackoverflow_context_t context = (void *) 0;
1153 # endif
1154
1155 (*stk_user_handler)(emergency,context);
1156 }
1157 }
1158 }
1159 }
1160 }
1161
1162
1163 SIGSEGV_FOR_ALL_SIGNALS (signo, signal (signo, SIG_DFL);)
1164 }
1165
1166 #endif
1167
1168
1169 #if HAVE_SIGSEGV_RECOVERY || HAVE_STACK_OVERFLOW_RECOVERY
1170
1171 static void
1172 install_for (int sig)
1173 {
1174 struct sigaction action;
1175
1176 # ifdef SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
1177 action.sa_sigaction = &sigsegv_handler;
1178 # else
1179 action.sa_handler = (void (*) (int)) &sigsegv_handler;
1180 # endif
1181
1182
1183
1184
1185
1186
1187
1188
1189 sigemptyset (&action.sa_mask);
1190 # ifdef SIGHUP
1191 sigaddset (&action.sa_mask,SIGHUP);
1192 # endif
1193 # ifdef SIGINT
1194 sigaddset (&action.sa_mask,SIGINT);
1195 # endif
1196 # ifdef SIGQUIT
1197 sigaddset (&action.sa_mask,SIGQUIT);
1198 # endif
1199 # ifdef SIGPIPE
1200 sigaddset (&action.sa_mask,SIGPIPE);
1201 # endif
1202 # ifdef SIGALRM
1203 sigaddset (&action.sa_mask,SIGALRM);
1204 # endif
1205 # ifdef SIGTERM
1206 sigaddset (&action.sa_mask,SIGTERM);
1207 # endif
1208 # ifdef SIGUSR1
1209 sigaddset (&action.sa_mask,SIGUSR1);
1210 # endif
1211 # ifdef SIGUSR2
1212 sigaddset (&action.sa_mask,SIGUSR2);
1213 # endif
1214 # ifdef SIGCHLD
1215 sigaddset (&action.sa_mask,SIGCHLD);
1216 # endif
1217 # ifdef SIGCLD
1218 sigaddset (&action.sa_mask,SIGCLD);
1219 # endif
1220 # ifdef SIGURG
1221 sigaddset (&action.sa_mask,SIGURG);
1222 # endif
1223 # ifdef SIGIO
1224 sigaddset (&action.sa_mask,SIGIO);
1225 # endif
1226 # ifdef SIGPOLL
1227 sigaddset (&action.sa_mask,SIGPOLL);
1228 # endif
1229 # ifdef SIGXCPU
1230 sigaddset (&action.sa_mask,SIGXCPU);
1231 # endif
1232 # ifdef SIGXFSZ
1233 sigaddset (&action.sa_mask,SIGXFSZ);
1234 # endif
1235 # ifdef SIGVTALRM
1236 sigaddset (&action.sa_mask,SIGVTALRM);
1237 # endif
1238 # ifdef SIGPROF
1239 sigaddset (&action.sa_mask,SIGPROF);
1240 # endif
1241 # ifdef SIGPWR
1242 sigaddset (&action.sa_mask,SIGPWR);
1243 # endif
1244 # ifdef SIGLOST
1245 sigaddset (&action.sa_mask,SIGLOST);
1246 # endif
1247 # ifdef SIGWINCH
1248 sigaddset (&action.sa_mask,SIGWINCH);
1249 # endif
1250
1251
1252 # ifdef SIGSEGV_FAULT_ADDRESS_FROM_SIGINFO
1253 action.sa_flags = SA_SIGINFO;
1254 # else
1255 action.sa_flags = 0;
1256 # endif
1257 # if HAVE_STACK_OVERFLOW_RECOVERY && HAVE_SIGALTSTACK
1258
1259
1260
1261
1262 if (stk_user_handler)
1263 action.sa_flags |= SA_ONSTACK;
1264 # endif
1265 sigaction (sig, &action, (struct sigaction *) NULL);
1266 }
1267
1268 #endif
1269
1270 int
1271 sigsegv_install_handler (sigsegv_handler_t handler)
1272 {
1273 #if HAVE_SIGSEGV_RECOVERY
1274 user_handler = handler;
1275
1276 SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);)
1277
1278 return 0;
1279 #else
1280 return -1;
1281 #endif
1282 }
1283
1284 void
1285 sigsegv_deinstall_handler (void)
1286 {
1287 #if HAVE_SIGSEGV_RECOVERY
1288 user_handler = (sigsegv_handler_t)NULL;
1289
1290 # if HAVE_STACK_OVERFLOW_RECOVERY
1291 if (!stk_user_handler)
1292 # endif
1293 {
1294 SIGSEGV_FOR_ALL_SIGNALS (sig, signal (sig, SIG_DFL);)
1295 }
1296 #endif
1297 }
1298
1299 int
1300 sigsegv_leave_handler (void (*continuation) (void*, void*, void*),
1301 void* cont_arg1, void* cont_arg2, void* cont_arg3)
1302 {
1303 #if HAVE_STACK_OVERFLOW_RECOVERY
1304
1305
1306
1307
1308
1309 sigsegv_reset_onstack_flag ();
1310 #endif
1311 (*continuation) (cont_arg1, cont_arg2, cont_arg3);
1312 return 1;
1313 }
1314
1315 int
1316 stackoverflow_install_handler (stackoverflow_handler_t handler,
1317 void *extra_stack, size_t extra_stack_size)
1318 {
1319 #if HAVE_STACK_OVERFLOW_RECOVERY
1320 # if HAVE_STACKVMA
1321 if (!stack_top)
1322 {
1323 int dummy;
1324 remember_stack_top (&dummy);
1325 if (!stack_top)
1326 return -1;
1327 }
1328 # endif
1329
1330 stk_user_handler = handler;
1331 stk_extra_stack = (uintptr_t) extra_stack;
1332 stk_extra_stack_size = extra_stack_size;
1333 {
1334 stack_t ss;
1335 # if SIGALTSTACK_SS_REVERSED
1336 ss.ss_sp = (char *) extra_stack + extra_stack_size - sizeof (void *);
1337 ss.ss_size = extra_stack_size - sizeof (void *);
1338 # else
1339 ss.ss_sp = extra_stack;
1340 ss.ss_size = extra_stack_size;
1341 # endif
1342 ss.ss_flags = 0;
1343 if (sigaltstack (&ss, (stack_t*)0) < 0)
1344 return -1;
1345 }
1346
1347
1348 SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);)
1349 return 0;
1350 #else
1351 return -1;
1352 #endif
1353 }
1354
1355 void
1356 stackoverflow_deinstall_handler (void)
1357 {
1358 #if HAVE_STACK_OVERFLOW_RECOVERY
1359 stk_user_handler = (stackoverflow_handler_t) NULL;
1360
1361 # if HAVE_SIGSEGV_RECOVERY
1362 if (user_handler)
1363 {
1364
1365
1366 SIGSEGV_FOR_ALL_SIGNALS (sig, install_for (sig);)
1367 }
1368 else
1369 # endif
1370 {
1371 SIGSEGV_FOR_ALL_SIGNALS (sig, signal (sig, SIG_DFL);)
1372 }
1373
1374 {
1375 stack_t ss;
1376 ss.ss_flags = SS_DISABLE;
1377 if (sigaltstack (&ss, (stack_t *) 0) < 0)
1378 perror ("gnulib sigsegv (stackoverflow_deinstall_handler)");
1379 }
1380 #endif
1381 }