generate_errors.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env perl
  2. # Generate error.c
  3. #
  4. # Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments,
  5. # or generate_errors.pl include_dir data_dir error_file
  6. #
  7. # Copyright The Mbed TLS Contributors
  8. # SPDX-License-Identifier: Apache-2.0
  9. #
  10. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. # not use this file except in compliance with the License.
  12. # You may obtain a copy of the License at
  13. #
  14. # http://www.apache.org/licenses/LICENSE-2.0
  15. #
  16. # Unless required by applicable law or agreed to in writing, software
  17. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. # See the License for the specific language governing permissions and
  20. # limitations under the License.
  21. use strict;
  22. use warnings;
  23. my ($include_dir, $data_dir, $error_file);
  24. if( @ARGV ) {
  25. die "Invalid number of arguments" if scalar @ARGV != 3;
  26. ($include_dir, $data_dir, $error_file) = @ARGV;
  27. -d $include_dir or die "No such directory: $include_dir\n";
  28. -d $data_dir or die "No such directory: $data_dir\n";
  29. } else {
  30. $include_dir = 'include/mbedtls';
  31. $data_dir = 'scripts/data_files';
  32. $error_file = 'library/error.c';
  33. unless( -d $include_dir && -d $data_dir ) {
  34. chdir '..' or die;
  35. -d $include_dir && -d $data_dir
  36. or die "Without arguments, must be run from root or scripts\n"
  37. }
  38. }
  39. my $error_format_file = $data_dir.'/error.fmt';
  40. my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH
  41. CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
  42. ENTROPY ERROR GCM HKDF HMAC_DRBG MD2 MD4 MD5
  43. NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
  44. SHA1 SHA256 SHA512 THREADING XTEA );
  45. my @high_level_modules = qw( CIPHER DHM ECP MD
  46. PEM PK PKCS12 PKCS5
  47. RSA SSL X509 );
  48. undef $/;
  49. open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
  50. my $error_format = <FORMAT_FILE>;
  51. close(FORMAT_FILE);
  52. my @files = <$include_dir/*.h>;
  53. my @necessary_include_files;
  54. my @matches;
  55. foreach my $file (@files) {
  56. open(FILE, "$file");
  57. my $content = <FILE>;
  58. close FILE;
  59. my $found = 0;
  60. while ($content =~ m[
  61. # Both the before-comment and the after-comment are optional.
  62. # Only the comment content is a regex capture group. The comment
  63. # start and end parts are outside the capture group.
  64. (?:/\*[*!](?!<) # Doxygen before-comment start
  65. ((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside)
  66. \*/)? # Comment end
  67. \s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name
  68. \s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign)
  69. (?:/\*[*!]< # Doxygen after-comment start
  70. ((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside)
  71. \*/)? # Comment end
  72. ]gsx) {
  73. my ($before, $name, $value, $after) = ($1, $2, $3, $4);
  74. # Discard Doxygen comments that are coincidentally present before
  75. # an error definition but not attached to it. This is ad hoc, based
  76. # on what actually matters (or mattered at some point).
  77. undef $before if defined($before) && $before =~ /\s*\\name\s/s;
  78. die "Description neither before nor after $name in $file\n"
  79. if !defined($before) && !defined($after);
  80. die "Description both before and after $name in $file\n"
  81. if defined($before) && defined($after);
  82. my $description = (defined($before) ? $before : $after);
  83. $description =~ s/^\s+//;
  84. $description =~ s/\n( *\*)? */ /g;
  85. $description =~ s/\.?\s+$//;
  86. push @matches, [$name, $value, $description];
  87. ++$found;
  88. }
  89. if ($found) {
  90. my $include_name = $file;
  91. $include_name =~ s!.*/!!;
  92. push @necessary_include_files, $include_name;
  93. }
  94. }
  95. my $ll_old_define = "";
  96. my $hl_old_define = "";
  97. my $ll_code_check = "";
  98. my $hl_code_check = "";
  99. my $headers = "";
  100. my %included_headers;
  101. my %error_codes_seen;
  102. foreach my $match (@matches)
  103. {
  104. my ($error_name, $error_code, $description) = @$match;
  105. die "Duplicated error code: $error_code ($error_name)\n"
  106. if( $error_codes_seen{$error_code}++ );
  107. $description =~ s/\\/\\\\/g;
  108. my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
  109. # Fix faulty ones
  110. $module_name = "BIGNUM" if ($module_name eq "MPI");
  111. $module_name = "CTR_DRBG" if ($module_name eq "CTR");
  112. $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
  113. my $define_name = $module_name;
  114. $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
  115. $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
  116. $define_name = "SSL_TLS" if ($define_name eq "SSL");
  117. $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
  118. my $include_name = $module_name;
  119. $include_name =~ tr/A-Z/a-z/;
  120. # Fix faulty ones
  121. $include_name = "net_sockets" if ($module_name eq "NET");
  122. $included_headers{"${include_name}.h"} = $module_name;
  123. my $found_ll = grep $_ eq $module_name, @low_level_modules;
  124. my $found_hl = grep $_ eq $module_name, @high_level_modules;
  125. if (!$found_ll && !$found_hl)
  126. {
  127. printf("Error: Do not know how to handle: $module_name\n");
  128. exit 1;
  129. }
  130. my $code_check;
  131. my $old_define;
  132. my $white_space;
  133. my $first;
  134. if ($found_ll)
  135. {
  136. $code_check = \$ll_code_check;
  137. $old_define = \$ll_old_define;
  138. $white_space = ' ';
  139. }
  140. else
  141. {
  142. $code_check = \$hl_code_check;
  143. $old_define = \$hl_old_define;
  144. $white_space = ' ';
  145. }
  146. if ($define_name ne ${$old_define})
  147. {
  148. if (${$old_define} ne "")
  149. {
  150. ${$code_check} .= "#endif /* ";
  151. $first = 0;
  152. foreach my $dep (split(/,/, ${$old_define}))
  153. {
  154. ${$code_check} .= " || " if ($first++);
  155. ${$code_check} .= "MBEDTLS_${dep}_C";
  156. }
  157. ${$code_check} .= " */\n\n";
  158. }
  159. ${$code_check} .= "#if ";
  160. $headers .= "#if " if ($include_name ne "");
  161. $first = 0;
  162. foreach my $dep (split(/,/, ${define_name}))
  163. {
  164. ${$code_check} .= " || " if ($first);
  165. $headers .= " || " if ($first++);
  166. ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
  167. $headers .= "defined(MBEDTLS_${dep}_C)" if
  168. ($include_name ne "");
  169. }
  170. ${$code_check} .= "\n";
  171. $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
  172. "#endif\n\n" if ($include_name ne "");
  173. ${$old_define} = $define_name;
  174. }
  175. ${$code_check} .= "${white_space}case -($error_name):\n".
  176. "${white_space} return( \"$module_name - $description\" );\n"
  177. };
  178. if ($ll_old_define ne "")
  179. {
  180. $ll_code_check .= "#endif /* ";
  181. my $first = 0;
  182. foreach my $dep (split(/,/, $ll_old_define))
  183. {
  184. $ll_code_check .= " || " if ($first++);
  185. $ll_code_check .= "MBEDTLS_${dep}_C";
  186. }
  187. $ll_code_check .= " */\n";
  188. }
  189. if ($hl_old_define ne "")
  190. {
  191. $hl_code_check .= "#endif /* ";
  192. my $first = 0;
  193. foreach my $dep (split(/,/, $hl_old_define))
  194. {
  195. $hl_code_check .= " || " if ($first++);
  196. $hl_code_check .= "MBEDTLS_${dep}_C";
  197. }
  198. $hl_code_check .= " */\n";
  199. }
  200. $error_format =~ s/HEADER_INCLUDED\n/$headers/g;
  201. $error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
  202. $error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
  203. open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
  204. print ERROR_FILE $error_format;
  205. close(ERROR_FILE);
  206. my $errors = 0;
  207. for my $include_name (@necessary_include_files)
  208. {
  209. if (not $included_headers{$include_name})
  210. {
  211. print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
  212. ++$errors;
  213. }
  214. }
  215. exit !!$errors;