Protect a region in memory to be not accessible.
| 1089 | |
| 1090 | // Protect a region in memory to be not accessible. |
| 1091 | static bool mi_os_protectx(void* addr, size_t size, bool protect) { |
| 1092 | // page align conservatively within the range |
| 1093 | size_t csize = 0; |
| 1094 | void* start = mi_os_page_align_area_conservative(addr, size, &csize); |
| 1095 | if (csize == 0) return false; |
| 1096 | /* |
| 1097 | if (_mi_os_is_huge_reserved(addr)) { |
| 1098 | _mi_warning_message("cannot mprotect memory allocated in huge OS pages\n"); |
| 1099 | } |
| 1100 | */ |
| 1101 | int err = 0; |
| 1102 | #ifdef _WIN32 |
| 1103 | DWORD oldprotect = 0; |
| 1104 | BOOL ok = VirtualProtect(start, csize, protect ? PAGE_NOACCESS : PAGE_READWRITE, &oldprotect); |
| 1105 | err = (ok ? 0 : GetLastError()); |
| 1106 | #elif defined(__wasi__) |
| 1107 | err = 0; |
| 1108 | #else |
| 1109 | err = mprotect(start, csize, protect ? PROT_NONE : (PROT_READ | PROT_WRITE)); |
| 1110 | if (err != 0) { err = errno; } |
| 1111 | #endif |
| 1112 | if (err != 0) { |
| 1113 | _mi_warning_message("mprotect error: start: %p, csize: 0x%zx, err: %i\n", start, csize, err); |
| 1114 | mi_mprotect_hint(err); |
| 1115 | } |
| 1116 | return (err == 0); |
| 1117 | } |
| 1118 | |
| 1119 | bool _mi_os_protect(void* addr, size_t size) { |
| 1120 | return mi_os_protectx(addr, size, true); |
no test coverage detected