Skip to content

Commit

Permalink
Add an example app for TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Aug 7, 2023
1 parent 04bd3ff commit efa4ae9
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
run: make ARCH=${{ matrix.arch }} A=apps/task/sleep
- name: Build task/priority
run: make ARCH=${{ matrix.arch }} A=apps/task/priority
- name: Build task/tls
run: make ARCH=${{ matrix.arch }} A=apps/task/tls
- name: Build fs/shell
run: make ARCH=${{ matrix.arch }} A=apps/fs/shell
- name: Build net/echoserver
Expand Down Expand Up @@ -157,6 +159,8 @@ jobs:
run: cargo build -p arceos-sleep
- name: Build task/priority
run: cargo build -p arceos-priority
- name: Build task/tls
run: cargo build -p arceos-tls
- name: Build fs/shell
run: cargo build -p arceos-shell
- name: Build net/echoserver
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ members = [
"apps/task/sleep",
"apps/task/yield",
"apps/task/priority",
"apps/task/tls",
]

[profile.release]
Expand Down
10 changes: 10 additions & 0 deletions apps/task/tls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "arceos-tls"
version = "0.1.0"
edition = "2021"
authors = ["Yuekai Jia <[email protected]>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axstd = { path = "../../../ulib/axstd", features = ["tls", "alloc", "multitask"], optional = true }
31 changes: 31 additions & 0 deletions apps/task/tls/expect_info_smp1_fifo.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
smp = 1
build_mode = release
log_level = info

Primary CPU 0 started,
Found physcial memory regions:
.text (READ | EXECUTE | RESERVED)
.rodata (READ | RESERVED)
.data .tdata .tbss .percpu (READ | WRITE | RESERVED)
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize platform devices...
Initialize scheduling...
use FIFO scheduler.
Primary CPU 0 init OK.
Running TLS tests...
main: true 0xaa 0xcafe 0xdeadbeed 0xa2ce05a2ce05 Hello, world!
1: false 0xab 0xcaff 0xdeadbeee 0xa2ce05a2ce06 Hello1 world!
2: true 0xac 0xcb00 0xdeadbeef 0xa2ce05a2ce07 Hello2 world!
3: false 0xad 0xcb01 0xdeadbef0 0xa2ce05a2ce08 Hello3 world!
4: true 0xae 0xcb02 0xdeadbef1 0xa2ce05a2ce09 Hello4 world!
5: false 0xaf 0xcb03 0xdeadbef2 0xa2ce05a2ce0a Hello5 world!
6: true 0xb0 0xcb04 0xdeadbef3 0xa2ce05a2ce0b Hello6 world!
7: false 0xb1 0xcb05 0xdeadbef4 0xa2ce05a2ce0c Hello7 world!
8: true 0xb2 0xcb06 0xdeadbef5 0xa2ce05a2ce0d Hello8 world!
9: false 0xb3 0xcb07 0xdeadbef6 0xa2ce05a2ce0e Hello9 world!
10: true 0xb4 0xcb08 0xdeadbef7 0xa2ce05a2ce0f Hello: world!
TLS tests run OK!
Shutting down...
38 changes: 38 additions & 0 deletions apps/task/tls/expect_info_smp4_rr.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
smp = 4
build_mode = release
log_level = info

Primary CPU 0 started,
Found physcial memory regions:
.text (READ | EXECUTE | RESERVED)
.rodata (READ | RESERVED)
.data .tdata .tbss .percpu (READ | WRITE | RESERVED)
boot stack (READ | WRITE | RESERVED)
.bss (READ | WRITE | RESERVED)
free memory (READ | WRITE | FREE)
Initialize global memory allocator...
Initialize platform devices...
Initialize scheduling...
use Round-robin scheduler.
Initialize interrupt handlers...
CPU 0 init OK
CPU 1 started
CPU 2 started
CPU 3 started
CPU 1 init OK
CPU 2 init OK
CPU 3 init OK
Running TLS tests...
main: true 0xaa 0xcafe 0xdeadbeed 0xa2ce05a2ce05 Hello, world!
1: false 0xab 0xcaff 0xdeadbeee 0xa2ce05a2ce06 Hello1 world!
2: true 0xac 0xcb00 0xdeadbeef 0xa2ce05a2ce07 Hello2 world!
3: false 0xad 0xcb01 0xdeadbef0 0xa2ce05a2ce08 Hello3 world!
4: true 0xae 0xcb02 0xdeadbef1 0xa2ce05a2ce09 Hello4 world!
5: false 0xaf 0xcb03 0xdeadbef2 0xa2ce05a2ce0a Hello5 world!
6: true 0xb0 0xcb04 0xdeadbef3 0xa2ce05a2ce0b Hello6 world!
7: false 0xb1 0xcb05 0xdeadbef4 0xa2ce05a2ce0c Hello7 world!
8: true 0xb2 0xcb06 0xdeadbef5 0xa2ce05a2ce0d Hello8 world!
9: false 0xb3 0xcb07 0xdeadbef6 0xa2ce05a2ce0e Hello9 world!
10: true 0xb4 0xcb08 0xdeadbef7 0xa2ce05a2ce0f Hello: world!
TLS tests run OK!
Shutting down...
112 changes: 112 additions & 0 deletions apps/task/tls/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#![cfg_attr(feature = "axstd", no_std)]
#![cfg_attr(feature = "axstd", no_main)]
#![feature(thread_local)]
#![allow(unused_unsafe)]

#[macro_use]
#[cfg(feature = "axstd")]
extern crate axstd as std;

use std::{thread, vec::Vec};

#[thread_local]
static mut BOOL: bool = true;

#[thread_local]
static mut U8: u8 = 0xAA;

#[thread_local]
static mut U16: u16 = 0xcafe;

#[thread_local]
static mut U32: u32 = 0xdeadbeed;

#[thread_local]
static mut U64: u64 = 0xa2ce05_a2ce05;

#[thread_local]
static mut STR: [u8; 13] = *b"Hello, world!";

macro_rules! get {
($var:expr) => {
unsafe { $var }
};
}

macro_rules! set {
($var:expr, $value:expr) => {
unsafe { $var = $value }
};
}

macro_rules! add {
($var:expr, $value:expr) => {
unsafe { $var += $value }
};
}

#[cfg_attr(feature = "axstd", no_mangle)]
#[inline(never)]
fn main() {
println!("Running TLS tests...");

println!(
"main: {} {:#x} {:#x} {:#x} {:#x} {}",
get!(BOOL),
get!(U8),
get!(U16),
get!(U32),
get!(U64),
get!(std::str::from_utf8_unchecked(&STR))
);
assert!(get!(BOOL));
assert_eq!(get!(U8), 0xAA);
assert_eq!(get!(U16), 0xcafe);
assert_eq!(get!(U32), 0xdeadbeed);
assert_eq!(get!(U64), 0xa2ce05_a2ce05);
assert_eq!(get!(&STR), b"Hello, world!");

let mut tasks = Vec::new();
for i in 1..=10 {
tasks.push(thread::spawn(move || {
set!(BOOL, i % 2 == 0);
add!(U8, i as u8);
add!(U16, i as u16);
add!(U32, i as u32);
add!(U64, i as u64);
set!(STR[5], 48 + i as u8);

thread::yield_now();

println!(
"{}: {} {:#x} {:#x} {:#x} {:#x} {}",
i,
get!(BOOL),
get!(U8),
get!(U16),
get!(U32),
get!(U64),
get!(std::str::from_utf8_unchecked(&STR))
);
assert_eq!(get!(BOOL), i % 2 == 0);
assert_eq!(get!(U8), 0xAA + i as u8);
assert_eq!(get!(U16), 0xcafe + i as u16);
assert_eq!(get!(U32), 0xdeadbeed + i as u32);
assert_eq!(get!(U64), 0xa2ce05_a2ce05 + i as u64);
assert_eq!(get!(STR[5]), 48 + i as u8);
assert_eq!(get!(STR.len()), 13);
}));
}

tasks.into_iter().for_each(|t| t.join().unwrap());

// TLS of main thread must not have been changed by the other thread.
assert!(get!(BOOL));
assert_eq!(get!(U8), 0xAA);
assert_eq!(get!(U16), 0xcafe);
assert_eq!(get!(U32), 0xdeadbeed);
assert_eq!(get!(U64), 0xa2ce05_a2ce05);
assert_eq!(get!(&STR), b"Hello, world!");

println!("TLS tests run OK!");
}
2 changes: 2 additions & 0 deletions apps/task/tls/test_cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test_one "LOG=info" "expect_info_smp1_fifo.out"
test_one "SMP=4 LOG=info FEATURES=sched_rr" "expect_info_smp4_rr.out"
1 change: 1 addition & 0 deletions scripts/test/app_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ if [ -z "$1" ]; then
"apps/task/parallel"
"apps/task/sleep"
"apps/task/priority"
"apps/task/tls"
"apps/net/httpclient"
"apps/c/helloworld"
"apps/c/memtest"
Expand Down

0 comments on commit efa4ae9

Please sign in to comment.