2 # Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
12 my $config = "crypto/err/openssl.ec";
23 my $YEAR = $t[5] + 1900;
28 print STDERR "\n---\n$text\n" if $debug;
34 mkerr.pl [options] [files...]
38 -conf FILE Use the named config file FILE instead of the default.
40 -debug Verbose output debugging on stderr.
42 -internal Generate code that is to be built as part of OpenSSL itself.
43 Also scans internal list of files.
45 -nowrite Do not write the header/source files, even if changed.
47 -rebuild Rebuild all header and C source files, even if there
50 -reindex Ignore previously assigned values (except for R records in
51 the config file) and renumber everything starting at 100.
53 -static Make the load/unload functions static.
55 -unref List all unreferenced function and reason codes on stderr;
58 -help Show this help text.
60 ... Additional arguments are added to the file list to scan,
61 if '-internal' was NOT specified on the command line.
68 last unless $arg =~ /-.*/;
69 $arg = $1 if $arg =~ /-(-.*)/;
70 if ( $arg eq "-conf" ) {
73 } elsif ( $arg eq "-debug" ) {
76 } elsif ( $arg eq "-internal" ) {
78 } elsif ( $arg eq "-nowrite" ) {
80 } elsif ( $arg eq "-rebuild" ) {
82 } elsif ( $arg eq "-reindex" ) {
84 } elsif ( $arg eq "-static" ) {
86 } elsif ( $arg eq "-unref" ) {
89 } elsif ( $arg =~ /-*h(elp)?/ ) {
92 } elsif ( $arg =~ /-.*/ ) {
93 die "Unknown option $arg; use -h for help.\n";
100 die "Cannot mix -internal and -static\n" if $static;
101 die "Extra parameters given.\n" if @ARGV;
102 @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
103 glob('ssl/*.c'), glob('ssl/*/*.c') );
108 # Data parsed out of the config and state files.
109 my %hinc; # lib -> header
110 my %libinc; # header -> lib
111 my %cskip; # error_file -> lib
112 my %errorfile; # lib -> error file name
113 my %fmax; # lib -> max assigned function code
114 my %rmax; # lib -> max assigned reason code
115 my %fassigned; # lib -> colon-separated list of assigned function codes
116 my %rassigned; # lib -> colon-separated list of assigned reason codes
117 my %fnew; # lib -> count of new function codes
118 my %rnew; # lib -> count of new reason codes
119 my %rextra; # "extra" reason code -> lib
120 my %rcodes; # reason-name -> value
121 my %ftrans; # old name -> #define-friendly name (all caps)
122 my %fcodes; # function-name -> value
123 my $statefile; # state file with assigned reason and function codes
125 # Read and parse the config file
126 open(IN, "$config") || die "Can't open config file $config, $!,";
128 next if /^#/ || /^$/;
129 if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
134 $libinc{$hdr} = $lib;
136 next if $err eq 'NONE';
137 $errorfile{$lib} = $err;
140 $fassigned{$lib} = ":";
141 $rassigned{$lib} = ":";
144 } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
147 } elsif ( /^S\s+(\S+)/ ) {
150 die "Illegal config line $_\n";
155 if ( ! $statefile ) {
156 $statefile = $config;
157 $statefile =~ s/.ec/.txt/;
160 # The statefile has all the previous assignments.
161 &phase("Reading state");
162 my $skippedstate = 0;
163 if ( ! $reindex && $statefile ) {
164 open(STATE, "<$statefile") || die "Can't open $statefile, $!";
166 # Scan function and reason codes and store them: keep a note of the
169 next if /^#/ || /^$/;
170 die "Bad line in $statefile:\n$_\n" unless /(\S+)\s+(\d+)/;
175 $lib = "SSL" if $lib =~ /TLS/;
176 if ( !defined $errorfile{$lib} ) {
181 if ( $name =~ /^[A-Z0-9]+_R_/ ) {
182 die "$lib reason code $code collision at $name\n"
183 if $rassigned{$lib} =~ /:$code:/;
184 $rassigned{$lib} .= "$code:";
185 if ( !exists $rextra{$name} ) {
186 $rmax{$lib} = $code if $code > $rmax{$lib};
188 $rcodes{$name} = $code;
189 } elsif ( $name =~ /^[A-Z0-9]+_F_/ ) {
190 die "$lib function code $code collision at $name\n"
191 if $fassigned{$lib} =~ /:$code:/;
192 $fassigned{$lib} .= "$code:";
193 $fmax{$lib} = $code if $code > $fmax{$lib};
194 $fcodes{$name} = $code;
196 die "Bad line in $statefile:\n$_\n";
202 foreach my $lib ( sort keys %rmax ) {
203 print STDERR "Reason codes for ${lib}:\n";
204 if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
205 my @rassigned = sort { $a <=> $b } split( ":", $1 );
206 print STDERR " ", join(' ', @rassigned), "\n";
208 print STDERR " --none--\n";
212 foreach my $lib ( sort keys %fmax ) {
213 print STDERR "Function codes for ${lib}:\n";
214 if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
215 my @fassigned = sort { $a <=> $b } split( ":", $1 );
216 print STDERR " ", join(' ', @fassigned), "\n";
218 print STDERR " --none--\n";
224 # Scan each header file and make a list of error codes
226 &phase("Scanning headers");
227 while ( ( my $hdr, my $lib ) = each %libinc ) {
228 next if $hdr eq "NONE";
229 print STDERR " ." if $debug;
235 open(IN, "<$hdr") || die "Can't open $hdr, $!,";
250 if ( not /\*\// ) { # multiline comment...
251 $line = $_; # ... just accumulate
254 s/\/\*.*?\*\///gs; # wipe it
260 $cpp-- if /^#\s*endif/;
263 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
265 next if /^\#/; # skip preprocessor directives
267 s/{[^{}]*}//gs; # ignore {} blocks
269 if ( /\{|\/\*/ ) { # Add a so editor works...
276 # Delete any DECLARE_ macros
278 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
279 foreach ( split /;/, $def ) {
281 # The goal is to collect function names from function declarations.
286 # Skip over recognized non-function declarations
287 next if /typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/;
289 # Remove STACK_OF(foo)
290 s/STACK_OF\(\w+\)/void/;
292 # Reduce argument lists to empty ()
293 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
294 while ( /\(.*\)/s ) {
295 s/\([^\(\)]+\)/\{\}/gs;
296 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
299 # pretend as we didn't use curly braces: {} -> ()
302 if ( /(\w+)\s*\(\).*/s ) { # first token prior [first] () is
303 my $name = $1; # a function name!
304 $name =~ tr/[a-z]/[A-Z]/;
306 } elsif ( /[\(\)]/ and not(/=/) ) {
307 print STDERR "Header $hdr: cannot parse: $_;\n";
313 if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
314 print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
315 print STDERR "Any new alerts must be added to $config.\n";
320 print STDERR "\n" if $debug;
322 # Scan each C source file and look for function and reason codes
323 # This is done by looking for strings that "look like" function or
324 # reason codes: basically anything consisting of all upper case and
325 # numerics which has _F_ or _R_ in it and which has the name of an
326 # error library at the start. This seems to work fine except for the
327 # oddly named structure BIO_F_CTX which needs to be ignored.
328 # If a code doesn't exist in list compiled from headers then mark it
329 # with the value "X" as a place holder to give it a value later.
330 # Store all function and reason codes found in %usedfuncs and %usedreasons
331 # so all those unreferenced can be printed out.
332 &phase("Scanning source");
335 foreach my $file ( @source ) {
336 # Don't parse the error source file.
337 next if exists $cskip{$file};
338 open( IN, "<$file" ) || die "Can't open $file, $!,";
341 print STDERR "$file:\n" if $debug;
344 # skip obsoleted source files entirely!
345 last if /^#error\s+obsolete/;
347 if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
348 /^([^()]*(\([^()]*\)[^()]*)*)\(/;
349 $1 =~ /([A-Za-z_0-9]*)$/;
353 if ( /(([A-Z0-9]+)_F_([A-Z0-9_]+))/ ) {
354 next unless exists $errorfile{$2};
355 next if $1 eq "BIO_F_BUFFER_CTX";
357 if ( !exists $fcodes{$1} ) {
358 print STDERR " New function $1\n" if $debug;
362 $ftrans{$3} = $func unless exists $ftrans{$3};
363 if ( uc($func) ne $3 ) {
364 print STDERR "ERROR: mismatch $file:$linenr $func:$3\n";
367 print STDERR " Function $1 = $fcodes{$1}\n"
370 if ( /(([A-Z0-9]+)_R_[A-Z0-9_]+)/ ) {
371 next unless exists $errorfile{$2};
372 $usedreasons{$1} = 1;
373 if ( !exists $rcodes{$1} ) {
374 print STDERR " New reason $1\n" if $debug;
378 print STDERR " Reason $1 = $rcodes{$1}\n" if $debug;
383 print STDERR "\n" if $debug;
385 # Now process each library in turn.
386 &phase("Writing files");
388 foreach my $lib ( keys %errorfile ) {
389 if ( ! $fnew{$lib} && ! $rnew{$lib} ) {
390 next unless $rebuild;
393 print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
394 print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
397 # If we get here then we have some new error codes so we
398 # need to rebuild the header file and C file.
400 # Make a sorted list of error and reason codes for later use.
401 my @function = sort grep( /^${lib}_/, keys %fcodes );
402 my @reasons = sort grep( /^${lib}_/, keys %rcodes );
404 # Rewrite the header file
406 my $hfile = $hinc{$lib};
407 $hfile =~ s/.h$/err.h/ if $internal;
408 open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
411 * Generated by util/mkerr.pl DO NOT EDIT
412 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
414 * Licensed under the OpenSSL license (the \"License\"). You may not use
415 * this file except in compliance with the License. You can obtain a copy
416 * in the file LICENSE in the source distribution or at
417 * https://www.openssl.org/source/license.html
420 #ifndef HEADER_${lib}ERR_H
421 # define HEADER_${lib}ERR_H
425 # Declare the load function because the generate C file
426 # includes "fooerr.h" not "foo.h"
431 int ERR_load_${lib}_strings(void);
438 # define ${lib}err(f, r) ERR_${lib}_error((f), (r), OPENSSL_FILE, OPENSSL_LINE)
447 int ERR_load_${lib}_strings(void);
448 void ERR_unload_${lib}_strings(void);
449 void ERR_${lib}_error(int function, int reason, char *file, int line);
457 print OUT "\n/*\n * $lib function codes.\n */\n";
458 foreach my $i ( @function ) {
459 my $z = 48 - length($i);
460 if ( $fcodes{$i} eq "X" ) {
461 $fassigned{$lib} =~ m/^:([^:]*):/;
463 $findcode = $fmax{$lib} if !defined $findcode;
464 while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
467 $fcodes{$i} = $findcode;
468 $fassigned{$lib} .= "$findcode:";
469 print STDERR "New Function code $i\n" if $debug;
471 printf OUT "# define $i%s $fcodes{$i}\n", " " x $z;
474 print OUT "\n/*\n * $lib reason codes.\n */\n";
475 foreach my $i ( @reasons ) {
476 my $z = 48 - length($i);
477 if ( $rcodes{$i} eq "X" ) {
478 $rassigned{$lib} =~ m/^:([^:]*):/;
480 $findcode = $rmax{$lib} if !defined $findcode;
481 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
484 $rcodes{$i} = $findcode;
485 $rassigned{$lib} .= "$findcode:";
486 print STDERR "New Reason code $i\n" if $debug;
488 printf OUT "# define $i%s $rcodes{$i}\n", " " x $z;
492 print OUT "#endif\n";
494 # Rewrite the C source file containing the error details.
496 # First, read any existing reason string definitions:
497 my %err_reason_strings;
498 my $cfile = $errorfile{$lib};
499 if ( open( IN, "<$cfile" ) ) {
502 s|\R$||; # Better chomp
505 if ( /{ERR_(PACK|FUNC|REASON)\(/ ) {
506 if ( /\b(${lib}_R_\w*)\b.*\"(.*)\"/ ) {
507 $err_reason_strings{$1} = $2;
508 } elsif ( /\b${lib}_F_(\w*)\b.*\"(.*)\"/ ) {
509 if ( !exists $ftrans{$1} && $1 ne $2 ) {
510 # Don't print warning, too noisy. :(
511 # print STDERR "WARNING: Mismatched/unused function $2\n";
522 my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
524 $hincf =~ s|.*include/||;
525 if ( $hincf =~ m|^openssl/| ) {
526 $hincf = "<${hincf}>";
528 $hincf = "\"${hincf}\"";
531 open( OUT, ">$cfile" )
532 || die "Can't open $cfile for writing, $!, stopped";
534 my $const = $internal ? 'const ' : '';
538 * Generated by util/mkerr.pl DO NOT EDIT
539 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
541 * Licensed under the OpenSSL license (the "License"). You may not use
542 * this file except in compliance with the License. You can obtain a copy
543 * in the file LICENSE in the source distribution or at
544 * https://www.openssl.org/source/license.html
547 #include <openssl/err.h>
550 #ifndef OPENSSL_NO_ERR
552 static ${const}ERR_STRING_DATA ${lib}_str_functs[] = {
555 # Add each function code: if a function name is found then use it.
556 foreach my $i ( @function ) {
558 $i =~ /^${lib}_F_(\S+)$/;
560 $fn = $ftrans{$fn} if exists $ftrans{$fn};
561 my $short = " {ERR_PACK($pack_lib, $i, 0), \"$fn\"},";
562 if ( length($short) <= 80 ) {
563 print OUT "$short\n";
565 print OUT " {ERR_PACK($pack_lib, $i, 0),\n \"$fn\"},\n";
572 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
575 # Add each reason code.
576 foreach my $i ( @reasons ) {
578 if ( exists $err_reason_strings{$i} ) {
579 $rn = $err_reason_strings{$i};
581 $i =~ /^${lib}_R_(\S+)$/;
583 $rn =~ tr/_[A-Z]/ [a-z]/;
585 my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
586 if ( length($short) <= 80 ) {
587 print OUT "$short\n";
589 print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
601 int ERR_load_${lib}_strings(void)
603 #ifndef OPENSSL_NO_ERR
604 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
605 ERR_load_strings_const(${lib}_str_functs);
606 ERR_load_strings_const(${lib}_str_reasons);
613 my $st = $static ? "static " : "";
616 static int lib_code = 0;
617 static int error_loaded = 0;
619 ${st}int ERR_load_${lib}_strings(void)
622 lib_code = ERR_get_next_error_library();
625 #ifndef OPENSSL_NO_ERR
626 ERR_load_strings(lib_code, ${lib}_str_functs);
627 ERR_load_strings(lib_code, ${lib}_str_reasons);
634 ${st}void ERR_unload_${lib}_strings(void)
637 #ifndef OPENSSL_NO_ERR
638 ERR_unload_strings(lib_code, ${lib}_str_functs);
639 ERR_unload_strings(lib_code, ${lib}_str_reasons);
645 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
648 lib_code = ERR_get_next_error_library();
649 ERR_PUT_error(lib_code, function, reason, file, line);
656 undef %err_reason_strings;
660 # Make a list of unreferenced function and reason codes
663 foreach ( keys %fcodes ) {
664 push( @funref, $_ ) unless exists $usedfuncs{$_};
667 foreach ( keys %rcodes ) {
668 push( @runref, $_ ) unless exists $usedreasons{$_};
671 print STDERR "The following function codes were not referenced:\n";
672 foreach ( sort @funref ) {
673 print STDERR " $_\n";
677 print STDERR "The following reason codes were not referenced:\n";
678 foreach ( sort @runref ) {
679 print STDERR " $_\n";
684 die "Found $errors errors, quitting" if $errors;
686 # Update the state file
688 open(OUT, ">$statefile.new")
689 || die "Can't write $statefile.new, $!";
690 foreach my $i ( sort keys %fcodes ) {
691 print OUT "$i $fcodes{$i}\n";
693 foreach my $i ( sort keys %rcodes ) {
694 print OUT "$i $rcodes{$i}\n" if !exists $rextra{$i};
697 if ( $skippedstate ) {
698 print "Skipped state, leaving update in $statefile.new";
700 rename "$statefile", "$statefile.old"
701 || die "Can't backup $statefile to $statefile.old, $!";
702 rename "$statefile.new", "$statefile"
703 || die "Can't rename $statefile to $statefile.new, $!";