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
124 my %strings; # define -> text
126 # Read and parse the config file
127 open(IN, "$config") || die "Can't open config file $config, $!,";
129 next if /^#/ || /^$/;
130 if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
135 $libinc{$hdr} = $lib;
137 next if $err eq 'NONE';
138 $errorfile{$lib} = $err;
141 $fassigned{$lib} = ":";
142 $rassigned{$lib} = ":";
145 } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
148 } elsif ( /^S\s+(\S+)/ ) {
151 die "Illegal config line $_\n";
156 my $statefile_prolog = '';
157 if ( ! $statefile ) {
158 $statefile = $config;
159 $statefile =~ s/.ec/.txt/;
162 # The statefile has all the previous assignments.
163 &phase("Reading state");
164 my $skippedstate = 0;
165 if ( ! $reindex && $statefile ) {
166 open(STATE, "<$statefile") || die "Can't open $statefile, $!";
168 # Scan function and reason codes and store them: keep a note of the
172 $statefile_prolog .= $_ if $collecting && ( /^#/ || /^$/ );
173 next if /^#/ || /^$/;
176 if ( /^(.+):(\d+):\\$/ ) {
180 $next =~ s/^\s*(.*)\s*$/$1/;
181 die "Duplicate define $name" if exists $strings{$name};
182 $strings{$name} = $next;
183 } elsif ( /^(\S+):(\d+):(.*)$/ ) {
186 die "Duplicate define $name" if exists $strings{$name};
187 $strings{$name} = $3;
189 die "Bad line in $statefile:\n$_\n";
194 $lib = "SSL" if $lib =~ /TLS/;
195 if ( !defined $errorfile{$lib} ) {
200 if ( $name =~ /^[A-Z0-9]+_R_/ ) {
201 die "$lib reason code $code collision at $name\n"
202 if $rassigned{$lib} =~ /:$code:/;
203 $rassigned{$lib} .= "$code:";
204 if ( !exists $rextra{$name} ) {
205 $rmax{$lib} = $code if $code > $rmax{$lib};
207 $rcodes{$name} = $code;
208 } elsif ( $name =~ /^[A-Z0-9]+_F_/ ) {
209 die "$lib function code $code collision at $name\n"
210 if $fassigned{$lib} =~ /:$code:/;
211 $fassigned{$lib} .= "$code:";
212 $fmax{$lib} = $code if $code > $fmax{$lib};
213 $fcodes{$name} = $code;
215 die "Bad line in $statefile:\n$_\n";
221 foreach my $lib ( sort keys %rmax ) {
222 print STDERR "Reason codes for ${lib}:\n";
223 if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
224 my @rassigned = sort { $a <=> $b } split( ":", $1 );
225 print STDERR " ", join(' ', @rassigned), "\n";
227 print STDERR " --none--\n";
231 foreach my $lib ( sort keys %fmax ) {
232 print STDERR "Function codes for ${lib}:\n";
233 if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
234 my @fassigned = sort { $a <=> $b } split( ":", $1 );
235 print STDERR " ", join(' ', @fassigned), "\n";
237 print STDERR " --none--\n";
243 # Scan each header file and make a list of error codes
245 &phase("Scanning headers");
246 while ( ( my $hdr, my $lib ) = each %libinc ) {
247 next if $hdr eq "NONE";
248 print STDERR " ." if $debug;
254 open(IN, "<$hdr") || die "Can't open $hdr, $!,";
269 if ( not /\*\// ) { # multiline comment...
270 $line = $_; # ... just accumulate
273 s/\/\*.*?\*\///gs; # wipe it
279 $cpp-- if /^#\s*endif/;
282 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
284 next if /^\#/; # skip preprocessor directives
286 s/{[^{}]*}//gs; # ignore {} blocks
288 if ( /\{|\/\*/ ) { # Add a so editor works...
295 # Delete any DECLARE_ macros
297 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
298 foreach ( split /;/, $def ) {
300 # The goal is to collect function names from function declarations.
305 # Skip over recognized non-function declarations
306 next if /typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/;
308 # Remove STACK_OF(foo)
309 s/STACK_OF\(\w+\)/void/;
311 # Reduce argument lists to empty ()
312 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
313 while ( /\(.*\)/s ) {
314 s/\([^\(\)]+\)/\{\}/gs;
315 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
318 # pretend as we didn't use curly braces: {} -> ()
321 # Last token just before the first () is a function name.
322 if ( /(\w+)\s*\(\).*/s ) {
324 $name =~ tr/[a-z]/[A-Z]/;
326 } elsif ( /[\(\)]/ and not(/=/) ) {
327 print STDERR "Header $hdr: cannot parse: $_;\n";
333 if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
334 print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
335 print STDERR "Any new alerts must be added to $config.\n";
340 print STDERR "\n" if $debug;
342 # Scan each C source file and look for function and reason codes
343 # This is done by looking for strings that "look like" function or
344 # reason codes: basically anything consisting of all upper case and
345 # numerics which has _F_ or _R_ in it and which has the name of an
346 # error library at the start. This seems to work fine except for the
347 # oddly named structure BIO_F_CTX which needs to be ignored.
348 # If a code doesn't exist in list compiled from headers then mark it
349 # with the value "X" as a place holder to give it a value later.
350 # Store all function and reason codes found in %usedfuncs and %usedreasons
351 # so all those unreferenced can be printed out.
352 &phase("Scanning source");
355 foreach my $file ( @source ) {
356 # Don't parse the error source file.
357 next if exists $cskip{$file};
358 open( IN, "<$file" ) || die "Can't open $file, $!,";
361 print STDERR "$file:\n" if $debug;
364 # skip obsoleted source files entirely!
365 last if /^#error\s+obsolete/;
367 if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
368 /^([^()]*(\([^()]*\)[^()]*)*)\(/;
369 $1 =~ /([A-Za-z_0-9]*)$/;
373 if ( /(([A-Z0-9]+)_F_([A-Z0-9_]+))/ ) {
374 next unless exists $errorfile{$2};
375 next if $1 eq "BIO_F_BUFFER_CTX";
377 if ( !exists $fcodes{$1} ) {
378 print STDERR " New function $1\n" if $debug;
382 $ftrans{$3} = $func unless exists $ftrans{$3};
383 if ( uc($func) ne $3 ) {
384 print STDERR "ERROR: mismatch $file:$linenr $func:$3\n";
387 print STDERR " Function $1 = $fcodes{$1}\n"
390 if ( /(([A-Z0-9]+)_R_[A-Z0-9_]+)/ ) {
391 next unless exists $errorfile{$2};
392 $usedreasons{$1} = 1;
393 if ( !exists $rcodes{$1} ) {
394 print STDERR " New reason $1\n" if $debug;
398 print STDERR " Reason $1 = $rcodes{$1}\n" if $debug;
403 print STDERR "\n" if $debug;
405 # Now process each library in turn.
406 &phase("Writing files");
408 foreach my $lib ( keys %errorfile ) {
409 if ( ! $fnew{$lib} && ! $rnew{$lib} ) {
410 next unless $rebuild;
413 print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
414 print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
417 # If we get here then we have some new error codes so we
418 # need to rebuild the header file and C file.
420 # Make a sorted list of error and reason codes for later use.
421 my @function = sort grep( /^${lib}_/, keys %fcodes );
422 my @reasons = sort grep( /^${lib}_/, keys %rcodes );
424 # Rewrite the header file
426 my $hfile = $hinc{$lib};
427 $hfile =~ s/.h$/err.h/ if $internal;
428 open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
431 * Generated by util/mkerr.pl DO NOT EDIT
432 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
434 * Licensed under the OpenSSL license (the \"License\"). You may not use
435 * this file except in compliance with the License. You can obtain a copy
436 * in the file LICENSE in the source distribution or at
437 * https://www.openssl.org/source/license.html
440 #ifndef HEADER_${lib}ERR_H
441 # define HEADER_${lib}ERR_H
445 # Declare the load function because the generate C file
446 # includes "fooerr.h" not "foo.h"
451 int ERR_load_${lib}_strings(void);
458 # define ${lib}err(f, r) ERR_${lib}_error((f), (r), OPENSSL_FILE, OPENSSL_LINE)
467 int ERR_load_${lib}_strings(void);
468 void ERR_unload_${lib}_strings(void);
469 void ERR_${lib}_error(int function, int reason, char *file, int line);
477 print OUT "\n/*\n * $lib function codes.\n */\n";
478 foreach my $i ( @function ) {
479 my $z = 48 - length($i);
480 if ( $fcodes{$i} eq "X" ) {
481 $fassigned{$lib} =~ m/^:([^:]*):/;
483 $findcode = $fmax{$lib} if !defined $findcode;
484 while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
487 $fcodes{$i} = $findcode;
488 $fassigned{$lib} .= "$findcode:";
489 print STDERR "New Function code $i\n" if $debug;
491 printf OUT "# define $i%s $fcodes{$i}\n", " " x $z;
494 print OUT "\n/*\n * $lib reason codes.\n */\n";
495 foreach my $i ( @reasons ) {
496 my $z = 48 - length($i);
497 if ( $rcodes{$i} eq "X" ) {
498 $rassigned{$lib} =~ m/^:([^:]*):/;
500 $findcode = $rmax{$lib} if !defined $findcode;
501 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
504 $rcodes{$i} = $findcode;
505 $rassigned{$lib} .= "$findcode:";
506 print STDERR "New Reason code $i\n" if $debug;
508 printf OUT "# define $i%s $rcodes{$i}\n", " " x $z;
512 print OUT "#endif\n";
514 # Rewrite the C source file containing the error details.
516 # First, read any existing reason string definitions:
517 my $cfile = $errorfile{$lib};
518 my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
520 $hincf =~ s|.*include/||;
521 if ( $hincf =~ m|^openssl/| ) {
522 $hincf = "<${hincf}>";
524 $hincf = "\"${hincf}\"";
527 open( OUT, ">$cfile" )
528 || die "Can't open $cfile for writing, $!, stopped";
530 my $const = $internal ? 'const ' : '';
534 * Generated by util/mkerr.pl DO NOT EDIT
535 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
537 * Licensed under the OpenSSL license (the "License"). You may not use
538 * this file except in compliance with the License. You can obtain a copy
539 * in the file LICENSE in the source distribution or at
540 * https://www.openssl.org/source/license.html
543 #include <openssl/err.h>
546 #ifndef OPENSSL_NO_ERR
548 static ${const}ERR_STRING_DATA ${lib}_str_functs[] = {
551 # Add each function code: if a function name is found then use it.
552 foreach my $i ( @function ) {
554 if ( exists $strings{$i} and $strings{$i} ne '' ) {
556 $fn = "" if $fn eq '*';
558 $i =~ /^${lib}_F_(\S+)$/;
560 $fn = $ftrans{$fn} if exists $ftrans{$fn};
563 my $short = " {ERR_PACK($pack_lib, $i, 0), \"$fn\"},";
564 if ( length($short) <= 80 ) {
565 print OUT "$short\n";
567 print OUT " {ERR_PACK($pack_lib, $i, 0),\n \"$fn\"},\n";
574 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
577 # Add each reason code.
578 foreach my $i ( @reasons ) {
580 if ( exists $strings{$i} ) {
582 $rn = "" if $rn eq '*';
584 $i =~ /^${lib}_R_(\S+)$/;
586 $rn =~ tr/_[A-Z]/ [a-z]/;
589 my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
590 if ( length($short) <= 80 ) {
591 print OUT "$short\n";
593 print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
605 int ERR_load_${lib}_strings(void)
607 #ifndef OPENSSL_NO_ERR
608 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
609 ERR_load_strings_const(${lib}_str_functs);
610 ERR_load_strings_const(${lib}_str_reasons);
617 my $st = $static ? "static " : "";
620 static int lib_code = 0;
621 static int error_loaded = 0;
623 ${st}int ERR_load_${lib}_strings(void)
626 lib_code = ERR_get_next_error_library();
629 #ifndef OPENSSL_NO_ERR
630 ERR_load_strings(lib_code, ${lib}_str_functs);
631 ERR_load_strings(lib_code, ${lib}_str_reasons);
638 ${st}void ERR_unload_${lib}_strings(void)
641 #ifndef OPENSSL_NO_ERR
642 ERR_unload_strings(lib_code, ${lib}_str_functs);
643 ERR_unload_strings(lib_code, ${lib}_str_reasons);
649 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
652 lib_code = ERR_get_next_error_library();
653 ERR_PUT_error(lib_code, function, reason, file, line);
663 # Make a list of unreferenced function and reason codes
666 foreach ( keys %fcodes ) {
667 push( @funref, $_ ) unless exists $usedfuncs{$_};
670 foreach ( keys %rcodes ) {
671 push( @runref, $_ ) unless exists $usedreasons{$_};
674 print STDERR "The following function codes were not referenced:\n";
675 foreach ( sort @funref ) {
676 print STDERR " $_\n";
680 print STDERR "The following reason codes were not referenced:\n";
681 foreach ( sort @runref ) {
682 print STDERR " $_\n";
687 die "Found $errors errors, quitting" if $errors;
689 # Update the state file
691 open(OUT, ">$statefile.new")
692 || die "Can't write $statefile.new, $!";
693 print OUT $statefile_prolog;
694 print OUT "# Function codes\n";
695 foreach my $i ( sort keys %fcodes ) {
696 my $short = "$i:$fcodes{$i}:";
697 my $t = exists $strings{$i} ? $strings{$i} : "";
698 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
699 print OUT "$short$t\n";
701 print OUT "\n#Reason codes\n";
702 foreach my $i ( sort keys %rcodes ) {
703 my $short = "$i:$rcodes{$i}:";
704 my $t = exists $strings{$i} ? "$strings{$i}" : "";
705 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
706 print OUT "$short$t\n" if !exists $rextra{$i};
709 if ( $skippedstate ) {
710 print "Skipped state, leaving update in $statefile.new";
712 rename "$statefile", "$statefile.old"
713 || die "Can't backup $statefile to $statefile.old, $!";
714 rename "$statefile.new", "$statefile"
715 || die "Can't rename $statefile to $statefile.new, $!";