2 # Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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
15 my $config = "crypto/err/openssl.ec";
27 my $YEAR = $t[5] + 1900;
32 print STDERR "\n---\n$text\n" if $debug;
38 mkerr.pl [options] [files...]
42 -conf FILE Use the named config file FILE instead of the default.
44 -debug Verbose output debugging on stderr.
46 -internal Generate code that is to be built as part of OpenSSL itself.
47 Also scans internal list of files.
49 -module M Only useful with -internal!
50 Only write files for library module M. Whether files are
51 actually written or not depends on other options, such as
53 Note: this option is cumulative. If not given at all, all
54 internal modules will be considered.
56 -nowrite Do not write the header/source files, even if changed.
58 -rebuild Rebuild all header and C source files, even if there
61 -reindex Ignore previously assigned values (except for R records in
62 the config file) and renumber everything starting at 100.
64 -static Make the load/unload functions static.
66 -unref List all unreferenced function and reason codes on stderr;
69 -help Show this help text.
71 ... Additional arguments are added to the file list to scan,
72 if '-internal' was NOT specified on the command line.
79 last unless $arg =~ /-.*/;
80 $arg = $1 if $arg =~ /-(-.*)/;
81 if ( $arg eq "-conf" ) {
84 } elsif ( $arg eq "-debug" ) {
87 } elsif ( $arg eq "-internal" ) {
89 } elsif ( $arg eq "-nowrite" ) {
91 } elsif ( $arg eq "-rebuild" ) {
93 } elsif ( $arg eq "-reindex" ) {
95 } elsif ( $arg eq "-static" ) {
97 } elsif ( $arg eq "-unref" ) {
100 } elsif ( $arg eq "-module" ) {
102 $modules{uc $ARGV[0]} = 1;
103 } elsif ( $arg =~ /-*h(elp)?/ ) {
106 } elsif ( $arg =~ /-.*/ ) {
107 die "Unknown option $arg; use -h for help.\n";
114 die "Cannot mix -internal and -static\n" if $static;
115 die "Extra parameters given.\n" if @ARGV;
116 @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
117 glob('ssl/*.c'), glob('ssl/*/*.c') );
119 die "-module isn't useful without -internal\n" if scalar keys %modules > 0;
123 # Data parsed out of the config and state files.
124 my %hinc; # lib -> header
125 my %libinc; # header -> lib
126 my %cskip; # error_file -> lib
127 my %errorfile; # lib -> error file name
128 my %fmax; # lib -> max assigned function code
129 my %rmax; # lib -> max assigned reason code
130 my %fassigned; # lib -> colon-separated list of assigned function codes
131 my %rassigned; # lib -> colon-separated list of assigned reason codes
132 my %fnew; # lib -> count of new function codes
133 my %rnew; # lib -> count of new reason codes
134 my %rextra; # "extra" reason code -> lib
135 my %rcodes; # reason-name -> value
136 my %ftrans; # old name -> #define-friendly name (all caps)
137 my %fcodes; # function-name -> value
138 my $statefile; # state file with assigned reason and function codes
139 my %strings; # define -> text
141 # Read and parse the config file
142 open(IN, "$config") || die "Can't open config file $config, $!,";
144 next if /^#/ || /^$/;
145 if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
150 $libinc{$hdr} = $lib;
152 next if $err eq 'NONE';
153 $errorfile{$lib} = $err;
156 $fassigned{$lib} = ":";
157 $rassigned{$lib} = ":";
160 } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
163 } elsif ( /^S\s+(\S+)/ ) {
166 die "Illegal config line $_\n";
171 if ( ! $statefile ) {
172 $statefile = $config;
173 $statefile =~ s/.ec/.txt/;
176 # The statefile has all the previous assignments.
177 &phase("Reading state");
178 my $skippedstate = 0;
179 if ( ! $reindex && $statefile ) {
180 open(STATE, "<$statefile") || die "Can't open $statefile, $!";
182 # Scan function and reason codes and store them: keep a note of the
185 next if /^#/ || /^$/;
188 if ( /^(.+):(\d+):\\$/ ) {
192 $next =~ s/^\s*(.*)\s*$/$1/;
193 die "Duplicate define $name" if exists $strings{$name};
194 $strings{$name} = $next;
195 } elsif ( /^(\S+):(\d+):(.*)$/ ) {
198 die "Duplicate define $name" if exists $strings{$name};
199 $strings{$name} = $3;
201 die "Bad line in $statefile:\n$_\n";
204 $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
205 $lib = "SSL" if $lib =~ /TLS/;
206 if ( !defined $errorfile{$lib} ) {
211 if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
212 die "$lib reason code $code collision at $name\n"
213 if $rassigned{$lib} =~ /:$code:/;
214 $rassigned{$lib} .= "$code:";
215 if ( !exists $rextra{$name} ) {
216 $rmax{$lib} = $code if $code > $rmax{$lib};
218 $rcodes{$name} = $code;
219 } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
220 die "$lib function code $code collision at $name\n"
221 if $fassigned{$lib} =~ /:$code:/;
222 $fassigned{$lib} .= "$code:";
223 $fmax{$lib} = $code if $code > $fmax{$lib};
224 $fcodes{$name} = $code;
226 die "Bad line in $statefile:\n$_\n";
232 foreach my $lib ( sort keys %rmax ) {
233 print STDERR "Reason codes for ${lib}:\n";
234 if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
235 my @rassigned = sort { $a <=> $b } split( ":", $1 );
236 print STDERR " ", join(' ', @rassigned), "\n";
238 print STDERR " --none--\n";
242 foreach my $lib ( sort keys %fmax ) {
243 print STDERR "Function codes for ${lib}:\n";
244 if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
245 my @fassigned = sort { $a <=> $b } split( ":", $1 );
246 print STDERR " ", join(' ', @fassigned), "\n";
248 print STDERR " --none--\n";
254 # Scan each header file and make a list of error codes
256 &phase("Scanning headers");
257 while ( ( my $hdr, my $lib ) = each %libinc ) {
258 next if $hdr eq "NONE";
259 print STDERR " ." if $debug;
265 open(IN, "<$hdr") || die "Can't open $hdr, $!,";
280 if ( not /\*\// ) { # multiline comment...
281 $line = $_; # ... just accumulate
284 s/\/\*.*?\*\///gs; # wipe it
290 $cpp-- if /^#\s*endif/;
293 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
295 next if /^\#/; # skip preprocessor directives
297 s/{[^{}]*}//gs; # ignore {} blocks
299 if ( /\{|\/\*/ ) { # Add a so editor works...
306 # Delete any DECLARE_ macros
308 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
309 foreach ( split /;/, $def ) {
311 # The goal is to collect function names from function declarations.
316 # Skip over recognized non-function declarations
317 next if /typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/;
319 # Remove STACK_OF(foo)
320 s/STACK_OF\(\w+\)/void/;
322 # Reduce argument lists to empty ()
323 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
324 while ( /\(.*\)/s ) {
325 s/\([^\(\)]+\)/\{\}/gs;
326 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
329 # pretend as we didn't use curly braces: {} -> ()
332 # Last token just before the first () is a function name.
333 if ( /(\w+)\s*\(\).*/s ) {
335 $name =~ tr/[a-z]/[A-Z]/;
337 } elsif ( /[\(\)]/ and not(/=/) ) {
338 print STDERR "Header $hdr: cannot parse: $_;\n";
344 if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
345 print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
346 print STDERR "Any new alerts must be added to $config.\n";
351 print STDERR "\n" if $debug;
353 # Scan each C source file and look for function and reason codes
354 # This is done by looking for strings that "look like" function or
355 # reason codes: basically anything consisting of all upper case and
356 # numerics which has _F_ or _R_ in it and which has the name of an
357 # error library at the start. This seems to work fine except for the
358 # oddly named structure BIO_F_CTX which needs to be ignored.
359 # If a code doesn't exist in list compiled from headers then mark it
360 # with the value "X" as a place holder to give it a value later.
361 # Store all function and reason codes found in %usedfuncs and %usedreasons
362 # so all those unreferenced can be printed out.
363 &phase("Scanning source");
366 foreach my $file ( @source ) {
367 # Don't parse the error source file.
368 next if exists $cskip{$file};
369 open( IN, "<$file" ) || die "Can't open $file, $!,";
372 print STDERR "$file:\n" if $debug;
375 # skip obsoleted source files entirely!
376 last if /^#error\s+obsolete/;
378 if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
379 /^([^()]*(\([^()]*\)[^()]*)*)\(/;
380 $1 =~ /([A-Za-z_0-9]*)$/;
384 if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) {
385 next unless exists $errorfile{$2};
386 next if $1 eq "BIO_F_BUFFER_CTX";
388 if ( !exists $fcodes{$1} ) {
389 print STDERR " New function $1\n" if $debug;
393 $ftrans{$3} = $func unless exists $ftrans{$3};
394 if ( uc($func) ne $3 ) {
395 print STDERR "ERROR: mismatch $file:$linenr $func:$3\n";
398 print STDERR " Function $1 = $fcodes{$1}\n"
401 if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
402 next unless exists $errorfile{$2};
403 $usedreasons{$1} = 1;
404 if ( !exists $rcodes{$1} ) {
405 print STDERR " New reason $1\n" if $debug;
409 print STDERR " Reason $1 = $rcodes{$1}\n" if $debug;
414 print STDERR "\n" if $debug;
416 # Now process each library in turn.
417 &phase("Writing files");
419 foreach my $lib ( keys %errorfile ) {
420 next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild;
421 next if scalar keys %modules > 0 && !$modules{$lib};
423 print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
424 print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
427 # If we get here then we have some new error codes so we
428 # need to rebuild the header file and C file.
430 # Make a sorted list of error and reason codes for later use.
431 my @function = sort grep( /^${lib}_/, keys %fcodes );
432 my @reasons = sort grep( /^${lib}_/, keys %rcodes );
434 # indent level for innermost preprocessor lines
437 # Rewrite the header file
439 my $hfile = $hinc{$lib};
440 $hfile =~ s/.h$/err.h/ if $internal;
441 open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
444 * Generated by util/mkerr.pl DO NOT EDIT
445 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
447 * Licensed under the Apache License 2.0 (the \"License\"). You may not use
448 * this file except in compliance with the License. You can obtain a copy
449 * in the file LICENSE in the source distribution or at
450 * https://www.openssl.org/source/license.html
453 #ifndef HEADER_${lib}ERR_H
454 # define HEADER_${lib}ERR_H
456 # ifndef HEADER_SYMHACKS_H
457 # include <openssl/symhacks.h>
462 # Declare the load function because the generate C file
463 # includes "fooerr.h" not "foo.h"
464 if ($lib ne "SSL" && $lib ne "ASYNC"
465 && grep { $lib eq uc $_ } @disablables) {
467 # include <openssl/opensslconf.h>
469 # ifndef OPENSSL_NO_${lib}
475 #${indent}ifdef __cplusplus
478 int ERR_load_${lib}_strings(void);
482 # define ${lib}err(f, r) ERR_${lib}_error((f), (r), OPENSSL_FILE, OPENSSL_LINE)
491 int ERR_load_${lib}_strings(void);
492 void ERR_unload_${lib}_strings(void);
493 void ERR_${lib}_error(int function, int reason, char *file, int line);
501 print OUT "\n/*\n * $lib function codes.\n */\n";
502 foreach my $i ( @function ) {
503 my $z = 48 - length($i);
505 if ( $fcodes{$i} eq "X" ) {
506 $fassigned{$lib} =~ m/^:([^:]*):/;
508 $findcode = $fmax{$lib} if !defined $findcode;
509 while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
512 $fcodes{$i} = $findcode;
513 $fassigned{$lib} .= "$findcode:";
514 print STDERR "New Function code $i\n" if $debug;
516 printf OUT "#${indent}define $i%s $fcodes{$i}\n", " " x $z;
519 print OUT "\n/*\n * $lib reason codes.\n */\n";
520 foreach my $i ( @reasons ) {
521 my $z = 48 - length($i);
523 if ( $rcodes{$i} eq "X" ) {
524 $rassigned{$lib} =~ m/^:([^:]*):/;
526 $findcode = $rmax{$lib} if !defined $findcode;
527 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
530 $rcodes{$i} = $findcode;
531 $rassigned{$lib} .= "$findcode:";
532 print STDERR "New Reason code $i\n" if $debug;
534 printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
538 while (length($indent) > 0) {
539 $indent = substr $indent, 0, -1;
540 print OUT "#${indent}endif\n";
543 # Rewrite the C source file containing the error details.
545 # First, read any existing reason string definitions:
546 my $cfile = $errorfile{$lib};
547 my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
549 $hincf =~ s|.*include/||;
550 if ( $hincf =~ m|^openssl/| ) {
551 $hincf = "<${hincf}>";
553 $hincf = "\"${hincf}\"";
556 open( OUT, ">$cfile" )
557 || die "Can't open $cfile for writing, $!, stopped";
559 my $const = $internal ? 'const ' : '';
563 * Generated by util/mkerr.pl DO NOT EDIT
564 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
566 * Licensed under the Apache License 2.0 (the "License"). You may not use
567 * this file except in compliance with the License. You can obtain a copy
568 * in the file LICENSE in the source distribution or at
569 * https://www.openssl.org/source/license.html
572 #include <openssl/err.h>
575 #ifndef OPENSSL_NO_ERR
577 static ${const}ERR_STRING_DATA ${lib}_str_functs[] = {
580 # Add each function code: if a function name is found then use it.
581 foreach my $i ( @function ) {
583 if ( exists $strings{$i} and $strings{$i} ne '' ) {
585 $fn = "" if $fn eq '*';
587 $i =~ /^${lib}_F_(\S+)$/;
589 $fn = $ftrans{$fn} if exists $ftrans{$fn};
592 my $short = " {ERR_PACK($pack_lib, $i, 0), \"$fn\"},";
593 if ( length($short) <= 80 ) {
594 print OUT "$short\n";
596 print OUT " {ERR_PACK($pack_lib, $i, 0),\n \"$fn\"},\n";
603 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
606 # Add each reason code.
607 foreach my $i ( @reasons ) {
609 if ( exists $strings{$i} ) {
611 $rn = "" if $rn eq '*';
613 $i =~ /^${lib}_R_(\S+)$/;
615 $rn =~ tr/_[A-Z]/ [a-z]/;
618 my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
619 if ( length($short) <= 80 ) {
620 print OUT "$short\n";
622 print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
634 int ERR_load_${lib}_strings(void)
636 #ifndef OPENSSL_NO_ERR
637 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
638 ERR_load_strings_const(${lib}_str_functs);
639 ERR_load_strings_const(${lib}_str_reasons);
646 my $st = $static ? "static " : "";
649 static int lib_code = 0;
650 static int error_loaded = 0;
652 ${st}int ERR_load_${lib}_strings(void)
655 lib_code = ERR_get_next_error_library();
658 #ifndef OPENSSL_NO_ERR
659 ERR_load_strings(lib_code, ${lib}_str_functs);
660 ERR_load_strings(lib_code, ${lib}_str_reasons);
667 ${st}void ERR_unload_${lib}_strings(void)
670 #ifndef OPENSSL_NO_ERR
671 ERR_unload_strings(lib_code, ${lib}_str_functs);
672 ERR_unload_strings(lib_code, ${lib}_str_reasons);
678 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
681 lib_code = ERR_get_next_error_library();
682 ERR_PUT_error(lib_code, function, reason, file, line);
692 # Make a list of unreferenced function and reason codes
695 foreach ( keys %fcodes ) {
696 push( @funref, $_ ) unless exists $usedfuncs{$_};
699 foreach ( keys %rcodes ) {
700 push( @runref, $_ ) unless exists $usedreasons{$_};
703 print STDERR "The following function codes were not referenced:\n";
704 foreach ( sort @funref ) {
705 print STDERR " $_\n";
709 print STDERR "The following reason codes were not referenced:\n";
710 foreach ( sort @runref ) {
711 print STDERR " $_\n";
716 die "Found $errors errors, quitting" if $errors;
718 # Update the state file
720 open(OUT, ">$statefile.new")
721 || die "Can't write $statefile.new, $!";
723 # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
725 # Licensed under the Apache License 2.0 (the "License"). You may not use
726 # this file except in compliance with the License. You can obtain a copy
727 # in the file LICENSE in the source distribution or at
728 # https://www.openssl.org/source/license.html
730 print OUT "\n# Function codes\n";
731 foreach my $i ( sort keys %fcodes ) {
732 my $short = "$i:$fcodes{$i}:";
733 my $t = exists $strings{$i} ? $strings{$i} : "";
734 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
735 print OUT "$short$t\n";
737 print OUT "\n#Reason codes\n";
738 foreach my $i ( sort keys %rcodes ) {
739 my $short = "$i:$rcodes{$i}:";
740 my $t = exists $strings{$i} ? "$strings{$i}" : "";
741 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
742 print OUT "$short$t\n" if !exists $rextra{$i};
745 if ( $skippedstate ) {
746 print "Skipped state, leaving update in $statefile.new";
748 rename "$statefile", "$statefile.old"
749 || die "Can't backup $statefile to $statefile.old, $!";
750 rename "$statefile.new", "$statefile"
751 || die "Can't rename $statefile to $statefile.new, $!";