Skip to content

Instantly share code, notes, and snippets.

View m1lkweed's full-sized avatar

m1lkweed

View GitHub Profile
#include <stdio.h>
#define SIMD_SSE 0x1
#define SIMD_SSE2 0x2
#define SIMD_SSE3 0x4
#define SIMD_SSE4_1 0x8
#define SIMD_SSE4_2 0x10
#define SIMD_AVX 0x20
#define SIMD_AVX2 0x40
#define SIMD_AVX512F 0x80
@m1lkweed
m1lkweed / memclear.c
Created April 7, 2022 12:45
Guarantee a call to memset, even when optimizing
#include <stddef.h>
static inline void *memclear(void *dest, size_t len){
asm inline volatile("call memset"
:
:"r"(dest),
"r"(0),
"r"(len)
:
);
@m1lkweed
m1lkweed / fatptr.c
Last active May 14, 2022 04:27
Pointers that store array size data, x86_64 only; not recommended for use.
#include <inttypes.h>
#define IS_ARRAY(x) _Generic(&x, default: 1, typeof(*x)**: 0)
#define COUNT_OF(x) _Generic(&x, default: (sizeof(x) / sizeof(0[x])), typeof(*x)**: 0)
#define array_to_fatptr(x) ((fatptr_t){.addr = x, .n_elems = (int16_t)COUNT_OF(x)})
#define fatptr_to_ptr(x, type) ((type*)(intptr_t)x.addr)
typedef union fatptr_t{
void *ptr;
@m1lkweed
m1lkweed / inject.c
Last active November 2, 2022 02:45
Inject a function in-between the current function and its caller
// Must be compiled with optimizations enabled, all options except -O0 are supported
#include <stddef.h>
#include <inttypes.h>
// Injects a function in-between the current function and its caller,
// injections are visited in reverse order, see example.
[[gnu::always_inline]] static inline void inject(void *addr){
asm("call .+5");
void * volatile *p = __builtin_frame_address(0);
@m1lkweed
m1lkweed / asm_sizeof.c
Last active May 19, 2022 18:58
Evaluate a body of code and return its size
// Works in GCC and clang, produces correct results at all optimization levels
// returns the contents of the instruction pointer
#define get_pc() ({ \
register void *temp; \
asm volatile("call .+5\n\t" \
"pop %0" \
:"=r"(temp) \
); \
temp; \
@m1lkweed
m1lkweed / preserve_dead_code.c
Created May 19, 2022 11:35
Preserve dead code during compilation
// works on all architectures and optimization levels in GCC and clang
// Dead code is optimized like running code and may be removed if the
// compiler can prove that no branches will ever reach it.
#define preserve_dead_code(body) ({ \
__label__ label; \
asm goto(""::::label); \
if(0)label:{body} \
&&label; \
})
@m1lkweed
m1lkweed / on_stack.c
Last active June 26, 2022 04:46
Returns true for addresses on the stack
[[gnu::noinline]] _Bool on_stack(void *addr){
char stack_check[1];
return (addr >= (void*)&stack_check[1]); // stack_check[1] is OoB but it produces better assembly
}
@m1lkweed
m1lkweed / get_pos.c
Created July 14, 2022 16:26
Get the position of the tty cursor
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int get_pos(int *col, int *line){
char buf[30]={0};
int ret, i, pow;
char ch;
*line = 0;
*col = 0;
@m1lkweed
m1lkweed / is_real_tty.c
Last active July 15, 2022 03:37
Returns true when running under a TTY, false under a VT
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <linux/kd.h>
#include <sys/ioctl.h>
static int is_a_console(int fd){
char arg = 0;
@m1lkweed
m1lkweed / posix_kbhit.c
Last active May 26, 2023 19:36
Windows' kbhit() ported to POSIX systems
#include <unistd.h> // STDIN_FILENO
#include <termios.h> //tcsetattr/tcgetattr
#include <sys/ioctl.h> // ioctl
int kbhit(){
struct termios term;
tcgetattr(STDIN_FILENO, &term);
struct termios term2 = term;
term2.c_lflag &= ~ICANON; // disable canonical mode
tcsetattr(STDIN_FILENO, TCSANOW, &term2);