Skip to content

Instantly share code, notes, and snippets.

View nickdesaulniers's full-sized avatar

Nick Desaulniers nickdesaulniers

View GitHub Profile
@nickdesaulniers
nickdesaulniers / 1a.c
Created September 25, 2026 23:52
More examples of clang stack slop
// Case 1A: Aggregate Call Return Temporaries (CodeGenFunction::EmitAnyExprToTemp, #68747
// https://github.com/llvm/llvm-project/issues/68747)
struct a {
long b[8];
};
struct a f(void);
void g(struct a);
void h(void) {
g(f());
g(f());
@nickdesaulniers
nickdesaulniers / hunting_private_dirty_memory.md
Last active August 20, 2026 18:06
Hunting Private Dirty Memory

Hunting Down Private Dirty Memory in Linux Shared Libraries

Shared libraries (.so files) on Linux are designed to save memory by sharing physical memory pages across processes via the kernel's Page Cache. But in practice, shared libraries often consume substantially more physical RAM than expected due to Copy-On-Write (COW) dirtying.

When a process writes to any page in a shared object—or when the dynamic linker (ld.so) writes relocated pointers at startup—that 4 KB page is duplicated in physical RAM as Private_Dirty memory. If a library has 5 MB of dirty pages and is loaded by 100 processes, that library silently costs 500 MB of unshareable RAM.

Here is a practical, command-line-driven playbook for identifying private dirty hotspots in shared objects and resolving them in source code.


. /tmpfs/src/git/ndk32/llvm-project/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
.. /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/c++/v1/csignal
... /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/c++/v1/__config
.... /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/c++/v1/__config_site
.... /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/c++/v1/__configuration/abi.h
..... /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/c++/v1/__configuration/compiler.h
..... /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/c++/v1/__configuration/platform.h
...... /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/features.h
....... /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/sys/cdefs.h
........ /tmpfs/src/git/ndk32/sysroots/ndk/x86/usr/include/android/versioning.h
@nickdesaulniers
nickdesaulniers / get_builtins.sh
Created December 5, 2024 21:22
list of symbols for compiler builtins
$ llvm-nm compiler-rt/lib/linux/libclang_rt.builtins-x86_64.a | grep T | cut -d ' ' -f 3
__absvdi2
__absvsi2
__absvti2
__adddf3
__addsf3
__addvdi3
__addvsi3
__addvti3
__ashldi3
@nickdesaulniers
nickdesaulniers / foo.gdb
Created November 19, 2024 22:34
scudo backtrace
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x0000fffff7ba0a60 in __GI_abort () at abort.c:79
#2 0x000000000040a8c8 in scudo::die ()
at /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/compiler-rt/lib/scudo/standalone/linux.cpp:48
#3 0x000000000040b248 in scudo::reportRawError (
Message=0xffffffffed78 "Scudo ERROR: internal map failure (error desc=Success)\n")
at /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/compiler-rt/lib/scudo/standalone/report.cpp:58
#4 0x000000000040b9a4 in scudo::reportMapError (SizeIfOOM=0)
at /home/libc-buildbot/libc-aarch64-ubuntu/libc-aarch64-ubuntu-fullbuild-dbg/llvm-project/compiler-rt/lib/scudo/standalone/report_linux.cpp:33
commit b0fb5270bc85dd049ddb5f4308e04136256d6783
Author: Nick Desaulniers <ndesaulniers@google.com>
Date: Thu Oct 3 08:18:10 2024 -0700
WIP fpbits
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 5d1f633bb56e..69307653a8e8 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
diff --git a/tools/testing/selftests/kselftest_harness.h b/tools/testing/selftests/kselftest_harness.h
index b634969cbb6f..da4c91793cdc 100644
--- a/tools/testing/selftests/kselftest_harness.h
+++ b/tools/testing/selftests/kselftest_harness.h
@@ -450,14 +450,18 @@ static inline pid_t clone3_vfork(void)
_metadata->exit_code = KSFT_FAIL; \
} \
} \
+ _Bool expected = false; \
+ _Bool desired = true; \
@nickdesaulniers
nickdesaulniers / all_targets.c
Last active April 10, 2024 20:02
A test file I was using while trying to fix https://github.com/llvm/llvm-project/issues/20571. It tries to forcibly cause register exhaustion by using inline asm which clobbers all GPRs. In these cases, "rm" contrained operands should fall back to "m" rather than fail to compile.
// -Wno-asm-operand-widths
// void input_pressure(int x) {
// // asm ("# %0"::"m"(x):"r0","r1","r2","r3","r12","lr","r4","r5","r6","r7","r8","r9","r10","r11");
// asm (""::"m"(x));
// }
struct foo {
long a, b, c, d, e;
};
int zzz (struct foo my_foo) {
bloaty -n 0 -s file after.a -- before.a
FILE SIZE VM SIZE
-------------- --------------
+234% +2.05Ki +212% +816 .rodata.cst16
+1.8% +1.48Ki [ = ] 0 .symtab
+0.8% +920 [ = ] 0 .strtab
+31% +864 +33% +870 .text._ZN22__llvm_libc_18_0_0_git8internal27hexadecimal_string_to_floatIeEENS_14StrToNumResultINS0_13ExpandedFloatIT_EEEEPKccNS0_14RoundDirectionE
+39% +736 +44% +760 .text._ZN22__llvm_libc_18_0_0_git8internal20decimal_exp_to_floatIeEENS0_18FloatConvertReturnIT_EENS0_13ExpandedFloatIS3_EEPKcbNS0_14RoundDirectionE
[NEW] +480 [NEW] +411 .text._ZN22__llvm_libc_18_0_0_gitL31construct_thread_name_file_pathERNS_3cpp12StringStreamEi
[NEW] +288 [NEW] +223 .text._ZN22__llvm_libc_18_0_0_git11printf_core11FloatWriter18write_middle_blockEj
struct foo {
long long x;
};
struct foo foo_factory (void);
struct foo div (struct foo, struct foo);
long long bar (void) {
return div(div(foo_factory(), foo_factory()), div(foo_factory(), foo_factory())).x;
}