root/trampolines.cpp
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- sbrk_wrapper
- sbrk_trampoline
- _dmtcp_setup_trampolines
1 /****************************************************************************
2 * Copyright (C) 2006-2013 by Tyler Denniston and Kapil Arya *
3 * tyler@ccs.neu.edu and kapil@ccs.neu.edu *
4 * *
5 * This file is part of DMTCP. *
6 * *
7 * DMTCP is free software: you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * DMTCP is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with DMTCP:dmtcp/src. If not, see *
19 * <http://www.gnu.org/licenses/>. *
20 ****************************************************************************/
21
22 #include "trampolines.h"
23 #include "syscallwrappers.h"
24 #include "../jalib/jassert.h"
25
26 static trampoline_info_t sbrk_trampoline_info;
27
28 /* All calls by glibc to extend or shrink the heap go through __sbrk(). On
29 * restart, the kernel may extend the end of data beyond where we want it. So
30 * sbrk will present an abstraction corresponding to the original end of heap
31 * before restart. FIXME: Potentially a user could call brk() directly, in
32 * which case we would want a wrapper for that too. */
33 static void *sbrk_wrapper(intptr_t increment)
34 {
35 static void *curbrk = NULL;
36 void *oldbrk = NULL;
37 /* Initialize curbrk. */
38 if (curbrk == NULL) {
39 /* The man page says syscall returns int, but unistd.h says long int. */
40 long retval = syscall(SYS_brk, NULL);
41 curbrk = (void *)retval;
42 }
43 oldbrk = curbrk;
44 curbrk = (void *)((char *)curbrk + increment);
45 if (increment > 0) {
46 syscall(SYS_brk, curbrk);
47 }
48 return oldbrk;
49 }
50
51 /* Calls to sbrk will land here. */
52 static void *sbrk_trampoline(intptr_t increment)
53 {
54 /* Unpatch sbrk. */
55 UNINSTALL_TRAMPOLINE(sbrk_trampoline_info);
56 void *retval = sbrk_wrapper(increment);
57 /* Repatch sbrk. */
58 INSTALL_TRAMPOLINE(sbrk_trampoline_info);
59 return retval;
60 }
61
62 /* Any trampolines which should be installed are done so via this function.
63 Called from DmtcpWorker constructor. */
64 void _dmtcp_setup_trampolines()
65 {
66 dmtcp_setup_trampoline("sbrk", (void*) &sbrk_trampoline,
67 &sbrk_trampoline_info);
68 }