Skip to content

Commit

Permalink
Cleanup & minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed Nov 19, 2023
1 parent 3473da8 commit f27d19b
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 41 deletions.
5 changes: 5 additions & 0 deletions kos/include/bits/crt/mallinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

#include <hybrid/typecore.h>

#undef _MALLINFO_MATCHES_MALLINFO2
#if __SIZEOF_INT__ == __SIZEOF_SIZE_T__
#define _MALLINFO_MATCHES_MALLINFO2
#endif /* __SIZEOF_INT__ == __SIZEOF_SIZE_T__ */

#ifdef __CC__
__DECL_BEGIN

Expand Down
2 changes: 1 addition & 1 deletion kos/include/kos/coredump.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


/* Coredump message string limits.
* WARNING: These values as used as alloca() limits, so don't set
* WARNING: These values are used as alloca() limits, so don't set
* them too high, or else user-space may be able to cause
* a kernel-space stack overflow... */
#define COREDUMP_ASSERT_EXPR_MAXLEN 512 /* ci_assert.ca_expr */
Expand Down
6 changes: 3 additions & 3 deletions kos/misc/build/autoconfig.dee
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function getPreviousBuildConfiguration(rootDir: string, targetName: string | non
local resultTimestamp = none;
local resultTargetName = targetName;
if (targetName is none) {
/* Figure out `targetName' was enumerating symlinks in "{rootDir}/bin"
* and picking whichever has the most-recent last-modified timestamp. */
/* Figure out `targetName' by enumerating symlinks in "{rootDir}/bin"
* and picking whichever has the most-recent last-modified timestamp. */
local mostRecentTargets = [];
for (local ent: posix.opendir(rootDir + "bin")) {
if (ent.d_type != posix.DT_LNK)
Expand Down Expand Up @@ -114,7 +114,7 @@ function getMiOptionsBuildConfig(rootDir: string): (Bytes, Bytes) | none {
@@Try to automatically determine the target/config for which to build
@@This function is used when you invoke `magic.dee` without supplying
@@a `--target=` or `--config=` option. It is also used by `make` when
@@needing (buf missing) a `TARGET` or `CONFIG` argument.
@@needing (but missing) a `TARGET` or `CONFIG` argument.
@@
@@The actual behavior of this function is not guarantied, but it tries
@@to figure out the target/configuration which KOS has last been built
Expand Down
13 changes: 8 additions & 5 deletions kos/src/kernel/core/memory/malloc-impl.c.inl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ LOCAL_NOTHROW(KCALL LOCAL_untraced_kmalloc_noslab)(size_t n_bytes, gfp_t flags)
assert(heapptr_getsiz(hptr) >= alloc_size);
result = (struct mptr *)heapptr_getptr(hptr);
mptr_init(result, heapptr_getsiz(hptr), flags & __GFP_HEAPMASK);
mptr_assert_paranoid(result);
return mptr_user(result);
LOCAL_IF_NX_ELSE(err:, err_overflow:)
LOCAL_IF_NX_ELSE(return NULL, THROW(E_BADALLOC_INSUFFICIENT_HEAP_MEMORY, n_bytes));
Expand All @@ -80,21 +81,20 @@ LOCAL_IF_NX_ELSE(err:, err_overflow:)
#define untraced_kmalloc_noslab untraced_kmalloc
#endif /* !... */

#ifdef CONFIG_HAVE_KERNEL_SLAB_ALLOCATORS
INTERN ATTR_BLOCKLIKE_GFP(flags) ATTR_MALLOC WUNUSED VIRT void *
LOCAL_NOTHROW(KCALL LOCAL_untraced_kmalloc)(size_t n_bytes, gfp_t flags) {
heapptr_t hptr;
struct mptr *result;
size_t alloc_size;
if unlikely(OVERFLOW_UADD(sizeof(struct mptr), n_bytes, &alloc_size))
goto LOCAL_IF_NX_ELSE(err, err_overflow);
#ifdef CONFIG_HAVE_KERNEL_SLAB_ALLOCATORS
if (n_bytes <= CONFIG_KERNEL_SLAB_MAXSIZE) {
void *slab_ptr;
slab_ptr = slab_malloc(n_bytes, flags);
if (slab_ptr)
return slab_ptr;
}
#endif /* CONFIG_HAVE_KERNEL_SLAB_ALLOCATORS */
if unlikely(OVERFLOW_UADD(sizeof(struct mptr), n_bytes, &alloc_size))
goto LOCAL_IF_NX_ELSE(err, err_overflow);
hptr = LOCAL_heap_alloc_untraced(&kernel_heaps[flags & __GFP_HEAPMASK],
alloc_size, flags);
LOCAL_IF_NX_ELSE(if unlikely(!heapptr_getsiz(hptr)) goto err;, )
Expand All @@ -106,6 +106,9 @@ LOCAL_NOTHROW(KCALL LOCAL_untraced_kmalloc)(size_t n_bytes, gfp_t flags) {
LOCAL_IF_NX_ELSE(err:, err_overflow:)
LOCAL_IF_NX_ELSE(return NULL, THROW(E_BADALLOC_INSUFFICIENT_HEAP_MEMORY, n_bytes));
}
#else /* CONFIG_HAVE_KERNEL_SLAB_ALLOCATORS */
DEFINE_INTERN_ALIAS(LOCAL_untraced_kmalloc, LOCAL_untraced_kmalloc_noslab);
#endif /* !CONFIG_HAVE_KERNEL_SLAB_ALLOCATORS */

INTERN ATTR_BLOCKLIKE_GFP(flags) ATTR_MALLOC WUNUSED VIRT void *
LOCAL_NOTHROW(KCALL LOCAL_untraced_kmemalign)(size_t min_alignment,
Expand Down Expand Up @@ -163,7 +166,7 @@ LOCAL_NOTHROW(KCALL LOCAL_get_realloc_size)(size_t n_bytes) {
return result;
err_overflow:
LOCAL_IF_NX_ELSE(return (size_t)-1,
THROW(E_BADALLOC_INSUFFICIENT_HEAP_MEMORY, n_bytes));
THROW(E_BADALLOC_INSUFFICIENT_HEAP_MEMORY, n_bytes));
}

INTERN ATTR_BLOCKLIKE_GFP(flags) VIRT void *
Expand Down
6 changes: 3 additions & 3 deletions kos/src/kernel/include/kernel/malloc-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ DECL_BEGIN
/*[[[end]]]*/

#ifndef HEAP_ALIGNMENT
#define HEAP_ALIGNMENT CONFIG_KERNEL_HEAP_ALIGNMENT
#endif
#define HEAP_ALIGNMENT CONFIG_KERNEL_HEAP_ALIGNMENT
#endif /* !HEAP_ALIGNMENT */
#if HEAP_ALIGNMENT < __ALIGNOF_POINTER__
#error "The kernel heap must support at least pointer alignment"
#endif /* HEAP_ALIGNMENT < __ALIGNOF_POINTER__ */
Expand All @@ -209,7 +209,7 @@ DECL_BEGIN


/* Kernel-space dynamic memory management. */
#define __SIZEOF_GFP_T__ __SIZEOF_INT__
#define __SIZEOF_GFP_T__ 4
#ifdef __CC__
typedef __UINT32_TYPE__ gfp_t;
#endif /* __CC__ */
Expand Down
2 changes: 1 addition & 1 deletion kos/src/libc/libc/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ NOTHROW(FCALL libc_vabortf_failure_core)(struct abortf_args *__restrict args) {
/* If the user has defined a custom sigaction for `SIGABRT',
* then instead of directly triggering a coredump, we must
* raise that signal at the given `&args->af_state'! */
/* TODO: */
/* TODO: Must still set `__abort_msg' before calling this! */
maybe_raise_SIGABRT(&args->af_state);

/* Normal abort handling. */
Expand Down
6 changes: 3 additions & 3 deletions kos/src/libc/libc/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ struct IO_FILE_84 {
*/

/*32|64-bit * .................................... * 32-bit | 64-bit */
/* 0| 0*/ uint32_t io84_flags; /* if_ptr | if_ptr */
/* 0| 0*/ uint32_t io84_flags; /* if_ptr | if_ptr */
#if __SIZEOF_POINTER__ > 4
/* 4*/ uint32_t _io84_pad; /* - | if_ptr (cont.) */
/* 4*/ uint32_t _io84_pad; /* - | if_ptr (cont.) */
#endif /* __SIZEOF_POINTER__ > 4 */
/* 4| 8*/ byte_t *io84_IO_read_ptr; /* if_cnt | if_cnt+__if_pad0 */
/* 8| 16*/ byte_t *io84_IO_read_end; /* if_base | if_base */
Expand Down Expand Up @@ -286,7 +286,7 @@ struct IO_FILE_84 {

struct linux_default_stdio_file {
#if __SIZEOF_POINTER__ == 4
byte_t ldsf_zero[sizeof(struct IO_FILE_84) - sizeof(FILE)]; /* 0 bytes... */
byte_t ldsf_zero[sizeof(struct IO_FILE_84) - sizeof(FILE)]; /* Pad with 0-bytes. */
FILE ldsf_stdio; /* The actual file object */
#elif __SIZEOF_POINTER__ == 8
FILE ldsf_stdio; /* The actual file object */
Expand Down
49 changes: 32 additions & 17 deletions kos/src/libc/user/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ DECL_BEGIN
/*[[[skip:libc_mallinfo]]]*/
/*[[[skip:libc_mallinfo2]]]*/

#ifdef _MALLINFO_MATCHES_MALLINFO2
static_assert(alignof(struct mallinfo) == alignof(struct mallinfo2));
static_assert(sizeof(struct mallinfo) == sizeof(struct mallinfo2));
static_assert(offsetof(struct mallinfo, arena) == offsetof(struct mallinfo2, arena));
static_assert(offsetof(struct mallinfo, ordblks) == offsetof(struct mallinfo2, ordblks));
static_assert(offsetof(struct mallinfo, smblks) == offsetof(struct mallinfo2, smblks));
static_assert(offsetof(struct mallinfo, hblks) == offsetof(struct mallinfo2, hblks));
static_assert(offsetof(struct mallinfo, hblkhd) == offsetof(struct mallinfo2, hblkhd));
static_assert(offsetof(struct mallinfo, usmblks) == offsetof(struct mallinfo2, usmblks));
static_assert(offsetof(struct mallinfo, fsmblks) == offsetof(struct mallinfo2, fsmblks));
static_assert(offsetof(struct mallinfo, uordblks) == offsetof(struct mallinfo2, uordblks));
static_assert(offsetof(struct mallinfo, fordblks) == offsetof(struct mallinfo2, fordblks));
static_assert(offsetof(struct mallinfo, keepcost) == offsetof(struct mallinfo2, keepcost));
#endif /* _MALLINFO_MATCHES_MALLINFO2 */

#if __ARCH_REDIRECT_MAXBYTES != 0
typedef void *(LIBCCALL *LPMALLOC)(size_t num_bytes);
typedef void (LIBCCALL *LPFREE)(void *ptr);
Expand Down Expand Up @@ -95,9 +110,9 @@ struct user_malloc_abi {
LPPVALLOC uma_pvalloc;
LPMALLOPT uma_mallopt;
LPMALLINFO uma_mallinfo;
#if __SIZEOF_INT__ != __SIZEOF_SIZE_T__
#ifndef _MALLINFO_MATCHES_MALLINFO2
LPMALLINFO2 uma_mallinfo2;
#endif /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#endif /* !_MALLINFO_MATCHES_MALLINFO2 */
LPMALLOC_TRIM uma_malloc_trim;
LPMALLOC_USABLE_SIZE uma_malloc_usable_size;

Expand Down Expand Up @@ -261,7 +276,7 @@ fallback_mallopt_NOOP(int param_number, int value) {
return 0;
}

#if __SIZEOF_INT__ != __SIZEOF_SIZE_T__
#ifndef _MALLINFO_MATCHES_MALLINFO2
PRIVATE struct mallinfo2 LIBCCALL
fallback_mallinfo2_with_mallinfo(void) {
struct mallinfo info = mallinfo();
Expand Down Expand Up @@ -302,7 +317,7 @@ fallback_mallinfo_with_mallinfo2(void) {
result.keepcost = (int)(unsigned int)info.keepcost;
return result;
}
#endif /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#endif /* !_MALLINFO_MATCHES_MALLINFO2 */

PRIVATE struct mallinfo LIBCCALL
fallback_mallinfo_NOOP(void) {
Expand Down Expand Up @@ -362,12 +377,12 @@ user_malloc_abi_load(struct user_malloc_abi *__restrict self) {
*(void **)&self->uma_pvalloc = user_malloc_abi_dlsym(nameof_pvalloc, (void *)&libc_pvalloc);
*(void **)&self->uma_mallopt = user_malloc_abi_dlsym(nameof_mallopt, (void *)&libc_mallopt);
*(void **)&self->uma_mallinfo = user_malloc_abi_dlsym(nameof_mallinfo, (void *)&libc_mallinfo);
#if __SIZEOF_INT__ != __SIZEOF_SIZE_T__
#ifndef _MALLINFO_MATCHES_MALLINFO2
*(void **)&self->uma_mallinfo2 = user_malloc_abi_dlsym(nameof_mallinfo2, (void *)&libc_mallinfo2);
#else /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#else /* !_MALLINFO_MATCHES_MALLINFO2 */
if (self->uma_mallinfo == NULL)
*(void **)&self->uma_mallinfo = user_malloc_abi_dlsym(nameof_mallinfo2, (void *)&libc_mallinfo2);
#endif /* __SIZEOF_INT__ == __SIZEOF_SIZE_T__ */
#endif /* _MALLINFO_MATCHES_MALLINFO2 */
*(void **)&self->uma_malloc_trim = user_malloc_abi_dlsym(nameof_malloc_trim, (void *)&libc_malloc_trim);
*(void **)&self->uma_malloc_usable_size = user_malloc_abi_dlsym(nameof_malloc_usable_size, (void *)&libc_malloc_usable_size);
if (*(void **)&self->uma_malloc_usable_size == NULL) {
Expand Down Expand Up @@ -421,7 +436,7 @@ user_malloc_abi_load(struct user_malloc_abi *__restrict self) {
self->uma_pvalloc = &fallback_pvalloc_with_valloc;
if (self->uma_mallopt == NULL)
self->uma_mallopt = &fallback_mallopt_NOOP;
#if __SIZEOF_INT__ != __SIZEOF_SIZE_T__
#ifndef _MALLINFO_MATCHES_MALLINFO2
if (self->uma_mallinfo2 == NULL) {
if (self->uma_mallinfo != NULL) {
self->uma_mallinfo2 = &fallback_mallinfo2_with_mallinfo;
Expand All @@ -437,10 +452,10 @@ user_malloc_abi_load(struct user_malloc_abi *__restrict self) {
self->uma_mallinfo = &fallback_mallinfo_NOOP;
}
}
#else /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#else /* !_MALLINFO_MATCHES_MALLINFO2 */
if (self->uma_mallinfo == NULL)
self->uma_mallinfo = &fallback_mallinfo_NOOP;
#endif /* __SIZEOF_INT__ == __SIZEOF_SIZE_T__ */
#endif /* _MALLINFO_MATCHES_MALLINFO2 */
if (self->uma_malloc_trim == NULL)
self->uma_malloc_trim = &fallback_malloc_trim_NOOP;
if (self->uma_malloc_usable_size == NULL)
Expand Down Expand Up @@ -483,9 +498,9 @@ user_malloc_abi_install(struct user_malloc_abi *__restrict self) {
libc_malloc_redirect(nameof_pvalloc, (void *)&libc_pvalloc, *(void **)&self->uma_pvalloc);
libc_malloc_redirect(nameof_mallopt, (void *)&libc_mallopt, *(void **)&self->uma_mallopt);
libc_malloc_redirect(nameof_mallinfo, (void *)&libc_mallinfo, *(void **)&self->uma_mallinfo);
#if __SIZEOF_INT__ != __SIZEOF_SIZE_T__
#ifndef _MALLINFO_MATCHES_MALLINFO2
libc_malloc_redirect(nameof_mallinfo2, (void *)&libc_mallinfo2, *(void **)&self->uma_mallinfo2);
#endif /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#endif /* !_MALLINFO_MATCHES_MALLINFO2 */
libc_malloc_redirect(nameof_malloc_trim, (void *)&libc_malloc_trim, *(void **)&self->uma_malloc_trim);
libc_malloc_redirect(nameof_malloc_usable_size, (void *)&libc_malloc_usable_size, *(void **)&self->uma_malloc_usable_size);
libc_malloc_redirect_opt(nameof_memdup, (void *)&libc_memdup, *(void **)&self->uma_memdup);
Expand Down Expand Up @@ -563,7 +578,7 @@ libc_init_malloc_hooks(void) {
* NOTE: Since this part happens *before* malloc() normally gets called, we don't
* have to worry about `libc_init_malloc_hooks()', since it will *never* get
* called! */
typedef void (*LP__MALLOC_INITIALIZE_HOOK)(void);
typedef void (LIBCCALL *LP__MALLOC_INITIALIZE_HOOK)(void);
typedef void (LIBCCALL *LP__FREE_HOOK)(void *ptr, void const *return_address);
typedef void *(LIBCCALL *LP__MALLOC_HOOK)(size_t num_bytes, void const *return_address);
typedef void *(LIBCCALL *LP__REALLOC_HOOK)(void *ptr, size_t num_bytes, void const *return_address);
Expand Down Expand Up @@ -593,12 +608,12 @@ libdlmalloc_so_abi_load(struct user_malloc_abi *__restrict self) {
*(void **)&self->uma_valloc = get_dlmalloc_function(lib_dlmalloc, nameof_valloc);
*(void **)&self->uma_pvalloc = get_dlmalloc_function(lib_dlmalloc, nameof_pvalloc);
*(void **)&self->uma_mallopt = get_dlmalloc_function(lib_dlmalloc, nameof_mallopt);
#if __SIZEOF_INT__ != __SIZEOF_SIZE_T__
#ifndef _MALLINFO_MATCHES_MALLINFO2
*(void **)&self->uma_mallinfo2 = get_dlmalloc_function(lib_dlmalloc, nameof_mallinfo2);
self->uma_mallinfo = &fallback_mallinfo_with_mallinfo2;
#else /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#else /* !_MALLINFO_MATCHES_MALLINFO2 */
*(void **)&self->uma_mallinfo = get_dlmalloc_function(lib_dlmalloc, nameof_mallinfo2);
#endif /* __SIZEOF_INT__ == __SIZEOF_SIZE_T__ */
#endif /* _MALLINFO_MATCHES_MALLINFO2 */
*(void **)&self->uma_malloc_trim = get_dlmalloc_function(lib_dlmalloc, nameof_malloc_trim);
*(void **)&self->uma_malloc_usable_size = get_dlmalloc_function(lib_dlmalloc, nameof_malloc_usable_size);
}
Expand Down Expand Up @@ -929,7 +944,7 @@ void *LIBCCALL libc_hooked_recallocv(void *mallptr, size_t elem_count, size_t el



PRIVATE ATTR_SECTION(".text.crt.heap.rare_helpers") void
PRIVATE ATTR_SECTION(".text.crt.heap.rare_helpers") void LIBCCALL
libc_malloc_hooks_doinit(void) {
struct user_malloc_abi abi;
syslog(LOG_INFO, "[libc] Enable support for `__malloc_hook'\n");
Expand Down
16 changes: 8 additions & 8 deletions kos/src/libdlmalloc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ DEFINE_DL_EXPORT_ALIAS(__libc_pvalloc, dlpvalloc);
DEFINE_DL_EXPORT_ALIAS(_msize, dlmalloc_usable_size);
DEFINE_DL_EXPORT_ALIAS(_msize_debug, dlmalloc_usable_size);

#if __SIZEOF_INT__ == __SIZEOF_SIZE_T__
#ifdef _MALLINFO_MATCHES_MALLINFO2
DEFINE_DL_EXPORT_ALIAS(__libc_mallinfo, dlmallinfo);
DEFINE_DL_EXPORT_ALIAS(mallinfo, dlmallinfo);
#else /* __SIZEOF_INT__ == __SIZEOF_SIZE_T__ */
#else /* _MALLINFO_MATCHES_MALLINFO2 */
DEFINE_DL_EXPORT_ALIAS(__libc_mallinfo, libc_mallinfo_int);
DEFINE_DL_EXPORT_ALIAS(mallinfo, libc_mallinfo_int);
#endif /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#endif /* !_MALLINFO_MATCHES_MALLINFO2 */
#endif /* __BUILDING_LIBC */
#undef DEFINE_DL_EXPORT_ALIAS

Expand All @@ -322,17 +322,17 @@ DEFINE_INTERN_ALIAS(libc_memalign, dlmemalign);
DEFINE_INTERN_ALIAS(libc_posix_memalign, dlposix_memalign);
DEFINE_INTERN_ALIAS(libc_valloc, dlvalloc);
DEFINE_INTERN_ALIAS(libc_mallopt, dlmallopt);
#if __SIZEOF_INT__ == __SIZEOF_SIZE_T__
#ifdef _MALLINFO_MATCHES_MALLINFO2
DEFINE_INTERN_ALIAS(libc_mallinfo, dlmallinfo);
#else /* __SIZEOF_INT__ == __SIZEOF_SIZE_T__ */
#else /* _MALLINFO_MATCHES_MALLINFO2 */
DEFINE_INTERN_ALIAS(libc_mallinfo, libc_mallinfo_int);
#endif /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#endif /* !_MALLINFO_MATCHES_MALLINFO2 */
DEFINE_INTERN_ALIAS(libc_mallinfo2, dlmallinfo);
DEFINE_INTERN_ALIAS(libc_pvalloc, dlpvalloc);
DEFINE_INTERN_ALIAS(libc_malloc_trim, dlmalloc_trim);
DEFINE_INTERN_ALIAS(libc_malloc_usable_size, dlmalloc_usable_size);

#if __SIZEOF_INT__ != __SIZEOF_SIZE_T__
#ifndef _MALLINFO_MATCHES_MALLINFO2
INTERN ATTR_SECTION(".text.crt.heap.malloc")
struct mallinfo libc_mallinfo_int(void) {
struct mallinfo2 info = dlmallinfo();
Expand All @@ -349,7 +349,7 @@ struct mallinfo libc_mallinfo_int(void) {
result.keepcost = (int)(unsigned int)info.keepcost;
return result;
}
#endif /* __SIZEOF_INT__ != __SIZEOF_SIZE_T__ */
#endif /* !_MALLINFO_MATCHES_MALLINFO2 */

/* Old aliases from Glibc 2.0.4 */
DEFINE_PUBLIC_ALIAS(__free, libc_free);
Expand Down

0 comments on commit f27d19b

Please sign in to comment.