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'), glob('providers/*.c'),
118 glob('providers/*/*.c'), glob('providers/*/*/*.c') );
120 die "-module isn't useful without -internal\n" if scalar keys %modules > 0;
124 # Data parsed out of the config and state files.
125 # We always map function-code values to zero, so items marked below with
126 # an asterisk could eventually be removed. TODO(4.0)
127 my %hinc; # lib -> header
128 my %libinc; # header -> lib
129 my %cskip; # error_file -> lib
130 my %errorfile; # lib -> error file name
131 my %fmax; # lib -> max assigned function code*
132 my %rmax; # lib -> max assigned reason code
133 my %fassigned; # lib -> colon-separated list of assigned function codes*
134 my %rassigned; # lib -> colon-separated list of assigned reason codes
135 my %fnew; # lib -> count of new function codes*
136 my %rnew; # lib -> count of new reason codes
137 my %rextra; # "extra" reason code -> lib
138 my %rcodes; # reason-name -> value
139 my %ftrans; # old name -> #define-friendly name (all caps)*
140 my %fcodes; # function-name -> value*
141 my $statefile; # state file with assigned reason and function codes
142 my %strings; # define -> text
144 # Read and parse the config file
145 open(IN, "$config") || die "Can't open config file $config, $!,";
147 next if /^#/ || /^$/;
148 if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
153 $libinc{$hdr} = $lib;
155 next if $err eq 'NONE';
156 $errorfile{$lib} = $err;
159 $fassigned{$lib} = ":";
160 $rassigned{$lib} = ":";
163 } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
166 } elsif ( /^S\s+(\S+)/ ) {
169 die "Illegal config line $_\n";
174 if ( ! $statefile ) {
175 $statefile = $config;
176 $statefile =~ s/.ec/.txt/;
179 # The statefile has all the previous assignments.
180 &phase("Reading state");
181 my $skippedstate = 0;
182 if ( ! $reindex && $statefile ) {
183 open(STATE, "<$statefile") || die "Can't open $statefile, $!";
185 # Scan function and reason codes and store them: keep a note of the
188 next if /^#/ || /^$/;
191 if ( /^(.+):(\d+):\\$/ ) {
195 $next =~ s/^\s*(.*)\s*$/$1/;
196 die "Duplicate define $name" if exists $strings{$name};
197 $strings{$name} = $next;
198 } elsif ( /^(\S+):(\d+):(.*)$/ ) {
201 die "Duplicate define $name" if exists $strings{$name};
202 $strings{$name} = $3;
204 die "Bad line in $statefile:\n$_\n";
207 $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
208 $lib = "SSL" if $lib =~ /TLS/;
209 if ( !defined $errorfile{$lib} ) {
213 } elsif ( $hinc{$lib} eq 'NONE' ) {
214 # When the header is NONE but the err file is specified,
215 # it signifies that the err file should be conserved but
216 # remain untouched, and the same goes for the symbols in
220 if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
221 die "$lib reason code $code collision at $name\n"
222 if $rassigned{$lib} =~ /:$code:/;
223 $rassigned{$lib} .= "$code:";
224 if ( !exists $rextra{$name} ) {
225 $rmax{$lib} = $code if $code > $rmax{$lib};
227 $rcodes{$name} = $code;
228 } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
229 $fassigned{$lib} .= "$code:";
230 $fmax{$lib} = $code if $code > $fmax{$lib};
231 $fcodes{$name} = $code;
233 die "Bad line in $statefile:\n$_\n";
239 foreach my $lib ( sort keys %rmax ) {
240 print STDERR "Reason codes for ${lib}:\n";
241 if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
242 my @rassigned = sort { $a <=> $b } split( ":", $1 );
243 print STDERR " ", join(' ', @rassigned), "\n";
245 print STDERR " --none--\n";
249 foreach my $lib ( sort keys %fmax ) {
250 print STDERR "Function codes for ${lib}:\n";
251 if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
252 my @fassigned = sort { $a <=> $b } split( ":", $1 );
253 print STDERR " ", join(' ', @fassigned), "\n";
255 print STDERR " --none--\n";
261 # Scan each header file and make a list of error codes
263 &phase("Scanning headers");
264 while ( ( my $hdr, my $lib ) = each %libinc ) {
265 next if $hdr eq "NONE";
266 print STDERR " ." if $debug;
272 open(IN, "<$hdr") || die "Can't open $hdr, $!,";
287 if ( not /\*\// ) { # multiline comment...
288 $line = $_; # ... just accumulate
291 s/\/\*.*?\*\///gs; # wipe it
297 $cpp-- if /^#\s*endif/;
300 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
302 next if /^\#/; # skip preprocessor directives
304 s/{[^{}]*}//gs; # ignore {} blocks
306 if ( /\{|\/\*/ ) { # Add a so editor works...
313 # Delete any DECLARE_ macros
315 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
316 foreach ( split /;/, $def ) {
318 # The goal is to collect function names from function declarations.
323 # Skip over recognized non-function declarations
324 next if /typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/;
326 # Remove STACK_OF(foo)
327 s/STACK_OF\(\w+\)/void/;
329 # Reduce argument lists to empty ()
330 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
331 while ( /\(.*\)/s ) {
332 s/\([^\(\)]+\)/\{\}/gs;
333 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
336 # pretend as we didn't use curly braces: {} -> ()
339 # Last token just before the first () is a function name.
340 if ( /(\w+)\s*\(\).*/s ) {
342 $name =~ tr/[a-z]/[A-Z]/;
344 } elsif ( /[\(\)]/ and not(/=/) ) {
345 print STDERR "Header $hdr: cannot parse: $_;\n";
351 if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
352 print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
353 print STDERR "Any new alerts must be added to $config.\n";
358 print STDERR "\n" if $debug;
360 # Scan each C source file and look for function and reason codes
361 # This is done by looking for strings that "look like" function or
362 # reason codes: basically anything consisting of all upper case and
363 # numerics which has _F_ or _R_ in it and which has the name of an
364 # error library at the start. This seems to work fine except for the
365 # oddly named structure BIO_F_CTX which needs to be ignored.
366 # If a code doesn't exist in list compiled from headers then mark it
367 # with the value "X" as a place holder to give it a value later.
368 # Store all function and reason codes found in %usedfuncs and %usedreasons
369 # so all those unreferenced can be printed out.
370 &phase("Scanning source");
373 foreach my $file ( @source ) {
374 # Don't parse the error source file.
375 next if exists $cskip{$file};
376 open( IN, "<$file" ) || die "Can't open $file, $!,";
379 print STDERR "$file:\n" if $debug;
382 # skip obsoleted source files entirely!
383 last if /^#error\s+obsolete/;
385 if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
386 /^([^()]*(\([^()]*\)[^()]*)*)\(/;
387 $1 =~ /([A-Za-z_0-9]*)$/;
391 if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) {
392 next unless exists $errorfile{$2};
393 next if $1 eq "BIO_F_BUFFER_CTX";
395 if ( !exists $fcodes{$1} ) {
396 print STDERR " New function $1\n" if $debug;
400 $ftrans{$3} = $func unless exists $ftrans{$3};
401 print STDERR " Function $1 = $fcodes{$1}\n"
404 if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
405 next unless exists $errorfile{$2};
406 $usedreasons{$1} = 1;
407 if ( !exists $rcodes{$1} ) {
408 print STDERR " New reason $1\n" if $debug;
412 print STDERR " Reason $1 = $rcodes{$1}\n" if $debug;
417 print STDERR "\n" if $debug;
419 # Now process each library in turn.
420 &phase("Writing files");
422 foreach my $lib ( keys %errorfile ) {
423 next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild;
424 next if scalar keys %modules > 0 && !$modules{$lib};
426 next if $hinc{$lib} eq 'NONE';
427 print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
428 print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
431 # If we get here then we have some new error codes so we
432 # need to rebuild the header file and C file.
434 # Make a sorted list of error and reason codes for later use.
435 my @function = sort grep( /^${lib}_/, keys %fcodes );
436 my @reasons = sort grep( /^${lib}_/, keys %rcodes );
438 # indent level for innermost preprocessor lines
441 # Rewrite the header file
443 my $hfile = $hinc{$lib};
444 $hfile =~ s/.h$/err.h/ if $internal;
445 open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
448 * Generated by util/mkerr.pl DO NOT EDIT
449 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
451 * Licensed under the Apache License 2.0 (the \"License\"). You may not use
452 * this file except in compliance with the License. You can obtain a copy
453 * in the file LICENSE in the source distribution or at
454 * https://www.openssl.org/source/license.html
457 #ifndef OPENSSL_${lib}ERR_H
458 # define OPENSSL_${lib}ERR_H
460 # include <openssl/opensslconf.h>
461 # include <openssl/symhacks.h>
466 # Declare the load function because the generate C file
467 # includes "fooerr.h" not "foo.h"
468 if ($lib ne "SSL" && $lib ne "ASYNC"
469 && (grep { $lib eq uc $_ } @disablables, @disablables_int)) {
471 # include <openssl/opensslconf.h>
473 # ifndef OPENSSL_NO_${lib}
479 #${indent}ifdef __cplusplus
482 int ERR_load_${lib}_strings(void);
486 # define ${lib}err(f, r) ERR_${lib}_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
495 int ERR_load_${lib}_strings(void);
496 void ERR_unload_${lib}_strings(void);
497 void ERR_${lib}_error(int function, int reason, char *file, int line);
505 print OUT "\n/*\n * $lib function codes.\n */\n";
506 print OUT "# ifndef OPENSSL_NO_DEPRECATED_3_0\n";
507 foreach my $i ( @function ) {
508 my $z = 48 - length($i);
510 if ( $fcodes{$i} eq "X" ) {
511 $fassigned{$lib} =~ m/^:([^:]*):/;
513 $findcode = $fmax{$lib} if !defined $findcode;
514 while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
517 $fcodes{$i} = $findcode;
518 $fassigned{$lib} .= "$findcode:";
519 print STDERR "New Function code $i\n" if $debug;
521 printf OUT "#${indent} define $i%s 0\n", " " x $z;
523 print OUT "# endif\n";
525 print OUT "\n/*\n * $lib reason codes.\n */\n";
526 foreach my $i ( @reasons ) {
527 my $z = 48 - length($i);
529 if ( $rcodes{$i} eq "X" ) {
530 $rassigned{$lib} =~ m/^:([^:]*):/;
532 $findcode = $rmax{$lib} if !defined $findcode;
533 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
536 $rcodes{$i} = $findcode;
537 $rassigned{$lib} .= "$findcode:";
538 print STDERR "New Reason code $i\n" if $debug;
540 printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
544 while (length($indent) > 0) {
545 $indent = substr $indent, 0, -1;
546 print OUT "#${indent}endif\n";
549 # Rewrite the C source file containing the error details.
551 # First, read any existing reason string definitions:
552 my $cfile = $errorfile{$lib};
553 my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
555 $hincf =~ s|.*include/||;
556 if ( $hincf =~ m|^openssl/| ) {
557 $hincf = "<${hincf}>";
559 $hincf = "\"${hincf}\"";
562 open( OUT, ">$cfile" )
563 || die "Can't open $cfile for writing, $!, stopped";
565 my $const = $internal ? 'const ' : '';
569 * Generated by util/mkerr.pl DO NOT EDIT
570 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
572 * Licensed under the Apache License 2.0 (the "License"). You may not use
573 * this file except in compliance with the License. You can obtain a copy
574 * in the file LICENSE in the source distribution or at
575 * https://www.openssl.org/source/license.html
578 #include <openssl/err.h>
581 #ifndef OPENSSL_NO_ERR
583 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
586 # Add each reason code.
587 foreach my $i ( @reasons ) {
589 if ( exists $strings{$i} ) {
591 $rn = "" if $rn eq '*';
593 $i =~ /^${lib}_R_(\S+)$/;
595 $rn =~ tr/_[A-Z]/ [a-z]/;
598 my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
599 if ( length($short) <= 80 ) {
600 print OUT "$short\n";
602 print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
614 int ERR_load_${lib}_strings(void)
616 #ifndef OPENSSL_NO_ERR
617 if (ERR_reason_error_string(${lib}_str_reasons[0].error) == NULL)
618 ERR_load_strings_const(${lib}_str_reasons);
624 my $st = $static ? "static " : "";
627 static int lib_code = 0;
628 static int error_loaded = 0;
630 ${st}int ERR_load_${lib}_strings(void)
633 lib_code = ERR_get_next_error_library();
636 #ifndef OPENSSL_NO_ERR
637 ERR_load_strings(lib_code, ${lib}_str_reasons);
644 ${st}void ERR_unload_${lib}_strings(void)
647 #ifndef OPENSSL_NO_ERR
648 ERR_unload_strings(lib_code, ${lib}_str_reasons);
654 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
657 lib_code = ERR_get_next_error_library();
658 ERR_raise(lib_code, reason);
659 ERR_set_debug(file, line, NULL);
669 # Make a list of unreferenced function and reason codes
672 foreach ( keys %fcodes ) {
673 push( @funref, $_ ) unless exists $usedfuncs{$_};
676 foreach ( keys %rcodes ) {
677 push( @runref, $_ ) unless exists $usedreasons{$_};
680 print STDERR "The following function codes were not referenced:\n";
681 foreach ( sort @funref ) {
682 print STDERR " $_\n";
686 print STDERR "The following reason codes were not referenced:\n";
687 foreach ( sort @runref ) {
688 print STDERR " $_\n";
693 die "Found $errors errors, quitting" if $errors;
695 # Update the state file
697 open(OUT, ">$statefile.new")
698 || die "Can't write $statefile.new, $!";
700 # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
702 # Licensed under the Apache License 2.0 (the "License"). You may not use
703 # this file except in compliance with the License. You can obtain a copy
704 # in the file LICENSE in the source distribution or at
705 # https://www.openssl.org/source/license.html
707 print OUT "\n# Function codes\n";
708 foreach my $i ( sort keys %fcodes ) {
709 my $short = "$i:$fcodes{$i}:";
710 my $t = exists $strings{$i} ? $strings{$i} : "";
711 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
712 print OUT "$short$t\n";
714 print OUT "\n#Reason codes\n";
715 foreach my $i ( sort keys %rcodes ) {
716 my $short = "$i:$rcodes{$i}:";
717 my $t = exists $strings{$i} ? "$strings{$i}" : "";
718 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
719 print OUT "$short$t\n" if !exists $rextra{$i};
722 if ( $skippedstate ) {
723 print "Skipped state, leaving update in $statefile.new";
725 rename "$statefile", "$statefile.old"
726 || die "Can't backup $statefile to $statefile.old, $!";
727 rename "$statefile.new", "$statefile"
728 || die "Can't rename $statefile to $statefile.new, $!";