2 # Copyright 1999-2020 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
13 use File::Spec::Functions qw(abs2rel rel2abs);
18 my $config = "crypto/err/openssl.ec";
30 my $YEAR = $t[5] + 1900;
35 print STDERR "\n---\n$text\n" if $debug;
41 mkerr.pl [options] [files...]
45 -conf FILE Use the named config file FILE instead of the default.
47 -debug Verbose output debugging on stderr.
49 -internal Generate code that is to be built as part of OpenSSL itself.
50 Also scans internal list of files.
52 -module M Only useful with -internal!
53 Only write files for library module M. Whether files are
54 actually written or not depends on other options, such as
56 Note: this option is cumulative. If not given at all, all
57 internal modules will be considered.
59 -nowrite Do not write the header/source files, even if changed.
61 -rebuild Rebuild all header and C source files, even if there
64 -reindex Ignore previously assigned values (except for R records in
65 the config file) and renumber everything starting at 100.
67 -static Make the load/unload functions static.
69 -unref List all unreferenced function and reason codes on stderr;
72 -help Show this help text.
74 ... Additional arguments are added to the file list to scan,
75 if '-internal' was NOT specified on the command line.
82 last unless $arg =~ /-.*/;
83 $arg = $1 if $arg =~ /-(-.*)/;
84 if ( $arg eq "-conf" ) {
87 } elsif ( $arg eq "-debug" ) {
90 } elsif ( $arg eq "-internal" ) {
92 } elsif ( $arg eq "-nowrite" ) {
94 } elsif ( $arg eq "-rebuild" ) {
96 } elsif ( $arg eq "-reindex" ) {
98 } elsif ( $arg eq "-static" ) {
100 } elsif ( $arg eq "-unref" ) {
103 } elsif ( $arg eq "-module" ) {
105 $modules{uc $ARGV[0]} = 1;
106 } elsif ( $arg =~ /-*h(elp)?/ ) {
109 } elsif ( $arg =~ /-.*/ ) {
110 die "Unknown option $arg; use -h for help.\n";
117 die "Cannot mix -internal and -static\n" if $static;
118 die "Extra parameters given.\n" if @ARGV;
119 @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
120 glob('ssl/*.c'), glob('ssl/*/*.c'), glob('providers/*.c'),
121 glob('providers/*/*.c'), glob('providers/*/*/*.c') );
123 die "-module isn't useful without -internal\n" if scalar keys %modules > 0;
127 # Data parsed out of the config and state files.
128 # We always map function-code values to zero, so items marked below with
129 # an asterisk could eventually be removed. TODO(4.0)
130 my %hpubinc; # lib -> public header
131 my %libpubinc; # public header -> lib
132 my %hprivinc; # lib -> private header
133 my %libprivinc; # private header -> lib
134 my %cskip; # error_file -> lib
135 my %errorfile; # lib -> error file name
136 my %fmax; # lib -> max assigned function code*
137 my %rmax; # lib -> max assigned reason code
138 my %fassigned; # lib -> colon-separated list of assigned function codes*
139 my %rassigned; # lib -> colon-separated list of assigned reason codes
140 my %fnew; # lib -> count of new function codes*
141 my %rnew; # lib -> count of new reason codes
142 my %rextra; # "extra" reason code -> lib
143 my %rcodes; # reason-name -> value
144 my %ftrans; # old name -> #define-friendly name (all caps)*
145 my %fcodes; # function-name -> value*
146 my $statefile; # state file with assigned reason and function codes
147 my %strings; # define -> text
149 # Read and parse the config file
150 open(IN, "$config") || die "Can't open config file $config, $!,";
152 next if /^#/ || /^$/;
153 if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s+$/ ) {
157 my $privhdr = $4 // 'NONE';
158 $hpubinc{$lib} = $pubhdr;
159 $libpubinc{$pubhdr} = $lib;
160 $hprivinc{$lib} = $privhdr;
161 $libprivinc{$privhdr} = $lib;
163 $errorfile{$lib} = $err;
164 next if $err eq 'NONE';
167 $fassigned{$lib} = ":";
168 $rassigned{$lib} = ":";
171 die "Public header file must be in include/openssl ($pubhdr is not)\n"
174 && $pubhdr !~ m|^include/openssl/|);
175 die "Private header file may only be specified with -internal ($privhdr given)\n"
176 unless ($privhdr eq 'NONE' || $internal);
177 } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
180 } elsif ( /^S\s+(\S+)/ ) {
183 die "Illegal config line $_\n";
188 if ( ! $statefile ) {
189 $statefile = $config;
190 $statefile =~ s/.ec/.txt/;
193 # The statefile has all the previous assignments.
194 &phase("Reading state");
195 my $skippedstate = 0;
196 if ( ! $reindex && $statefile ) {
197 open(STATE, "<$statefile") || die "Can't open $statefile, $!";
199 # Scan function and reason codes and store them: keep a note of the
202 next if /^#/ || /^$/;
205 if ( /^(.+):(\d+):\\$/ ) {
209 $next =~ s/^\s*(.*)\s*$/$1/;
210 die "Duplicate define $name" if exists $strings{$name};
211 $strings{$name} = $next;
212 } elsif ( /^(\S+):(\d+):(.*)$/ ) {
215 die "Duplicate define $name" if exists $strings{$name};
216 $strings{$name} = $3;
218 die "Bad line in $statefile:\n$_\n";
221 $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
222 $lib = "SSL" if $lib =~ /TLS/;
223 if ( !defined $errorfile{$lib} ) {
228 next if $errorfile{$lib} eq 'NONE';
229 if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
230 die "$lib reason code $code collision at $name\n"
231 if $rassigned{$lib} =~ /:$code:/;
232 $rassigned{$lib} .= "$code:";
233 if ( !exists $rextra{$name} ) {
234 $rmax{$lib} = $code if $code > $rmax{$lib};
236 $rcodes{$name} = $code;
237 } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
238 $fassigned{$lib} .= "$code:";
239 $fmax{$lib} = $code if $code > $fmax{$lib};
240 $fcodes{$name} = $code;
242 die "Bad line in $statefile:\n$_\n";
248 foreach my $lib ( sort keys %rmax ) {
249 print STDERR "Reason codes for ${lib}:\n";
250 if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
251 my @rassigned = sort { $a <=> $b } split( ":", $1 );
252 print STDERR " ", join(' ', @rassigned), "\n";
254 print STDERR " --none--\n";
258 foreach my $lib ( sort keys %fmax ) {
259 print STDERR "Function codes for ${lib}:\n";
260 if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
261 my @fassigned = sort { $a <=> $b } split( ":", $1 );
262 print STDERR " ", join(' ', @fassigned), "\n";
264 print STDERR " --none--\n";
270 # Scan each public header file and make a list of function codes and names
271 &phase("Scanning headers");
272 while ( ( my $hdr, my $lib ) = each %libpubinc ) {
273 next if $hdr eq "NONE";
274 print STDERR " ." if $debug;
281 || open(IN, "<$hdr.in")
282 || die "Can't open $hdr or $hdr.in, $!,";
297 if ( not /\*\// ) { # multiline comment...
298 $line = $_; # ... just accumulate
301 s/\/\*.*?\*\///gs; # wipe it
307 $cpp-- if /^#\s*endif/;
310 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
312 next if /^\#/; # skip preprocessor directives
314 s/{[^{}]*}//gs; # ignore {} blocks
316 if ( /\{|\/\*/ ) { # Add a so editor works...
323 # Delete any DECLARE_ macros
325 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
326 foreach ( split /;/, $def ) {
328 # The goal is to collect function names from function declarations.
333 # Skip over recognized non-function declarations
336 # Remove STACK_OF(foo)
337 s/STACK_OF\(\w+\)/void/;
339 # Reduce argument lists to empty ()
340 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
341 while ( /\(.*\)/s ) {
342 s/\([^\(\)]+\)/\{\}/gs;
343 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
346 # pretend as we didn't use curly braces: {} -> ()
349 # Last token just before the first () is a function name.
350 if ( /(\w+)\s*\(\).*/s ) {
352 $name =~ tr/[a-z]/[A-Z]/;
354 } elsif ( /[\(\)]/ and not(/=/) ) {
355 print STDERR "Header $hdr: cannot parse: $_;\n";
361 if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
362 print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
363 print STDERR "Any new alerts must be added to $config.\n";
368 print STDERR "\n" if $debug;
370 # Scan each C source file and look for function and reason codes
371 # This is done by looking for strings that "look like" function or
372 # reason codes: basically anything consisting of all upper case and
373 # numerics which has _F_ or _R_ in it and which has the name of an
374 # error library at the start. This seems to work fine except for the
375 # oddly named structure BIO_F_CTX which needs to be ignored.
376 # If a code doesn't exist in list compiled from headers then mark it
377 # with the value "X" as a place holder to give it a value later.
378 # Store all function and reason codes found in %usedfuncs and %usedreasons
379 # so all those unreferenced can be printed out.
380 &phase("Scanning source");
383 foreach my $file ( @source ) {
384 # Don't parse the error source file.
385 next if exists $cskip{$file};
386 open( IN, "<$file" ) || die "Can't open $file, $!,";
389 print STDERR "$file:\n" if $debug;
392 # skip obsoleted source files entirely!
393 last if /^#error\s+obsolete/;
395 if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
396 /^([^()]*(\([^()]*\)[^()]*)*)\(/;
397 $1 =~ /([A-Za-z_0-9]*)$/;
401 if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) {
402 next unless exists $errorfile{$2};
403 next if $errorfile{$2} eq 'NONE';
404 next if $1 eq "BIO_F_BUFFER_CTX";
406 if ( !exists $fcodes{$1} ) {
407 print STDERR " New function $1\n" if $debug;
411 $ftrans{$3} = $func unless exists $ftrans{$3};
412 print STDERR " Function $1 = $fcodes{$1}\n"
415 if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
416 next unless exists $errorfile{$2};
417 next if $errorfile{$2} eq 'NONE';
418 $usedreasons{$1} = 1;
419 if ( !exists $rcodes{$1} ) {
420 print STDERR " New reason $1\n" if $debug;
424 print STDERR " Reason $1 = $rcodes{$1}\n" if $debug;
429 print STDERR "\n" if $debug;
431 # Now process each library in turn.
432 &phase("Writing files");
434 foreach my $lib ( keys %errorfile ) {
435 next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild;
436 next if scalar keys %modules > 0 && !$modules{$lib};
438 print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
439 print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
442 # If we get here then we have some new error codes so we
443 # need to rebuild the header file and C file.
445 # Make a sorted list of error and reason codes for later use.
446 my @function = sort grep( /^${lib}_/, keys %fcodes );
447 my @reasons = sort grep( /^${lib}_/, keys %rcodes );
449 # indent level for innermost preprocessor lines
452 # Flag if the sub-library is disablable
453 # There are a few exceptions, where disabling the sub-library
454 # doesn't actually remove the whole sub-library, but rather implements
455 # it with a NULL backend.
457 ($lib ne "SSL" && $lib ne "ASYNC" && $lib ne "DSO"
458 && (grep { $lib eq uc $_ } @disablables, @disablables_int));
460 # Rewrite the internal header file if there is one ($internal only!)
462 if ($hprivinc{$lib} ne 'NONE') {
463 my $hfile = $hprivinc{$lib};
466 if ($guard =~ m|^include/|) {
469 $guard = basename($guard);
471 $guard = "OSSL_" . join('_', split(m|[./]|, uc $guard));
473 open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
476 * Generated by util/mkerr.pl DO NOT EDIT
477 * Copyright 2020-$YEAR The OpenSSL Project Authors. All Rights Reserved.
479 * Licensed under the Apache License 2.0 (the \"License\"). You may not use
480 * this file except in compliance with the License. You can obtain a copy
481 * in the file LICENSE in the source distribution or at
482 * https://www.openssl.org/source/license.html
489 # include <openssl/opensslconf.h>
490 # include <openssl/symhacks.h>
500 # ifndef OPENSSL_NO_${lib}
506 int err_load_${lib}_strings_int(void);
509 # If this library doesn't have a public header file, we write all
510 # definitions that would end up there here instead
511 if ($hpubinc{$lib} eq 'NONE') {
512 print OUT "\n/*\n * $lib function codes.\n */\n";
513 print OUT "#${indent}ifndef OPENSSL_NO_DEPRECATED_3_0\n";
514 foreach my $i ( @function ) {
515 my $z = 48 - length($i);
517 if ( $fcodes{$i} eq "X" ) {
518 $fassigned{$lib} =~ m/^:([^:]*):/;
520 $findcode = $fmax{$lib} if !defined $findcode;
521 while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
524 $fcodes{$i} = $findcode;
525 $fassigned{$lib} .= "$findcode:";
526 print STDERR "New Function code $i\n" if $debug;
528 printf OUT "#${indent} define $i%s 0\n", " " x $z;
530 print OUT "#${indent}endif\n";
532 print OUT "\n/*\n * $lib reason codes.\n */\n";
533 foreach my $i ( @reasons ) {
534 my $z = 48 - length($i);
536 if ( $rcodes{$i} eq "X" ) {
537 $rassigned{$lib} =~ m/^:([^:]*):/;
539 $findcode = $rmax{$lib} if !defined $findcode;
540 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
543 $rcodes{$i} = $findcode;
544 $rassigned{$lib} .= "$findcode:";
545 print STDERR "New Reason code $i\n" if $debug;
547 printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
552 # This doesn't go all the way down to zero, to allow for the ending
553 # brace for 'extern "C" {'.
554 while (length($indent) > 1) {
555 $indent = substr $indent, 0, -1;
556 print OUT "#${indent}endif\n";
569 # Rewrite the public header file
571 if ($hpubinc{$lib} ne 'NONE') {
575 ? "# include <openssl/cryptoerr_legacy.h>\n"
576 : "# include <openssl/sslerr_legacy.h>\n")
578 my $hfile = $hpubinc{$lib};
580 $guard =~ s|^include/||;
581 $guard = join('_', split(m|[./]|, uc $guard));
582 $guard = "OSSL_" . $guard unless $internal;
584 open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
587 * Generated by util/mkerr.pl DO NOT EDIT
588 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
590 * Licensed under the Apache License 2.0 (the \"License\"). You may not use
591 * this file except in compliance with the License. You can obtain a copy
592 * in the file LICENSE in the source distribution or at
593 * https://www.openssl.org/source/license.html
600 # include <openssl/opensslconf.h>
601 # include <openssl/symhacks.h>
609 # ifndef OPENSSL_NO_${lib}
616 # define ${lib}err(f, r) ERR_${lib}_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
625 int ERR_load_${lib}_strings(void);
626 void ERR_unload_${lib}_strings(void);
627 void ERR_${lib}_error(int function, int reason, char *file, int line);
635 print OUT "\n/*\n * $lib function codes.\n */\n";
636 print OUT "#${indent}ifndef OPENSSL_NO_DEPRECATED_3_0\n";
637 foreach my $i ( @function ) {
638 my $z = 48 - length($i);
640 if ( $fcodes{$i} eq "X" ) {
641 $fassigned{$lib} =~ m/^:([^:]*):/;
643 $findcode = $fmax{$lib} if !defined $findcode;
644 while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
647 $fcodes{$i} = $findcode;
648 $fassigned{$lib} .= "$findcode:";
649 print STDERR "New Function code $i\n" if $debug;
651 printf OUT "#${indent} define $i%s 0\n", " " x $z;
653 print OUT "#${indent}endif\n";
655 print OUT "\n/*\n * $lib reason codes.\n */\n";
656 foreach my $i ( @reasons ) {
657 my $z = 48 - length($i);
659 if ( $rcodes{$i} eq "X" ) {
660 $rassigned{$lib} =~ m/^:([^:]*):/;
662 $findcode = $rmax{$lib} if !defined $findcode;
663 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
666 $rcodes{$i} = $findcode;
667 $rassigned{$lib} .= "$findcode:";
668 print STDERR "New Reason code $i\n" if $debug;
670 printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
674 while (length($indent) > 0) {
675 $indent = substr $indent, 0, -1;
676 print OUT "#${indent}endif\n";
681 # Rewrite the C source file containing the error details.
683 if ($errorfile{$lib} ne 'NONE') {
684 # First, read any existing reason string definitions:
685 my $cfile = $errorfile{$lib};
686 my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
687 my $hpubincf = $hpubinc{$lib};
688 my $hprivincf = $hprivinc{$lib};
691 if ($hpubincf ne 'NONE') {
692 $hpubincf =~ s|^include/||;
693 $includes .= "#include <${hpubincf}>\n";
695 if ($hprivincf =~ m|^include/|) {
698 $hprivincf = abs2rel(rel2abs($hprivincf),
699 rel2abs(dirname($cfile)));
701 $includes .= "#include \"${hprivincf}\"\n";
703 $includes .= "#include \"${hpubincf}\"\n";
706 open( OUT, ">$cfile" )
707 || die "Can't open $cfile for writing, $!, stopped";
709 my $const = $internal ? 'const ' : '';
713 * Generated by util/mkerr.pl DO NOT EDIT
714 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
716 * Licensed under the Apache License 2.0 (the "License"). You may not use
717 * this file except in compliance with the License. You can obtain a copy
718 * in the file LICENSE in the source distribution or at
719 * https://www.openssl.org/source/license.html
722 #include <openssl/err.h>
729 #ifndef OPENSSL_NO_${lib}
736 #${indent}ifndef OPENSSL_NO_ERR
738 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
741 # Add each reason code.
742 foreach my $i ( @reasons ) {
744 if ( exists $strings{$i} ) {
746 $rn = "" if $rn eq '*';
748 $i =~ /^${lib}_R_(\S+)$/;
750 $rn =~ tr/_[A-Z]/ [a-z]/;
753 my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
754 if ( length($short) <= 80 ) {
755 print OUT "$short\n";
757 print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
769 int err_load_${lib}_strings_int(void)
771 #${indent}ifndef OPENSSL_NO_ERR
772 if (ERR_reason_error_string(${lib}_str_reasons[0].error) == NULL)
773 ERR_load_strings_const(${lib}_str_reasons);
779 my $st = $static ? "static " : "";
782 static int lib_code = 0;
783 static int error_loaded = 0;
785 ${st}int ERR_load_${lib}_strings(void)
788 lib_code = ERR_get_next_error_library();
791 #ifndef OPENSSL_NO_ERR
792 ERR_load_strings(lib_code, ${lib}_str_reasons);
799 ${st}void ERR_unload_${lib}_strings(void)
802 #ifndef OPENSSL_NO_ERR
803 ERR_unload_strings(lib_code, ${lib}_str_reasons);
809 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
812 lib_code = ERR_get_next_error_library();
813 ERR_raise(lib_code, reason);
814 ERR_set_debug(file, line, NULL);
820 while (length($indent) > 1) {
821 $indent = substr $indent, 0, -1;
822 print OUT "#${indent}endif\n";
824 if ($internal && $disablable) {
827 NON_EMPTY_TRANSLATION_UNIT
836 # Make a list of unreferenced function and reason codes
839 foreach ( keys %fcodes ) {
840 push( @funref, $_ ) unless exists $usedfuncs{$_};
843 foreach ( keys %rcodes ) {
844 push( @runref, $_ ) unless exists $usedreasons{$_};
847 print STDERR "The following function codes were not referenced:\n";
848 foreach ( sort @funref ) {
849 print STDERR " $_\n";
853 print STDERR "The following reason codes were not referenced:\n";
854 foreach ( sort @runref ) {
855 print STDERR " $_\n";
860 die "Found $errors errors, quitting" if $errors;
862 # Update the state file
864 open(OUT, ">$statefile.new")
865 || die "Can't write $statefile.new, $!";
867 # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
869 # Licensed under the Apache License 2.0 (the "License"). You may not use
870 # this file except in compliance with the License. You can obtain a copy
871 # in the file LICENSE in the source distribution or at
872 # https://www.openssl.org/source/license.html
874 print OUT "\n# Function codes\n";
875 foreach my $i ( sort keys %fcodes ) {
876 my $short = "$i:$fcodes{$i}:";
877 my $t = exists $strings{$i} ? $strings{$i} : "";
878 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
879 print OUT "$short$t\n";
881 print OUT "\n#Reason codes\n";
882 foreach my $i ( sort keys %rcodes ) {
883 my $short = "$i:$rcodes{$i}:";
884 my $t = exists $strings{$i} ? "$strings{$i}" : "";
885 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
886 print OUT "$short$t\n" if !exists $rextra{$i};
889 if ( $skippedstate ) {
890 print "Skipped state, leaving update in $statefile.new";
892 rename "$statefile", "$statefile.old"
893 || die "Can't backup $statefile to $statefile.old, $!";
894 rename "$statefile.new", "$statefile"
895 || die "Can't rename $statefile to $statefile.new, $!";