4

I'd like to make malloc() fail by limiting the memory available.

$ ulimit -v 1000
$ ./main.exe 10000000
0x102bfb000

But even with ulimit, the following program still finishes correctly. Does anybody know how to make malloc() fail? Thanks.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    size_t size = atoi(argv[1]);
    void *ptr = NULL;

    if((ptr = malloc(size)) == NULL) {
        perror("malloc()");
        exit(1);
    }

    printf("%p\n", ptr);
    free(ptr);
    return 0;
}

EDIT: The above is on Mac OS X.

On Linux, I got segmentation fault. Why malloc() can cause segmentation fault? How to make malloc() return a NULL pointer?

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • you want -m don't you? -v just limits virtual memory – Jerry Jeremiah Feb 12 '19 at 22:54
  • `ulimit -Sm 1` still does not cause any problem with `realloc()`. – user1424739 Feb 12 '19 at 22:57
  • What shell are you using on what OS? If I do `ulimit -v 1` on dash, bash and fish on Linux 4.15 pretty much anything breaks due to failed allocations even before managing to launch your program. For the rest `ulimit -v` with bigger values works as expected on my machine - keeping in mind that `ulimit -v` takes a value in KiB, while your program takes it in bytes. – Matteo Italia Feb 12 '19 at 23:26
  • https://unix.stackexchange.com/questions/44985/limit-memory-usage-for-a-single-linux-process – Hans Passant Feb 12 '19 at 23:29
  • I use bash on Mac OS X. When call `ulimit -v 1`, nothing breaks. I am not sure what is wrong. – user1424739 Feb 13 '19 at 00:33

2 Answers2

2

The memory limit of 1MB is not big enough to reach the actual malloc call. On my linux system alone libc is 2MB. So 1MB is not enough memory to completely load the application and even reach the main function.

For me, using ulimit -v 5000 is sufficient to print the malloc(): Cannot allocate memory message.

Joachim
  • 71
  • 6
1

Based on documentation: In case that ptr is a null pointer, the function behaves like malloc, assigning a new block of size bytes and returning a pointer to its beginning

If you want to limit memory that program can allocate you can use:

#include <sys/time.h>
#include <sys/resource.h>
rlimit l;
getrlimit(RLIMIT_AS, &l);
l.rlim_cur = 1000;
setrlimit(RLIMIT_AS, &l);

http://man7.org/linux/man-pages/man2/setrlimit.2.html

  • My question is how to make `realloc()` fail. I don't think your answer addresses my question. – user1424739 Feb 12 '19 at 22:53
  • I edited my answer to answer your question. But based on your code you should change realloc to malloc and change the title of question. Because you want to fail to allocate memory not to reallocate it. – Michał Marszałek Feb 12 '19 at 23:13
  • @MichałMarszałek I am glad to know how to limit it - I have wondered about that in the past. Just out of curiosity, do you know why ulimit doesn't stop it from working? – Jerry Jeremiah Feb 13 '19 at 00:15
  • This works on Linux. But again has no effect on Mac OS X. @MichałMarszałek Why ulimit does not work on Mac and why it cause segment fault on Linux? – user1424739 Feb 13 '19 at 03:29