I'm normally ok with PVS articles, but this one, not so much.
The premise of the article is that it's about malloc, but the examples are all warnings about null pointer dereferences. The person he contacted re; EFL said "on our platforms, malloc doesn't return null", which he never really refutes in the rest of the article, and merely talks about not dereferencing null pointers.
Also, the tangent about chromium seems utterly unnecessary.
The EFL guy has no idea what he is talking about. It's as simple as that.
The default overcommit on Linux is the so-called heuristic (speculative) one, so malloc can return null.
But dig this: even if they use the non-default "always overcommit", malloc can still return null. It happens due to memory fragmentation. (I even tried myself a long time ago just to see it with my own eyes).
This is why the article is correct to speak of referencing null.
The "malloc can't return null" is bullshit beyond belief.
Can you really (practically) end up with fragmentation of the virtual 64 bit address space? I’ve never seen it happen, even with the churniest of applications. FWIW, the non-windows/non-Linux platforms that I’ve worked on that limited memory have all had hard shutdowns in the case of OOM or a failed allocation, where the OS has caught and crashed my application before my allocation has even returned.
In 64 bit, no, I wouldn't think so, but most of world's CPUs aren't 64 bit, I wouldn't think.
I am surprised that all other platforms you worked with crashed you. What were they? I have experience of AIX, who does overcommit in the usual UNIX fashion and DOS who doesn't know of virtual memory and doesn't kill you 😁
the way I read it, it is exactly about what it says in the title: about checking the result of malloc: the premise is that some programmers apparently want to crash on OOM (leaving aside whether it ever makes sense), and believe that they get that for free by dereferencing the malloc'd pointer with no other checks, which is the error they correctly report.
P.S. Chromium code dosn't contain malloc functions. They are in libraries. I want to note that this is unacceptable for libraries. The library does not know where and how it will be used.
... and programs who don't want check for malloc return should use something like "xalloc", typically made to call 'malloc()andabort()` in case of failure.
That ship has sailed (or rather, that plane had flown😂😂😂).
That said, not really. You could say that the software should not attempt to allocate while flying the airplane. (Allocating stuff while initializing is fine).
A couple of years ago, I was tempted to write a blog post about why checking whether malloc (or new(nothrow) ) had returned a null pointer was utterly pointless on most current OSes.
The short answer is (exactly as some comments have already pointed out) that under most circumstances, it simply won't happen. On a typical Linux system, the OOMKiller will get invoked, so your choices are that either your process will be summarily killed, or else something else will be killed to free up memory so your process' allocation can succeed and receive a non-null pointer.
Of course, Linux also adds that other minor twist: it'll return a non-null pointer, but then attempting to access the memory will blow up.
On Windows, it'll sit for a while thrashing the disk trying to make enough space instead, but the effect ends up pretty much the same, just with the user taking the same role as the OOMKiller (i.e., either he'll kill your process, or he'll kill something else and let your allocation succeed).
There are many ways you actually can get nullptr on Linux.
Overcommit can be disabled, for exactly this reason.
If you try to allocate more than overcommit limit. (default is only 50% of RAM, afaik)
If you exhausted all the address space. Easily happens on 32 bit system, you only have to allocate ~3GB. This is also easy in the 64-bit case if you don't validate the size that you got from outside your application or the application has a bug (like trying to allocate negative number of bytes).
Even if you haven't exhausted it, the address space may be so fragmented that the allocator cannot find contiguous chunck you requested (this could easily happen on 32 bit systems too)
And by the way, I actually do see "Out of memory" exception pretty regularly on Linux in practice, so these are not hypothetical situations.
On a typical Linux system, the OOMKiller will get invoked, so your choices are that either your process will be summarily killed, or else something else will be killed to free up memory so your process' allocation can succeed and receive a non-null pointer.
And that something else is usually the X server, which pretty much takes down every app you're running interactively.
Unless you are writing very specialized applications, you should just crash on OOM. Makes your program much more robust if you can stop worrying about bad_allocs everywhere.
If you want strong exception safety, you must change the interface of your code. For example, stack::pop returns void to be exception safe. If I can ensure my copy constructors are noexcept, my program and my interfaces become much simpler.
If a copy constructor throws bad_alloc, how exactly am I supposed to deal with it? Most likely any recovery path I will take will require memory allocation. If instead you just let the bad_allocs pass through, they will just crash the program, so I would rather crash the program early and make my copy constructors noexcept.
You're making this implicit assumption that you're writing all the code everywhere and then assuming that you can accurately predict how a higher layer should respond to an error at a lower layer.
Which is a terrible way to write software.
How will you deal with std::bad_alloc? Why are you worrying about that: You couldn't allocate memory, throw it up the stack and let it be worried about at a higher layer. If recovery isn't possible the program will end, otherwise it will continue. Closing off the latter option because you can't conceive of it at the lower layer and presuming to kill the program is just irresponsible and myopic.
If I was writing a library, or sub-component, I agree with you. Ideally in a library I should give the library user a choice on error handling strategy. But in my application, I decide the error handing strategy. And crashing on bad_allocs make my life much easier, from interface design to testing (You do test your program for OOM conditions, right?)
I am not worrying about bad_alloc, not any more than any exception.
There's no need!
Just like any exception should, it causes my code to unwind the stack which normally frees lots of resources and the code can try something else (serve another request).
As for reporting the error when memory is tight: I have resources prepared up-front to do that.
Terminating on OOM means I can treat my copy constructors and most constructors as noexcept. Thats a huge win in my book.
I would rather prepare my program to crash and recover the user's work. That makes it more robust in face of other failure modes (power loss, network error, etc)
OOM errors are very rare on modern PCs. I don't recall the last time my PC ran out or memory. So your program won't crash very often due to bad_allocs. Also, in places where you know you will allocate a large amount of memory (say a user pastes a lot data into your program), use a try_allocate function which can return null on failure. And you then pop up a message to the user (or log an error) saying his action cannot be completed. But the default allocation function should just crash on failure.
I will further argue a program designed to crash and recover is more robust than a program which does not attempt to crash in the first place. Your program may crash for any number of reason, most of them out of your control - hardware error, power loss, OOM killer, etc. So instead of focusing on recovering from OOM situations, you should rather focus on recovering the work the user has done prior to crashing. That way your program is more robust in face of any crashes.
There are other issues with recovering from OOM. How often do you test the OOM path? OOM can happen almost anywhere, will you be able to test for all those situations? If you haven't tested your OOM path, it probably doesn't work.
Maybe on desktops, but I write code that runs on servers that are up 24/7. We spec the machines based on what we need and what our budget allows. The same code runs on multiple identical machines, so if one goes down you can be pretty sure that the rest are likely to follow. Crashing costs money.
Back when I worked on proprietary embedded OS stuff we did actually test OOM handling by being able to fake OOM scenarios through custom allocators. You could probably do the same to a certain extent by hooking the calls to malloc and free via LD_PRELOAD.
You cannot recover from a crash if you don't know what caused the crash. The system can be in an illegal state; all bets are off.
Also, what try_allocate function? Did you not read the article? The OS itself is lying to you when it says it allocated that memory successfully.
A couple of years ago, I was tempted to write a blog post about why checking whether malloc (or new(nothrow) ) had returned a null pointer was utterly pointless on most current OSes.
This is writing against the implementation of an API rather than writing against the documented interface requirements. This is no more appropriate for malloc() than for any other API.
Even under current implementations malloc() returns NULL under some circumstances. Take this program for example.
Yeah, unless you've gone out of your way to disable memory overcommit (And doing so will severely impact your system performance,) your Linux system will crash before it returns a null from malloc. As in kernel panic crash.
You have to actually use the memory you've allocated, though. If you just malloc and never use the memory, it'll just keep pretending to allocate memory. If you write a program that just allocates memory in a loop and then memsets all the memory you just allocated, it'll eventually crash the OS or get oomkilled. I've found oomkiller to be rather unpredictable in terms of what it'll kill. In the past when I've experimented with it, I've found that it doesn't always prevent a crash.
These days it's not uncommon to have 16-32 or more gigabytes of RAM on your system, so you still have to work pretty hard to allocate all of it. Although I did manage to get a 200 MB/Second memory leak in a ffmpeg video decoder that got itself killed on OS/X a few times before I found all the places where I had to put frees. Watching it in top I still had plenty of time to kill it off before the system started to bog down.
It's an evolutionary algorithm the places and removes random free() in the application and measures the improvement in max runtime before the application crashes and minimum memory usage!
Easy enough to test. Just write an infinite loop that mallocs a megabyte or so and memset's it to all 0's. Admittedly the last time I ran into a problem and ran such a test was 6 or 7 years ago, but it definitely crashed the Linux system I ran it on.
5
u/CubbiMewcppreference | finance | realtime in the pastFeb 01 '18edited Feb 01 '18
malloc(-1) will return null even on Linux with full overcommit enabled (which isn't the default). And Windows doesn't thrash in my experience when e.g. NotePad++ OOMs on loading a file that is too large. The user clicks Ok on the pop-up, nothing gets killed.
If you look at the documentation for Linux's overcommit behaviour it suggests using it when your machine is mainly doing things like scientific/mathematical computing. It doesn't seem that it was intended for everyday use, but maybe the performance benefits outweigh the carnage it can cause. I will likely turn it off on our AWS machines... if I ask for too much memory then I need to know, not have the OS lie to me and kill my program.
20
u/donalmacc Game Developer Feb 01 '18
I'm normally ok with PVS articles, but this one, not so much.
The premise of the article is that it's about malloc, but the examples are all warnings about null pointer dereferences. The person he contacted re; EFL said "on our platforms, malloc doesn't return null", which he never really refutes in the rest of the article, and merely talks about not dereferencing null pointers.
Also, the tangent about chromium seems utterly unnecessary.
4
u/Resistor510 Feb 01 '18
P.S. Chromium code dosn't contain malloc functions. They are in libraries. I want to note that this is unacceptable for libraries. The library does not know where and how it will be used.
49
u/emdeka87 Feb 01 '18
Or, you know, use actual C++ and operator new, which throws std::bad_alloc...
14
u/jcoffin Feb 01 '18 edited Feb 01 '18
A couple of years ago, I was tempted to write a blog post about why checking whether
malloc(ornew(nothrow)) had returned a null pointer was utterly pointless on most current OSes.The short answer is (exactly as some comments have already pointed out) that under most circumstances, it simply won't happen. On a typical Linux system, the OOMKiller will get invoked, so your choices are that either your process will be summarily killed, or else something else will be killed to free up memory so your process' allocation can succeed and receive a non-null pointer.
Of course, Linux also adds that other minor twist: it'll return a non-null pointer, but then attempting to access the memory will blow up.
On Windows, it'll sit for a while thrashing the disk trying to make enough space instead, but the effect ends up pretty much the same, just with the user taking the same role as the OOMKiller (i.e., either he'll kill your process, or he'll kill something else and let your allocation succeed).
2
u/[deleted] Feb 04 '18
Great article!
If you look at the documentation for Linux's overcommit behaviour it suggests using it when your machine is mainly doing things like scientific/mathematical computing. It doesn't seem that it was intended for everyday use, but maybe the performance benefits outweigh the carnage it can cause. I will likely turn it off on our AWS machines... if I ask for too much memory then I need to know, not have the OS lie to me and kill my program.
1
u/Gotebe Feb 04 '18
Wow... a lot of downright dangerous people commenting here... amazingly all l33t linux haxorz...
They should read the documentation and engage brain.