Skip to content

Instantly share code, notes, and snippets.

View MaskRay's full-sized avatar
🏠
Working from home. Need food

Fangrui Song MaskRay

🏠
Working from home. Need food
View GitHub Profile
@MaskRay
MaskRay / asan.md
Created January 7, 2024 08:11
Notes on AddressSanitizer

AddressSanitizer (ASan) is a compiler technology that checks addressability-related memory errors with some add-on checks. It consists of two parts: compiler instrumentation and runtime library. To put it in the simplest way,

  • The compiler instruments global variables, stack frames, and heap allocations to track shadow memory.
  • The compiler instruments memory access instructions to check shadow memory.
  • In case of an error, the inserted code calls a callback (implemented in the runtime library) to report an error with a stack trace. Normally the program will exit after the error message is printed.

Clang 3.1 implemented AddressSanitizer in 2011. GCC 4.8 integrated AddressSanitizer in 2012. MSVC (starting in Visual Studio 2019 version 16.9) added /INFERASANLIBS.

@MaskRay
MaskRay / differentials-not-closed.tsv
Last active January 17, 2024 01:14
reviews.llvm.org differentials that are not "Closed"
We can't make this file beautiful and searchable because it's too large.
https://reviews.llvm.org/D1 Abandoned Test klimek
https://reviews.llvm.org/D6 Abandoned Update all diagnostic messages in DiagnosticSemaKinds.td to use the new diffing format for types. rtrieu
https://reviews.llvm.org/D7 Abandoned test dblaikie
https://reviews.llvm.org/D9 Abandoned Test for playing around with emails klimek
https://reviews.llvm.org/D10 Abandoned A new test revision for funz klimek
https://reviews.llvm.org/D11 Abandoned Let's try again klimek
https://reviews.llvm.org/D12 Abandoned And yet another test klimek
https://reviews.llvm.org/D13 Abandoned test klimek
https://reviews.llvm.org/D14 Abandoned Test test klimek
https://reviews.llvm.org/D16 Abandoned Big test klimek
@MaskRay
MaskRay / optimize-lld-elf.md
Created October 26, 2023 05:29
Optimize lld/ELF

theme: ./theme class: text-center highlighter: shiki lineNumbers: false drawings: persist: false css: unocss routerMode: hash title: Optimize lld/ELF

@MaskRay
MaskRay / test-stack-protector.sh
Created October 25, 2023 02:39
Test Clang's -fstack-protector code generation for AArch32
#!/bin/zsh
CC=/tmp/Rel/bin/clang
if [[ -n $1 ]]; then
update=1
echo updated
else
update=
fi
[[ ! -e a.cc ]] && echo 'void bar(char *); void foo() { char a[200]; bar(a); }' > a.cc
@MaskRay
MaskRay / fix.py
Created July 26, 2023 20:37
Update tests for `[Driver] -###: exit with code 1 if hasErrorOccurred`
#!/usr/bin/env python3
import re, subprocess, sys
def main():
for line in open(sys.argv[1]).readlines():
line = line.strip()
m = re.match(r'Clang :: (.*)', line)
if not m: continue
name = m.group(1)
print(name)
@MaskRay
MaskRay / stack-unwinding-in-glibc.md
Created May 19, 2023 03:30
Stack unwinding in glibc

glibc functions perform stack unwinding mainly in two places, pthread_exit/pthread_cancel, and backtrace-family functions.

pthread_exit/pthread_cancel.

Here is a C++ program that calls pthread_exit. Shall the destructors ~A and ~B be called?

#include <pthread.h>
#include <stdio.h>

void foo() { pthread_exit(NULL); }
@MaskRay
MaskRay / kernel-arm64.md
Created May 2, 2023 06:57
Linux kernel's arm64 port

In the kernel source directory, run

make O=/tmp/linux/arm64 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j60 defconfig all

Then create an Alpine aarch64 image following https://hackmd.io/@starnight/Run_Alpine_on_QEMU_aarch64_Virtual_Machine

wget https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/netboot/vmlinuz-lts https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/aarch64/netboot/config-lts
@MaskRay
MaskRay / CMakeLists.txt
Last active August 13, 2024 22:56
cminify: shorten function/variable/type names in a C source file
cmake_minimum_required(VERSION 3.14)
project(cminify LANGUAGES C CXX)
add_executable(cminify "")
set(DEFAULT_CMAKE_BUILD_TYPE Release)
set_property(TARGET cminify PROPERTY CXX_STANDARD 17)
set_property(TARGET cminify PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET cminify PROPERTY CXX_EXTENSIONS OFF)
find_package(Clang REQUIRED)
@MaskRay
MaskRay / b.c
Created November 6, 2022 21:43
-fgnu89-inline / C99 / C++ inline modes
//__attribute__((gnu_inline))
EXTERN inline int foo(int a) { return a * a; }
int bar(int a) { return foo(a+1); }
@MaskRay
MaskRay / bench-lld.sh
Last active January 8, 2023 00:25
lld with different malloc implementations
# Build malloc implementations.
mkdir -p /tmp/p
pushd /tmp/p
if [[ ! -d jemalloc ]]; then
git clone https://github.com/jemalloc/jemalloc
(cd jemalloc
./autogen.sh
mkdir -p out/release && cd out/release
../../configure && make -j $(nproc))
fi