- } else {
- /* We are allocating memory. Make sure we have an open file
- descriptor if not working with anonymous memory. */
- if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
- fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
- abort();
- } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
- /* The request would move us past the end of the currently
- mapped memory, so map in enough more memory to satisfy
- the request. This means we also have to grow the mapped-to
- file by an appropriate amount, since mmap cannot be used
- to extend a file. */
- moveto = PAGE_ALIGN((char *) mdp->breakval + size);
- mapbytes = (char *) moveto - (char *) mdp->top;
- foffset = (char *) mdp->top - (char *) mdp->base;
-
- if (mdp->fd > 0) {
- /* FIXME: Test results of lseek() */
- lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
- test = write(mdp->fd, &buf, 1);
- if (test == -1) {
- fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
- abort();
- }
- }
-
- /* Let's call mmap. Note that it is possible that mdp->top
- is 0. In this case mmap will choose the address for us */
- mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
- MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
- MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
-
- if (mapto == (void *) -1/* That's MAP_FAILED */) {
- fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s",strerror(errno));
- abort();
- }
-
- if (mdp->top == 0)
- mdp->base = mdp->breakval = mapto;
-
- mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
- result = (void *) mdp->breakval;
- mdp->breakval = (char *) mdp->breakval + size;
- } else {
- result = (void *) mdp->breakval;
- mdp->breakval = (char *) mdp->breakval + size;
+ } else if ((char*)mdp->breakval + size > (char*)mdp->top) {
+ /* The request would move us past the end of the currently mapped memory, so map in enough more memory to satisfy
+ the request. This means we also have to grow the mapped-to file by an appropriate amount, since mmap cannot
+ be used to extend a file. */
+ moveto = PAGE_ALIGN((char*)mdp->breakval + size);
+ mapbytes = (char*)moveto - (char*)mdp->top;
+
+ /* Let's call mmap. Note that it is possible that mdp->top is 0. In this case mmap will choose the address for us.
+ This call might very well overwrite an already existing memory mapping (leading to weird bugs).
+ */
+ mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+
+ if (mapto == MAP_FAILED) {
+ char buff[1024];
+ fprintf(stderr, "Internal error: mmap returned MAP_FAILED! error: %s\n", strerror(errno));
+ snprintf(buff, 1024, "cat /proc/%d/maps", getpid());
+ int status = system(buff);
+ if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
+ fprintf(stderr, "Something went wrong when trying to %s\n", buff);
+ sleep(1);
+ abort();