Skip to content

Instantly share code, notes, and snippets.

@heatd

heatd/repro.cpp Secret

Created March 19, 2026 18:17
Show Gist options
  • Select an option

  • Save heatd/1450d273005aba91fa5744f44dfcd933 to your computer and use it in GitHub Desktop.

Select an option

Save heatd/1450d273005aba91fa5744f44dfcd933 to your computer and use it in GitHub Desktop.
/* SPDX-License-Identifier: GPL-2.0-only */
#include <stdexcept>
#include <benchmark/benchmark.h>
#include <sys/mman.h>
static void mprotect_bench(benchmark::State& state)
{
int i = 0;
void *ptr;
size_t size = 40000 * 1024;
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (ptr == MAP_FAILED)
throw std::runtime_error("ohno");
madvise(ptr, size, MADV_NOHUGEPAGE);
madvise(ptr, size, MADV_POPULATE_WRITE);
for (auto _ : state) {
if (i & 1)
mprotect(ptr, size, PROT_NONE);
else
mprotect(ptr, size, PROT_READ | PROT_WRITE);
i++;
}
}
BENCHMARK(mprotect_bench);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment