From 8d4b47298b6c9eeb9a3f79a5b80d894f80ed4a45 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 27 Dec 2025 03:05:05 +0000 Subject: Do away with memcpy/memset/memcmp Now everything is either explicitly a rep thing, or explicitly some other small run of reasonably efficient instructions. No library calls or function call overhead of any kind. Code size remains nice and small. Ban a few other bad libc functions while we're at it. More can be added later if we decide that's a useful idea. This will require some discipline and workarounds to cope with Clang's tendency to shove mem* calls in places you don't want them even when you tell it not to. Well, c'est la vie. I'm a little nervous that the __builtin_memmove() usage in shuntvars() could still one day emit actual library calls. It appears we're not importing memmove() from anywhere at the moment, though. So I guess it's good enough for now. Also I had to get rid of alloca() in con_.c but that was kind of bad anyway, so that's fine. --- src/hook.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/hook.c') diff --git a/src/hook.c b/src/hook.c index 845d8b1..6738723 100644 --- a/src/hook.c +++ b/src/hook.c @@ -15,8 +15,6 @@ * PERFORMANCE OF THIS SOFTWARE. */ -#include - #include "chunklets/x86.h" #include "hook.h" #include "intdefs.h" @@ -55,10 +53,10 @@ struct _hook_prep_ret _hook_prep(uchar *func, uchar *trampoline) { } len += ilen; if (len >= 5) { - memcpy(trampoline, p, len); + mem_copy(trampoline, p, len); trampoline[len] = X86_JMPIW; s32 diff = p - (trampoline + 5); // goto the continuation - memcpy(trampoline + len + 1, &diff, 4); + mem_stores32(trampoline + len + 1, diff); return (struct _hook_prep_ret){func, len, 0}; } if_cold (p[len] == X86_JMPIW) { @@ -76,13 +74,14 @@ bool hook_inline_mprot(void *hookpos) { void _hook_inline_commit(uchar *restrict hookpos, const uchar *restrict target) { s32 diff = (uchar *)target - (hookpos + 5); // goto the hook target hookpos[0] = X86_JMPIW; - memcpy(hookpos + 1, &diff, 4); + mem_stores32(hookpos + 1, diff); } void _unhook_inline(uchar *trampoline, int len) { s32 off = mem_loads32(trampoline + len + 1); uchar *orig = trampoline + off + 5; - memcpy(orig, trampoline, 5); + mem_storeu32(orig, mem_loadu32(trampoline)); + orig[4] = trampoline[4]; } // vi: sw=4 ts=4 noet tw=80 cc=80 -- cgit v1.2.3-54-g00ecf