summaryrefslogtreecommitdiff
path: root/lib/mmap-stack.c
blob: 30d1da8d1d1aa5001ffb438ed69d5a6f37206e29 (plain)
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
/* Run with a larger (mmap'd) stack.

   Copyright (C) 2003 Free Software Foundation, Inc.

   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 2, 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, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* Written by Jim Meyering.  */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <stdarg.h>
#if HAVE_SYS_MMAN_H
# include <sys/mman.h>
#endif
#if HAVE_UCONTEXT_H
# include <ucontext.h>
#endif

#include "mmap-stack.h"

#ifndef MAP_ANONYMOUS
# ifdef MAP_ANON
#  define MAP_ANONYMOUS MAP_ANON
# else
#  define MAP_ANONYMOUS 0
# endif
#endif

/* Set up context, *CTX, so that it may be used via makecontext,
   using a block of SIZE bytes of mmap'd memory for its stack.
   Return nonzero upon error.  */
static int
get_context (ucontext_t *ctx, size_t size)
{
  void *stack;
  int fd = -1;  /* This must be -1 in order for Solaris' MAP_ANON to work.  */

  if (getcontext (ctx))
    return 1;

  /* use tmpfile if MAP_ANONYMOUS is not useful. */
#if MAP_ANONYMOUS == 0
  {
    FILE *fp = tmpfile ();
    if (!fp)
      return 1;
    fd = fileno (fp);
  }
#endif

  stack = mmap (NULL, size,
		PROT_READ | PROT_WRITE,
		MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
  if (stack == MAP_FAILED)
    return 1;
  ctx->uc_stack.ss_sp = stack;
  ctx->uc_stack.ss_size = size;
  ctx->uc_link = NULL;
  return 0;
}

/* Yep, this is arbitrary.  tough :-)  */
#define ARGC_MAX 6

/* Set up a context without conventional stack limitations,
   then call function FUNC with the specified number (ARGC) of arguments.
   If get_context and setcontext succeed, this function does not return,
   so FUNC must be sure to exit appropriately.  Otherwise, this function
   does return, and the caller should diagnose the failure.  */
void
run_on_mmaped_stack (void (*func) (void), size_t argc, ...)
{
    ucontext_t ctx;
    size_t size = 256 * 1024 * 1024;
    long argv[ARGC_MAX];
    unsigned i;
    va_list ap;

    assert (argc <= ARGC_MAX);
    va_start (ap, argc);
    for (i = 0; i < argc; i++)
      argv[i] = va_arg (ap, long);
    va_end (ap);

    if (get_context (&ctx, size) == 0)
      {
	makecontext (&ctx, func, argc,
		     argv[0], argv[1], argv[2],
		     argv[3], argv[4], argv[5]);
	setcontext (&ctx);
      }

    /* get_context or setcontext failed;
       Resort to using the conventional stack.  */
}