Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * rodata_test.c: functional test for mark_rodata_ro function
0004  *
0005  * (C) Copyright 2008 Intel Corporation
0006  * Author: Arjan van de Ven <arjan@linux.intel.com>
0007  */
0008 #define pr_fmt(fmt) "rodata_test: " fmt
0009 
0010 #include <linux/rodata_test.h>
0011 #include <linux/uaccess.h>
0012 #include <asm/sections.h>
0013 
0014 static const int rodata_test_data = 0xC3;
0015 
0016 void rodata_test(void)
0017 {
0018     unsigned long start, end;
0019     int zero = 0;
0020 
0021     /* test 1: read the value */
0022     /* If this test fails, some previous testrun has clobbered the state */
0023     if (!rodata_test_data) {
0024         pr_err("test 1 fails (start data)\n");
0025         return;
0026     }
0027 
0028     /* test 2: write to the variable; this should fault */
0029     if (!copy_to_kernel_nofault((void *)&rodata_test_data,
0030                 (void *)&zero, sizeof(zero))) {
0031         pr_err("test data was not read only\n");
0032         return;
0033     }
0034 
0035     /* test 3: check the value hasn't changed */
0036     if (rodata_test_data == zero) {
0037         pr_err("test data was changed\n");
0038         return;
0039     }
0040 
0041     /* test 4: check if the rodata section is PAGE_SIZE aligned */
0042     start = (unsigned long)__start_rodata;
0043     end = (unsigned long)__end_rodata;
0044     if (start & (PAGE_SIZE - 1)) {
0045         pr_err("start of .rodata is not page size aligned\n");
0046         return;
0047     }
0048     if (end & (PAGE_SIZE - 1)) {
0049         pr_err("end of .rodata is not page size aligned\n");
0050         return;
0051     }
0052 
0053     pr_info("all tests were successful\n");
0054 }