Skip to content

Instantly share code, notes, and snippets.

View larytet's full-sized avatar

Arkady Miasnikov larytet

View GitHub Profile
@larytet
larytet / kernel_debug.sh
Created July 10, 2017 12:59
Kernel debug memo
wget https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux
chmod +x extract-vmlinux
sudo ./extract-vmlinux /boot/vmlinuz-`uname -r` > vmlinux
crash vmlinux
@larytet
larytet / syscalls_counter.stp
Last active July 10, 2017 17:04
Collect system number of syscalls using STAP
// Use MAXMAPENTRIES to set the maximum size of the array
// for example sudo stap -D MAXMAPENTRIES=40000 ./src/driver/load_measurement.stp
global probe_frequency%
global processes%
probe begin
{
printf("Ctrl-C to print the results\n");
}
@larytet
larytet / shared_memory.h
Last active July 12, 2017 13:01
Shared memory API targeting SystemTap
/*
Shared memory (SHM_FIFO) is a byte ring buffer in the virtual memory. Driver pushes the data to
the tail of the FIFO (producer) and user space application reads the data from the head of the
buffer. Driver always places data structures in consecutive memory. If there is not enough
room to place the whole structure into the ring buffer without wrap around driver places special
"skip" code until the last byte of the allocated virtual memory and writes the structure
starting with offset zero. Application knows to handle the wrap around correctly by checking
if there is enough space for a strcuture of minimum size and skipping the data to the end of the
virtual memory.
@larytet
larytet / alloc_free.c
Created July 14, 2017 17:35
Memory allocation in the kernel
static void *rvmalloc(unsigned long size)
{
void *mem;
unsigned long adr;
size = PAGE_ALIGN(size);
mem = vzalloc(size); //syscalls_trace_buffer;//vmalloc(size);
# if (SHM_RESERVE_PAGES > 0)
if (mem) {
// memset(mem, 0, size); vzalloc() will zero the memory
#include <stdio.h>
#include <stddef.h>
// Based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
struct a
{
int a;
int b;
int c;
};
@larytet
larytet / password_card.py
Last active August 21, 2017 10:07
Password cards
#!/usr/bin/env python
# See also http://www.averk.net/passgen.html
# https://www.passwordcard.org/en
'''
The script will output something like
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 W j ~ E ^ y x ! N q S ! r e e g a q f P c Q : q & Q
@larytet
larytet / string_compare.c
Last active August 26, 2017 08:56
An implementation of strcmp()
// https://godbolt.org/g/56Lv4f - 62 opcodes
int strcmp_original(char *a, char *b)
{
int c = strlen(a) > strlen(b) ? strlen(a) : strlen(b);
char * d;
for (d = a;d < a + c;++d, ++b)
{
if (*d != *b)
{
// Shorter version of the same, failed after 400 driver restarts
/*
Load and run:
scl enable devtoolset-4 bash
stap -g -p4 -m test_open -v -k test_open.stp
count=0;while [ 1 ];do echo $count;count=$(($count+1));sudo rmmod test_open;sudo staprun test_open.ko -L ;done;
*/
global ARRAY_FILENAME%