int sum1(int a[], size_t N) {
int s = 0;
for (size_t i = 0; i < N; i++) {
s += a[i];
}
return s;
}
#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; | |
} |
#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; | |
} |
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.
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.
// 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 { |
- 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
#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> |
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; |