Skip to content

Instantly share code, notes, and snippets.

View heimskr's full-sized avatar
🍅
hej

Kai Tamkun heimskr

🍅
hej
View GitHub Profile
@heimskr
heimskr / atomicrmw_grimoire.md
Created August 24, 2022 09:26
An exploration of how Clang 14 translates the atomicrmw instruction to x86_64 assembly.

add

%3 = atomicrmw volatile add i64* %2, i64 %0 acq_rel, align 8
lock  xaddq  %rdi, -8(%rsp)

# Note: just addq if the result operand is unused
@heimskr
heimskr / libevent_multi_worker.cpp
Created May 2, 2022 16:15 — forked from fxsjy/libevent_multi_worker
libevent multithread worker example
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/thread.h>
# Crash reproducer for Lemon clang version 13.0.0 (https://github.com/LemonOSProject/llvm-project.git 841704471e60b2d2a8a56c2050abd3047a1a21be)
# Driver args: "--driver-mode=g++" "-I" "Kernel/kernel.sys.p" "-I" "Kernel/" "-I" "../Kernel" "-I" "../Kernel/include" "-I" "../Kernel/include/Arch/x86_64" "-I" "../subprojects/lai/include" "-I" "../subprojects/frigg/include" "-I" "../subprojects/cxxshim/stage2/include" "-Xclang" "-fcolor-diagnostics" "-pipe" "-D" "_FILE_OFFSET_BITS=64" "-Wall" "-Winvalid-pch" "-Wnon-virtual-dtor" "-Wextra" "-Werror" "-std=c++2a" "-g" "-msse3" "-mssse3" "-msse4.1" "-msse4.2" "-mpopcnt" "-mcx16" "-msahf" "-Wno-write-strings" "-Wno-unused-parameter" "-Wno-sign-compare" "-D" "Lemon64" "-ffreestanding" "-nostdlib" "-mcmodel=large" "-mno-red-zone" "-fno-pic" "-mno-mmx" "-mno-sse" "-mno-sse2" "-fno-stack-protector" "-fno-builtin" "-finline-functions" "-fno-exceptions" "-fno-rtti" "-Wno-deprecated-volatile" "-Wno-non-c-typedef-for-linkage" "-MD" "-MQ" "Kernel/kernel.sys.p/src_Objects_Process
# 1 "../Kernel/src/Objects/Process.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 403 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "../Kernel/src/Objects/Process.cpp" 2
# 1 "../Kernel/include/Objects/Process.h" 1
@heimskr
heimskr / struct.cpp
Created November 11, 2021 07:38
Says whether a {i32, i32, i24, i24, i32, i24, i24, i32} struct has padding on mips64
#include <iostream>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/LLVMContext.h>
#include <string>
int main() {
llvm::DataLayout datalayout("e-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128"); // mips64el data layout
llvm::LLVMContext llvm;
llvm::IntegerType *i24 = llvm::IntegerType::get(llvm, 24);
llvm::IntegerType *i32 = llvm::IntegerType::get(llvm, 32);
#include <iostream>
#include <map>
int main() {
int x = 0, y = 0;
enum Direction {Up, Right, Down, Left};
Direction direction = Up;
int max_extent = 1;
int extent = 0;
std::map<Direction, const char *> dirs {{Up, "up"}, {Right, "right"}, {Down, "down"}, {Left, "left"}};
@heimskr
heimskr / optional.cpp
Created February 6, 2020 04:54
Demonstrates strange behavior of std::optional
#include <iostream>
#include <optional>
#include <string>
struct Delete {
std::string str = "delete";
Delete() = delete;
Delete(const Delete &) = delete;
Delete(Delete &&) = delete;
Delete(const std::string &str_): str(str_) {
@heimskr
heimskr / get_core_id.cpp
Last active October 30, 2019 19:19
Returns the logical CPU core ID of the currently executing thread.
unsigned int get_core_id() {
unsigned int ebx;
__asm__("cpuid" : "=b" (ebx) : "a" (1));
// When you call cpuid with eax=1, the upper 8 bits of ebx contain the CPU ID.
return ebx >> 24;
}
unsigned int get_x2apic_id() {
unsigned int edx;
// When calling cpuid with eax=0xb, it seems you need to have ebx=0 to prevent a segfault.
@heimskr
heimskr / slowslrm.sh
Last active September 28, 2019 23:23
If your client is so terrible that decslrm.sh crashes it, run this and count the number of times you press enter before the crash to discover the culprit.
#!/bin/bash
enable_hmargins() { echo -ne "\x1b[?69h"; }
disable_hmargins() { echo -ne "\x1b[?69l"; }
hmargins() { echo -ne "\x1b[$1;$2s"; } # left, right
vmargins() { echo -ne "\x1b[$1;$2r"; } # top, bottom
origin_on() { echo -ne "\x1b[?6h"; }
origin_off() { echo -ne "\x1b[?6l"; }
scroll_up() { echo -ne "\x1b[$1S"; }
scroll_down() { echo -ne "\x1b[$1T"; }
@heimskr
heimskr / decslrm.sh
Last active September 28, 2019 23:24
Tests whether DECSLRM works properly.
#!/bin/bash
enable_hmargins() { echo -ne "\x1b[?69h"; }
disable_hmargins() { echo -ne "\x1b[?69l"; }
hmargins() { echo -ne "\x1b[$1;$2s"; } # left, right
vmargins() { echo -ne "\x1b[$1;$2r"; } # top, bottom
origin_on() { echo -ne "\x1b[?6h"; }
origin_off() { echo -ne "\x1b[?6l"; }
scroll_up() { echo -ne "\x1b[$1S"; }
scroll_down() { echo -ne "\x1b[$1T"; }