/// <summary> /// set r/w/x protection on a portion of memory. rounded to encompassing pages /// </summary> public override void Protect(ulong start, ulong length, Protection prot) { if (length == 0) { return; } int pstart = GetPage(start); int pend = GetPage(start + length - 1); var p = GetMemoryProtectionValue(prot); for (int i = pstart; i <= pend; i++) { _pageData[i] = prot; // also store the value for later use } if (Active) // it's legal to Protect() if we're not active; the information is just saved for the next activation { var computedStart = WaterboxUtils.AlignDown(start); var computedEnd = WaterboxUtils.AlignUp(start + length); var computedLength = computedEnd - computedStart; if (mprotect(Z.US(computedStart), Z.UU(computedLength), p) != 0) { throw new InvalidOperationException("mprotect() returned -1!"); } } }
/// <summary> /// set r/w/x protection on a portion of memory. rounded to encompassing pages /// </summary> public void Protect(ulong start, ulong length, Protection prot) { if (length == 0) { return; } int pstart = GetPage(start); int pend = GetPage(start + length - 1); var p = GetKernelMemoryProtectionValue(prot); for (int i = pstart; i <= pend; i++) { _pageData[i] = prot; // also store the value for later use } if (Active) // it's legal to Protect() if we're not active; the information is just saved for the next activation { var computedStart = WaterboxUtils.AlignDown(start); var computedEnd = WaterboxUtils.AlignUp(start + length); var computedLength = computedEnd - computedStart; Kernel32.MemoryProtection old; if (!Kernel32.VirtualProtect(Z.UU(computedStart), Z.UU(computedLength), p, out old)) { throw new InvalidOperationException($"{nameof(Kernel32.VirtualProtect)}() returned FALSE!"); } } }
/// <exception cref="InvalidOperationException">failed to protect memory</exception> public override void Protect(ulong start, ulong length, Protection prot) { if (length == 0) { return; } var pstart = GetPage(start); var pend = GetPage(start + length - 1); for (var i = pstart; i <= pend; i++) { _pageData[i] = prot; // also store the value for later use } if (!Active) { return; // it's legal to call this method if we're not active; the information is just saved for the next activation } var computedStart = WaterboxUtils.AlignDown(start); var protEnum = prot.ToMemoryProtection(); var exitCode = mprotect( Z.US(computedStart), Z.UU(WaterboxUtils.AlignUp(start + length) - computedStart), protEnum ); if (exitCode != 0) { throw new InvalidOperationException($"{nameof(mprotect)}() returned {exitCode}!"); } }