luaur_code_gen/functions/
make_pages_read_only_code_allocator.rs1use crate::macros::codegen_assert::CODEGEN_ASSERT;
2use crate::records::code_allocator::CodeAllocator;
3
4#[allow(non_snake_case)]
5pub fn make_pages_read_only(mem: *mut u8, size: usize) -> bool {
6 CODEGEN_ASSERT!(CodeAllocator::align_to_page_size(mem as usize) == mem as usize);
7 CODEGEN_ASSERT!(size == CodeAllocator::align_to_page_size(size));
8
9 #[cfg(target_os = "windows")]
10 {
11 use core::ffi::c_void;
12 use windows_sys::Win32::System::Memory::{VirtualProtect, PAGE_READONLY};
13
14 let mut old_protect: u32 = 0;
15 unsafe { VirtualProtect(mem as *const c_void, size, PAGE_READONLY, &mut old_protect) != 0 }
16 }
17
18 #[cfg(target_os = "linux")]
19 {
20 use core::ffi::c_int;
21 use core::ffi::c_void;
22
23 extern "C" {
24 fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int;
25 }
26
27 const PROT_READ: c_int = 0x1;
28
29 unsafe { mprotect(mem as *mut c_void, size, PROT_READ) == 0 }
30 }
31
32 #[cfg(target_os = "macos")]
33 {
34 use core::ffi::c_int;
35 use core::ffi::c_void;
36
37 extern "C" {
38 fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int;
39 }
40
41 const PROT_READ: c_int = 0x1;
42
43 unsafe { mprotect(mem as *mut c_void, size, PROT_READ) == 0 }
44 }
45
46 #[cfg(target_os = "freebsd")]
47 {
48 use core::ffi::c_int;
49 use core::ffi::c_void;
50
51 extern "C" {
52 fn mprotect(addr: *mut c_void, len: usize, prot: c_int) -> c_int;
53 }
54
55 const PROT_READ: c_int = 0x1;
56
57 unsafe { mprotect(mem as *mut c_void, size, PROT_READ) == 0 }
58 }
59
60 #[cfg(not(any(
61 target_os = "windows",
62 target_os = "linux",
63 target_os = "macos",
64 target_os = "freebsd"
65 )))]
66 {
67 false
68 }
69}