diff --git a/content/chapters/part1/chapter-04.md b/content/chapters/part1/chapter-04.md index f057096..05dfadc 100644 --- a/content/chapters/part1/chapter-04.md +++ b/content/chapters/part1/chapter-04.md @@ -2,13 +2,13 @@ title: "A First Look at the C Programming Language" description: "This chapter introduces the C programming language for complete beginners." author: "Edson Brandi" -date: "2025-08-21" +date: "2025-08-25" status: "draft" part: 1 chapter: 4 reviewer: "TBD" translator: "TBD" -estimatedReadTime: 240 +estimatedReadTime: 480 --- # A First Look at the C Programming Language @@ -21,6 +21,23 @@ Our goal here isn't to become master C programmers in one chapter. Instead, I wa By the time we're done, you'll be able to read and write basic C programs, understand the core syntax, and feel confident enough to take the next steps toward kernel development. But that part will come later, for now, let's focus on learning the essentials. +## Reader Guidance: How to Use This Chapter + +This chapter is not just a quick read; it's both a **reference** and a **hands-on bootcamp** in C programming with a FreeBSD flavour. How much time you'll spend here depends on how deep you go: + +- **Reading only:** Around **8 hours** to read all explanations and FreeBSD kernel examples at a beginner's pace. +- **Reading + labs:** Around **12-13 hours** if you pause to type, compile, and run each of the practical labs on your FreeBSD system. +- **Reading + labs + challenges:** Around **14-15 hours or more**, since the challenge exercises will require you to stop, think, debug, and sometimes revisit earlier material. + +### How to Get the Most Out of This Chapter + +- **Take it in sections.** Don't try to do it all in one sitting. Each section (variables, operators, control flow, pointers, etc.) can be studied independently and practised before moving on. +- **Type the code yourself.** Copy-pasting examples skips the muscle memory. Typing them builds fluency in C and FreeBSD's development environment. +- **Use the FreeBSD source tree.** Many examples link directly to real kernel code. Open the referenced files and read them in context to see how theory connects to production code. +- **Do the challenges last.** They're meant to consolidate everything. Attempt them once you feel comfortable with the main text and labs. + +This chapter is long because C is the foundation for everything else in FreeBSD device drivers. Think of it as your **toolbox**: once you master it, all the later chapters will make much more sense. + ## 4.1 Introduction Let's start at the beginning: what is C, and why is it important to us? @@ -5159,4 +5176,1095 @@ By now you can clearly see the difference between an array of pointers and a poi With this foundation in place, we are ready to take the next step: moving from fixed arrays to dynamically allocated memory. In the following section on **Dynamic Memory Allocation**, you will learn how to use functions such as `malloc`, `calloc`, `realloc`, and `free` to create arrays at runtime. We will connect this to pointers by showing how to allocate each element separately, how to request one contiguous block when you need a pointer to an array, and how to clean up properly if something goes wrong. This transition from static to dynamic memory is essential for real systems programming and will prepare you for the way memory is managed inside the FreeBSD kernel. +### Dynamic Memory Allocation + +So far, most of the memory we used in examples was **fixed-size**: arrays with a known length or structures allocated on the stack. But when writing system-level code like FreeBSD device drivers, you often don't know in advance how much memory you'll need. Maybe a device reports the number of buffers only after probing, or the amount of data depends on user input. That's when **dynamic memory allocation** comes into play. + +Dynamic allocation allows your code to **ask the system for memory while it's running**, and to give it back when it's no longer needed. This flexibility is essential for drivers, where hardware and workload conditions can change at runtime. + +#### User Space vs Kernel Space + +In user space, you've probably seen functions like: + +- `malloc(size)` - allocates a block of memory. +- `calloc(count, size)` - allocates and zeroes a block. +- `free(ptr)` - releases previously allocated memory. + +Example: + +```c +#include +#include + +int main(void) { + int *nums; + int count = 5; + + nums = malloc(count * sizeof(int)); + if (nums == NULL) { + printf("Memory allocation failed!\n"); + return 1; + } + + for (int i = 0; i < count; i++) { + nums[i] = i * 10; + printf("nums[%d] = %d\n", i, nums[i]); + } + + free(nums); // Always release what you allocated + return 0; +} +``` + +In user space, memory comes from the **heap**, managed by the C runtime and the operating system. + +But inside the **FreeBSD kernel**, we cannot use ``'s `malloc()` or `free()`. The kernel has its own allocator, designed with stricter rules and better tracking. The kernel API is documented in `malloc(9)`. + +#### Visualising Memory in User Space + +``` ++-----------------------------------------------------------+ +| STACK (grows down) | +| - Local variables | +| - Function call frames | +| - Fixed-size arrays | ++-----------------------------------------------------------+ +| ^ | +| | | +| HEAP (grows up) | +| - malloc()/calloc()/free() | +| - Dynamic structures and arrays | ++-----------------------------------------------------------+ +| DATA SEGMENT | +| - Globals | +| - static variables | +| - .data (initialized) / .bss (zero-initialized) | ++-----------------------------------------------------------+ +| CODE / TEXT SEGMENT | +| - Program instructions | ++-----------------------------------------------------------+ +``` + +**Note:** Stack and heap grow toward each other at runtime. Fixed-size data lives on the stack or in the data segment, while dynamic allocations come from the heap. + +#### Kernel-Space Allocation with malloc(9) + +To allocate memory in the FreeBSD kernel you use: + +```c +#include + +void *malloc(size_t size, struct malloc_type *type, int flags); +void free(void *addr, struct malloc_type *type); +``` + +Example from the kernel: + +```c +char *buf = malloc(1024, M_TEMP, M_WAITOK | M_ZERO); +/* ... use buf ... */ +free(buf, M_TEMP); +``` + +**Breaking it down:** + +- `1024` → the number of bytes. +- `M_TEMP` → memory type tag (explained below). +- `M_WAITOK` → wait if memory is temporarily unavailable. +- `M_ZERO` → ensure the block is zeroed. +- `free(buf, M_TEMP)` → release the memory. + +#### Kernel malloc(9) Workflow + +``` +┌──────────────────────────┐ +│ Driver code │ +│ │ +│ ptr = malloc(size, │ +│ TYPE, │ +│ FLAGS); │ +└─────────────┬────────────┘ + │ request + v +┌──────────────────────────┐ +│ Kernel allocator │ +│ - Typed pools (TYPE) │ +│ - Honors FLAGS: │ +│ M_WAITOK / NOWAIT │ +│ M_ZERO │ +│ - Accounting & tracing │ +└─────────────┬────────────┘ + │ returns pointer + v +┌──────────────────────────┐ +│ Driver uses buffer │ +│ - Fill/IO/queues/etc. │ +│ - Lifetime under driver │ +│ responsibility │ +└─────────────┬────────────┘ + │ later + v +┌──────────────────────────┐ +│ free(ptr, TYPE); │ +│ - Returns memory to │ +│ kernel pool │ +│ - TYPE must match │ +└──────────────────────────┘ +``` + +**Note:** In the kernel, every allocation is **typed** and controlled by flags. Pair every `malloc(9)` with a `free(9)` on all code paths, including errors. + +#### Memory Types and Flags + +One unique aspect of FreeBSD's kernel allocator is the **type system**: every allocation must be tagged. This makes debugging and leak tracking easier. + +Some common types: + +- `M_TEMP` - temporary allocations. +- `M_DEVBUF` - buffers for device drivers. +- `M_TTY` - terminal subsystem memory. + +Common flags: + +- `M_WAITOK` - sleep until memory is available. +- `M_NOWAIT` - return immediately if memory cannot be allocated. +- `M_ZERO` - zero out memory before returning. + +This explicit style encourages safe, predictable memory usage in critical kernel code. + +#### Hands-On Lab 1: Allocating and Freeing a Buffer + +In this exercise, we'll create a simple kernel module that allocates memory when loaded and frees it when unloaded. + +**my_malloc_module.c** + +```c +#include +#include +#include +#include +#include + +static char *buffer; +#define BUFFER_SIZE 128 + +MALLOC_DEFINE(M_MYBUF, "my_malloc_buffer", "Buffer for malloc module"); + +static int +load_handler(module_t mod, int event, void *arg) +{ + switch (event) { + case MOD_LOAD: + buffer = malloc(BUFFER_SIZE, M_MYBUF, M_WAITOK | M_ZERO); + if (buffer == NULL) + return (ENOMEM); + + snprintf(buffer, BUFFER_SIZE, "Hello from kernel space!\n"); + printf("my_malloc_module: %s", buffer); + return (0); + + case MOD_UNLOAD: + if (buffer != NULL) { + free(buffer, M_MYBUF); + printf("my_malloc_module: Memory freed\n"); + } + return (0); + default: + return (EOPNOTSUPP); + } +} + +static moduledata_t my_malloc_mod = { + "my_malloc_module", load_handler, NULL +}; + +DECLARE_MODULE(my_malloc_module, my_malloc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); +``` + +**What to do:** + +1. Build with `make`. +2. Load with `kldload ./my_malloc_module.ko`. +3. Check `dmesg` for the message. +4. Unload with `kldunload my_malloc_module`. + +You'll see how memory is reserved and later freed. + +When you load and unload the module, you will see the allocation and freeing in action. + +#### Hands-On Lab 2: Allocating an Array of Structures + +Now let's extend the idea by creating an array of structs dynamically. + +```c +struct my_entry { + int id; + char name[32]; +}; + +MALLOC_DEFINE(M_MYSTRUCT, "my_struct_array", "Array of my_entry"); + +static struct my_entry *entries; +#define ENTRY_COUNT 5 +``` + +On load: + +- Allocate memory for five entries. +- Initialize each one with an ID and name. +- Print them out. + +On unload: + +- Free the memory. + +This exercise mirrors what real drivers do when they keep track of device states, DMA buffers, or I/O queues. + +#### Real FreeBSD Example: Building the corefile path in `coredump_vnode.c` + +Let's look at a real example from FreeBSD's source code: `sys/kern/coredump_vnode.c`. This function builds the path for a process core dump. It allocates a temporary buffer, uses it to assemble the path string, and later frees it. I've added additional comments to the example code below to make it easier for you to understand what happens at each step: + +```c +/* + * corefile_open(...) builds the final core file path into a temporary + * kernel buffer named `name`. The buffer must be large enough to hold + * a path, so MAXPATHLEN is used. The buffer is freed by the caller + * (see the coredump_vnode() snippet further below). + */ +static int +corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td, + int compress, int signum, struct vnode **vpp, char **namep) +{ + struct sbuf sb; + const char *format; + char *hostname, *name; + int indexpos, indexlen, ncores; + + hostname = NULL; + format = corefilename; + + /* + * Allocate a zeroed path buffer from the kernel allocator. + * - size: MAXPATHLEN bytes (fits a full path) + * - M_TEMP: a temporary allocation type tag (helps tracking/debug) + * - M_WAITOK: ok to sleep if memory is briefly unavailable + * - M_ZERO: return the buffer zeroed (no stale data) + */ + name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO); /* <-- allocate */ + indexlen = 0; + indexpos = -1; + ncores = num_cores; + + /* + * Initialize an sbuf that writes directly into `name`. + * SBUF_FIXEDLEN means: do not auto-grow, error if too long. + */ + (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN); + + /* + * The format string (kern.corefile) may include tokens like %N, %P, %U. + * Iterate, expand tokens, and append to `sb`. If %H (hostname) appears, + * allocate a second small buffer for it, then free it immediately after. + */ + /* ... formatting loop omitted for brevity ... */ + + /* hostname was conditionally allocated above; free it now if used */ + free(hostname, M_TEMP); /* <-- free small temp */ + + /* + * If compression is requested, append a suffix like ".gz" or ".zst". + * If the sbuf overflowed, clean up and return ENOMEM. + */ + if (sbuf_error(&sb) != 0) { + sbuf_delete(&sb); /* dispose sbuf wrapper (no malloc here) */ + free(name, M_TEMP); /* <-- free on error path */ + return (ENOMEM); + } + sbuf_finish(&sb); + sbuf_delete(&sb); + + /* + * On success, return `name` to the caller via namep. + * Ownership of `name` transfers to the caller, who must free it. + */ + *namep = name; + return (0); +} +``` + +What to notice here: + +- The buffer is **typed** with `M_TEMP` to help kernel memory accounting and leak detection. This is a FreeBSD kernel convention that you'll reuse for your own drivers by defining your own `MALLOC_DEFINE` tag. +- `M_WAITOK` is chosen because this path can safely sleep; the kernel allocator will wait rather than fail spuriously. If you are in a context where sleeping is unsafe, you must use `M_NOWAIT` and handle allocation failure immediately. +- Error paths **free what they allocate** before returning. This is the habit to ingrain early: every `malloc(9)` must have a clear and reliable `free(9)` in all paths. + +Now let's see where the caller cleans up: + +```c +/* + * coredump_vnode(...) calls corefile_open() to obtain both the vnode (vp) + * and the dynamically built `name` path. After it finishes writing the core + * or handling errors, it must free `name`. This snippet shows both another + * temporary allocation (for cwd when needed) and the final free(name). + */ +static int +coredump_vnode(struct thread *td, off_t limit) +{ + struct vnode *vp; + char *name; /* corefile path returned by corefile_open() */ + char *fullpath, *freepath = NULL; + size_t fullpathsize; + struct sbuf *sb; + int error, error1; + + /* Build name + open/create target vnode */ + error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, + compress_user_cores, sig, &vp, &name); + if (error != 0) + return (error); + + /* ... write/extend coredump, lock ranges, set attributes, etc ... */ + + /* + * When emitting a devctl notification, if the core path is relative, + * allocate a small temporary buffer to fetch the current working dir, + * then free it once we've appended it to the sbuf. + */ + if (name[0] != '/') { + fullpathsize = MAXPATHLEN; + freepath = malloc(fullpathsize, M_TEMP, M_WAITOK); /* <-- allocate temp */ + if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) { + free(freepath, M_TEMP); /* <-- free on error */ + /* ... fall through to cleanup below ... */ + } + /* use fullpath ... */ + free(freepath, M_TEMP); /* <-- free on success */ + /* ... continue building notification ... */ + } + +out: + /* + * Close the vnode we opened and then free the dynamically built `name`. + * This pairs with the malloc(MAXPATHLEN, ...) in corefile_open(). + */ + error1 = vn_close(vp, FWRITE, cred, td); + if (error == 0) + error = error1; + free(name, M_TEMP); /* <-- final free */ + return (error); +} +``` + +This example highlights several important practices that apply directly to device driver development. Temporary kernel strings and small work buffers are often created with `malloc(9)` and must always be released, whether the code succeeds or fails, as shown in the careful cleanup logic of `coredump_vnode()`. T + +he choice between `M_WAITOK` and `M_NOWAIT` also depends on context: in code paths where the kernel can safely sleep, `M_WAITOK` ensures the allocation will eventually succeed, while in contexts such as interrupt handlers, where sleeping is forbidden, `M_NOWAIT` must be used and a `NULL` pointer handled immediately. Finally, keeping allocations local and freeing them as soon as their last use is complete reduces the risk of memory leaks and use-after-free errors. + +The handling of the short-lived `freepath` buffer is a clear demonstration of this principle in practice. + +#### Why This Matters in FreeBSD Device Drivers + +In real-world drivers, memory needs are rarely predictable. A network card might advertise the number of receive descriptors only after you probe it. A storage controller could require buffers sized according to device-specific registers. Some devices maintain tables that grow or shrink depending on the workload, such as pending I/O requests or active sessions. All of these cases require **dynamic memory allocation**. + +Static arrays cannot cover such situations because they are fixed at compile time, wasting memory if oversized or failing outright if undersized. With `malloc(9)` and `free(9)`, a driver can adapt to the actual hardware and workload, allocating exactly what is needed and returning memory once it is no longer in use. + +However, this flexibility comes with responsibility. Unlike in user space, memory management errors in the kernel can destabilise the entire system. A missed `free()` becomes a memory leak that weakens long-term stability. An invalid pointer access after freeing can crash the kernel instantly. Overruns and underruns can silently corrupt memory structures used by other subsystems, sometimes turning into security vulnerabilities. + +This is why learning to allocate, use, and release memory correctly is one of the foundational skills for FreeBSD driver developers. Getting this right ensures that your driver not only works under normal conditions but also behaves safely under stress, making the system reliable as a whole. + +##### Real Driver Scenarios + +Here are some practical cases where dynamic memory allocation is essential in FreeBSD device drivers: + +- **Network drivers:** Allocate rings of packet descriptors whose size depends on the NIC's capabilities. +- **USB drivers:** Create transfer buffers sized to the maximum packet length reported by the device. +- **PCI storage controllers:** Build command tables that expand with the number of active requests. +- **Character devices:** Manage per-open data structures that exist only while a user process holds the device open. + +These examples show that dynamic allocation is not just an academic exercise: it is a daily requirement for making real drivers interact safely and efficiently with hardware. + +#### Common Beginner Pitfalls + +Dynamic allocation in kernel code introduces some traps that are easy to overlook: + +**1. Leaking memory on error paths** + It's not enough to free memory in the "happy path." If an error occurs after you've allocated but before the function exits, forgetting to free will leak memory inside the kernel. + *Tip:* Always trace every exit path and make sure each allocated block is either used or freed. Using a single cleanup label at the end of your function is a common pattern in FreeBSD. + +**2. Freeing with the wrong type** + Every `malloc(9)` call is tagged with a type. Freeing with a mismatched type may confuse the kernel's memory accounting and debugging tools. + *Tip:* Define a custom tag for your driver with `MALLOC_DEFINE()` and always free with that same tag. + +**3. Assuming allocation always succeeds** + In user space, `malloc()` often succeeds unless the system is badly constrained. In the kernel, especially with `M_NOWAIT`, allocation can legitimately fail. + *Tip:* Always check for `NULL` and handle the failure gracefully. + +**4. Choosing the wrong allocation flag** + Using `M_WAITOK` in contexts that cannot sleep (like interrupt handlers) can deadlock the kernel. Using `M_NOWAIT` when sleeping is safe may force needless failure handling. + *Tip:* Understand the context of your allocation and pick the correct flag. + +#### Challenge Questions + +1. In a driver's `detach()` routine, what can happen if you forget to free dynamically allocated buffers? +2. Why is it important that the type passed to `free(9)` matches the one used in `malloc(9)`? +3. Imagine you allocate memory with `M_NOWAIT` during an interrupt. The call returns `NULL`. What should your driver do next? +4. Why is checking every error path after a successful allocation just as important as freeing on the success path? +5. If you use `M_WAITOK` inside an interrupt filter, what dangerous condition might arise? + +#### Wrapping Up + +You have now seen how C's dynamic memory allocation works in user space and how FreeBSD extends this idea with its own `malloc(9)` and `free(9)` for the kernel. You learned why allocations must always be paired with cleanups, how memory types and flags guide safe allocation, and how real FreeBSD code uses these patterns every day. + +Dynamic allocation gives your driver the flexibility to adapt to hardware and workload demands, but it also introduces new responsibilities. Handling every error path, choosing the right flags, and keeping allocations short-lived are the habits that separate safe kernel code from fragile code. + +In the next section, we will build directly on this foundation by looking at **memory safety in kernel code**. There, you will learn techniques to protect against leaks, overflows, and use-after-free errors, making your driver not only functional but also reliable and secure. + +### Memory Safety in Kernel Code + +When writing kernel code, especially device drivers, we are working in a privileged and unforgiving environment. There is no safety net. In user-space programming, a crash usually terminates only your process. In kernel-space, a single bug can panic or reboot the entire operating system. That is why memory safety is not optional. It is the foundation of stable and secure FreeBSD driver development. + +You must constantly remember that the kernel is persistent and long-running. A memory leak will accumulate for as long as the system remains up. A buffer overflow can silently overwrite unrelated data structures and later trigger a mysterious crash. Using an uninitialized pointer can panic the system instantly. + +This section introduces the most common mistakes, shows you how to avoid them, and gives you practice through real experiments, both in user space and inside a small kernel module. + +#### What Can Go Wrong? + +Most kernel bugs caused by beginners can be traced to unsafe memory handling. Let's list the most frequent and dangerous ones: + +- **Using uninitialized pointers**: a pointer that is not set to a valid address contains garbage. Dereferencing it usually causes a panic. +- **Accessing freed memory (use-after-free)**: once memory is released, it must never be touched again. Doing so corrupts memory and destabilises the kernel. +- **Memory leaks**: failing to call `free()` after `malloc()` means the memory remains reserved forever, slowly consuming kernel resources. +- **Buffer overflows**: writing beyond the end of a buffer overwrites unrelated memory. This can corrupt kernel state or introduce security vulnerabilities. +- **Off-by-one array errors**: accessing one index past the end of an array is enough to destroy adjacent kernel data. + +Unlike user space, where tools like `valgrind` can sometimes save you, in kernel programming these errors can lead to instant crashes or subtle corruption that is very difficult to debug. + +#### Best Practices for Safer Kernel Code + +FreeBSD provides mechanisms and conventions to help developers write robust code. Follow these guidelines: + +1. **Always initialise pointers.** + If you do not yet have a valid memory address, set the pointer to `NULL`. This makes accidental dereferences easier to detect. + + ```c + struct my_entry *ptr = NULL; + ``` + +2. **Check the result of `malloc()`.** + Memory allocation may fail. Never assume success. + + ```c + ptr = malloc(sizeof(*ptr), M_MYTAG, M_NOWAIT); + if (ptr == NULL) { + // Handle gracefully, avoid panic + } + ``` + +3. **Free what you allocate.** + Every `malloc()` must have a matching `free()`. In kernel space, leaks accumulate until reboot. + + ```c + free(ptr, M_MYTAG); + ``` + +4. **Avoid buffer overflows.** + Use safer functions such as `strlcpy()` or `snprintf()`, which take the buffer size as an argument. + + ```c + strlcpy(buffer, "FreeBSD", sizeof(buffer)); + ``` + +5. **Use `M_ZERO` to avoid garbage values.** + This flag ensures allocated memory starts clean. + + ```c + ptr = malloc(sizeof(*ptr), M_MYTAG, M_WAITOK | M_ZERO); + ``` + +6. **Use proper allocation flags.** + + - `M_WAITOK` is used when allocation can safely sleep until memory becomes available. + - `M_NOWAIT` must be used in interrupt handlers or any context where sleeping is forbidden. + +#### A Real Example from FreeBSD 14.3 + +In the FreeBSD source tree, memory is often managed through pre-allocated buffers rather than frequent dynamic allocations. Here is a snippet from `sys/kern/tty_info.c`, it's located at line 303: + +```c +(void)sbuf_new(&sb, tp->t_prbuf, tp->t_prbufsz, SBUF_FIXEDLEN); +sbuf_set_drain(&sb, sbuf_tty_drain, tp); +``` + +What happens here? + +- `sbuf_new()` creates a string buffer (`sb`) using an already allocated memory region (`tp->t_prbuf`). +- The size is fixed (`tp->t_prbufsz`) and protected by the `SBUF_FIXEDLEN` flag, ensuring no writes beyond the limit. +- `sbuf_set_drain()` then specifies a controlled function (`sbuf_tty_drain`) to handle buffer output. + +This pattern demonstrates a safe kernel strategy: memory is allocated once during subsystem initialisation and carefully reused, rather than repeatedly allocated and freed. It reduces fragmentation, avoids allocation failures at runtime, and keeps memory usage predictable. + +#### Dangerous Code to Avoid + +The following snippet is **wrong** because it uses a pointer that was never given a valid address: + +```c +struct my_entry *ptr; // Declared, but not initialised. 'ptr' contains garbage. + +ptr->id = 5; // Crash risk: dereferencing an uninitialised pointer +``` + +`ptr` does not point anywhere valid. When you try to access `ptr->id`, the kernel will likely panic because you are touching memory you do not own. In user space, this would usually be a segmentation fault. In kernel space, it can crash the whole system. + +#### The Correct Pattern + +Below is a safe version that allocates memory, checks that the allocation worked, uses the memory, and then releases it. The comments explain each step and why it matters in kernel code. + +```c +#include +#include + +/* + * Give your allocations a custom tag. This helps the kernel track who owns + * the memory and makes debugging leaks much easier. + */ +MALLOC_DECLARE(M_MYTAG); // Declare the tag (usually in a header) +MALLOC_DEFINE(M_MYTAG, "mydriver", "My driver allocations"); // Define the tag + +struct my_entry { + int id; +}; + +void example(void) +{ + struct my_entry *ptr = NULL; // Start with NULL to avoid using garbage + + /* + * Allocate enough space for ONE struct my_entry. + * We use sizeof(*ptr) so if the type of 'ptr' changes, + * the size stays correct automatically. + * + * Flags: + * - M_WAITOK: allocation is allowed to sleep until memory is available. + * Use this only in contexts where sleeping is safe + * (for example, during driver attach or module load). + * + * - M_ZERO: zero-fill the memory so all fields start in a known state. + * This prevents accidental use of uninitialised data. + */ + ptr = malloc(sizeof(*ptr), M_MYTAG, M_WAITOK | M_ZERO); + + if (ptr == NULL) { + /* + * Always check for failure, even with M_WAITOK. + * If allocation fails, handle it gracefully: log, unwind, or return. + */ + printf("mydriver: allocation failed\n"); + return; + } + + /* + * At this point 'ptr' is valid and zero-initialised. + * It is now safe to access its fields. + */ + ptr->id = 5; + + /* + * ... use 'ptr' for whatever work is needed ... + */ + + /* + * When you are done, free the memory with the SAME tag you used to allocate. + * Pairing malloc/free is essential in kernel code to avoid leaks + * that accumulate for the entire uptime of the machine. + */ + free(ptr, M_MYTAG); + ptr = NULL; // Optional but helpful to prevent accidental reuse +} +``` + +#### Why this pattern matters + +1. **Initialise pointers**: starting with `NULL` makes accidental use obvious during reviews and easier to catch in tests. +2. **Size safely**: `sizeof(*ptr)` follows the pointer's type automatically, reducing the chance of wrong sizes when refactoring. +3. **Pick the right flags**: + - Use `M_WAITOK` when the code can sleep, such as during attach, open, or module load paths. + - Use `M_NOWAIT` in interrupt handlers or other non-sleepable contexts, and handle `NULL` immediately. +4. **Zero on allocate**: `M_ZERO` prevents hidden state from previous allocations, which avoids surprising behaviour. +5. **Always free**: every `malloc()` must be paired with `free()` using the same tag. This is non-negotiable in kernel code. +6. **Set to NULL after free**: it reduces the risk of use-after-free bugs if the pointer is referenced later by mistake. + +#### If your context do not allow sleep + +Sometimes you are in a context where sleeping is forbidden, such as an interrupt handler. In that case use `M_NOWAIT`, check immediately for failure, and defer work if needed: + +```c +ptr = malloc(sizeof(*ptr), M_MYTAG, M_NOWAIT | M_ZERO); +if (ptr == NULL) { + /* Defer the work or drop it safely; do NOT block here. */ + return; +} +``` + +Keeping these habits from the beginning will save you from many of the most painful kernel crashes and midnight debugging sessions. + +#### Hands-On Lab 1: Crashing with an Uninitialized Pointer + +This exercise demonstrates why you must never use a pointer before giving it a valid address. We will first write a broken program that uses an uninitialised pointer, then fix it with `malloc()`. + +##### Broken Version: `lab1_crash.c` + +```c +#include + +struct data { + int value; +}; + +int main(void) { + struct data *ptr; // Declared but not initialised + + // At this point, 'ptr' points to some random location in memory. + // Trying to use it will cause undefined behaviour. + ptr->value = 42; // Segmentation fault very likely here + + printf("Value: %d\n", ptr->value); + + return 0; +} +``` + +##### What to Expect + +- This program compiles without warnings. +- When you run it, it will almost certainly crash with a segmentation fault. +- The crash happens because `ptr` does not point to valid memory, yet we try to write to `ptr->value`. +- In the kernel, this same mistake would likely panic the entire system. + +##### What is wrong here? + +``` +STACK (main) ++---------------------------+ +| ptr : ??? (uninitialised) | -> not a real address you own ++---------------------------+ + +HEAP ++---------------------------+ +| no allocation yet | ++---------------------------+ + +Action performed: + write 42 into ptr->value + +Result: + You are dereferencing a garbage address. Crash. +``` + +##### Fixed Version: `lab1_fixed.c` + +```c +#include +#include // For malloc() and free() + +struct data { + int value; +}; + +int main(void) { + struct data *ptr; + + // Allocate memory for ONE struct data on the heap + ptr = malloc(sizeof(struct data)); + if (ptr == NULL) { + // Always check in case malloc fails + printf("Allocation failed!\n"); + return 1; + } + + // Now 'ptr' points to valid memory, safe to use + ptr->value = 42; + printf("Value: %d\n", ptr->value); + + // Always free what you allocate + free(ptr); + + return 0; +} +``` + +##### What Changed + +- We used `malloc()` to allocate enough space for one `struct data`. +- We checked that the result was not `NULL`. +- We safely wrote into the struct's field. +- We freed the memory before exiting, preventing a leak. + +##### Why this works + +``` +STACK (main) ++---------------------------+ +| ptr : 0xHHEE... | -> valid heap address returned by malloc ++---------------------------+ + +HEAP ++---------------------------+ +| struct data block | +| value = 42 | ++---------------------------+ + +Actions: + 1) ptr = malloc(sizeof(struct data)) -> ptr now points to a valid block + 2) ptr->value = 42 -> write inside your own block + 3) free(ptr) -> return memory to the system + +Result: + No crash. No leak. +``` + +#### Hands-On Lab 2: Memory Leak and the Forgotten Free + +This exercise shows what happens when you forget to release allocated memory. In user space, leaks disappear when your program exits. In the kernel, leaks accumulate for the system's entire uptime, which is why this habit must be fixed early. + +##### Leaky Version: `lab2_leak.c` + +```c +#include +#include +#include + +int main(void) { + // Allocate 128 bytes of memory + char *buffer = malloc(128); + if (buffer == NULL) return 1; + + // Copy a string into the allocated buffer + strcpy(buffer, "FreeBSD device drivers are awesome!"); + printf("%s\n", buffer); + + // Memory was allocated but never freed + // This is a memory leak + return 0; +} +``` + +##### What to Expect + +- The program prints the string normally. +- You may not notice the problem right away because the OS reclaims process memory when the program exits. +- In the kernel this would be serious. The memory would remain allocated across operations and only a reboot clears it. + +##### Leak vs program exit + +``` +Before exit: + +STACK (main) ++---------------------------+ +| buffer : 0xABCD... | -> heap address ++---------------------------+ + +HEAP ++--------------------------------------------------+ +| 128-byte block | +| "FreeBSD device drivers are awesome!\0 ..." | ++--------------------------------------------------+ + +Action: + Program returns without free(buffer) + +Consequence in user space: + OS reclaims process memory at exit, so you do not notice. + +Consequence in kernel space: + The block remains allocated across operations and accumulates. +``` + +##### Fixed Version: `lab2_fixed.c` + +```c +#include +#include +#include + +int main(void) { + char *buffer = malloc(128); + if (buffer == NULL) return 1; + + strcpy(buffer, "FreeBSD device drivers are awesome!"); + printf("%s\n", buffer); + + // Free the memory once we are done + free(buffer); + + return 0; +} +``` + +##### What Changed + +- We added `free(buffer);`. +- This single line ensures that all memory is returned to the system. Make this a habit. + +##### Proper lifecycle + +``` +1) Allocation + HEAP: [ 128-byte block ] <- buffer points here + +2) Use + Write string into the block, then print it + +3) Free + free(buffer) + HEAP: [ block returned to allocator ] + buffer (optional) -> set to NULL to avoid accidental reuse +``` + +#### Detecting Memory Leaks with AddressSanitizer + +On FreeBSD, when compiling user-space programs with Clang, you can detect leaks automatically using AddressSanitizer: + +```sh +% cc -fsanitize=address -g -o lab2_leak lab2_leak.c +% ./lab2_leak +``` + +You will see a report indicating memory was allocated and never freed. Although AddressSanitizer does not apply to kernel code, the lesson is identical. Always release what you allocate. + +#### Mini-Lab 3: Memory Allocation in a Kernel Module + +Now let's try a FreeBSD kernel experiment. Create `memlab.c`: + +```c +#include +#include +#include +#include + +MALLOC_DEFINE(M_MEMLAB, "memlab", "Memory Lab Example"); + +static void *buffer = NULL; + +static int +memlab_load(struct module *m, int event, void *arg) +{ + int error = 0; + + switch (event) { + case MOD_LOAD: + printf("memlab: Loading module\n"); + + buffer = malloc(128, M_MEMLAB, M_WAITOK | M_ZERO); + if (buffer == NULL) { + printf("memlab: malloc failed!\n"); + error = ENOMEM; + } else { + printf("memlab: allocated 128 bytes\n"); + } + break; + + case MOD_UNLOAD: + printf("memlab: Unloading module\n"); + + if (buffer != NULL) { + free(buffer, M_MEMLAB); + printf("memlab: memory freed\n"); + } + break; + + default: + error = EOPNOTSUPP; + break; + } + + return error; +} + +static moduledata_t memlab_mod = { + "memlab", memlab_load, NULL +}; + +DECLARE_MODULE(memlab, memlab_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); +``` + +Compile and load: + +```sh +% cc -O2 -pipe -nostdinc -I/usr/src/sys -D_KERNEL -DKLD_MODULE \ + -fno-common -o memlab.o -c memlab.c +% cc -shared -nostdlib -o memlab.ko memlab.o +% sudo kldload ./memlab.ko +``` + +Unload with: + +```sh +% sudo kldunload memlab +``` + +##### Detecting Leaks + +Comment out the `free()` line, recompile, and load/unload several times. Now inspect memory: + +``` +% vmstat -m | grep memlab +``` + +You will see lines like: + +```yaml +memlab 128 4 4 0 0 1 +``` + +indicating four allocations of 128 bytes are still "in use" because they were never freed. With the fix in place, the line disappears after unload. + +##### Optional: DTrace View + +To see allocations live: + +```sh +% sudo dtrace -n 'fbt::malloc:entry { trace(arg1); }' +``` + +When you load the module, you will see the `128` bytes being allocated. + +##### Challenge: Prove the Leak + +It is one thing to read that kernel leaks accumulate, but another to **see it with your own eyes**. This short experiment will let you prove it on your own system. + +1. Open the `memlab.c` module you created earlier and **comment out the `free(buffer, M_MEMLAB);` line** in the unload function. This means the module will allocate memory on load, but never release it on unload. + +2. Rebuild the module and then **load and unload it four times in a row**: + + ```sh + % sudo kldload ./memlab.ko + % sudo kldunload memlab + % sudo kldload ./memlab.ko + % sudo kldunload memlab + % sudo kldload ./memlab.ko + % sudo kldunload memlab + % sudo kldload ./memlab.ko + % sudo kldunload memlab + ``` + +3. Now inspect the kernel's memory allocation table with: + + ```sh + % vmstat -m | grep memlab + ``` + + You should see output similar to: + + ```yaml + memlab 128 4 4 0 0 1 + ``` + + This means four allocations of 128 bytes were made, and none were freed. Each time you loaded the module, the kernel allocated more memory that was never released. + +4. Finally, **restore the `free()` line**, recompile, and repeat the load/unload cycle. This time, when you run `vmstat -m | grep memlab`, the line should disappear after unload, confirming that memory is released properly. + +This simple test demonstrates a critical fact: in user space, leaks usually vanish when your process exits. In kernel space, leaks **survive across module reloads** and continue to accumulate. In production systems, such mistakes are not just messy; they are fatal. Over time, leaks can exhaust all available kernel memory and cause the system to crash. + +#### Common Beginner Pitfalls + +Memory safety is one of the hardest lessons for new C programmers, and in kernel space the consequences are much harsher. Let's highlight a few traps that beginners often fall into, and how you can avoid them: + +- **Forgetting to free memory.** + Every `malloc()` must have a matching `free()` in the proper cleanup path. If you allocate during module load, remember to free during module unload. This habit prevents leaks that otherwise accumulate for the entire uptime of the system. +- **Using freed memory.** + Accessing a pointer after `free()` is called is a classic bug known as *use-after-free*. The pointer may still contain the old address, tricking you into thinking it is valid. A safe habit is to set the pointer to `NULL` immediately after freeing it. That way, any accidental use will be obvious. +- **Choosing the wrong allocation flag.** + FreeBSD provides different allocation behaviours for different contexts. If you call `malloc()` with `M_WAITOK`, the kernel may put the thread to sleep until memory becomes available, which is fine during module load or attach, but catastrophic inside an interrupt handler. Conversely, `M_NOWAIT` never sleeps and fails immediately if memory is not available. Learning to pick the correct flag is an essential skill. +- **Skipping malloc tags.** + Always use `MALLOC_DEFINE()` to give your driver a custom memory tag. These tags appear in `vmstat -m` and make debugging leaks much easier. Without them, your allocations may be lumped into generic categories, making it difficult to trace where memory is coming from. + +By keeping these pitfalls in mind and practising the good habits shown earlier, you will dramatically reduce the risk of introducing memory bugs into your drivers. These lessons might feel repetitive now, but in real kernel development they are the difference between a stable driver and one that crashes production systems. + +#### Golden Rules for Kernel Memory + +``` +1. Every malloc() must have a matching free(). +2. Never use a pointer before initialising it (or after freeing it). +3. Use the correct allocation flag (M_WAITOK or M_NOWAIT) for the context. +``` + +Keep these three rules in mind whenever you write kernel code. They may look simple, but following them consistently is what separates a stable FreeBSD driver from a crash-prone one. + +#### Pointers Recap: The Lifecycle of Memory in C + +``` +Step 1: Declare a pointer +------------------------- +struct data *ptr; + + STACK + +-------------------+ + | ptr : ??? | -> uninitialised (dangerous!) + +-------------------+ + + +Step 2: Allocate memory +----------------------- +ptr = malloc(sizeof(struct data)); + + STACK HEAP + +-------------------+ +----------------------+ + | ptr : 0xABCD... | -----> | struct data block | + +-------------------+ | value = ??? | + +----------------------+ + + +Step 3: Use the memory +---------------------- +ptr->value = 42; + + HEAP + +----------------------+ + | struct data block | + | value = 42 | + +----------------------+ + + +Step 4: Free the memory +----------------------- +free(ptr); +ptr = NULL; + + STACK + +-------------------+ + | ptr : NULL | -> safe, prevents reuse + +-------------------+ + + HEAP + +----------------------+ + | (block released) | + +----------------------+ +``` + +**Golden Reminder**: + +- Never use an uninitialised pointer. +- Always check your allocations. +- Free what you allocate, and set pointers to `NULL` after freeing. + +#### Pointers Recap Quiz + +Test yourself with these quick questions before moving on. Answers are at the end of this chapter. + +1. What happens if you declare a pointer but never initialise it and then dereference it? +2. Why should you always check the return value of `malloc()`? +3. What is the purpose of the `M_ZERO` flag when allocating memory in the FreeBSD kernel? +4. After calling `free(ptr, M_TAG);`, why is it a good habit to set `ptr = NULL;`? +5. In which contexts must you use `M_NOWAIT` instead of `M_WAITOK` when allocating memory in kernel code? + +#### Wrapping Up + +With this section, we have reached the end of our journey through **pointers in C**. Along the way you learned what pointers are, how they relate to arrays and structures, and why they are so powerful but also so dangerous. We concluded with one of the most important lessons: **memory safety**. + +Every pointer must be treated with care. Every allocation must be checked. Every buffer must have a known size. In user space, mistakes usually crash only your program. In kernel space, the very same mistakes can corrupt memory or bring down the entire operating system. + +By following FreeBSD's allocation patterns, checking results, freeing memory diligently, and using debugging tools like `vmstat -m` and DTrace, you will be on the path to writing drivers that are both stable and reliable. + +In the next section, we begin our study of **arrays and strings** in C. Arrays are the natural companion to pointers, and strings are nothing more than arrays of characters. They are central to nearly every C program and appear constantly in kernel code. Understanding how they are stored, manipulated, and protected from overflows will prepare you for the next steps in FreeBSD device driver development. + *continue soon...*