1 /*
2  *  Copyright (c) Linux Test Project, 2014
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Library General Public License for more details.
13  */
14 /*
15  * This is a regression test for madvise(2) system call. It tests kernel
16  * for NULL ptr deref Oops fixed by:
17  *   commit ee53664bda169f519ce3c6a22d378f0b946c8178
18  *   Author: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
19  *   Date:   Fri Dec 20 15:10:03 2013 +0200
20  *     mm: Fix NULL pointer dereference in madvise(MADV_WILLNEED) support
21  *
22  * On buggy kernel with CONFIG_TRANSPARENT_HUGEPAGE=y CONFIG_DEBUG_LOCK_ALLOC=y
23  * this testcase should produce Oops and/or be killed. On fixed/good kernel
24  * this testcase runs to completion (retcode is 0)
25  */
26 
27 #include <sys/mman.h>
28 #include <errno.h>
29 #include "tst_test.h"
30 
31 #define ALLOC_SIZE (32 * 1024 * 1024)
32 
33 static void verify_madvise(void)
34 {
35 	void *p;
36 
37 	p = SAFE_MMAP(NULL, ALLOC_SIZE, PROT_READ,
38 			MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
39 
40 	TEST(mprotect(p, ALLOC_SIZE, PROT_NONE));
41 	if (TEST_RETURN == -1)
42 		tst_brk(TBROK | TTERRNO, "mprotect failed");
43 	TEST(madvise(p, ALLOC_SIZE, MADV_WILLNEED));
44 	SAFE_MUNMAP(p, ALLOC_SIZE);
45 
46 	if (TEST_RETURN == 0) {
47 		tst_res(TPASS, "issue has not been reproduced");
48 		return;
49 	}
50 
51 	if (TEST_ERRNO == EBADF)
52 		tst_brk(TCONF, "CONFIG_SWAP=n");
53 	else
54 		tst_brk(TBROK | TTERRNO, "madvise failed");
55 }
56 
57 static struct tst_test test = {
58 	.min_kver = "3.9.0",
59 	.test_all = verify_madvise,
60 };
61