C (Programming Language)
Table of Contents
1. Pointers, UB and Optimization
- Advanced C: The UB and optimizations that trick good programmers: https://www.youtube.com/watch?v=w3_e9vZj7D8
What Every C Programmer Should Know About Undefined Behavior: https://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
by Chris Latner
- Undefined Behaviors exits to enable compiler to make your code faster. E.g.
Signed interger overflow
INT_MAX + 1 doesn't need to be INT_MIN. This undefined behavior allows the optimizing
X + 1 > X
totrue
.
- See the paper: What every compiler writer should know about programmers or Optimization based on undefined behaviour hurts performance [pdf]
About pointer provenance:
Pointers Are Complicated, or: What's in a Byte? [https://www.ralfj.de/blog/2018/07/24/pointers-and-bytes.html]
Pointers Are Complicated II, or: We need better language specs [https://www.ralfj.de/blog/2020/12/14/provenance.html#fnref:ub-difference]
2. Against C
- About C standard trying to not introduce warnings hinder the growth and safety of C: Why the C Language Will Never Stop You from Making Mistakes
- C languages prioritizes backwards compatability
- New warnings are considered breaking behaviour, so obvious safety improvements are ignored
- C committee resists changes that might disrupt old coldbases
- C must die: https://veresov.pro/cmustdie/ [Russian]
- Initially, C simplified low-level programming and portability but C is now unpredictable. This is because of its abstract machine model and undefined behaviors combined with aggressive compiler optimization.
- Compilers exploit undefined behavior for optimization, leading to unexpected results, security vulnerabilities, and non-portable programs.
- Developers like Linus Torvalds criticize the C standard's detachment from reality. However, compiler developers often prioritize strict adherence to the standard, without regard for practical consequences.
3. Debugging C
3.1. Address Sanitizer
You can compile C/C++ program with Address santizer which checks for memory leaks, use after free, and heap overflow. This helps catch bugs early on and makes debugging easier.
During compilation add -fsanitize=address
(optionally add debug symbols -g
and preserve stack traces -fno-omit-frame-pointer
). And during linking add statically link asan library -static-libasan
.
gcc -c main.c -fsanitize=addresss -g gc main.o -o main -fsanitize=address -static-libasan
Or when compilation and linking is done is single step:
gcc main.c -o main -fsanitize=address -g -static-libasan
Now when those memory related error happen the program will crash with a report that says where the memory was allocated, and where it was misused; along with stacktraces at both locations.
4. Misc
Kitsune: Efficient, General-purpose Dynamic Software Updating for C
Loads your program as a shared library, and allows updating the program while it is running. It works by allowing programmers to specify some points in program where update maybe done. And provides facilities to do data migration.
It was suppored by ExaScale Computing Project.