-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ostep: add code snippet demo'ing virtual addr space
- Loading branch information
1 parent
f00980e
commit c7378c1
Showing
6 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
target/ |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test") | ||
|
||
# Alternatively, run with cargo: | ||
# | ||
# RUSTFLAGS="-C link-args=-no-pie" cargo run --bin all_virtual | ||
rust_binary( | ||
name = "all_virtual", | ||
srcs = ["src/bin/all_virtual.rs"], | ||
# If this codegen (-C) flag is not set, then address space layout randomization (ASLR) | ||
# will mean that the locations printed will NOT be consistent across executions. | ||
# Ref: https://doc.rust-lang.org/rustc/codegen-options/index.html#primary-relocation-models | ||
rustc_flags = [ | ||
"-C", | ||
"link-args=-no-pie", | ||
], | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "ostep" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## _Operating Systems: Three Easy Pieces_ | ||
|
||
I worked through this textbook as part of the [teachyourselfcs.com](https://teachyourselfcs.com/) program. |
This file contains 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/// A Rust translation of the code snippet found in the book's | ||
/// 'Every address you see if virtual' aside, page 126 in version 1.0.0. | ||
use std::alloc::{alloc, dealloc, Layout}; | ||
|
||
fn main() { | ||
println!("Location of CODE : {:p}", main as fn()); | ||
// Rust does not provide manual memory management without `unsafe`. | ||
// In order to translate the C demo code, which uses malloc(), this | ||
// `unsafe` block is used to allocate and deallocate a byte of memory | ||
// on the heap. | ||
unsafe { | ||
let layout = Layout::new::<u8>(); | ||
// This ptr must have *some* type, so alloc is coded to always return `*mut u8`. | ||
// You're supposed to cast the pointer to it's actual pointer type, which is | ||
// done below. | ||
let ptr = alloc(layout); | ||
println!("Location of HEAP : {:p}", ptr as *const u8); | ||
*(ptr as *mut u8) = 42; | ||
assert_eq!(*(ptr as *mut u8), 42); | ||
// Deallocate to prevent memory leak (even though memory will quickly be reclaimed at process | ||
// cleanup. | ||
dealloc(ptr, layout); | ||
} | ||
let first = 5; | ||
let second = 10; | ||
println!("Location of STACK : {:p}", &first); | ||
println!("Location of first stack var : {:p}", &first); | ||
println!("Location of seconds stack var : {:p}", &second); | ||
} |