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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
#![allow(unused_imports, dead_code)]
use alloc::format;
use alloc::string::String;
use core::{
ffi::CStr,
mem::MaybeUninit,
ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign},
sync::atomic::{AtomicBool, AtomicI32, AtomicU32, Ordering},
};
/// Virtual memory information.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Info {
/// The size of a page of virtual memory.
pub page_size: u32,
/// The granularity of a page of virtual memory.
pub page_granularity: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct MemoryFlags(pub u32);
impl From<MemoryFlags> for u32 {
fn from(val: MemoryFlags) -> Self {
val.0
}
}
impl From<u32> for MemoryFlags {
fn from(value: u32) -> Self {
Self(value)
}
}
impl MemoryFlags {
/// No flags
pub const NONE: u32 = 0;
/// Memory is readable.
pub const ACCESS_READ: u32 = 0x00000001;
/// Memory is writable.
pub const ACCESS_WRITE: u32 = 0x00000002;
/// Memory is executable.
pub const ACCESS_EXECUTE: u32 = 0x00000004;
/// Memory is readable and writable.
pub const ACCESS_RW: u32 = Self::ACCESS_READ | Self::ACCESS_WRITE;
/// Memory is readable and executable.
pub const ACCESS_RX: u32 = Self::ACCESS_READ | Self::ACCESS_EXECUTE;
/// Memory is readable, writable and executable.
pub const ACCESS_RWX: u32 = Self::ACCESS_READ | Self::ACCESS_WRITE | Self::ACCESS_EXECUTE;
/// Use a `MAP_JIT` flag available on Apple platforms (introduced by Mojave), which allows JIT code to be
/// executed in a MAC bundle.
///
/// This flag may be turned on by the allocator if there is no other way of allocating executable memory.
///
/// ## Note
/// This flag can only be used with [alloc], `MAP_JIT` only works on OSX and not on iOS.
/// When a process uses `fork()` the child process has no access to the pages mapped with `MAP_JIT`.
pub const MMAP_ENABLE_JIT: u32 = 0x00000010;
/// Pass `PROT_MAX(PROT_READ)` or `PROT_MPROTECT(PROT_READ)` to `mmap()` on platforms that support it.
///
/// This flag allows to set a "maximum access" that the memory page can get during its lifetime. Use
/// [protect] to change the access flags.
///
/// ## Note
/// This flag can only be used with [alloc] and [alloc_dual_mapping].
/// However [alloc_dual_mapping] may automatically use this if `AccessRead` is used.
pub const MMAP_MAX_ACCESS_READ: u32 = 0x00000020;
/// Pass `PROT_MAX(PROT_WRITE)` or `PROT_MPROTECT(PROT_WRITE)` to `mmap()` on platforms that support it.
///
/// This flag allows to set a "maximum access" that the memory page can get during its lifetime. Use
/// [protect] to change the access flags.
///
/// ## Note
/// This flag can only be used with [alloc] and [alloc_dual_mapping].
/// However [alloc_dual_mapping] may automatically use this if `AccessWrite` is used.
pub const MMAP_MAX_ACCESS_WRITE: u32 = 0x00000040;
/// Pass `PROT_MAX(PROT_EXEC)` or `PROT_MPROTECT(PROT_EXEC)` to `mmap()` on platforms that support it.
///
/// This flag allows to set a "maximum access" that the memory page can get during its lifetime. Use
/// [protect] to change the access flags.
///
/// ## Note
/// This flag can only be used with [alloc] and [alloc_dual_mapping].
/// However [alloc_dual_mapping] may automatically use this if `AccessExecute` is used.
pub const MMAP_MAX_ACCESS_EXECUTE: u32 = 0x00000080;
pub const MMAP_MAX_ACCESS_RW: u32 = Self::MMAP_MAX_ACCESS_READ | Self::MMAP_MAX_ACCESS_WRITE;
pub const MMAP_MAX_ACCESS_RX: u32 = Self::MMAP_MAX_ACCESS_READ | Self::MMAP_MAX_ACCESS_EXECUTE;
pub const MMAP_MAX_ACCESS_RWX: u32 =
Self::MMAP_MAX_ACCESS_READ | Self::MMAP_MAX_ACCESS_WRITE | Self::MMAP_MAX_ACCESS_EXECUTE;
/// Use `MAP_SHARED` when calling mmap().
///
/// ## Note
/// In some cases `MAP_SHARED` may be set automatically. For example, some dual mapping implementations must
/// use `MAP_SHARED` instead of `MAP_PRIVATE` to ensure that the OS would not apply copy on write on RW page, which
/// would cause RX page not having the updated content.
pub const MAP_SHARED: u32 = 0x00000100;
/// Not an access flag, only used by `alloc_dual_mapping()` to override the default allocation strategy to always use
/// a 'tmp' directory instead of "/dev/shm" (on POSIX platforms). Please note that this flag will be ignored if the
/// operating system allows to allocate an executable memory by a different API than `open()` or `shm_open()`. For
/// example on Linux `memfd_create()` is preferred and on BSDs `shm_open(SHM_ANON, ...)` is used if SHM_ANON is
/// defined.
///
/// ## Note
/// This flag can only be used with [alloc].
pub const MAPPING_PREFER_TMP: u32 = 0x80000000;
}
impl MemoryFlags {
pub fn contains(self, other: u32) -> bool {
(self.0 & other) != 0
}
}
impl BitOr<MemoryFlags> for MemoryFlags {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl BitOr<u32> for MemoryFlags {
type Output = Self;
#[inline]
fn bitor(self, rhs: u32) -> Self::Output {
Self(self.0 | rhs)
}
}
impl BitOrAssign<MemoryFlags> for MemoryFlags {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}
impl BitOrAssign<u32> for MemoryFlags {
#[inline]
fn bitor_assign(&mut self, rhs: u32) {
*self = *self | rhs;
}
}
impl BitAnd<MemoryFlags> for MemoryFlags {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self::Output {
Self(self.0 & rhs.0)
}
}
impl BitAnd<u32> for MemoryFlags {
type Output = Self;
#[inline]
fn bitand(self, rhs: u32) -> Self::Output {
Self(self.0 & rhs)
}
}
impl BitAndAssign<MemoryFlags> for MemoryFlags {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs;
}
}
impl BitAndAssign<u32> for MemoryFlags {
#[inline]
fn bitand_assign(&mut self, rhs: u32) {
*self = *self & rhs;
}
}
impl PartialEq<u32> for MemoryFlags {
#[inline]
fn eq(&self, other: &u32) -> bool {
self.0 == *other
}
}
/// Dual memory mapping used to map an anonymous memory into two memory regions where one region is read-only, but
/// executable, and the second region is read+write, but not executable. See [alloc_dual_mapping] for
/// more details.
pub struct DualMapping {
/// Pointer to data with 'Read+Execute' access (this memory is not writable).
pub rx: *const u8,
/// Pointer to data with 'Read+Write' access (this memory is not executable).
pub rw: *mut u8,
}
/// Hardened runtime flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(u32)]
pub enum HardenedRuntimeFlags {
/// No flags
#[default]
None = 0,
/// Hardened runtime is enabled - it's not possible to have "Write & Execute" memory protection. The runtime
/// enforces W^X (either write or execute).
///
/// ## Note
/// If the runtime is hardened it means that an operating system specific protection is used. For example
/// on Apple OSX it's possible to allocate memory with MAP_JIT flag and then use `pthread_jit_write_protect_np()`
/// to temporarily swap access permissions for the current thread. Dual mapping is also a possibility on X86/X64
/// architecture.
Enabled = 0x00000001,
/// Read+Write+Execute can only be allocated with MAP_JIT flag (Apple specific, only available on OSX).
MapJit = 0x00000002,
EnabledMapJit = Self::Enabled as u32 | Self::MapJit as u32,
}
#[derive(Default)]
pub struct HardenedRuntimeInfo {
pub flags: HardenedRuntimeFlags,
}
/// Values that can be used with [`protect_jit_memory`](protect_jit_memory) function.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u32)]
pub enum ProtectJitAccess {
/// Protect JIT memory with Read+Write permissions.
ReadWrite = 0,
/// Protect JIT memory with Read+Execute permissions.
ReadExecute = 1,
}
pub const DUAL_MAPPING_FILTER: [u32; 2] = [
MemoryFlags::ACCESS_WRITE | MemoryFlags::MMAP_MAX_ACCESS_WRITE,
MemoryFlags::ACCESS_EXECUTE | MemoryFlags::MMAP_MAX_ACCESS_EXECUTE,
];
use errno::errno;
use libc::*;
use crate::Error;
cfgenius::define! {
vm_shm_detect = cfg(
any(
target_vendor="apple",
target_os="android"
)
);
has_shm_open = cfg(not(target_os="android"));
has_pthread_jit_write_protect_np = cfg(all(
target_os="macos",
target_arch="aarch64"
));
has_shm_anon = cfg(target_os="freebsd");
}
fn error_from_errno() -> Error {
match errno().0 {
EACCES | EAGAIN | ENODEV | EPERM => Error::InvalidState,
EFBIG | ENOMEM | EOVERFLOW => Error::OutOfMemory,
EMFILE | ENFILE => Error::TooManyHandles,
_ => Error::InvalidArgument,
}
}
cfgenius::cond! {
if cfg(not(windows))
{
fn get_vm_info() -> Info {
extern "C" {
fn getpagesize() -> c_int;
}
let page_size = unsafe { getpagesize() as usize };
Info {
page_size: page_size as _,
page_granularity: 65536.max(page_size) as _,
}
}
#[cfg(target_os="macos")]
fn get_osx_version() -> i32 {
static GLOBAL_VERSION: AtomicI32 = AtomicI32::new(0);
let mut ver = GLOBAL_VERSION.load(Ordering::Relaxed);
if ver == 0 {
unsafe {
let mut osname: MaybeUninit<utsname> = MaybeUninit::uninit();
uname(osname.as_mut_ptr());
ver = atoi(CStr::from_ptr((*osname.as_ptr()).release).to_bytes().as_ptr());
GLOBAL_VERSION.store(ver, Ordering::Relaxed);
}
}
ver
}
fn mm_prot_from_memory_flags(memory_flags: MemoryFlags) -> i32 {
let mut prot = 0;
let x = memory_flags;
if x.contains(MemoryFlags::ACCESS_READ) { prot |= PROT_READ }
if x.contains(MemoryFlags::ACCESS_WRITE) { prot |= PROT_WRITE }
if x.contains(MemoryFlags::ACCESS_EXECUTE) { prot |= PROT_EXEC }
prot
}
// Some operating systems don't allow /dev/shm to be executable. On Linux this happens when /dev/shm is mounted with
// 'noexec', which is enforced by systemd. Other operating systems like MacOS also restrict executable permissions
// regarding /dev/shm, so we use a runtime detection before attempting to allocate executable memory. Sometimes we
// don't need the detection as we know it would always result in `AnonymousMemoryStrategy::TmpDir`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AnonymousMemoryStrategy {
Unknown = 0,
DevShm = 1,
TmpDir = 2,
}
#[cfg(not(target_os="freebsd"))]
fn get_tmp_dir() -> String {
unsafe{
let env = getenv(b"TMPDIR\0".as_ptr() as *const _);
if !env.is_null() {
CStr::from_ptr(env).to_string_lossy().into_owned()
} else {
String::from("/tmp")
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum FileType {
None,
Shm,
Tmp,
}
struct AnonymousMemory {
fd: i32,
filetype: FileType,
tmpname: String,
}
#[allow(clippy::needless_late_init)]
impl AnonymousMemory {
fn open(&mut self, prefer_tmp_over_dev_shm: bool) -> Result<(), Error> {
cfgenius::cond! {
if cfg(target_os="linux") {
// Linux specific 'memfd_create' - if the syscall returns `ENOSYS` it means
// it's not available and we will never call it again (would be pointless).
//
// NOTE: There is also memfd_create() libc function in FreeBSD, but it internally
// uses `shm_open(SHM_ANON, ...)` so it's not needed to add support for it (it's
// not a syscall as in Linux).
/// If ever changed to '1' that would mean the syscall is not
/// available and we must use `shm_open()` and `shm_unlink()` (or regular `open()`).
static MEMFD_CREATE_NOT_SUPPORTED: AtomicBool = AtomicBool::new(false);
if !MEMFD_CREATE_NOT_SUPPORTED.load(Ordering::Relaxed) {
unsafe {
self.fd = libc::syscall(libc::SYS_memfd_create, b"vmem\0".as_ptr(), libc::MFD_CLOEXEC) as i32;
if self.fd >= 0 {
return Ok(());
}
if errno().0 == ENOSYS {
MEMFD_CREATE_NOT_SUPPORTED.store(true, Ordering::Relaxed);
} else {
return Err(error_from_errno());
}
}
}
}
}
cfgenius::cond! {
if all(macro(has_shm_open), macro(has_shm_anon)) {
unsafe {
let _ = prefer_tmp_over_dev_shm;
self.fd = shm_open(libc::SHM_ANON, libc::O_RDWR | libc::O_CREAT | libc::O_EXCL, libc::S_IRUSR | libc::S_IWUSR);
if self.fd >= 0 {
return Ok(())
} else {
return Err(error_from_errno());
}
}
} else {
// POSIX API. We have to generate somehow a unique name. This is nothing cryptographic, just using a bit from
// the stack address to always have a different base for different threads (as threads have their own stack)
// and retries for avoiding collisions. We use `shm_open()` with flags that require creation of the file so we
// never open an existing shared memory.
static INTERNAL_COUNTER: AtomicU32 = AtomicU32::new(0);
let retry_count = 100;
let mut bits = self as *const Self as u64 & 0x55555555;
for _ in 0..retry_count {
bits = bits.wrapping_sub(crate::os::get_tick_count() as u64 * 773703683);
bits = ((bits >> 14) ^ (bits << 6)) + INTERNAL_COUNTER.fetch_add(1, Ordering::AcqRel) as u64 + 10619863;
let use_tmp;
cfgenius::cond! {
if macro(vm_shm_detect) {
use_tmp = true;
} else {
use_tmp = prefer_tmp_over_dev_shm;
}
};
if use_tmp {
self.tmpname.push_str(&get_tmp_dir());
self.tmpname.push_str(&format!("/shm-id-{:016X}\0", bits));
unsafe {
self.fd = libc::open(
self.tmpname.as_ptr() as *const c_char,
libc::O_RDWR | libc::O_CREAT | libc::O_EXCL,
0
);
if self.fd >= 0 {
self.filetype = FileType::Tmp;
return Ok(());
}
}
} else {
self.tmpname = format!("shm-id-{:016X}\0", bits);
unsafe {
self.fd = libc::shm_open(
self.tmpname.as_ptr() as *const c_char,
libc::O_RDWR | libc::O_CREAT | libc::O_EXCL,
0
);
if self.fd >= 0 {
self.filetype = FileType::Shm;
return Ok(());
}
}
}
if errno().0 != EEXIST {
return Err(error_from_errno());
}
}
}
}
Err(Error::FailedToOpenAnonymousMemory)
}
fn unlink(&mut self) {
#[allow(unused_variables)]
let typ = self.filetype;
self.filetype = FileType::None;
cfgenius::cond! {
if macro(has_shm_open) {
if typ== FileType::Shm {
unsafe {
libc::shm_unlink(self.tmpname.as_ptr() as *const c_char);
return;
}
}
}
}
#[allow(unreachable_code)]
if typ == FileType::Tmp {
unsafe {
libc::unlink(self.tmpname.as_ptr() as *const c_char);
}
}
}
fn close(&mut self) {
if self.fd >= 0 {
unsafe {
libc::close(self.fd);
}
self.fd = -1;
}
}
const fn new() -> Self {
Self {
fd: -1,
filetype: FileType::None,
tmpname: String::new(),
}
}
fn allocate(&self, size: usize) -> Result<(), Error> {
unsafe {
if libc::ftruncate(self.fd, size as _) != 0 {
return Err(error_from_errno());
}
Ok(())
}
}
}
impl Drop for AnonymousMemory {
fn drop(&mut self) {
self.unlink();
self.close();
}
}
}
}
cfgenius::cond! {
if macro(vm_shm_detect) {
fn detect_anonymous_memory_strategy() -> Result<AnonymousMemoryStrategy, Error> {
let mut anon_mem = AnonymousMemory::new();
let vm_info = info();
anon_mem.open(false)?;
anon_mem.allocate(vm_info.page_size as usize)?;
unsafe {
let ptr = libc::mmap(std::ptr::null_mut(), vm_info.page_size, libc::PROT_READ | libc::PROT_EXEC, libc::MAP_SHARED, anon_mem.fd, 0);
if ptr == libc::MAP_FAILED {
if errno().0 == EINVAL {
return Ok(AnonymousMemoryStrategy::TmpDir);
}
return Err(error_from_errno());
} else {
libc::munmap(ptr, vm_info.page_size);
Ok(AnonymousMemoryStrategy::DevShm)
}
}
}
}
}
cfgenius::cond! {
if cfg(not(windows)) {
pub fn get_anonymous_memory_strategy() -> Result<AnonymousMemoryStrategy, Error> {
cfgenius::cond! {
if macro(vm_shm_detect) {
use std::sync::atomic::AtomicU8;
static GLOBAL_STRATEGY: AtomicU8 = AtomicU8::new(0);
if GLOBAL_STRATEGY.load(Ordering::Acquire) != 0 {
return Ok(unsafe { std::mem::transmute(GLOBAL_STRATEGY.load(Ordering::Acquire)) });
}
let strategy = detect_anonymous_memory_strategy()?;
GLOBAL_STRATEGY.store(strategy as u8, Ordering::Release);
Ok(strategy)
}
}
Ok(AnonymousMemoryStrategy::TmpDir)
}
/// Detects whether the current process is hardened, which means that pages that have WRITE and EXECUTABLE flags
/// cannot be normally allocated. On OSX + AArch64 such allocation requires MAP_JIT flag, other platforms don't
/// support this combination.
#[cfg(not(windows))]
pub fn has_hardened_runtime() -> bool {
cfgenius::cond! {
if cfg(all(target_os="macos", target_arch="aarch64")) {
true
} else {
static GLOBAL_HARDENED_FLAG: AtomicU32 = AtomicU32::new(0);
let mut flag = GLOBAL_HARDENED_FLAG.load(Ordering::Acquire);
if flag == 0 {
let page_size = info().page_size;
unsafe {
let ptr = libc::mmap(core::ptr::null_mut(), page_size as _, libc::PROT_READ | libc::PROT_WRITE | libc::PROT_EXEC, libc::MAP_PRIVATE | libc::MAP_ANONYMOUS, -1, 0);
if ptr == libc::MAP_FAILED {
flag = 2;
} else {
flag = 1;
libc::munmap(ptr, page_size as _);
}
}
GLOBAL_HARDENED_FLAG.store(flag, Ordering::Release);
}
flag == 2
}
}
}
pub const fn has_map_jit_support() -> bool {
cfgenius::cond! {
if cfg(all(target_os="macos", target_arch="aarch64")) {
true
} else {
false
}
}
}
pub fn map_jit_from_memory_flags(memory_flags: MemoryFlags) -> i32 {
cfgenius::cond! {
if cfg(target_vendor="apple") {
// Always use MAP_JIT flag if user asked for it (could be used for testing on non-hardened processes) and detect
// whether it must be used when the process is actually hardened (in that case it doesn't make sense to rely on
// user `memoryFlags`).
//
// MAP_JIT is not required when dual-mapping memory and is incompatible with MAP_SHARED, so it will not be
// added when the latter is enabled.
let use_map_jit = (memory_flags.contains(MemoryFlags::MMapEnableJit) || has_hardened_runtime())
&& !memory_flags.contains(MemoryFlags::MapShared);
if use_map_jit {
if has_map_jit_support() {
return libc::MAP_JIT as i32;
} else {
0
}
} else {
0
}
} else {
let _ = memory_flags;
0
}
}
}
pub fn get_hardened_runtime_flags() -> HardenedRuntimeFlags {
let mut flags = 0;
if has_hardened_runtime() {
flags = HardenedRuntimeFlags::Enabled as u32;
}
if has_map_jit_support() {
flags |= HardenedRuntimeFlags::MapJit as u32;
}
match flags {
0 => HardenedRuntimeFlags::None,
1 => HardenedRuntimeFlags::Enabled,
2 => HardenedRuntimeFlags::MapJit,
3 => HardenedRuntimeFlags::EnabledMapJit,
_ => unreachable!(),
}
}
pub fn max_access_flags_to_regular_access_flags(memory_flags: MemoryFlags) -> MemoryFlags {
const MAX_PROT_SHIFT: u32 = MemoryFlags::MMAP_MAX_ACCESS_READ.trailing_zeros();
MemoryFlags((memory_flags.0 & MemoryFlags::MMAP_MAX_ACCESS_RWX) >> MAX_PROT_SHIFT)
}
pub fn regular_access_flags_to_max_access_flags(memory_flags: MemoryFlags) -> MemoryFlags {
const MAX_PROT_SHIFT: u32 = MemoryFlags::MMAP_MAX_ACCESS_READ.trailing_zeros();
MemoryFlags((memory_flags.0 & MemoryFlags::MMAP_MAX_ACCESS_RWX) << MAX_PROT_SHIFT)
}
pub fn mm_max_prot_from_memory_flags(_memory_flags: MemoryFlags) -> i32 {
_memory_flags.0 as _
}
fn map_memory(
size: usize,
memory_flags: MemoryFlags,
fd: i32,
offset: libc::off_t,
) -> Result<*mut u8, Error> {
if size == 0 {
return Err(Error::InvalidArgument);
}
let protection = mm_prot_from_memory_flags(memory_flags);
let mut mm_flags = map_jit_from_memory_flags(memory_flags);
mm_flags |= if memory_flags.contains(MemoryFlags::MAP_SHARED) {
libc::MAP_SHARED
} else {
libc::MAP_PRIVATE
};
if fd == -1 {
mm_flags |= libc::MAP_ANONYMOUS;
}
unsafe {
let ptr = libc::mmap(
core::ptr::null_mut(),
size as _,
protection,
mm_flags,
fd,
offset,
);
if ptr == libc::MAP_FAILED {
return Err(error_from_errno());
}
Ok(ptr.cast())
}
}
fn unmap_memory(ptr: *mut u8, size: usize) -> Result<(), Error> {
if size == 0 {
return Err(Error::InvalidArgument);
}
unsafe {
if libc::munmap(ptr.cast(), size as _) == 0 {
Ok(())
} else {
Err(error_from_errno())
}
}
}
pub fn alloc(size: usize, memory_flags: MemoryFlags) -> Result<*mut u8, Error> {
map_memory(size, memory_flags, -1, 0)
}
pub fn release(ptr: *mut u8, size: usize) -> Result<(), Error> {
unmap_memory(ptr, size)
}
pub fn protect(p: *mut u8, size: usize, memory_flags: MemoryFlags) -> Result<(), Error> {
let protection = mm_prot_from_memory_flags(memory_flags);
unsafe {
if libc::mprotect(p.cast(), size as _, protection) == 0 {
Ok(())
} else {
Err(error_from_errno())
}
}
}
fn unmap_dual_mapping(dm: &mut DualMapping, size: usize) -> Result<(), Error> {
let err1 = unmap_memory(dm.rx as _, size);
let mut err2 = Ok(());
if dm.rx != dm.rw {
err2 = unmap_memory(dm.rw as _, size);
}
err1?;
err2?;
dm.rx = core::ptr::null_mut();
dm.rw = core::ptr::null_mut();
Ok(())
}
/// Allocates virtual memory and creates two views of it where the first view has no write access. This is an addition
/// to the API that should be used in cases in which the operating system either enforces W^X security policy or the
/// application wants to use this policy by default to improve security and prevent an accidental (or purposed)
/// self-modifying code.
///
/// The memory returned in the `dm` are two independent mappings of the same shared memory region. You must use
/// [release_dual_mapping](release_dual_mapping) to release it when it's no longer needed. Never use [release](release) to
/// release the memory returned by `alloc_dual_mapping()` as that would fail on Windows.
///
/// Both pointers in `dm` would be set to `null` if the function fails.
pub fn alloc_dual_mapping(size: usize, memory_flags: MemoryFlags) -> Result<DualMapping, Error> {
let mut dm = DualMapping {
rx: core::ptr::null_mut(),
rw: core::ptr::null_mut(),
};
if size as isize <= 0 {
return Err(Error::InvalidArgument);
}
let mut prefer_tmp_over_dev_shm = memory_flags.contains(MemoryFlags::MAPPING_PREFER_TMP);
if !prefer_tmp_over_dev_shm {
let strategy = get_anonymous_memory_strategy()?;
prefer_tmp_over_dev_shm = strategy == AnonymousMemoryStrategy::TmpDir;
}
let mut anon_mem = AnonymousMemory::new();
anon_mem.open(prefer_tmp_over_dev_shm)?;
anon_mem.allocate(size)?;
let mut ptr = [core::ptr::null_mut(), core::ptr::null_mut()];
for i in 0..2 {
let restricted_memory_flags = memory_flags.0 & !DUAL_MAPPING_FILTER[i];
ptr[i] = match map_memory(
size,
(restricted_memory_flags | MemoryFlags::MAP_SHARED).into(),
anon_mem.fd,
0,
) {
Ok(p) => p,
Err(e) => {
if i == 1 {
let _ = unmap_memory(ptr[0], size);
}
return Err(e);
}
};
}
dm.rx = ptr[0];
dm.rw = ptr[1];
Ok(dm)
}
/// Releases virtual memory mapping previously allocated by [alloc_dual_mapping()](alloc_dual_mapping).
///
/// Both pointers in `dm` would be set to `nullptr` if the function succeeds.
pub fn release_dual_mapping(dm: &mut DualMapping, size: usize) -> Result<(), Error> {
unmap_dual_mapping(dm, size)
}
}
}
pub fn info() -> Info {
static INFO: once_cell::sync::Lazy<Info> = once_cell::sync::Lazy::new(|| get_vm_info());
*INFO
}
/// Flushes instruction cache in the given region.
///
/// Only useful on non-x86 architectures, however, it's a good practice to call it on any platform to make your
/// code more portable.
pub fn flush_instruction_cache(p: *const u8, size: usize) {
cfgenius::cond! {
if cfg(any(target_arch="x86", target_arch="x86_64")) {
let _ = p;
let _ = size;
} else if cfg(target_vendor="apple") {
extern "C" {
fn sys_icache_invalidate(p: *const u8, size: usize);
}
unsafe {
sys_icache_invalidate(p, size);
}
} else if cfg(windows) {
extern "C" {
fn GetCurrentProcess() -> *mut libc::c_void;
fn FlushInstructionCache(
proc: *mut libc::c_void,
lp: *const u8,
dw_size: usize,
) -> i32;
}
unsafe {
FlushInstructionCache(GetCurrentProcess(), p, size);
}
} else if cfg(target_arch="aarch64")
{
let code = p as usize;
let end = code + size;
let addr;
use core::arch::asm;
const ICACHE_LINE_SIZE: usize = 4;
const DCACHE_LINE_SIZE: usize = 4;
let mut addr = code & (DCACHE_LINE_SIZE - 1);
while addr < end {
unsafe {
asm!("dc civac {}", in(reg) addr);
}
addr += ICACHE_LINE_SIZE;
}
unsafe {
asm!("dsb ish");
}
addr = code & (ICACHE_LINE_SIZE - 1);
while addr < end {
unsafe {
asm!("ic ivau {}", in(reg) addr);
}
addr += ICACHE_LINE_SIZE;
}
unsafe {
asm!(
"dsb ish"
);
asm!(
"isb"
);
}
} else if cfg(target_arhc="riscv64") {
unsafe {
let _ = wasmtime_jit_icache_coherence::clear_cache(p.cast(), size);
let _ = wasmtime_jit_icache_coherence::pipeline_flush_mt();
}
} else {
// TODO: Should we error here?
//compile_error!("icache invalidation not implemented for target platform");
}
}
}
#[cfg(not(windows))]
pub fn hardened_runtime_info() -> HardenedRuntimeInfo {
HardenedRuntimeInfo {
flags: get_hardened_runtime_flags(),
}
}
/// Protects access of memory mapped with MAP_JIT flag for the current thread.
///
/// # Note
/// This feature is only available on Apple hardware (AArch64) at the moment and and uses a non-portable
/// `pthread_jit_write_protect_np()` call when available.
///
/// This function must be called before and after a memory mapped with MAP_JIT flag is modified. Example:
///
/// ```mustfail,rust
/// let code_ptr = ...;
/// let code_size = ...;
///
/// protect_jit_memory(ProtectJitAccess::ReadWrite);
/// copy_nonoverlapping(source, code_ptr, code_size);
/// protect_jit_memory(ProtectJitAccess::ReadOnly);
/// flush_instruction_cache(code_ptr, code_size);
///
/// ```
pub fn protect_jit_memory(access: ProtectJitAccess) {
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
{
unsafe {
let x = match access {
ProtectJitAccess::ReadWrite => 0,
_ => 1,
};
libc::pthread_jit_write_protect_np(x);
}
}
let _ = access;
}
cfgenius::cond! {
if cfg(windows) {
use winapi::um::sysinfoapi::SYSTEM_INFO;
use winapi::{
shared::{minwindef::DWORD, ntdef::HANDLE},
um::{
handleapi::{CloseHandle, INVALID_HANDLE_VALUE},
memoryapi::{
CreateFileMappingW, MapViewOfFile, UnmapViewOfFile, VirtualAlloc, VirtualFree,
VirtualProtect, FILE_MAP_EXECUTE, FILE_MAP_READ, FILE_MAP_WRITE,
},
sysinfoapi::GetSystemInfo,
winnt::{
MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE,
PAGE_READONLY, PAGE_READWRITE,
},
},
};
struct ScopedHandle {
value: HANDLE
}
impl ScopedHandle {
fn new() -> Self {
Self { value: core::ptr::null_mut() }
}
}
impl Drop for ScopedHandle {
fn drop(&mut self) {
if !self.value.is_null() {
unsafe {
CloseHandle(self.value);
}
}
}
}
fn get_vm_info() -> Info {
let mut system_info = MaybeUninit::<SYSTEM_INFO>::uninit();
unsafe {
GetSystemInfo(system_info.as_mut_ptr());
let system_info = system_info.assume_init();
Info {
page_size: system_info.dwPageSize as u32,
page_granularity: system_info.dwAllocationGranularity as u32,
}
}
}
fn protect_flags_from_memory_flags(memory_flags: MemoryFlags) -> DWORD {
let protect_flags;
if memory_flags.contains(MemoryFlags::ACCESS_EXECUTE) {
protect_flags = if memory_flags.contains(MemoryFlags::ACCESS_WRITE) {
PAGE_EXECUTE_READWRITE
} else {
PAGE_EXECUTE_READ
};
} else if memory_flags.contains(MemoryFlags::ACCESS_RW) {
protect_flags = if memory_flags.contains(MemoryFlags::ACCESS_WRITE) {
PAGE_READWRITE
} else {
PAGE_READONLY
};
} else {
protect_flags = PAGE_READONLY;
}
protect_flags
}
fn desired_access_from_memory_flags(memory_flags: MemoryFlags) -> DWORD {
let mut access = if memory_flags.contains(MemoryFlags::ACCESS_WRITE) {
FILE_MAP_WRITE
} else {
FILE_MAP_READ
};
if memory_flags.contains(MemoryFlags::ACCESS_EXECUTE) {
access |= FILE_MAP_EXECUTE;
}
access
}
pub fn alloc(size: usize, memory_flags: MemoryFlags) -> Result<*mut u8, Error> {
if size == 0 {
return Err(Error::InvalidArgument)
}
unsafe {
let protect = protect_flags_from_memory_flags(memory_flags);
let result = VirtualAlloc(core::ptr::null_mut(), size, MEM_COMMIT | MEM_RESERVE, protect);
if result.is_null() {
return Err(Error::OutOfMemory)
}
Ok(result as *mut u8)
}
}
pub fn release(ptr: *mut u8, size: usize) -> Result<(), Error> {
if size == 0 || ptr.is_null() {
return Err(Error::InvalidArgument)
}
unsafe {
if VirtualFree(ptr as *mut _, 0, MEM_RELEASE) == 0 {
return Err(Error::InvalidArgument)
}
}
Ok(())
}
pub fn protect(p: *mut u8, size: usize, memory_flags: MemoryFlags) -> Result<(), Error> {
let protect_flags = protect_flags_from_memory_flags(memory_flags);
let mut old_flags = 0;
unsafe {
if VirtualProtect(p as _, size, protect_flags, &mut old_flags) != 0 {
return Ok(())
}
Err(Error::InvalidArgument)
}
}
pub fn alloc_dual_mapping(size: usize, memory_flags: MemoryFlags) -> Result<DualMapping, Error> {
if size == 0 {
return Err(Error::InvalidArgument)
}
let mut handle = ScopedHandle::new();
unsafe {
handle.value = CreateFileMappingW(
INVALID_HANDLE_VALUE,
core::ptr::null_mut(),
PAGE_EXECUTE_READWRITE,
((size as u64) >> 32) as _,
(size & 0xFFFFFFFF) as _,
core::ptr::null_mut()
);
if handle.value.is_null() {
return Err(Error::OutOfMemory);
}
let mut ptr = [core::ptr::null_mut(), core::ptr::null_mut()];
for i in 0..2 {
let access_flags = memory_flags.0 & !DUAL_MAPPING_FILTER[i];
let desired_access = desired_access_from_memory_flags(access_flags.into());
ptr[i] = MapViewOfFile(handle.value, desired_access, 0, 0, size);
if ptr[i].is_null() {
if i == 0 {
UnmapViewOfFile(ptr[0]);
}
return Err(Error::OutOfMemory);
}
}
Ok(DualMapping {
rx: ptr[0] as _,
rw: ptr[1] as _,
})
}
}
pub fn release_dual_mapping(dm: &mut DualMapping, _size: usize) -> Result<(), Error> {
let mut failed = false;
unsafe {
if UnmapViewOfFile(dm.rx as _) == 0 {
failed = true;
}
if dm.rx != dm.rw && UnmapViewOfFile(dm.rw as _) == 0 {
failed = true;
}
if failed {
return Err(Error::InvalidArgument);
}
dm.rx = core::ptr::null_mut();
dm.rw = core::ptr::null_mut();
Ok(())
}
}
}
}