Steven-Jaro
☰ Menu

Paradigms, C and compilation

Paradigms

Well, it is time to start learning. The problem here is: which paradigm and which philosophy should I choose? The answer is complicated and depends a lot on one's goals. Fortunately, in the previous post I had already described the focus I wanted to acquire, point by point. Just as a reminder, here they are again:

1. Open and auditable code.
2. Minimalist code, not bloated, with a reduced attack surface.
3. Readable and well-documented code.
4. Efficient and portable code that can be used on whatever system the user prefers.
5. Secure code that handles failures correctly.

Now all that is left is to do some research to narrow our focus and find out which existing paradigms and philosophies will give us a hand so we do not reinvent the wheel. 1. As many of you know, Linux is mentioned as the alternative to Windows and Mac because its code is completely public, modifiable and redistributable. I will not explain this whole topic from scratch; I will just say that in the world where you are allowed to read the code of the tools you use, there are paradigms: "Open Source" and "Free Software". Both are options that fit well for now; we will not commit to either one since we are at a very early stage and it is not necessary. These are the resources for both paradigms. To understand the paradigms properly, it is important to carefully read what the main resources say:
Open Source: https://opensource.org/
Free Software: https://www.fsf.org/

2. The previous point already narrowed down the possible candidates considerably. Now it is minimalism's turn. Although it may sound simple, minimalism in the world of auditable code is a whole world of its own, with quite a few paradigms. Based on what I have seen in my experience, the main references for minimalism in auditable-code programming are the following:
Suckless: https://suckless.org/
OpenBSD: https://www.openbsd.org/
NetBSD: https://www.netbsd.org/
SeL4: https://sel4.systems/
Note: There may be more, but based on my research and experience, I think it is more worthwhile to focus on the four above, since minimalism is one of their main goals. Unfortunately, Linux does not pass this filter, since it tends to focus more on general use, which makes it hard for me to consider Linux code minimalist despite it being considered high quality, especially compared to proprietary code.

3. Now we have four references to choose from; let's keep filtering. In terms of readability, all four references are quite good, but they approach it differently. Suckless focuses on the code itself being extremely easy to understand for a programmer at first glance. SeL4, OpenBSD and NetBSD do not consider it as necessary for the code itself to state what the program does as extremely directly as Suckless demands; instead, they focus on providing extremely thorough documentation, but even so, all three still care a great deal about the code being readable.

4. This is where Suckless loses to OpenBSD and NetBSD. Suckless software is very efficient and cares a lot about the code being super readable and understandable for advanced users, to the point of seeing abstractions as problematic in certain places, and because of this, the code is not really made to be super portable without having to touch the code. It is important to note that Suckless sees it as entirely positive for users to modify the code, and in fact you have to do so to get the most out of their tools. NetBSD is the main reference in terms of portability, since it takes abstractions and support for different architectures extremely seriously. OpenBSD and SeL4, for somewhat more collateral reasons, are also very portable, but they do not focus primarily on this aspect the way OpenBSD does.

5. The last point is where there is some conflict. In terms of security, all 3 are definitely very good references, but the definitive order would be SeL4, OpenBSD and NetBSD. SeL4 was built using extremely advanced computer science methodologies, namely proving through mathematical methods and theorems that the code is secure and predictable, as can be seen in SeL4's official documentation. The methodology SeL4 was built on is definitely supreme, but at the same time it is the most costly in terms of time, and I do not think it is a good idea for me as a beginner. Fortunately, according to what I was able to research, in theory any program could apply the methodologies SeL4 was built on at more advanced stages. This leaves me with OpenBSD as the philosophy I can take as a reference for this project, since it focuses on minimalism, thanks to the way it handles security it ends up being quite portable, it is quite demanding, and overall it is a very solid reference for well-structured code.

Note: OpenBSD was chosen as the main reference and SeL4 as a possible secondary reference, but that does not mean I will commit 100% to their philosophies; I may differ at times in practices and tools. There is no fanaticism here; I will not try to be exactly like the developers of these tools, since it makes no practical sense. They are just a kind of compass for the standards and practices I will follow when programming. I know I summarized a lot and did not go deep into each one's philosophy, but I do not see it as necessary, since ideology is only an optional frame of reference.

Tools

Now that we have chosen our references, let's choose the tool stack we will use.

The operating system we will use is OpenBSD: https://www.openbsd.org/

As for the text editor, I tried using vi, but honestly I am not used to its single-color nature yet. So I decided to use Vim: https://man.freebsd.org/cgi/man.cgi?vim(1)

The compiler will be Clang. It is not very different from gcc in terms of usage. This is the resource I will use as a reference: https://clang.llvm.org/docs/UsersManual.html

To make development easier, I will use BSD Make: https://man.openbsd.org/make

The programming language I will learn is C.

C

To learn C, I will use the most obvious resource possible, the "C programming v2 manual". I will follow the steps as I see fit.
Let's go step by step. I installed an OpenBSD virtual machine in Virt Manager, with Qemu and KVM. I already had this stack, and my host OS is Void Linux (I will not go into details about my host). For a more comfortable experience, I decided to program on the OpenBSD VM over SSH.
I used the doas and pkg_add tools to install what I needed.
To keep the story short, I used mkdir to create a directory called prueba and entered it with cd. Then, with vim, I created the first file, called main.c.
I followed the C manual's instructions up to page 10 and, using clang, managed to compile my first C program.

openjazz$ mkdir prueba
openjazz$ cd prueba/
openjazz$ vim main.c
    
#include <stdio.h>

main()
{
        printf("hello, world\n");
}
    
openjazz$ clang main.c                                                                                                                                                                        
main.c:3:1: warning: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
    3 | main()
      | ^
      | int
1 warning generated.
openjazz$ ls  
a.out  main.c
openjazz$ ./a.out                                                                                                                                                                             
hello, world
    

Perfect, the C manual has been very helpful so far. The challenge will come in the future when we start applying OpenBSD standards to the code I write.