Skip to content

Instantly share code, notes, and snippets.

@pankkor
pankkor / macOS_input_monitor.md
Last active July 4, 2024 13:57
macOS input monitoring and data collection

Snippets

Quickly limit resources for the process with ulimit

LIMIT_MB=50
(ulimit -Sv $((LIMIT_MB << 10)); /path/to/binary)

On OpenBSD use

@pankkor
pankkor / macos_aarch64_syscalls.c
Last active June 6, 2024 12:57
macOS aarch64 syscall
i64 syscall1(i64 sys_num, i64 a0) {
i64 ret;
__asm__ volatile (
"mov x16, %[sys_num]\n"
"mov x0, %[a0]\n"
"svc 0x80\n"
"mov %0, x0\n"
: "=r" (ret)
: [sys_num] "r" (sys_num), [a0] "r" (a0)
: "x16", "x0"
// Counting sort algorithm
// https://en.m.wikipedia.org/wiki/Counting_sort
//
// Build clang:
// clang -Wall -Wextra -Wpedantic -g counting_sort.c -o counting_sort
//
// Build gcc:
// gcc -Wall -Wextra -Wpedantic -g counting_sort.c -o counting_sort
typedef int i32;
@pankkor
pankkor / inline_asm.c
Last active May 15, 2024 20:12
Example of using gcc inline assembly syntax. NOTE: last time I checked clang, didn't support Intel assembly syntax, so AT&T it is.
static void test_asm(u8 *data, u64 bytes) {
#if defined(__aarch64__)
__asm__ volatile (
"cbz %[bytes], 1f\n\t"
"mov x9, #0\n\t"
"0: strb w9, [%[data], x9]\n\t"
"add x9, x9, #1\n\t"
"cmp %[bytes], x9\n\t"
"b.ne 0b\n\t"
"1:\n\t"
@pankkor
pankkor / test_util.c
Created April 26, 2024 19:11
All you ever need test utils (https://godbolt.org/z/WEoavaqGc)
#include <stdio.h>
#define STR1(s) # s
#define STR(s) STR1(s)
#define TEST_EXPECT(condition) test_expect(!!(condition), __FILE__ ":" STR(__LINE__) ": (" STR(condition) ") expected to be true\n")
static inline void test_expect(int condition, const char *msg) {
if (!condition) {
fputs(msg, stderr);
@pankkor
pankkor / check_grades.c
Last active April 18, 2024 13:00
Twitter troll task to remove if-else from grading function. Compile with clang/gcc -Wpedantic -Wall -Wextra -O3 -msse -mpopcnt
#include <immintrin.h>
typedef unsigned int u32;
typedef unsigned char u8;
char check_grade(u8 score) {
score = score > 100 ? 100 : score;
__m128i scores = _mm_set1_epi8(score);
__m128i brackets = _mm_set_epi8(89, 79, 69, 59, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F);
@pankkor
pankkor / large_huge_super_pages.md
Last active October 22, 2023 22:13
Windows large pages, Linux huge pages, macOS super pages support
@pankkor
pankkor / no_alsr_lin.sh
Created October 2, 2023 14:10
Disable ASLR Linux
# Check what ASLR protection do you have (typically 2)
cat /proc/sys/kernel/randomize_va_space
# Disable ASLR global
sysctl -w kernel.randomize_va_space=0
# Revert ASLR global
sysctl -w kernel.randomize_va_space=2
# Run <my_binary> with ASLR disabled (-R option)