kmalloc() bugfix
Václav Hajšman

Václav Hajšman @vhajsman

Location:
Prague
Joined:
Jul 6, 2025

kmalloc() bugfix

Publish Date: Jul 6
0 0

Yesterday, after a huge amount of time, after a lot of trying, i was able to finally fix a bug in kmalloc function of my kernel.

The kmalloc function in kernel is a function responsible for locating a block of size specified, marking it as used and returning it's address.

What caused the bug and how the bug was fixed

The bug was that the address was stored in a result variable and instead of its value, its address was returned In a summary: A classic beginner mistake - when instead of addr, the function returns &addr.

This caused the function to not only return an invalid address, but always the same value, which i simply did not notice for a long amount of time.

Also a refactor has been done, removing the potentionaly useless functions, separating a long functions into separate files and focusing a bit more on safe code.

The proof of bug fix

As seen in debug logs, attempting of allocation of 8 memory blocks of size 8 bytes results in success and returns different address each time.

./qemu.sh
cat serial.log | grep "malloc(8)"
DEBUG: [   ***   ][kmalloc] malloc(8) = 0x1323024
DEBUG: [   ***   ][kmalloc] malloc(8) = 0x1318928
 ...
DEBUG: [   ***   ][kmalloc] malloc(8) = 0x1294352
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment