summaryrefslogtreecommitdiff
path: root/Libraries/LibC/malloc.cpp
AgeCommit message (Collapse)Author
2021-01-01Kernel: Remove MAP_PURGEABLE from mmapTom
This brings mmap more in line with other operating systems. Prior to this, it was impossible to request memory that was definitely committed, instead MAP_PURGEABLE would provide a region that was not actually purgeable, but also not fully committed, which meant that using such memory still could cause crashes when the underlying pages could no longer be allocated. This fixes some random crashes in low-memory situations where non-volatile memory is mapped (e.g. malloc, tls, Gfx::Bitmap, etc) but when a page in these regions is first accessed, there is insufficient physical memory available to commit a new page.
2020-12-26LibC: Fix some incorrect printf usagesSahan Fernando
2020-11-16LibC: Notify UE at the start of free() instead of at the endAndreas Kling
This way, if we end up deallocating an entire ChunkedBlock, UE doesn't get confused thinking the freed pointer has never been allocated.
2020-11-14LibC: Move some of malloc's data structures into mallocdefs.hAndreas Kling
This allows UE to see what the heap metadata looks like.
2020-11-13LibC: Adjust malloc size classes to ensure 8-byte aligned pointersAndreas Kling
The pointers returned by malloc should always be 8-byte aligned on x86. We were not consistent about this, as some ChunkedBlock size classes were not divisible by 8. This fixes some OOB reads found by running GCC in UE.
2020-11-08UserspaceEmulator+LibC: Have UE notice realloc() and update accountingAndreas Kling
When a mallocation is shrunk/grown without moving, UE needs to update its precise metadata about the mallocation, since it tracks *exactly* how many bytes were allocated, not just the malloc chunk size.
2020-11-08LibC: Add two little assertions in malloc() and malloc_size()Andreas Kling
2020-09-25Meta+LibC through LibHTTP: Make clang-format-10 cleanBen Wiederhake
Why break at LibHTTP? Because "Meta+Libraries" would be insanely large, and breaking between LibHTTP and LibJS makes the commits roughly evenly large.
2020-08-17malloc: Keep some stats and dump them at process exit if ↵Nico Weber
LIBC_DUMP_MALLOC_STATS is set Very bare-bones and barely useful. Can go away once the perf_event-based malloc tracking is further along.
2020-08-17malloc: Remove unused Bitmap.h includeNico Weber
2020-08-17malloc: Fix build with RECYCLE_BIG_ALLOCATIONS not definedNico Weber
2020-08-17malloc: Add comments spelling out the MAGIC_HEADERS in asciiNico Weber
2020-08-16AK: Rename KB, MB, GB to KiB, MiB, GiBNico Weber
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9". The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30". Let's use the correct name, at least in code. Only changes the name of the constants, no other behavior change.
2020-08-13LibC: Some calloc() and realloc() improvements (#3108)Muhammad Zahalqa
If the space cannot be allocated, the original memory block shall remain unchanged and the function should return nullptr. Also add a function attribute and some null checks.
2020-08-12LibC: Avoid ninja-imports of system functionsBen Wiederhake
This adds a new header <sys/internals.h>, which provides access to LibC internals. This is in the interest of type-checking LibC itself, as well as enabling less-hacky access for uses like LinkDemo. And, of course, this progresses LibC towards building cleanly with -Wmissing-declarations.
2020-08-12LibC: Mark compilation-unit-only functions as staticBen Wiederhake
This enables a nice warning in case a function becomes dead code. For example with the unused function malloc_good_size() :^) I found these places by using -Wmissing-declarations. The Kernel still shows these issues, which I think are false-positives, but don't want to touch: - Libraries/LibC/crt0.cpp:41:5: int _start(int, char**, char**) Not sure how to handle this. - Libraries/LibC/cxxabi.cpp:48:5: int __cxa_atexit(AtExitFunction, void*, void*) - Libraries/LibC/cxxabi.cpp:58:6: void __cxa_finalize(void*) Not sure how to tell the compiler that the compiler is already using them. - Libraries/LibC/libcinit.cpp:36:6: void __libc_init() - Libraries/LibC/libcinit.cpp:55:19: void __stack_chk_fail() - Libraries/LibC/malloc.cpp:430:6: void __malloc_init() - Libraries/LibC/stdio.cpp:562:6: void __stdio_init() These are ninja-imported by other LibC functions. Maybe we should have some kind of "internals.h" header.
2020-07-31LibC: Flatten malloc() and free()Andreas Kling
This allows UE to ignore the full range of these functions instead of just the malloc() and free() bodies.
2020-07-21LibC: Make sure malloc chunks are 8-byte alignedAndreas Kling
I noticed this while doing some instruction-level debugging. :^)
2020-07-21LibC: Notify UserspaceEmulator about malloc *after* scrubbingAndreas Kling
This makes sure that the emulator marks new malloc memory as uninitialized (even after we've "initialized" it by scrubbing with the scrub byte.)
2020-07-16LibC: Notify UserspaceEmulator about BigAllocationBlock mallocsAndreas Kling
We were forgetting to inform UE about these, which caused it to believe subsequent calls to free() were invalid.
2020-07-15LibC: Communicate malloc() and free() operations to UserspaceEmulatorAndreas Kling
Use the sneaky SALC secret mechanism of UserspaceEmulator to inform it about malloc operations.
2020-07-11LibC: Some s/int/size_t/ in the malloc codeAndreas Kling
2020-05-20Revert "AK+LibC: Move non-placement new/delete into LibC"Andreas Kling
This reverts commit 2c823473930121aecbacf0422c8372a0912e581b.
2020-05-20AK+LibC: Move non-placement new/delete into LibCAndrew Kaster
This allows operator new and operator delete to be available to anyone that links -lc (everyone) rather than just people that include kmalloc.h (almost no one).
2020-03-08Userspace: Add missing #includes now that AK/StdLibExtras.h is smallerAndreas Kling
2020-03-08AK: Add global FlatPtr typedef. It's u32 or u64, based on sizeof(void*)Andreas Kling
Use this instead of uintptr_t throughout the codebase. This makes it possible to pass a FlatPtr to something that has u32 and u64 overloads.
2020-03-06LibC: Fix crash in free() now that mprotect() works correctlyAndreas Kling
After we mprotect(PROT_NONE) an allocation block, we can't expect to read the m_size from that block right after. :^)
2020-02-18LibC: Statically allocate allocatorsSergey Bugaev
These allocators take up 660 bytes, combined. Let's not waste two physical pages for them in each process :^)
2020-02-16malloc: Use 64KB blocks (instead of 4KB)Andreas Kling
We now allocate 64KB at a time and divide them into chunks for handing out to malloc() callers. This significantly reduces the number of system calls made due to memory allocation. This yields a ~15% speedup when compiling Process.cpp inside SerenityOS (down from 24 sec to 20 sec on my machine.) There's more performance on the table here, no doubt.
2020-02-02LibC: Allow opting into malloc() and free() performance event loggingAndreas Kling
If a program is started with LIBC_PROFILE_MALLOC in the environment, it will now generate PERF_EVENT_MALLOC and PERF_EVENT_FREE.
2020-01-18Meta: Add license header to source filesAndreas Kling
As suggested by Joshua, this commit adds the 2-clause BSD license as a comment block to the top of every source file. For the first pass, I've just added myself for simplicity. I encourage everyone to add themselves as copyright holders of any file they've added or modified in some significant way. If I've added myself in error somewhere, feel free to replace it with the appropriate copyright holder instead. Going forward, all new source files should include a license header.
2019-12-22LibC: Prevent assertions in malloc/free at exit timeAndrew Kaster
This is a bit sad, but, with the Allocators as static globals their destructors were running before some user code. Which doesn't really make much sense, as none of the members of (at least the basic one) do any real heavy lifting or have many resources to RAII. To avoid the problem, just mmap the memory for the global arrays of Allocators in __malloc_init and let the Kernel collect the memory when we're done with the process.
2019-12-20LibC: Make empty malloc blocks purgeableSergey Bugaev
2019-12-18LibC: Store empty malloc blocks in an array instead of a linked listSergey Bugaev
2019-12-02LibC: Also mark empty-but-kept-around BigAllocationBlocks as PROT_NONEAndreas Kling
This extends the opportunistic protection of empty-but-kept-around to also cover BigAllocationBlocks. Since we only cache 4KB BAB's at the moment, this sees limited use, but it does work.
2019-12-02LibC: Protect empty-but-kept-around ChunkedBlocks with PROT_NONEAndreas Kling
We now keep a separate queue of empty ChunkedBlocks in each allocator. The underlying memory for each block is mprotect'ed with PROT_NONE to provoke crashes on use-after-free. This is not going to catch *all* use-after-frees, but if it catches some, that's still pretty nice. :^) The malloc memory region names are now updated to reflect their reuse status: "malloc: ChunkedBlock(size) (free/reused)"
2019-09-29LibC: Some build fixes for strange platformsAndreas Kling
Patch from Anonymous.
2019-08-26LibThread: Move CLock to LibThread::LockSergey Bugaev
And adapt all the code that uses it.
2019-07-25LibC: Don't clobber errno in free().Andreas Kling
This one is a bit mysterious. I can't find any authoritative answer on what the correct behavior is, but it seems reasonable to me that free() doesn't step on errno, since it returns "void" and thus the caller won't know to inspect errno anyway.
2019-07-17LibC: Fix MALLOC_DEBUG to workRobin Burchell
2019-07-13LibC: Protect the malloc heap with a basic lock.Andreas Kling
2019-07-04Libraries: Create top level directory for libraries.Andreas Kling
Things were getting a little crowded in the project root, so this patch moves the Lib*/ directories into Libraries/.