test_rodata.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * test_rodata.c: functional test for mark_rodata_ro function
  3. *
  4. * (C) Copyright 2008 Intel Corporation
  5. * Author: Arjan van de Ven <arjan@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; version 2
  10. * of the License.
  11. */
  12. #include <linux/module.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/sections.h>
  15. int rodata_test(void)
  16. {
  17. unsigned long result;
  18. unsigned long start, end;
  19. /* test 1: read the value */
  20. /* If this test fails, some previous testrun has clobbered the state */
  21. if (!rodata_test_data) {
  22. printk(KERN_ERR "rodata_test: test 1 fails (start data)\n");
  23. return -ENODEV;
  24. }
  25. /* test 2: write to the variable; this should fault */
  26. /*
  27. * If this test fails, we managed to overwrite the data
  28. *
  29. * This is written in assembly to be able to catch the
  30. * exception that is supposed to happen in the correct
  31. * case
  32. */
  33. result = 1;
  34. asm volatile(
  35. "0: mov %[zero],(%[rodata_test])\n"
  36. " mov %[zero], %[rslt]\n"
  37. "1:\n"
  38. ".section .fixup,\"ax\"\n"
  39. "2: jmp 1b\n"
  40. ".previous\n"
  41. ".section __ex_table,\"a\"\n"
  42. " .align 16\n"
  43. #ifdef CONFIG_X86_32
  44. " .long 0b,2b\n"
  45. #else
  46. " .quad 0b,2b\n"
  47. #endif
  48. ".previous"
  49. : [rslt] "=r" (result)
  50. : [rodata_test] "r" (&rodata_test_data), [zero] "r" (0UL)
  51. );
  52. if (!result) {
  53. printk(KERN_ERR "rodata_test: test data was not read only\n");
  54. return -ENODEV;
  55. }
  56. /* test 3: check the value hasn't changed */
  57. /* If this test fails, we managed to overwrite the data */
  58. if (!rodata_test_data) {
  59. printk(KERN_ERR "rodata_test: Test 3 failes (end data)\n");
  60. return -ENODEV;
  61. }
  62. /* test 4: check if the rodata section is 4Kb aligned */
  63. start = (unsigned long)__start_rodata;
  64. end = (unsigned long)__end_rodata;
  65. if (start & (PAGE_SIZE - 1)) {
  66. printk(KERN_ERR "rodata_test: .rodata is not 4k aligned\n");
  67. return -ENODEV;
  68. }
  69. if (end & (PAGE_SIZE - 1)) {
  70. printk(KERN_ERR "rodata_test: .rodata end is not 4k aligned\n");
  71. return -ENODEV;
  72. }
  73. return 0;
  74. }
  75. MODULE_LICENSE("GPL");
  76. MODULE_DESCRIPTION("Testcase for the DEBUG_RODATA infrastructure");
  77. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");