-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Open
dreamos82
wants to merge
3
commits into
master
Choose a base branch
from
shtifting_bug_explanation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+36
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or, not xor ( |
||
|
|
||
| ``` | ||
| 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), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.