4 ms·
If you leave something in a corrupted state when execution is aborted at some random point, that means that your code (and possibly your on-disk data structure)
by zAy0LfpBZLC8mAC 11y ago
If you leave something in a corrupted state when execution is aborted at some random point, that means that your code (and possibly your on-disk data structure) is defective. It is perfectly possible to handle that case safely (using transactions, journaling, rollback, atomic replace, ...).
However, if you don't check for allocation failures, you get undefined behaviour, which you indeed cannot handle safely ... other than by avoiding the undefined behaviour in the first place by checking for allocation failures.
Also, if you check for allocation failures, you won't get a SIGSEGV. SIGSEGV is for invalid virtual addresses, not for lack of resources.
- nightski 11y agoYou might be technically right, but I guarantee you that by your definition the vast majority of software out there is defective. The solutions you mention are non-trivial. It is really easy to make a mistake. While it may not be the operating systems fault at the end of the day, designing a system like this is setting up the overall experience for failure. A well designed system should make it easy to get right and not the other way around.
- zAy0LfpBZLC8mAC 11y ago> You might be technically right, but I guarantee you that by your definition the vast majority of software out there is defective. Your point being? That it's untechnically correct? > The solutions you mention are non-trivial. It is really easy to make a mistake. While it may not be the operating systems fault at the end of the day, designing a system like this is setting up the overall experience for failure. A well designed system should make it easy to get right and not the other way around. Which is all kindof true, but doesn't change that those APIs are the way they are, for historical reasons. Just because some API is broken, doesn't mean your code will work correctly when you pretend it's not broken. edit: just to be sure: yes, the special-casing of zero-sized allocations is somewhat of a bug, which we still have to live with. Other than that, the API is actuall perfectly fine--if you don't check for allocation failures and you can't handle it if your program gets interrupted at any point, that's just a bug in your code, there is no useful general way to abstract those problems away.
- deleted 11y ago[deleted]
- joosters 11y agoYou can get memory errors outside of malloc() on an OS that overcommits RAM. Whether that is a SIGSEGV or some other signal, or just the heavy boots of the OOM killer nuking your process, doesn't really matter. The key point is that your program cannot handle them. It is impossible to handle every case. Allocation failures can and do occur outside of malloc() when overcommit is the memory policy. Malloc might return memory that it believes is valid, but when you try to write to it later on, the OS discovers that it has no free space in the swap and no spare pages to evict. Result: process death (SIGSEGV? SIGBUS? not sure, but it doesn't matter)
- zAy0LfpBZLC8mAC 11y ago> You can get memory errors outside of malloc() on an OS that overcommits RAM. Did anyone claim otherwise? > Whether that is a SIGSEGV or some other signal, or just the heavy boots of the OOM killer nuking your process, doesn't really matter. It does, because you can catch SIGSEGV. > The key point is that your program cannot handle them. It is impossible to handle every case. It can, and it has to, if you don't want it to be defective. (Where "handling" does not mean "continue execution", but "don't corrupt persistent state"--you cannot continue execution with insufficient resources anyway, there is no way around that). > Result: process death (SIGSEGV? SIGBUS? not sure, but it doesn't matter) SIGKILL. See above.
- btilly 11y agoHere is the mechanism that I wish existed... Have a system call that allocates memory to a process or process group, but doesn't assign it. This call will fail if overall memory usage is too high. When the process tries to get memory, it will only tap into that reserved allocation if the system is out of memory. The OOM killer will never kill processes that have reserved spare memory. The result? Long-running daemons with stable memory usage can be relatively easily protected against the OOM killer. Ideally the OOM killer never runs amok. But in the real world, it would be nice to be able to limit the damage if it has, and guarantee a usable shell for a superuser while it is going on.
- zAy0LfpBZLC8mAC 11y ago