blob: e950bc5fb6715e09d8247109e8fc9de0cf8f8b05 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02002
3# Generate error.c
Paul Bakker9d781402011-05-09 16:17:09 +00004#
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02005# Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments,
6# or generate_errors.pl include_dir data_dir error_file
Bence Szépkúti700ee442020-05-26 00:33:31 +02007#
Bence Szépkúti1e148272020-08-07 13:07:28 +02008# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02009# SPDX-License-Identifier: Apache-2.0
10#
11# Licensed under the Apache License, Version 2.0 (the "License"); you may
12# not use this file except in compliance with the License.
13# You may obtain a copy of the License at
14#
15# http://www.apache.org/licenses/LICENSE-2.0
16#
17# Unless required by applicable law or agreed to in writing, software
18# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20# See the License for the specific language governing permissions and
21# limitations under the License.
Paul Bakker9d781402011-05-09 16:17:09 +000022
23use strict;
Gilles Peskine8c92d5c2021-08-02 22:53:40 +020024use warnings;
Paul Bakker9d781402011-05-09 16:17:09 +000025
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020026my ($include_dir, $data_dir, $error_file);
27
28if( @ARGV ) {
29 die "Invalid number of arguments" if scalar @ARGV != 3;
30 ($include_dir, $data_dir, $error_file) = @ARGV;
31
32 -d $include_dir or die "No such directory: $include_dir\n";
33 -d $data_dir or die "No such directory: $data_dir\n";
34} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020036 $data_dir = 'scripts/data_files';
37 $error_file = 'library/error.c';
38
39 unless( -d $include_dir && -d $data_dir ) {
40 chdir '..' or die;
41 -d $include_dir && -d $data_dir
42 or die "Without arguments, must be run from root or scripts\n"
43 }
44}
45
Paul Bakker9d781402011-05-09 16:17:09 +000046my $error_format_file = $data_dir.'/error.fmt';
47
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000048my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020049 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
Janos Follath60f6b642019-12-03 15:55:56 +000050 ENTROPY ERROR GCM HKDF HMAC_DRBG MD2 MD4 MD5
Gilles Peskine1fcf7212020-02-26 18:26:46 +010051 NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
Gilles Peskine92143272018-01-25 23:26:24 +010052 SHA1 SHA256 SHA512 THREADING XTEA );
53my @high_level_modules = qw( CIPHER DHM ECP MD
54 PEM PK PKCS12 PKCS5
Gilles Peskine314bc892020-02-26 18:28:25 +010055 RSA SSL X509 );
Paul Bakker9d781402011-05-09 16:17:09 +000056
Paul Bakker9d781402011-05-09 16:17:09 +000057undef $/;
58
59open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
60my $error_format = <FORMAT_FILE>;
61close(FORMAT_FILE);
62
Thomas Daubney0c6052f2023-01-09 18:48:01 +000063my @files = glob qq("$include_dir/*.h");
Gilles Peskine5241f852020-05-25 12:21:22 +020064my @necessary_include_files;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010065my @matches;
66foreach my $file (@files) {
Thomas Daubney0c6052f2023-01-09 18:48:01 +000067 open(FILE, '<:crlf', $file) or die("$0: $file: $!");
Gilles Peskine80605bb2021-07-26 18:39:53 +020068 my $content = <FILE>;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010069 close FILE;
Gilles Peskine80605bb2021-07-26 18:39:53 +020070 my $found = 0;
71 while ($content =~ m[
Gilles Peskine8978c5b2021-07-26 19:30:08 +020072 # Both the before-comment and the after-comment are optional.
73 # Only the comment content is a regex capture group. The comment
74 # start and end parts are outside the capture group.
75 (?:/\*[*!](?!<) # Doxygen before-comment start
76 ((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside)
77 \*/)? # Comment end
78 \s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name
79 \s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign)
80 (?:/\*[*!]< # Doxygen after-comment start
81 ((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside)
82 \*/)? # Comment end
Gilles Peskine80605bb2021-07-26 18:39:53 +020083 ]gsx) {
84 my ($before, $name, $value, $after) = ($1, $2, $3, $4);
85 # Discard Doxygen comments that are coincidentally present before
86 # an error definition but not attached to it. This is ad hoc, based
87 # on what actually matters (or mattered at some point).
Gilles Peskine8c92d5c2021-08-02 22:53:40 +020088 undef $before if defined($before) && $before =~ /\s*\\name\s/s;
Gilles Peskine80605bb2021-07-26 18:39:53 +020089 die "Description neither before nor after $name in $file\n"
90 if !defined($before) && !defined($after);
91 die "Description both before and after $name in $file\n"
92 if defined($before) && defined($after);
93 my $description = (defined($before) ? $before : $after);
94 $description =~ s/^\s+//;
Gilles Peskined15ed312021-07-26 18:45:22 +020095 $description =~ s/\n( *\*)? */ /g;
Gilles Peskine80605bb2021-07-26 18:39:53 +020096 $description =~ s/\.?\s+$//;
97 push @matches, [$name, $value, $description];
98 ++$found;
99 }
100 if ($found) {
101 my $include_name = $file;
102 $include_name =~ s!.*/!!;
103 push @necessary_include_files, $include_name;
104 }
Andres Amaya Garcia36855d62017-10-09 17:22:07 +0100105}
Paul Bakker9d781402011-05-09 16:17:09 +0000106
107my $ll_old_define = "";
108my $hl_old_define = "";
109
110my $ll_code_check = "";
111my $hl_code_check = "";
112
113my $headers = "";
Gilles Peskine5241f852020-05-25 12:21:22 +0200114my %included_headers;
Paul Bakker9d781402011-05-09 16:17:09 +0000115
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000116my %error_codes_seen;
117
Gilles Peskine80605bb2021-07-26 18:39:53 +0200118foreach my $match (@matches)
Paul Bakker9d781402011-05-09 16:17:09 +0000119{
Gilles Peskine80605bb2021-07-26 18:39:53 +0200120 my ($error_name, $error_code, $description) = @$match;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000121
122 die "Duplicated error code: $error_code ($error_name)\n"
123 if( $error_codes_seen{$error_code}++ );
124
Paul Bakker9d781402011-05-09 16:17:09 +0000125 $description =~ s/\\/\\\\/g;
Paul Bakker9d781402011-05-09 16:17:09 +0000126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
Paul Bakker9d781402011-05-09 16:17:09 +0000128
129 # Fix faulty ones
130 $module_name = "BIGNUM" if ($module_name eq "MPI");
Paul Bakker880ac7e2011-11-27 14:50:49 +0000131 $module_name = "CTR_DRBG" if ($module_name eq "CTR");
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +0100132 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
Paul Bakker9d781402011-05-09 16:17:09 +0000133
134 my $define_name = $module_name;
Gilles Peskine1bf45e12020-02-26 18:28:23 +0100135 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
Paul Bakkerdceecd82011-11-15 16:38:34 +0000136 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
Gilles Peskine314bc892020-02-26 18:28:25 +0100137 $define_name = "SSL_TLS" if ($define_name eq "SSL");
Paul Bakkercff68422013-09-15 20:43:33 +0200138 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
Paul Bakker9d781402011-05-09 16:17:09 +0000139
140 my $include_name = $module_name;
141 $include_name =~ tr/A-Z/a-z/;
Paul Bakker9d781402011-05-09 16:17:09 +0000142
Gilles Peskine1fcf7212020-02-26 18:26:46 +0100143 # Fix faulty ones
144 $include_name = "net_sockets" if ($module_name eq "NET");
145
Gilles Peskine5241f852020-05-25 12:21:22 +0200146 $included_headers{"${include_name}.h"} = $module_name;
147
Paul Bakker9d781402011-05-09 16:17:09 +0000148 my $found_ll = grep $_ eq $module_name, @low_level_modules;
149 my $found_hl = grep $_ eq $module_name, @high_level_modules;
150 if (!$found_ll && !$found_hl)
151 {
Manuel Pégourié-Gonnard48573f82015-08-06 17:24:56 +0200152 printf("Error: Do not know how to handle: $module_name\n");
Paul Bakker9d781402011-05-09 16:17:09 +0000153 exit 1;
154 }
155
156 my $code_check;
157 my $old_define;
158 my $white_space;
Paul Bakkercff68422013-09-15 20:43:33 +0200159 my $first;
Paul Bakker9d781402011-05-09 16:17:09 +0000160
161 if ($found_ll)
162 {
163 $code_check = \$ll_code_check;
164 $old_define = \$ll_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700165 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000166 }
167 else
168 {
169 $code_check = \$hl_code_check;
170 $old_define = \$hl_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700171 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000172 }
173
174 if ($define_name ne ${$old_define})
175 {
176 if (${$old_define} ne "")
177 {
Paul Bakkercff68422013-09-15 20:43:33 +0200178 ${$code_check} .= "#endif /* ";
179 $first = 0;
180 foreach my $dep (split(/,/, ${$old_define}))
181 {
182 ${$code_check} .= " || " if ($first++);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 ${$code_check} .= "MBEDTLS_${dep}_C";
Paul Bakkercff68422013-09-15 20:43:33 +0200184 }
185 ${$code_check} .= " */\n\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000186 }
187
Paul Bakkercff68422013-09-15 20:43:33 +0200188 ${$code_check} .= "#if ";
189 $headers .= "#if " if ($include_name ne "");
190 $first = 0;
191 foreach my $dep (split(/,/, ${define_name}))
192 {
193 ${$code_check} .= " || " if ($first);
194 $headers .= " || " if ($first++);
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
197 $headers .= "defined(MBEDTLS_${dep}_C)" if
Paul Bakkercff68422013-09-15 20:43:33 +0200198 ($include_name ne "");
199 }
200 ${$code_check} .= "\n";
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000201 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
Paul Bakker9d781402011-05-09 16:17:09 +0000202 "#endif\n\n" if ($include_name ne "");
203 ${$old_define} = $define_name;
204 }
205
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700206 ${$code_check} .= "${white_space}case -($error_name):\n".
Gaurav Aggarwalcabde252020-04-22 08:13:25 -0700207 "${white_space} return( \"$module_name - $description\" );\n"
Paul Bakker9d781402011-05-09 16:17:09 +0000208};
209
210if ($ll_old_define ne "")
211{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200212 $ll_code_check .= "#endif /* ";
213 my $first = 0;
214 foreach my $dep (split(/,/, $ll_old_define))
215 {
216 $ll_code_check .= " || " if ($first++);
217 $ll_code_check .= "MBEDTLS_${dep}_C";
218 }
219 $ll_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000220}
221if ($hl_old_define ne "")
222{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200223 $hl_code_check .= "#endif /* ";
224 my $first = 0;
225 foreach my $dep (split(/,/, $hl_old_define))
226 {
227 $hl_code_check .= " || " if ($first++);
228 $hl_code_check .= "MBEDTLS_${dep}_C";
229 }
230 $hl_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000231}
232
233$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
234$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
235$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
236
237open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
238print ERROR_FILE $error_format;
239close(ERROR_FILE);
Gilles Peskine5241f852020-05-25 12:21:22 +0200240
241my $errors = 0;
242for my $include_name (@necessary_include_files)
243{
244 if (not $included_headers{$include_name})
245 {
246 print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
247 ++$errors;
248 }
249}
250
251exit !!$errors;