blob: 0134c94f0762ac1fd2d96c0082e5148a09cd9c3f [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
Dave Rodgman16799db2023-11-02 19:47:20 +00009# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker9d781402011-05-09 16:17:09 +000010
11use strict;
Gilles Peskine58887ba2021-08-02 22:53:40 +020012use warnings;
Paul Bakker9d781402011-05-09 16:17:09 +000013
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020014my ($include_dir, $data_dir, $error_file);
15
16if( @ARGV ) {
17 die "Invalid number of arguments" if scalar @ARGV != 3;
18 ($include_dir, $data_dir, $error_file) = @ARGV;
19
20 -d $include_dir or die "No such directory: $include_dir\n";
21 -d $data_dir or die "No such directory: $data_dir\n";
22} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020024 $data_dir = 'scripts/data_files';
25 $error_file = 'library/error.c';
26
27 unless( -d $include_dir && -d $data_dir ) {
28 chdir '..' or die;
29 -d $include_dir && -d $data_dir
30 or die "Without arguments, must be run from root or scripts\n"
31 }
32}
33
Paul Bakker9d781402011-05-09 16:17:09 +000034my $error_format_file = $data_dir.'/error.fmt';
35
TRodziewicz10e8cf52021-05-31 17:58:57 +020036my @low_level_modules = qw( AES ARIA ASN1 BASE64 BIGNUM
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
Raef Coles7dce69a2022-08-24 14:07:06 +010038 ENTROPY ERROR GCM HKDF HMAC_DRBG LMS MD5
Gilles Peskine1fcf7212020-02-26 18:26:46 +010039 NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020040 SHA1 SHA256 SHA512 SHA3 THREADING );
Gilles Peskine92143272018-01-25 23:26:24 +010041my @high_level_modules = qw( CIPHER DHM ECP MD
42 PEM PK PKCS12 PKCS5
Nayna Jainca07f062020-06-12 18:44:04 +000043 RSA SSL X509 PKCS7 );
Paul Bakker9d781402011-05-09 16:17:09 +000044
Paul Bakker9d781402011-05-09 16:17:09 +000045undef $/;
46
Gilles Peskinea4d32732021-04-23 09:44:59 +020047open(FORMAT_FILE, '<:crlf', "$error_format_file") or die "Opening error format file '$error_format_file': $!";
Paul Bakker9d781402011-05-09 16:17:09 +000048my $error_format = <FORMAT_FILE>;
49close(FORMAT_FILE);
50
Thomas Daubney33878ed2023-01-09 18:17:02 +000051my @files = glob qq("$include_dir/*.h");
Gilles Peskine5241f852020-05-25 12:21:22 +020052my @necessary_include_files;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010053my @matches;
54foreach my $file (@files) {
Thomas Daubney33878ed2023-01-09 18:17:02 +000055 open(FILE, '<:crlf', $file) or die("$0: $file: $!");
Gilles Peskine47b09562021-07-26 18:39:53 +020056 my $content = <FILE>;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010057 close FILE;
Gilles Peskine47b09562021-07-26 18:39:53 +020058 my $found = 0;
59 while ($content =~ m[
Gilles Peskine7f8e2772021-07-26 19:30:08 +020060 # Both the before-comment and the after-comment are optional.
61 # Only the comment content is a regex capture group. The comment
62 # start and end parts are outside the capture group.
63 (?:/\*[*!](?!<) # Doxygen before-comment start
64 ((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside)
65 \*/)? # Comment end
66 \s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name
67 \s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign)
68 (?:/\*[*!]< # Doxygen after-comment start
69 ((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside)
70 \*/)? # Comment end
Gilles Peskine47b09562021-07-26 18:39:53 +020071 ]gsx) {
72 my ($before, $name, $value, $after) = ($1, $2, $3, $4);
73 # Discard Doxygen comments that are coincidentally present before
74 # an error definition but not attached to it. This is ad hoc, based
75 # on what actually matters (or mattered at some point).
Gilles Peskine58887ba2021-08-02 22:53:40 +020076 undef $before if defined($before) && $before =~ /\s*\\name\s/s;
Gilles Peskine47b09562021-07-26 18:39:53 +020077 die "Description neither before nor after $name in $file\n"
78 if !defined($before) && !defined($after);
79 die "Description both before and after $name in $file\n"
80 if defined($before) && defined($after);
81 my $description = (defined($before) ? $before : $after);
82 $description =~ s/^\s+//;
Gilles Peskine05aa5432021-07-26 18:45:22 +020083 $description =~ s/\n( *\*)? */ /g;
Gilles Peskine47b09562021-07-26 18:39:53 +020084 $description =~ s/\.?\s+$//;
85 push @matches, [$name, $value, $description];
86 ++$found;
87 }
88 if ($found) {
89 my $include_name = $file;
90 $include_name =~ s!.*/!!;
91 push @necessary_include_files, $include_name;
92 }
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010093}
Paul Bakker9d781402011-05-09 16:17:09 +000094
95my $ll_old_define = "";
96my $hl_old_define = "";
97
98my $ll_code_check = "";
99my $hl_code_check = "";
100
101my $headers = "";
Gilles Peskine5241f852020-05-25 12:21:22 +0200102my %included_headers;
Paul Bakker9d781402011-05-09 16:17:09 +0000103
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000104my %error_codes_seen;
105
Gilles Peskine47b09562021-07-26 18:39:53 +0200106foreach my $match (@matches)
Paul Bakker9d781402011-05-09 16:17:09 +0000107{
Gilles Peskine47b09562021-07-26 18:39:53 +0200108 my ($error_name, $error_code, $description) = @$match;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000109
110 die "Duplicated error code: $error_code ($error_name)\n"
111 if( $error_codes_seen{$error_code}++ );
112
Paul Bakker9d781402011-05-09 16:17:09 +0000113 $description =~ s/\\/\\\\/g;
Paul Bakker9d781402011-05-09 16:17:09 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
Paul Bakker9d781402011-05-09 16:17:09 +0000116
117 # Fix faulty ones
118 $module_name = "BIGNUM" if ($module_name eq "MPI");
Paul Bakker880ac7e2011-11-27 14:50:49 +0000119 $module_name = "CTR_DRBG" if ($module_name eq "CTR");
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +0100120 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
Paul Bakker9d781402011-05-09 16:17:09 +0000121
122 my $define_name = $module_name;
Gilles Peskine1bf45e12020-02-26 18:28:23 +0100123 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
Paul Bakkerdceecd82011-11-15 16:38:34 +0000124 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
Gilles Peskine314bc892020-02-26 18:28:25 +0100125 $define_name = "SSL_TLS" if ($define_name eq "SSL");
Paul Bakkercff68422013-09-15 20:43:33 +0200126 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
Nayna Jainca07f062020-06-12 18:44:04 +0000127 $define_name = "PKCS7" if ($define_name eq "PKCS7");
Paul Bakker9d781402011-05-09 16:17:09 +0000128
129 my $include_name = $module_name;
130 $include_name =~ tr/A-Z/a-z/;
Paul Bakker9d781402011-05-09 16:17:09 +0000131
Gilles Peskine1fcf7212020-02-26 18:26:46 +0100132 # Fix faulty ones
133 $include_name = "net_sockets" if ($module_name eq "NET");
134
Gilles Peskine5241f852020-05-25 12:21:22 +0200135 $included_headers{"${include_name}.h"} = $module_name;
136
Paul Bakker9d781402011-05-09 16:17:09 +0000137 my $found_ll = grep $_ eq $module_name, @low_level_modules;
138 my $found_hl = grep $_ eq $module_name, @high_level_modules;
139 if (!$found_ll && !$found_hl)
140 {
Manuel Pégourié-Gonnard48573f82015-08-06 17:24:56 +0200141 printf("Error: Do not know how to handle: $module_name\n");
Paul Bakker9d781402011-05-09 16:17:09 +0000142 exit 1;
143 }
144
145 my $code_check;
146 my $old_define;
147 my $white_space;
Paul Bakkercff68422013-09-15 20:43:33 +0200148 my $first;
Paul Bakker9d781402011-05-09 16:17:09 +0000149
150 if ($found_ll)
151 {
152 $code_check = \$ll_code_check;
153 $old_define = \$ll_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700154 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000155 }
156 else
157 {
158 $code_check = \$hl_code_check;
159 $old_define = \$hl_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700160 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000161 }
162
163 if ($define_name ne ${$old_define})
164 {
165 if (${$old_define} ne "")
166 {
Paul Bakkercff68422013-09-15 20:43:33 +0200167 ${$code_check} .= "#endif /* ";
168 $first = 0;
169 foreach my $dep (split(/,/, ${$old_define}))
170 {
171 ${$code_check} .= " || " if ($first++);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 ${$code_check} .= "MBEDTLS_${dep}_C";
Paul Bakkercff68422013-09-15 20:43:33 +0200173 }
174 ${$code_check} .= " */\n\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000175 }
176
Paul Bakkercff68422013-09-15 20:43:33 +0200177 ${$code_check} .= "#if ";
178 $headers .= "#if " if ($include_name ne "");
179 $first = 0;
180 foreach my $dep (split(/,/, ${define_name}))
181 {
182 ${$code_check} .= " || " if ($first);
183 $headers .= " || " if ($first++);
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
186 $headers .= "defined(MBEDTLS_${dep}_C)" if
Paul Bakkercff68422013-09-15 20:43:33 +0200187 ($include_name ne "");
188 }
189 ${$code_check} .= "\n";
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000190 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
Paul Bakker9d781402011-05-09 16:17:09 +0000191 "#endif\n\n" if ($include_name ne "");
192 ${$old_define} = $define_name;
193 }
194
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700195 ${$code_check} .= "${white_space}case -($error_name):\n".
Gaurav Aggarwalcabde252020-04-22 08:13:25 -0700196 "${white_space} return( \"$module_name - $description\" );\n"
Paul Bakker9d781402011-05-09 16:17:09 +0000197};
198
199if ($ll_old_define ne "")
200{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200201 $ll_code_check .= "#endif /* ";
202 my $first = 0;
203 foreach my $dep (split(/,/, $ll_old_define))
204 {
205 $ll_code_check .= " || " if ($first++);
206 $ll_code_check .= "MBEDTLS_${dep}_C";
207 }
208 $ll_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000209}
210if ($hl_old_define ne "")
211{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200212 $hl_code_check .= "#endif /* ";
213 my $first = 0;
214 foreach my $dep (split(/,/, $hl_old_define))
215 {
216 $hl_code_check .= " || " if ($first++);
217 $hl_code_check .= "MBEDTLS_${dep}_C";
218 }
219 $hl_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000220}
221
222$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
223$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
224$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
225
226open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
227print ERROR_FILE $error_format;
228close(ERROR_FILE);
Gilles Peskine5241f852020-05-25 12:21:22 +0200229
230my $errors = 0;
231for my $include_name (@necessary_include_files)
232{
233 if (not $included_headers{$include_name})
234 {
235 print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
236 ++$errors;
237 }
238}
239
240exit !!$errors;