Skip to content

Commit 47b4f7d

Browse files
authored
Merge pull request #32 from PacktPublishing/issue31
fixes #31. Migrate to `naked_asm` inside naked functions
2 parents 08fcd32 + 3cf7a78 commit 47b4f7d

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

ch05/c-fibers/src/main.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
/// FIX #31:
2+
/// Inline assembly blocks inside naked functions now need to use
3+
/// the `naked_asm` macro instead of the good old `asm` macro.
4+
/// The `noreturn` option is implicitly set by the `naked_asm`
5+
/// macro so there is no need to set that.
6+
///
7+
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
8+
/// for more information.
19
#![feature(naked_functions)]
2-
use std::arch::asm;
10+
use std::arch::{asm, naked_asm};
311

412
const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
513
const MAX_THREADS: usize = 4;
@@ -141,7 +149,7 @@ fn guard() {
141149

142150
#[naked]
143151
unsafe extern "C" fn skip() {
144-
asm!("ret", options(noreturn))
152+
naked_asm!("ret")
145153
}
146154

147155
pub fn yield_thread() {
@@ -155,7 +163,7 @@ pub fn yield_thread() {
155163
#[no_mangle]
156164
#[cfg_attr(target_os = "macos", export_name = "\x01switch")] // see: How-to-MacOS-M.md for explanation
157165
unsafe extern "C" fn switch() {
158-
asm!(
166+
naked_asm!(
159167
"mov [rdi + 0x00], rsp",
160168
"mov [rdi + 0x08], r15",
161169
"mov [rdi + 0x10], r14",
@@ -170,8 +178,7 @@ unsafe extern "C" fn switch() {
170178
"mov r12, [rsi + 0x20]",
171179
"mov rbx, [rsi + 0x28]",
172180
"mov rbp, [rsi + 0x30]",
173-
"ret",
174-
options(noreturn)
181+
"ret"
175182
);
176183
}
177184

ch05/d-fibers-closure/src/main.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
/// FIX #31:
2+
/// Inline assembly blocks inside naked functions now need to use
3+
/// the `naked_asm` macro instead of the good old `asm` macro.
4+
/// The `noreturn` option is implicitly set by the `naked_asm`
5+
/// macro so there is no need to set that.
6+
///
7+
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
8+
/// for more information.
19
#![feature(naked_functions)]
2-
use std::{arch::asm, ptr};
10+
use std::{arch::{asm, naked_asm}};
311

412
const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
513
const MAX_THREADS: usize = 4;
@@ -152,8 +160,8 @@ fn call(thread: u64) {
152160
}
153161

154162
#[naked]
155-
unsafe fn skip() {
156-
asm!("ret", options(noreturn))
163+
unsafe extern "C" fn skip() {
164+
naked_asm!("ret")
157165
}
158166

159167
// this function is changed
@@ -175,8 +183,8 @@ pub fn yield_thread() {
175183
#[naked]
176184
#[no_mangle]
177185
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
178-
unsafe fn switch() {
179-
asm!(
186+
unsafe extern "C" fn switch() {
187+
naked_asm!(
180188
"mov 0x00[rdi], rsp",
181189
"mov 0x08[rdi], r15",
182190
"mov 0x10[rdi], r14",
@@ -192,8 +200,7 @@ unsafe fn switch() {
192200
"mov rbx, 0x28[rsi]",
193201
"mov rbp, 0x30[rsi]",
194202
"mov rdi, 0x38[rsi]",
195-
"ret",
196-
options(noreturn)
203+
"ret"
197204
);
198205
}
199206

ch05/e-fibers-windows/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ Last we need to change our `switch()`function and update our assembly. After all
248248
#[naked]
249249
#[no_mangle]
250250
unsafe extern "C" fn switch() {
251-
asm!(
251+
naked_asm!(
252252
"movaps [rcx + 0x00], xmm6",
253253
"movaps [rcx + 0x10], xmm7",
254254
"movaps [rcx + 0x20], xmm8",
@@ -295,7 +295,7 @@ unsafe extern "C" fn switch() {
295295
"mov gs:0x08, rax",
296296
"mov rax, [rdx + 0xf0]",
297297
"mov gs:0x10, rax",
298-
"ret", options(noreturn)
298+
"ret"
299299
);
300300
}
301301
```

ch05/e-fibers-windows/src/main.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
/// FIX #31:
2+
/// Inline assembly blocks inside naked functions now need to use
3+
/// the `naked_asm` macro instead of the good old `asm` macro.
4+
/// The `noreturn` option is implicitly set by the `naked_asm`
5+
/// macro so there is no need to set that.
6+
///
7+
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
8+
/// for more information.
19
#![feature(naked_functions)]
2-
use std::arch::asm;
10+
use std::arch::{asm, naked_asm};
311

412
const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
513
const MAX_THREADS: usize = 4;
@@ -145,7 +153,7 @@ impl Runtime {
145153

146154
#[naked]
147155
unsafe extern "C" fn skip() {
148-
asm!("ret", options(noreturn))
156+
naked_asm!("ret")
149157
}
150158

151159

@@ -168,7 +176,7 @@ pub fn yield_thread() {
168176
#[no_mangle]
169177
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
170178
unsafe extern "C" fn switch() {
171-
asm!(
179+
naked_asm!(
172180
"mov [rdi + 0x00], rsp",
173181
"mov [rdi + 0x08], r15",
174182
"mov [rdi + 0x10], r14",
@@ -183,7 +191,7 @@ unsafe extern "C" fn switch() {
183191
"mov r12, [rsi + 0x20]",
184192
"mov rbx, [rsi + 0x28]",
185193
"mov rbp, [rsi + 0x30]",
186-
"ret", options(noreturn)
194+
"ret"
187195
);
188196
}
189197

0 commit comments

Comments
 (0)