rodata_test.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * rodata_test.c: functional test for mark_rodata_ro function
  4. *
  5. * (C) Copyright 2008 Intel Corporation
  6. * Author: Arjan van de Ven <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "rodata_test: " fmt
  9. #include <linux/rodata_test.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/mm.h>
  12. #include <asm/sections.h>
  13. static const int rodata_test_data = 0xC3;
  14. void rodata_test(void)
  15. {
  16. int zero = 0;
  17. /* test 1: read the value */
  18. /* If this test fails, some previous testrun has clobbered the state */
  19. if (!rodata_test_data) {
  20. pr_err("test 1 fails (start data)\n");
  21. return;
  22. }
  23. /* test 2: write to the variable; this should fault */
  24. if (!copy_to_kernel_nofault((void *)&rodata_test_data,
  25. (void *)&zero, sizeof(zero))) {
  26. pr_err("test data was not read only\n");
  27. return;
  28. }
  29. /* test 3: check the value hasn't changed */
  30. if (rodata_test_data == zero) {
  31. pr_err("test data was changed\n");
  32. return;
  33. }
  34. /* test 4: check if the rodata section is PAGE_SIZE aligned */
  35. if (!PAGE_ALIGNED(__start_rodata)) {
  36. pr_err("start of .rodata is not page size aligned\n");
  37. return;
  38. }
  39. if (!PAGE_ALIGNED(__end_rodata)) {
  40. pr_err("end of .rodata is not page size aligned\n");
  41. return;
  42. }
  43. pr_info("all tests were successful\n");
  44. }