Skip to content

Commit 1f88109

Browse files
supercomputer7ADKaster
authored andcommitted
Documentation/Kernel: Explain why and when to use FixedCharBuffers
1 parent 58b5095 commit 1f88109

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Documentation/Kernel/DevelopmentGuidelines.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ but because of the async operation, we can't send the `errno` code back to userl
3636
to ensure that internal functions still use the `ErrorOr<>` return type, and in main calling function, we use
3737
other meaningful infrastructure utilities in the Kernel to indicate that the operation failed.
3838

39+
## KStrings vs FixedStringBuffers
40+
41+
As you might understand, we put a respectable amount of effort into making the kernel code OOM-safe.
42+
One approach to achieve this is to allow error propagation where possible.
43+
The other approach is to eliminate heap allocations altogether where possible.
44+
45+
To do so, the FixedStringBuffer class was introduced into the AK library, and is used
46+
extensively in kernel syscall handlers' code.
47+
The idea is very simple - if we know the maximum length of an inspected string during
48+
a syscall and it's relatively short (so it doesn't exceed the stack size), something like
49+
1024 bytes is the total max length (but in theory we could just make the stack size bigger),
50+
it could be copied from userspace to that stack storage instead of doing an heap allocation
51+
to create a KString. This is especially useful when inspecting a string only during the
52+
syscall handler scope, because doing an heap allocation is wasteful on memory resources
53+
and puts a strain on the kernel memory manager for no good reason.
54+
55+
The FixedStringBuffer puts some safety guards - like zeroing the memory when storing new
56+
StringView, as well as truncating it if its length exceeds the allocated stack storage size.
57+
58+
It should be noted that there are helpers to handle a FixedStringBuffer storage:
59+
* `Process::get_syscall_name_string_argument_into_static_char_buffer(...)`
60+
* `Process::get_syscall_string_argument_into_static_char_buffer(...)`
61+
* `try_copy_name_from_user_into_static_char_buffer(...)`
62+
* `try_copy_string_from_user_into_static_char_buffer(...)`
63+
64+
These helpers will ensure that if the given string is exceeding the allocated stack storage size, then an error will be released instead of just truncating the string and continue execution.
65+
3966
## We don't break userspace - the SerenityOS version
4067

4168
We don't break userspace. However, in contrast to the Linux vision on this statement,

0 commit comments

Comments
 (0)