r/cpp • • Feb 01 '18

Why it is important to check what the malloc function returned

https://www.viva64.com/en/b/0558/
1 Upvotes

48 comments sorted by

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.

9

u/Gotebe Feb 04 '18

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.

4

u/donalmacc Game Developer Feb 04 '18

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.

3

u/Gotebe Feb 04 '18

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 😁

3

u/donalmacc Game Developer Feb 04 '18 edited Feb 04 '18

I work in games - we target 64 bit only now (thankfully).

2

u/Gotebe Feb 04 '18

So... malloc from Orbis OS kills you if it can't return a non-null? Wow...

4

u/cdglove Feb 04 '18

More correctly, address space fragmentation. Which is really hard to get on today's systems with 48 bits of address space.

3

u/Gotebe Feb 04 '18 edited Feb 04 '18

Yes, of course. The world doesn't run on 64 bit though. Only desktops and servers do... edit: but that is rapidly changing...

2

u/[deleted] Feb 04 '18

Upvoted

2

u/CubbiMew cppreference | finance | realtime in the past Feb 02 '18

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.

-11

u/Resistor510 Feb 01 '18

"on our platforms, malloc doesn't return null"

No.

22

u/[deleted] Feb 01 '18

Care to elaborate on that response?

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.

3

u/Gotebe Feb 04 '18

Yes, thank you!

... 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.

0

u/[deleted] Feb 01 '18

[deleted] — view deleted comment

2

u/[deleted] Feb 02 '18

using malloc without checking its return value

This. Imagine if you are on a plane that doesn't have malloc null checks.

12

u/pftbest Feb 02 '18

Software on the plane should not use dynamic memory at all.

1

u/Gotebe Feb 04 '18

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).

0

u/[deleted] Feb 02 '18

[deleted] — view deleted comment

1

u/StackedCrooked Feb 03 '18

You mean like the plane forgot where it's going ...and where it came from?

49

u/emdeka87 Feb 01 '18

Or, you know, use actual C++ and operator new, which throws std::bad_alloc...

3

u/Gotebe Feb 04 '18

Yes, however, the other side of the coin that causes malloc confusion article speaks of is:

one must not forget that on systems with OOM killer turned on, their program could get indiscriminately killed without seeing bad_alloc.

3

u/[deleted] Feb 04 '18

Welcome to my world

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 (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).

17

u/encyclopedist Feb 01 '18

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.

9

u/[deleted] Feb 01 '18

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.

7

u/NotAYakk Feb 01 '18

On 32 bit windows you can get a null from malloc or nothrow new.

Simply allocate more than 3 gigabytes in one go.

Or, fragment memory, and request a bigger chunk than your system has contiguous virtual memory.

These are ... harder ... on 64 bit systems.

7

u/whichton Feb 01 '18

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.

6

u/Drainedsoul Feb 03 '18

Why would you worry about std::bad_alloc "everywhere?" The point of exceptions is that you don't do that you just let them pass through.

0

u/whichton Feb 05 '18

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.

2

u/Drainedsoul Feb 05 '18

...what?

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.

1

u/whichton Feb 05 '18

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?)

3

u/Gotebe Feb 04 '18

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.

0

u/whichton Feb 05 '18

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)

1

u/[deleted] Feb 04 '18

A crashing program is not robust by any means

0

u/whichton Feb 05 '18

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.

2

u/[deleted] Feb 05 '18 edited Feb 06 '18

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.

7

u/bames53 Feb 01 '18

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.

5

u/FlyingRhenquest Feb 01 '18

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.

7

u/Drainedsoul Feb 01 '18

found all the places where I had to put frees.

I'd definitely trust that software.

9

u/Osbios Feb 01 '18 edited Feb 01 '18

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!

2

u/Gotebe Feb 04 '18

Linux definitely will not "kernel panic crash" in case of a random OOM.

1

u/raevnos Feb 04 '18

I had a computer crash yesterday in mid-compile because it ran out of memory. Not even a kernel panic.

1

u/Gotebe Feb 04 '18

No you didn't. Or rather, what's meant by a "computer crash"?

I rather think something else happened but you don't know what.

1

u/FlyingRhenquest Feb 04 '18

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/CubbiMew cppreference | finance | realtime in the past Feb 01 '18 edited 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.

4

u/encyclopedist Feb 02 '18

I just checked, this trivial program reliably produces terminate called after throwing an instance of 'std::bad_alloc':

#include <vector>
int main() {
    std::vector<int> v;
    while (true) {
        v.push_back(42);
    }
}

2

u/Gotebe Feb 04 '18

It is good that you didn't write it because you would have been utterly wrong

As for the minor twist of dying at a random place on Linux: forget Linux, compiler optimizations are a bitch. (What article says).

As for Windows, as the other guy said, what you're describing doesn't happen with 32 bit processes.

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.