-
-
Notifications
You must be signed in to change notification settings - Fork 66
Shtifting bug explanation #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
2770a86
b42a3ee
1adc11c
d6c3353
6398b2e
0949f8d
4f9f84b
3c9132f
e328563
63cd454
3dac2e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -335,7 +335,7 @@ What the heap will look like after the code above? | |
| | 6 | F | X | .. | X | 6 | F | X | .. | X | 6 | F | .. | X | | | | ||
|
|
||
|
|
||
| Now, all of the memory in the heap is available to allocate (except for the overhead used to store the status of each chunk), and everything looks perfectly fine. But now the code keeps executing, and it will arrive at the following instruction: | ||
| Now, all of the memory in the heap is available to allocate (except for the overhead used to store the status of each chunk), and everything looks perfectly fine. But the code keeps executing, and it will arrive at the following instruction: | ||
|
|
||
| ```c | ||
| alloc(7); | ||
|
|
@@ -416,7 +416,7 @@ This means that the allocator (before marking this location as free and returnin | |
|
|
||
|  | ||
|
|
||
| The fields in bold are the fields that are changed. The exact implementation of this code is left to the reader. | ||
| The fields in bold are the fields that are changed. The exact implementation of this code is left to the reader. Please note that some books refers to this technique, calling it _coalescing_, but is the same concepts. | ||
|
||
|
|
||
| ### Part 6: Splitting | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,38 @@ It is worth mentioning that inline assembly syntax is the At&t syntax, so the us | |
| asm("movl $5, %rcx;"); | ||
| ``` | ||
|
|
||
| ## Dealing With Literals and Bitwise Operation | ||
|
|
||
| This is one of the most misleading and subtle issue we can face while osdeving. And most of the time we face it the hard way. | ||
|
||
|
|
||
| So what is the problem? let's imagine we have a 64 bit variable, and we need some to do a bitwise operation like `setting` the bit at the position `x`, this is easily achieved using the _left shift_ (`|=`) operator combined with a _xor_ (`|=`), like in the following pseudocode: | ||
|
||
|
|
||
| ``` | ||
| variable example_var |= (1 << x) | ||
| ``` | ||
|
|
||
| And that is correct, and it probably could work in many languages (of course changing the syntax), but let's see what happens with C and C++ languages. | ||
| In _C_, the statement above become: | ||
|
|
||
| ```c | ||
| uint64_t example_var |= (1 << x); | ||
| ``` | ||
|
|
||
| And we do few test, for `x=1, 2, 10, 20, 31`, everything works fine, so what is the issue? The issue is when the shift is above 31, because of the C _Integer promotion rule_. | ||
|
|
||
| In the above example, `1` is a literal, and by default C converts it to `int`, and this type in C is 32 bits, the bitwise operation is executed using the type of the left operand, so we are trying to shift left a bit of a number of position that is higher, than the size of the variable, causing an undefined behavior. | ||
|
|
||
| Then what are the solutions? Below few example of how to potentially fix it: | ||
|
|
||
| ```c | ||
| #define ONE 1ULL | ||
| const uint64_t one = 1; | ||
|
|
||
| uint64_t example_one |= one << 42; | ||
| uint64_t example_two |= ONE << 42; | ||
| uint64_t example_three |= 1ULL << 42; | ||
| ``` | ||
|
|
||
| ## C +(+) assembly together - Calling Conventions | ||
|
|
||
| Different C compilers feature a number of [calling conventions](https://en.wikipedia.org/wiki/X86_calling_conventions), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should just introduce it as coalescing in the first place. It is already standard terminology in undergraduate curricula. So maybe early on, we could ask the question: what are some things we want to do with (free) heap blocks? The two natural answers are:
The examples should show that splitting and coalescing go hand in hand. Imagine you only split, then your block sizes will be non-increasing, which means harder chance to repurpose free blocks. This is why coalescing is helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just reread the whole paragraph, and the main thing is that the approach is basicall incremental, so the "coalesce" concept is only introduced by consecutive examples expanding the previous implementation, and i haven´t found a better place of where to introduce that name earlier. I'll try anyway to rephrase the paragraph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to introduce the coalescing term at the beginning of the chapter.
Also I think that the splitting and merging are explained in the way you suggested.