Guest User

Untitled

a guest
Jan 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. // libuser syscalls:
  2. /// Creates a thread in the current process.
  3. pub fn create_thread(ip: fn() -> !, arg: usize, sp: *const u8, _priority: u32, _processor_id: u32) -> Result<Thread, KernelError> {
  4. unsafe {
  5. let (out_handle, ..) = syscall(nr::CreateThread, ip as usize, arg, sp as _, _priority as _, _processor_id as _, 0)?;
  6. Ok(Thread(Handle::new(out_handle as _)))
  7. }
  8. }
  9.  
  10. // Function I'm writing:
  11. fn test_threads() -> Terminal {
  12. fn thread_b(c: usize) -> ! {
  13. for _ in 0..10 {
  14. println!("{}", c);
  15. libuser::syscalls::sleep_thread(0);
  16. }
  17. }
  18. libuser::syscalls::exit_thread()
  19. }
  20.  
  21. #[naked]
  22. fn function_wrapper() {
  23. unsafe {
  24. asm!("
  25. push eax
  26. call $0
  27. " :: "i"(thread_b) :: "intel");
  28. }
  29. }
  30.  
  31. const THREAD_STACK_SIZE: usize = 0x2000;
  32.  
  33. let terminal = Arc::new(Mutex::new(terminal));
  34. let stack = Box::new([0u8; THREAD_STACK_SIZE]);
  35. let sp = (Box::into_raw(stack) as *const u8).wrapping_offset(THREAD_STACK_SIZE as isize);
  36. let ip : fn() -> ! = unsafe {
  37. // Safety: This is changing the return type from () to !. It's safe. It
  38. // sucks though. This is, yet again, an instance of "naked functions are
  39. // fucking horrible".
  40. core::mem::transmute(function_wrapper) // THE BUG IS HERE
  41. };
  42. let thread_handle = libuser::syscalls::create_thread(ip, Arc::into_raw(terminal.clone()) as usize, sp, 0, 0)
  43. .expect("svcCreateThread returned an error");
  44. thread_handle.start()
  45. .expect("svcStartThread returned an error");
  46.  
  47. // Wait for thread_b to terminate.
  48. loop {
  49. if let Ok(terminal) = Arc::try_unwrap(terminal) {
  50. break terminal.into_inner()
  51. }
  52. libuser::syscalls::sleep_thread(0);
  53. }
  54. }
Add Comment
Please, Sign In to add comment