Skip to content

hamkee-dev-group/C-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snippets of C

A collection of small, self-contained C programs. Each file demonstrates a focused systems-programming concept using minimal, technical, production-style code.

The collection is designed as a reference for developers who work with:

  • low-level Linux/POSIX APIs
  • concurrency primitives
  • memory management
  • filesystem correctness
  • networking and IPC
  • kernel interfaces
  • secure and defensive programming patterns
  • JIT compilation and raw machine-code execution

Topics

Concurrency & Synchronization

  • futex wait/wake, mutex, latch
  • atomics and memory ordering
  • seqlock
  • sense-reversing barrier, eventfd barrier
  • SPSC and MPMC lock-free ring buffers
  • reader-writer lock (fair)
  • Treiber stack with epoch-based reclamation
  • RCU-style linked list
  • CRDT PN-counter
  • thread pool
  • fiber / ucontext cooperative scheduler
  • POSIX robust mutex (owner-death recovery)
  • POSIX message queues with priority ordering

Memory Management

  • mmap arenas with guard pages
  • huge pages (MAP_HUGETLB)
  • memfd sealing
  • sliding-window memory mapping
  • reference-counted objects
  • LIFO scratch-region allocator
  • userfaultfd demand-paging
  • buddy system allocator (power-of-2, split/coalesce)

Filesystem & Storage

  • atomic file replacement (write + rename)
  • directory walking via openat
  • log-structured record store with compaction
  • key-value store with write-ahead journal
  • filesystem transactions with rollback
  • file-descriptor cache
  • ring-buffer logger in shared memory

Linux Kernel Interfaces

  • pidfd
  • userfaultfd
  • fanotify
  • inotify
  • epoll (edge-triggered event loop with timerfd)
  • eventfd
  • io_uring
  • kqueue (BSD/macOS)
  • BPF socket counter
  • perf event monitoring
  • vDSO time
  • syscall tracing via ptrace

Networking & IPC

  • edge-triggered TCP server with epoll
  • UNIX socket credential passing (SCM_CREDENTIALS)
  • file descriptor passing over UNIX sockets (SCM_RIGHTS)
  • POSIX shared memory SPSC ring
  • zero-copy file serving with sendfile
  • zero-copy pipe pumping with splice
  • token-bucket rate limiter

Security & Sandboxing

  • strict seccomp and seccomp-BPF filters
  • OpenBSD pledge / unveil
  • Landlock LSM filesystem restriction (Linux ≥ 5.13)
  • user namespace isolation (userns_spawn, userns_unshare)
  • O_PATH capability-style access
  • cgroup v2 memory limit enforcement

Signals, Timers & Async

  • alternate signal stack for crash handlers
  • hierarchical timer wheel
  • POSIX AIO
  • signalfd / self-pipe fallback
  • accurate periodic timer with timerfd and missed-tick detection

Cryptography & Randomness

  • HKDF key derivation via OpenSSL EVP
  • constant-time byte comparison
  • cryptographically secure token generation

JIT & Machine Code

  • runtime machine-code generation: mmap (RW) → write bytes → mprotect (RX) → call
  • W^X page discipline
  • extracting compiled functions as raw byte blobs (objcopy, nm, xxd -i)
  • self-contained syscall-only payload with no libc, no relocations (sysinfo_jit)
  • generic JIT host loader (jit_host)

Build

Build all snippets:

make

Build a single snippet:

make futex_wait_wake

List all detected targets:

make list

Clean binaries:

make clean

The Makefile inspects each source file and links only the libraries it needs:

Condition Extra flag
Uses pthreads -pthread
Uses POSIX AIO or shm_open -lrt
Includes <liburing.h> -luring
Includes <openssl/...> -lcrypto

JIT pipeline

For snippets in the JIT & Machine Code category, the build flow differs:

# 1. compile to object file — do NOT link
gcc -O2 -fno-stack-protector -std=c11 -c payload.c -o payload.o

# 2. must print nothing — any output means broken relocations
objdump -r payload.o

# 3. extract the raw .text blob
objcopy -O binary --only-section=.text payload.o payload.bin

# 4. find the entry-point offset inside the blob
nm payload.o | grep ' T run'

# 5. generate a C array to embed in the host
xxd -i payload.bin

# standalone test (no host needed)
gcc -O2 -fno-stack-protector -DSTANDALONE -nostartfiles \
    -std=c11 payload.c -o payload_test && ./payload_test

Notes

  • Most snippets are Linux-only due to specific kernel syscalls.
  • kqueue_timer and sandbox_pledge_unveil are BSD/macOS-only.
  • JIT snippets additionally require x86-64.
  • Each file is intentionally isolated: no shared headers, no inter-file dependencies.
  • Designed for experimentation, auditing, and reference — every syscall is checked, every resource released.

License

MIT

About

Snippets of C code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors