Skip to content

Instantly share code, notes, and snippets.

View dannas's full-sized avatar

Daniel Näslund dannas

View GitHub Profile
@dannas
dannas / codegen.c
Last active October 2, 2020 20:06
Examples of gcc codegen for ARM cortex-M4
#include <stdbool.h>
#include <math.h>
// https://godbolt.org/z/sb5cof
int add2(int x) {
x = x + 1;
int y = x + 1;
//int y = x;
return x + y;
}
@dannas
dannas / licm.c
Last active September 25, 2020 06:01
Examples of loop hoisting (LICM)
#include <stddef.h>
// TODO(dannas): Find examples of loop hoisting. WIP
// https://godbolt.org/z/YWf5c7
// baseline
void f1(int a[], size_t N) {
for (size_t i = 0; i < N; i++) {
a[i] = i;
}
@dannas
dannas / loops.md
Last active September 11, 2020 20:10
int sum1(int a[], size_t N) {
    int s = 0;
    for (size_t i = 0; i < N; i++) {
        s += a[i];
    }
    return s;
}

gcc -O0 "Jump to Middle"

@dannas
dannas / debugging_optimized_code.md
Last active September 6, 2020 17:58
Tentative outline for debugging optimized code.

My background: Not an expert on debuggers nor embedded systems.

Scope: GNU toolchain. What code does gcc generate? How does gdb interpret it?

END GOAL: An embedded developer should have an intuition for how call flow, control flow and data flow is presented by the debugger.

Prior Art

Many people have described DWARF and how to debug optimized code, but I haven't found an article that gives practical examples of debugging sessions with optimized code.

@dannas
dannas / gen_union.c
Created May 11, 2020 19:30
Experiment with union/struct field access generation
// Experiment with expressions for reading from structs and union fields.
// https://godbolt.org/z/G_fsbD
typedef union {
struct {
long u;
short v;
char w;
} t1;
struct {
@dannas
dannas / spectre_notes.md
Created May 5, 2020 05:30
Notes from reading the Spectre paper

Background

  • side-channel attacks have been around since 90's
  • speculative execution can execute the program in a possible incorrect way, but state is rolled back before visible (atleast that's the idea)
  • There has been micro-architectural vuln before: cache timings, rowhammer, branch target buffers
  • Trick the cpu into exec instr that should not have been executed
  • Attacks both using native code, javascript and eBPF
  • An attack
    • Locate or introduce instr which act as covert channel
@dannas
dannas / test_prefetcher_stride.c
Created April 30, 2020 14:24
Experiment with cacheline strides for prefetching
#define RUN_ME /*
gcc -O2 -Wall $0 -o "$(basename $0 .c)" && ./"$(basename $0 .c)" ; exit
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
@dannas
dannas / adhoc_event.md
Created April 23, 2020 16:12
Notes for Adhoc event April 23rd

Adhoc: a virtual event to explore system engineering

https://adhoc.community/#

Why mmap is faster than system calls

Q: From MDB post: Some op benefit from mmap at the expense of others: Any more insight here? Q: How do you deal with I/O errors? They cause SIGSEGV, right? Q: Did you have any problems with network drives: May give SIGBUS instead of SIGSEGV Q: Does WiredTiger support Windows? Any problems using MapViewOfFile instead of mmap. I've heard MapViewOfFile puts in a lock?

// https://godbolt.org/z/pbULFF
#include <atomic>
int read(std::atomic<int>& a) {
return a.load(std::memory_order_seq_cst);
}
void write(std::atomic<int>& a, int value) {
return a.store(value, std::memory_order_seq_cst);
// Unfinished example of clobber and doNotOptimize functions.
// https://godbolt.org/z/CkZ9Bh
void f() {
int val;
//asm volatile("" : : "r,m"(val) : "memory");
//asm volatile("" : : "rm"(val) : "memory");
asm volatile("" : "+m"(val) : : "memory");
val = 1;
asm volatile("" : : : "memory");
val = 2;