From d66db7412ee8e16e08b340767f4c00a3b570e730 Mon Sep 17 00:00:00 2001 From: Matthew Malcomson Date: Tue, 1 Dec 2020 11:40:18 +0000 Subject: [PATCH] testsuite: Correct check_effective_target_hwaddress_exec This test should ensure that we can compile with hwasan, that such a compiled binary runs as expected, *and* that we're running on a kernel which implements the capability to ignore the top bytes of pointers in syscalls. It was expected that a basic test of `int main(void) { return 0; }` would check this, since there is a check called during `__hwasan_init` in libhwasan to ensure that the kernel we're running on provides a `prctl` to request the relaxed ABI. Unfortunately, the check in libhwasan has a bug in it, and does not correctly fail when the kernel feature is not around. This means that check is not automatically provided by the runtime. The sanitizer runtime will be fixed but would like to install this fix here in case fixing the library is not quick enough for the release (and so that people running the testsuite do not see spurious errors in the meantime). Tested by running testsuite on an AArch64 machine with and without the required kernel. Observed that the test does indeed fail when the kernel feature is unavailable and pass when the feature is available. Note that this test is directly targetting AArch64 by using `prctl` numbers specific to it. That's unfortunate, but once the runtime fix has gone in we will be able to remove that requirement. Ok for trunk? gcc/testsuite/ChangeLog: * lib/hwasan-dg.exp (check_effective_target_hwaddress_exec): Fix check for correct kernel version. --- gcc/testsuite/lib/hwasan-dg.exp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/gcc/testsuite/lib/hwasan-dg.exp b/gcc/testsuite/lib/hwasan-dg.exp index 892f2ba..bd2a011 100644 --- a/gcc/testsuite/lib/hwasan-dg.exp +++ b/gcc/testsuite/lib/hwasan-dg.exp @@ -41,7 +41,24 @@ proc check_effective_target_fsanitize_hwaddress {} { proc check_effective_target_hwaddress_exec {} { if ![check_runtime hwaddress_exec { - int main (void) { return 0; } + #ifdef __cplusplus + extern "C" { + #endif + extern int prctl(int, unsigned long, unsigned long, unsigned long, unsigned long); + #ifdef __cplusplus + } + #endif + int main (void) { + #define PR_SET_TAGGED_ADDR_CTRL 55 + #define PR_GET_TAGGED_ADDR_CTRL 56 + #define PR_TAGGED_ADDR_ENABLE (1UL << 0) + if (prctl (PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0) == -1) + return 1; + if (prctl(PR_SET_TAGGED_ADDR_CTRL, PR_TAGGED_ADDR_ENABLE, 0, 0, 0) == -1 + || !prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0)) + return 1; + return 0; + } }] { return 0; } -- 2.7.4