1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*!
`x86-alignment-check` is set `ac` flag in `eflags` on `x86` or `x86_64`

# Features
- set `ac` flag bit into ON, its included `eflags` of `x86`.
- `x86_64` are supported too.
- `#![no_std]`

# Example 1: If your code is correctly controlled by alignment
First, add the following to `Cargo.toml`:

```text
[target.'cfg(any(target_arch = "x86_64", target_arch = "x86"))'.dev-dependencies]
x86-alignment-check = "*"
```

Second, enclose your test code with `x86_alignment_check()` as follows:

```rust
    use x86_alignment_check::x86_alignment_check;
    //
    let old_flag = x86_alignment_check(true);
    //
    // here your test codes, processing anythings, a bus error may occur.
    //
    let _ = x86_alignment_check(old_flag);
```

Finally execute `cargo test`

# Example 2: call_once style
```rust
    let val = x86_alignment_check::ac_call_once(|| {
        // here is alignment check
        // processing anythings
        // return value for assertion
        1
    });
    assert_eq!(val, 1);
```
For now, assertions such as `assert_eq!()` cannot be included inside `FnOnce`,
because of the rust runtime bug.

# Example 3: call_once style, but not alignment check
```rust
    let val = x86_alignment_check::no_ac_call_once(|| {
        // here is not alignment check
        // processing anythings
        // return value for assertion
        1
    });
    assert_eq!(val, 1);
```

*/
#![no_std]

/// alignment check flag manipulation
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
pub fn x86_alignment_check(b: bool) -> bool {
    let old_eflags = unsafe { __read_eflags() };
    let new_eflags = if b {
        old_eflags | EFLAGS_AC_BIT
    } else {
        old_eflags & !EFLAGS_AC_BIT
    };
    unsafe { __write_eflags(new_eflags) };
    //
    (old_eflags & EFLAGS_AC_BIT) != 0
}

#[cfg(target_arch = "x86")]
const EFLAGS_AC_BIT: u32 = 1 << 18; // 0x0004_0000

#[cfg(target_arch = "x86_64")]
const EFLAGS_AC_BIT: u64 = 1 << 18; // 0x0004_0000

#[cfg(target_arch = "x86")]
#[inline(always)]
unsafe fn __read_eflags() -> u32 {
    let mut eflags: u32;
    core::arch::asm!("pushfd; pop {eflags:e}", eflags = out(reg) eflags);
    eflags
}

#[cfg(target_arch = "x86")]
#[inline(always)]
unsafe fn __write_eflags(eflags: u32) {
    core::arch::asm!("push {eflags:e}; popfd", eflags = in(reg) eflags);
}

#[cfg(target_arch = "x86_64")]
#[inline(always)]
unsafe fn __read_eflags() -> u64 {
    let mut rflags: u64;
    core::arch::asm!("pushfq; pop {rflags}", rflags = out(reg) rflags);
    rflags
}

#[cfg(target_arch = "x86_64")]
#[inline(always)]
unsafe fn __write_eflags(rflags: u64) {
    core::arch::asm!("push {rflags}; popfq", rflags = in(reg) rflags);
}

/// execute under alignment check
pub fn ac_call_once<F, T>(f: F) -> T
where
    F: FnOnce() -> T,
{
    let old = x86_alignment_check(true);
    let r = f();
    let _ = x86_alignment_check(old);
    r
}

/// execute under no alignment check
pub fn no_ac_call_once<F, T>(f: F) -> T
where
    F: FnOnce() -> T,
{
    let old = x86_alignment_check(false);
    let r = f();
    let _ = x86_alignment_check(old);
    r
}

// reference:
// https://www.felixcloutier.com/x86/pushf:pushfd:pushfq

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works_0() {
        let old_0 = x86_alignment_check(true);
        let old_1 = x86_alignment_check(true);
        let old_2 = x86_alignment_check(false);
        let old_3 = x86_alignment_check(true);
        let old_4 = x86_alignment_check(false);
        let _old_5 = x86_alignment_check(old_0);
        //
        assert!(old_1);
        assert!(old_2);
        assert!(!old_3);
        assert!(old_4);
    }
    #[test]
    fn it_works_1() {
        let val = ac_call_once(|| 1);
        assert_eq!(val, 1);
    }
    #[test]
    fn it_works_2() {
        let val = no_ac_call_once(|| 1);
        assert_eq!(val, 1);
    }
    #[test]
    fn it_works_3() {
        let buf = [0_u8; 100];
        //
        let val = ac_call_once(|| {
            let val = no_ac_call_once(|| {
                let ptr = buf.as_ptr();
                let ptr = unsafe { ptr.add(3) };
                // next should "(signal: 7, SIGBUS: access to undefined memory)"
                // under alignment check, but here is not alignment check
                let _v: u32 = unsafe { (ptr as *const u32).read() };
                1
            });
            val + 1
        });
        assert_eq!(val, 2);
    }
    #[test]
    #[ignore]
    fn it_works_ignore_0() {
        let buf = [0_u8; 100];
        //
        let _old_0 = x86_alignment_check(true);
        {
            let ptr = buf.as_ptr();
            let ptr = unsafe { ptr.add(3) };
            // next should "(signal: 7, SIGBUS: access to undefined memory)"
            let _v: u32 = unsafe { (ptr as *const u32).read() };
        }
    }
}