Skip to content

Commit bc0989a

Browse files
authored
Detect and report ASLR status (#1977)
* Detect and report ASLR status Theoretically, we could just disable ASLR in `main()`, but that seems to be disallowed by default by some other security features. Refs. #461 * Add a note about ASLR on other OS.
1 parent b616354 commit bc0989a

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

docs/reducing_variance.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ The benchmarks you subsequently run will have less variance.
3939

4040
<a name="reducing-variance" />
4141

42+
## Disabling ASLR
43+
44+
If you see this error:
45+
46+
```
47+
***WARNING*** ASLR is enabled, the results may have unreproducible noise in them.
48+
```
49+
50+
you might want to disable the ASLR security hardening feature while running the
51+
benchmark.
52+
53+
To globally disable ASLR on Linux, run
54+
```
55+
echo 0 > /proc/sys/kernel/randomize_va_space
56+
```
57+
58+
To run a single benchmark with ASLR disabled on Linux, do:
59+
```
60+
setarch `uname -m` -R ./a_benchmark
61+
```
62+
63+
Note that for the information on how to disable ASLR on other operating systems,
64+
please refer to their documentation.
65+
4266
## Reducing Variance in Benchmarks
4367

4468
The Linux CPU frequency governor [discussed

include/benchmark/benchmark.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,10 @@ struct BENCHMARK_EXPORT CPUInfo {
17311731

17321732
// Adding Struct for System Information
17331733
struct BENCHMARK_EXPORT SystemInfo {
1734+
enum class ASLR { UNKNOWN, ENABLED, DISABLED };
1735+
17341736
std::string name;
1737+
ASLR ASLRStatus;
17351738
static const SystemInfo& Get();
17361739

17371740
private:

src/json_reporter.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ bool JSONReporter::ReportContext(const Context& context) {
144144
<< ",\n";
145145
}
146146

147+
const SystemInfo& sysinfo = context.sys_info;
148+
if (SystemInfo::ASLR::UNKNOWN != sysinfo.ASLRStatus) {
149+
out << indent
150+
<< FormatKV("aslr_enabled",
151+
sysinfo.ASLRStatus == SystemInfo::ASLR::ENABLED)
152+
<< ",\n";
153+
}
154+
147155
out << indent << "\"caches\": [\n";
148156
indent = std::string(6, ' ');
149157
std::string cache_indent(8, ' ');

src/reporter.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ void BenchmarkReporter::PrintBasicContext(std::ostream *out,
8888
"overhead.\n";
8989
}
9090

91+
const SystemInfo &sysinfo = context.sys_info;
92+
if (SystemInfo::ASLR::ENABLED == sysinfo.ASLRStatus) {
93+
Out << "***WARNING*** ASLR is enabled, the results may have unreproducible "
94+
"noise in them.\n";
95+
}
96+
9197
#ifndef NDEBUG
9298
Out << "***WARNING*** Library was built as DEBUG. Timings may be "
9399
"affected.\n";

src/sysinfo.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
#include <pthread.h>
5555
#endif
5656

57+
#if defined(BENCHMARK_OS_LINUX)
58+
#include <sys/personality.h>
59+
#endif
60+
5761
#include <algorithm>
5862
#include <array>
5963
#include <bitset>
@@ -493,6 +497,17 @@ std::string GetSystemName() {
493497
#endif // Catch-all POSIX block.
494498
}
495499

500+
SystemInfo::ASLR GetASLR() {
501+
#ifdef BENCHMARK_OS_LINUX
502+
const auto curr_personality = personality(0xffffffff);
503+
return (curr_personality & ADDR_NO_RANDOMIZE) ? SystemInfo::ASLR::DISABLED
504+
: SystemInfo::ASLR::ENABLED;
505+
#else
506+
// FIXME: support detecting ASLR on other OS.
507+
return SystemInfo::ASLR::UNKNOWN;
508+
#endif
509+
}
510+
496511
int GetNumCPUsImpl() {
497512
#ifdef BENCHMARK_OS_WINDOWS
498513
SYSTEM_INFO sysinfo;
@@ -881,5 +896,5 @@ const SystemInfo& SystemInfo::Get() {
881896
return *info;
882897
}
883898

884-
SystemInfo::SystemInfo() : name(GetSystemName()) {}
899+
SystemInfo::SystemInfo() : name(GetSystemName()), ASLRStatus(GetASLR()) {}
885900
} // end namespace benchmark

0 commit comments

Comments
 (0)