Skip to content

Commit

Permalink
Add basic aarch64 cache coherency test.
Browse files Browse the repository at this point in the history
  • Loading branch information
CensoredUsername committed Sep 27, 2024
1 parent 6796c0b commit 4c9c71e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
60 changes: 60 additions & 0 deletions testing/tests/aarch64_cache_coherency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file contains test cases designed to validate proper assembler cache invalidation
// this is needed because aarch64's modified harvard architecture has an incoherent instruction and
// data cache. Therefore, it is needed to explicitly command the cache hierarchy to flush the dcache
// to the coherent layers, invalidate the icache, and ensure no stale data is left in the
// instruction pipeline. Testcases in this file are designed to break if this isn't handled properly
#![allow(unused_imports)]

extern crate dynasmrt;

use dynasmrt::dynasm;
use dynasmrt::{DynasmApi};

#[cfg(target_arch="aarch64")]
#[test]
fn test_cache_coherency_same_core() {
let mut ops = dynasmrt::aarch64::Assembler::new().unwrap();
let reader = ops.reader();

// write some code
let start = ops.offset();
dynasm!(ops
; .arch aarch64
; mov w0, 0xABCD
; ret
);
let end = ops.offset();

ops.commit().unwrap();

// execute it once
{
let buf = reader.lock();
let callable: extern "C" fn() -> u32 = unsafe { std::mem::transmute(buf.ptr(start)) };
assert_eq!(callable(), 0xABCD);
drop(buf);
}

// change the code
ops.alter(|modifier| {
modifier.goto(start);

dynasm!(modifier
; .arch aarch64
; mov w0, 0xCDEF
; ret
);
modifier.check_exact(end).unwrap();

}).unwrap();

ops.commit().unwrap();

// execute it again!
{
let buf = reader.lock();
let callable: extern "C" fn() -> u32 = unsafe { std::mem::transmute(buf.ptr(start)) };
assert_eq!(callable(), 0xCDEF);
drop(buf);
}
}
3 changes: 1 addition & 2 deletions testing/tests/aarch64_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use dynasmrt::components::LitPool;
macro_rules! my_dynasm {
($ops:ident $($t:tt)*) => {
dynasm!($ops
; .arch x64
; .arch aarch64
; .alias test, x1
$($t)*
)
Expand All @@ -35,7 +35,6 @@ fn complex() {

// interesting testcases
my_dynasm!(ops
; .arch aarch64
; aligned:
// no args
; nop
Expand Down

0 comments on commit 4c9c71e

Please sign in to comment.