aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloca/needstack.c
blob: 094b3ad7778dddacb5062f586086acab729c8b53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * slibc — Yet another C library
 * Copyright © 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <slibc/internals.h>
#include <alloca.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>



/**
 * Unless the stack can grow with `n` bytes
 * without overflowing, the program shall
 * either be killed by the kernel (SIGSEGV)
 * or this function shall indicate that the
 * stack cannot be grown.
 * 
 * @param   n  The number of bytes the stack must be able to grow.
 * @return     0 if the stack can grow, 1 otherwise.
 */
int stack_will_overflow(intptr_t n)
{
  void* prev_brk;
  
  /* TODO This function shall block calls to brk and sbrk. */
  
  prev_brk = sbrk(n);
  if (prev_brk == (void*)-1)
    return 1;
  if (brk(prev_brk))
    return 1; /* Should not happen. */
  
  {
    /* TODO This function shall block calls to signal.h functions. */
    
    sigset_t old_mask;
    sigset_t new_mask;
    void* old_sigsegv_handler;
    
    if (sigemptyset(&new_mask) || sigaddset(&new_mask, SIGSEGV))
      return 1;
    
    old_sigsegv_handler = signal(SIGSEGV, SIG_DFL);
    /* Sorry, we cannot have a signal handler, the OS kernel
     * will make the CPU halt and catch fire until we die. */
    if (old_sigsegv_handler == SIG_ERR)
      return 1;
    
    if (pthread_sigmask(SIG_UNBLOCK, &new_mask, &old_mask))
      return 1;
    
    (void) alloca((size_t)n);
    
    if (pthread_sigmask(SIG_SETMASK, &old_mask, NULL))
      return 1;
    
    if (signal(SIGSEGV, old_sigsegv_handler) == SIG_ERR)
      return 1;
  }
  
  return 0;
}


/**
 * Check for execution stack overflow. If the stack of
 * overflow, or will overflow, the program shall abort.
 * 
 * It is possible that the process is killed by SIGSEGV
 * instead of aborting.
 * 
 * This is a Plan 9 from Bell Labs extension.
 * 
 * @etymology  I (need) addition (stack) space.
 * 
 * @param  n  The number of bytes the stack will grow by.
 *            0 can be used to check for a current stack
 *            overflow. Must be non-negative.
 * 
 * @since  Always.
 */
void (needstack)(int n)
{
  int tty;
  
  if (stack_will_overflow((intptr_t)n) == 0)
    return;
  
  tty = isatty(STDERR_FILENO);
  fprintf(stderr,
	  _("%(\033[00;01m%)%s%(\033[00m%): "
	    "%(\033[31m%)%s%(\033[00m%)"
	    "%(: %(\033[31m%)hinted stack growth: %i bytes%(\033[00m%)%)\n"),
	  tty, program_invocation_name, tty,
	  tty, (n ? _("imminent stack overflow detected") : _("stack overflow detected")), tty,
	  n != 0, tty, n, tty);
  
  fflush(NULL); /* Flush all streams. */
  abort();
}


/**
 * This function is called if the `needstack` macro is called.
 * It enhances the `needstack` function with detail on where
 * the program failed.
 * 
 * You should not use this function directly.
 * 
 * @param  n     The number of bytes the stack will grow by. 0 can be used
 *               to check for a current stack overflow. Must be non-negative.
 * @param  file  The filename of the source cose whence the check was made.
 * @param  line  The line in the source code whence the check was made.
 * @param  func  The function in the source code whence the check was made,
 *               `NULL` if unknown (C99 is required.)
 */
void __needstack(int n, const char* file, int line, const char* func)
{
  int tty;
  
  if (stack_will_overflow((intptr_t)n) == 0)
    return;
  
  tty = isatty(STDERR_FILENO);
  fprintf(stderr,
	  _("%(\033[00;01m%)%s%(\033[00m%): "
	    "%(\033[31m%)%s%(\033[00m%) "
	    "at line %(\033[33m%)%i%(\033[00m%) "
	    "of file %(\033[35m%)%s%(\033[00m%)"
	    "%(, function %(\033[1;34m%)%s%(\033[00m%)%)"
	    "%(: %(\033[31m%)hinted stack growth: %i bytes%(\033[00m%)%)\n"),
	  tty, program_invocation_name, tty,
	  tty, (n ? _("imminent stack overflow detected") : _("stack overflow detected")), tty,
	  tty, line, tty,
	  tty, file, tty,
	  func != NULL, tty, func, tty,
	  n != 0, tty, n, tty);
  
  fflush(NULL); /* Flush all streams. */
  abort();
}