reply_mig_hack.awk 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Reply server mig-output massager
  2. #
  3. # Copyright (C) 1995-2022 Free Software Foundation, Inc.
  4. #
  5. # Written by Miles Bader <miles@gnu.ai.mit.edu>
  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 as
  9. # published by the Free Software Foundation; either version 3, or (at
  10. # your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # This awk script hacks the output of mig-generated reply server code
  21. # so that it allows replies with just the error-code in them (as this is
  22. # how mig returns errors).
  23. #
  24. # It is highly, highly, dependent on the exact format of mig output. Ick.
  25. #
  26. BEGIN { parse_phase = 0; }
  27. /^}/ { parse_phase = 0; }
  28. parse_phase == 0 && /^mig_internal void _X[a-zA-Z0-9_]*_reply/ {
  29. # The start of a mig server routine. Reset everything. Note that we only
  30. # mess with rpcs that have the suffix `_reply'.
  31. num_args = 0;
  32. num_checks = 0;
  33. parse_phase = 1;
  34. print; next;
  35. }
  36. parse_phase == 1 && /^[\t ]*typedef struct/ {
  37. # The first structure in the server routine should describe the arguments
  38. parse_phase = 2;
  39. print; next;
  40. }
  41. parse_phase == 2 {
  42. # The message header field in the args structure, which skip.
  43. parse_phase = 3;
  44. print; next;
  45. }
  46. parse_phase == 3 && /} Request/ {
  47. # The args structure is over.
  48. if (num_args > 1)
  49. parse_phase = 5;
  50. else
  51. # There's no extra args that could screw up the normal mechanism for
  52. # error returns, so we don't have to insert any new code.
  53. parse_phase = 0;
  54. print; next;
  55. }
  56. parse_phase == 3 && num_args == 0 {
  57. # The type field for an argument.
  58. # This won't be accurate in case of unions being used in the Request struct,
  59. # but that doesn't matter, as we'll only be looking at arg_type_code_name[0],
  60. # which will not be a union type.
  61. arg_type_code_name[num_args] = $2;
  62. sub (/;$/, "", arg_type_code_name[num_args]) # Get rid of the semi-colon
  63. parse_phase = 4;
  64. print; next;
  65. }
  66. parse_phase == 3 && num_args == 1 {
  67. # We've got more than one argument (but we don't care what it is).
  68. num_args++;
  69. print; next;
  70. }
  71. parse_phase == 3 {
  72. # We've know everything we need; now just wait for the end of the Request
  73. # struct.
  74. print; next;
  75. }
  76. parse_phase == 4 {
  77. # The value field for an argument.
  78. # This won't be accurate in case of unions being used in the Request struct,
  79. # but that doesn't matter, as we'll only be looking at arg_name[0], which
  80. # will not be a union type.
  81. arg_name[num_args] = $2;
  82. sub (/;$/, "", arg_name[num_args]) # Get rid of the semi-colon
  83. num_args++;
  84. parse_phase = 3;
  85. print; next;
  86. }
  87. parse_phase == 5 && /^[ \t]*(auto |static )?const mach_msg_type_t/ {
  88. # The type check structure for an argument.
  89. arg_check_name[num_checks] = $(NF - 2);
  90. num_checks++;
  91. print; next;
  92. }
  93. parse_phase == 5 && /^[ \t]*mig_external kern_return_t/ {
  94. # The declaration of the user server function for this rpc.
  95. user_function_name = $3;
  96. print; next;
  97. }
  98. parse_phase == 5 && /^#if[ \t]TypeCheck/ {
  99. # Keep going if we have not yet collected the type check structures.
  100. if (num_checks == 0)
  101. {
  102. print; next;
  103. }
  104. # The first args type checking statement; we need to insert our chunk of
  105. # code that bypasses all the type checks if this is an error return, after
  106. # which we're done until we get to the next function. Handily, the size
  107. # of mig's Reply structure is also the size of the alternate Request
  108. # structure that we want to check for.
  109. print "\tif (In0P->Head.msgh_size == sizeof (Reply)";
  110. print "\t && ! (In0P->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)";
  111. print "\t && ! BAD_TYPECHECK(&In0P->" arg_type_code_name[0] ", &" arg_check_name[0] ")";
  112. print "\t && In0P->" arg_name[0] " != 0)";
  113. print "\t /* Error return, only the error code argument is passed. */";
  114. print "\t {";
  115. # Force the function user_function_name into a type that only takes the first
  116. # two arguments.
  117. # This is possibly bogus, but easier than supplying bogus values for all
  118. # the other args (we can't just pass 0 for them, as they might not be scalar).
  119. print "\t void * __error_call = " user_function_name ";";
  120. print "\t OutP->RetCode = (*(kern_return_t (*)(mach_port_t, kern_return_t)) __error_call) (In0P->Head.msgh_request_port, In0P->" arg_name[0] ");";
  121. print "\t return;";
  122. print "\t }";
  123. print "";
  124. parse_phase = 0;
  125. print; next;
  126. }
  127. { print; }