Skip to content

Commit

Permalink
selftests/powerpc: Add a test of SEGV error behaviour
Browse files Browse the repository at this point in the history
Add a test case of the error code reported when we take a SEGV on a
mapped but inaccessible area. We broke this recently.

Based on a test case from John Sperbeck <[email protected]>.

Acked-by: John Sperbeck <[email protected]>
Signed-off-by: Michael Ellerman <[email protected]>
  • Loading branch information
mpe committed Jan 3, 2018
1 parent 5769bea commit 6ed3615
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tools/testing/selftests/powerpc/mm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
hugetlb_vs_thp_test
subpage_prot
tempfile
prot_sao
prot_sao
segv_errors
2 changes: 1 addition & 1 deletion tools/testing/selftests/powerpc/mm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
noarg:
$(MAKE) -C ../

TEST_GEN_PROGS := hugetlb_vs_thp_test subpage_prot prot_sao
TEST_GEN_PROGS := hugetlb_vs_thp_test subpage_prot prot_sao segv_errors
TEST_GEN_FILES := tempfile

include ../../lib.mk
Expand Down
78 changes: 78 additions & 0 deletions tools/testing/selftests/powerpc/mm/segv_errors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: GPL-2.0

/*
* Copyright 2017 John Sperbeck
*
* Test that an access to a mapped but inaccessible area causes a SEGV and
* reports si_code == SEGV_ACCERR.
*/

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
#include <assert.h>
#include <ucontext.h>

#include "utils.h"

static bool faulted;
static int si_code;

static void segv_handler(int n, siginfo_t *info, void *ctxt_v)
{
ucontext_t *ctxt = (ucontext_t *)ctxt_v;
struct pt_regs *regs = ctxt->uc_mcontext.regs;

faulted = true;
si_code = info->si_code;
regs->nip += 4;
}

int test_segv_errors(void)
{
struct sigaction act = {
.sa_sigaction = segv_handler,
.sa_flags = SA_SIGINFO,
};
char c, *p = NULL;

p = mmap(NULL, getpagesize(), 0, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
FAIL_IF(p == MAP_FAILED);

FAIL_IF(sigaction(SIGSEGV, &act, NULL) != 0);

faulted = false;
si_code = 0;

/*
* We just need a compiler barrier, but mb() works and has the nice
* property of being easy to spot in the disassembly.
*/
mb();
c = *p;
mb();

FAIL_IF(!faulted);
FAIL_IF(si_code != SEGV_ACCERR);

faulted = false;
si_code = 0;

mb();
*p = c;
mb();

FAIL_IF(!faulted);
FAIL_IF(si_code != SEGV_ACCERR);

return 0;
}

int main(void)
{
return test_harness(test_segv_errors, "segv_errors");
}

0 comments on commit 6ed3615

Please sign in to comment.