Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / src / xbt / mmalloc / mmorecore.c
1 /* Support for an sbrk-like function that uses mmap. */
2
3 /* Copyright (c) 2010-2021. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 /* Copyright 1992, 2000 Free Software Foundation, Inc.
10
11    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com */
12
13 #include "src/internal_config.h"
14 #if HAVE_UNISTD_H
15 #include <unistd.h>             /* Prototypes for lseek */
16 #endif
17 #include <stdio.h>
18 #include <fcntl.h>
19 #include <sys/mman.h>
20 #include <sys/wait.h>
21 #include <errno.h>
22
23 #include "mmprivate.h"
24
25 #ifndef MAP_ANONYMOUS
26 #define MAP_ANONYMOUS MAP_ANON
27 #endif
28
29 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + xbt_pagesize - 1) &   \
30                                   ~((long)xbt_pagesize - 1))
31
32 /* Return MAP_PRIVATE if MDP represents /dev/zero.  Otherwise, return
33    MAP_SHARED.  */
34 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
35                                     ? MAP_PRIVATE                       \
36                                     : MAP_SHARED)
37
38 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
39 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
40                                ? MAP_ANONYMOUS                      \
41                                : 0)
42
43 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
44 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
45                              ? -1                                 \
46                              : (MDP) -> fd)
47
48 /* Return 0if MDP uses anonymous mapping. Otherwise, return off */
49 #define MAP_ANON_OR_OFFSET(MDP, off) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
50                              ? 0                                           \
51                              : off)
52
53 /** @brief Add memory to this heap
54  *
55  *  Get core for the memory region specified by MDP, using SIZE as the
56  *  amount to either add to or subtract from the existing region.  Works
57  *  like sbrk(), but using mmap().
58  *
59  *  It never returns NULL. Instead, it dies verbosely on errors.
60  *
61  *  @param mdp  The heap
62  *  @param size Bytes to allocate for this heap (or <0 to free memory from this heap)
63  */
64 void *mmorecore(struct mdesc *mdp, ssize_t size)
65 {
66   void* result;                 // please keep it uninitialized to track issues
67   off_t foffset;                /* File offset at which new mapping will start */
68   size_t mapbytes;              /* Number of bytes to map */
69   void* moveto;                 /* Address where we wish to move "break value" to */
70   void* mapto;                  /* Address we actually mapped to */
71   char buf = 0;                 /* Single byte to write to extend mapped file */
72
73   if (size == 0) {
74     /* Just return the current "break" value. */
75     return mdp->breakval;
76   }
77
78   if (size < 0) {
79     /* We are deallocating memory.  If the amount requested would cause us to try to deallocate back past the base of
80      * the mmap'd region then die verbosely.  Otherwise, deallocate the memory and return the old break value. */
81     if (((char*)mdp->breakval) + size >= (char*)mdp->base) {
82       result        = mdp->breakval;
83       mdp->breakval = (char*)mdp->breakval + size;
84       moveto = PAGE_ALIGN(mdp->breakval);
85       munmap(moveto, (size_t)(((char*)mdp->top) - ((char*)moveto)) - 1);
86       mdp->top = moveto;
87     } else {
88       fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
89       abort();
90     }
91   } else {
92     /* We are allocating memory. Make sure we have an open file descriptor if not working with anonymous memory. */
93     if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
94       fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
95       abort();
96     } else if ((char*)mdp->breakval + size > (char*)mdp->top) {
97       /* The request would move us past the end of the currently mapped memory, so map in enough more memory to satisfy
98          the request.  This means we also have to grow the mapped-to file by an appropriate amount, since mmap cannot
99          be used to extend a file. */
100       moveto   = PAGE_ALIGN((char*)mdp->breakval + size);
101       mapbytes = (char*)moveto - (char*)mdp->top;
102       foffset  = (char*)mdp->top - (char*)mdp->base;
103
104       if (mdp->fd > 0) {
105         if (lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET) == -1) {
106           fprintf(stderr, "Internal error: lseek into mmap'ed fd failed! error: %s", strerror(errno));
107           abort();
108         }
109         if (write(mdp->fd, &buf, 1) == -1) {
110           fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
111           abort();
112         }
113       }
114
115       /* Let's call mmap. Note that it is possible that mdp->top is 0. In this case mmap will choose the address for us.
116          This call might very well overwrite an already existing memory mapping (leading to weird bugs).
117        */
118       mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
119                    MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
120                    MAP_FIXED, MAP_ANON_OR_FD(mdp), MAP_ANON_OR_OFFSET(mdp, foffset));
121
122       if (mapto == MAP_FAILED) {
123         char buff[1024];
124         fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s\n",strerror(errno));
125         snprintf(buff,1024,"cat /proc/%d/maps",getpid());
126         int status = system(buff);
127         if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
128           fprintf(stderr, "Something went wrong when trying to %s\n", buff);
129         sleep(1);
130         abort();
131       }
132
133       if (mdp->top == 0)
134         mdp->base = mdp->breakval = mapto;
135
136       mdp->top      = PAGE_ALIGN((char*)mdp->breakval + size);
137       result        = mdp->breakval;
138       mdp->breakval = (char*)mdp->breakval + size;
139     } else {
140       /* Memory is already mapped, we only need to increase the breakval: */
141       result        = mdp->breakval;
142       mdp->breakval = (char*)mdp->breakval + size;
143     }
144   }
145   return (result);
146 }
147
148 void* __mmalloc_remap_core(const s_xbt_mheap_t* mdp)
149 {
150   /* FIXME:  Quick hack, needs error checking and other attention. */
151
152   return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
153               PROT_READ | PROT_WRITE | PROT_EXEC,
154               MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);
155 }
156