Skip to content

Commit

Permalink
ostep: add code snippet demo'ing virtual addr space
Browse files Browse the repository at this point in the history
  • Loading branch information
thundergolfer committed Aug 2, 2022
1 parent f00980e commit c7378c1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions books/ostep/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
16 changes: 16 additions & 0 deletions books/ostep/BUILD.bazel
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",
],
)
7 changes: 7 additions & 0 deletions books/ostep/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions books/ostep/Cargo.toml
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]
3 changes: 3 additions & 0 deletions books/ostep/readme.md
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.
29 changes: 29 additions & 0 deletions books/ostep/src/bin/all_virtual.rs
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);
}

0 comments on commit c7378c1

Please sign in to comment.