xref: /freebsd-src/crypto/openssl/util/ck_errf.pl (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert# This is just a quick script to scan for cases where the 'error'
10*e0c4386eSCy Schubert# function name in a XXXerr() macro is wrong.
11*e0c4386eSCy Schubert#
12*e0c4386eSCy Schubert# Run in the top level by going
13*e0c4386eSCy Schubert# perl util/ck_errf.pl */*.c */*/*.c
14*e0c4386eSCy Schubert#
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubertuse strict;
17*e0c4386eSCy Schubertuse warnings;
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubertmy $config;
20*e0c4386eSCy Schubertmy $err_strict = 0;
21*e0c4386eSCy Schubertmy $debug      = 0;
22*e0c4386eSCy Schubertmy $internal   = 0;
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubertsub help
25*e0c4386eSCy Schubert{
26*e0c4386eSCy Schubert    print STDERR <<"EOF";
27*e0c4386eSCy Schubertmkerr.pl [options] [files...]
28*e0c4386eSCy Schubert
29*e0c4386eSCy SchubertOptions:
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert    -conf FILE  Use the named config file FILE instead of the default.
32*e0c4386eSCy Schubert
33*e0c4386eSCy Schubert    -debug      Verbose output debugging on stderr.
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert    -internal   Generate code that is to be built as part of OpenSSL itself.
36*e0c4386eSCy Schubert                Also scans internal list of files.
37*e0c4386eSCy Schubert
38*e0c4386eSCy Schubert    -strict     If any error was found, fail with exit code 1, otherwise 0.
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert    -help       Show this help text.
41*e0c4386eSCy Schubert
42*e0c4386eSCy Schubert    ...         Additional arguments are added to the file list to scan,
43*e0c4386eSCy Schubert                if '-internal' was NOT specified on the command line.
44*e0c4386eSCy Schubert
45*e0c4386eSCy SchubertEOF
46*e0c4386eSCy Schubert}
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubertwhile ( @ARGV ) {
49*e0c4386eSCy Schubert    my $arg = $ARGV[0];
50*e0c4386eSCy Schubert    last unless $arg =~ /-.*/;
51*e0c4386eSCy Schubert    $arg = $1 if $arg =~ /-(-.*)/;
52*e0c4386eSCy Schubert    if ( $arg eq "-conf" ) {
53*e0c4386eSCy Schubert        $config = $ARGV[1];
54*e0c4386eSCy Schubert        shift @ARGV;
55*e0c4386eSCy Schubert    } elsif ( $arg eq "-debug" ) {
56*e0c4386eSCy Schubert        $debug = 1;
57*e0c4386eSCy Schubert    } elsif ( $arg eq "-internal" ) {
58*e0c4386eSCy Schubert        $internal = 1;
59*e0c4386eSCy Schubert    } elsif ( $arg eq "-strict" ) {
60*e0c4386eSCy Schubert        $err_strict = 1;
61*e0c4386eSCy Schubert    } elsif ( $arg =~ /-*h(elp)?/ ) {
62*e0c4386eSCy Schubert        &help();
63*e0c4386eSCy Schubert        exit;
64*e0c4386eSCy Schubert    } elsif ( $arg =~ /-.*/ ) {
65*e0c4386eSCy Schubert        die "Unknown option $arg; use -h for help.\n";
66*e0c4386eSCy Schubert    }
67*e0c4386eSCy Schubert    shift @ARGV;
68*e0c4386eSCy Schubert}
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubertmy @source;
71*e0c4386eSCy Schubertif ( $internal ) {
72*e0c4386eSCy Schubert    die "Extra parameters given.\n" if @ARGV;
73*e0c4386eSCy Schubert    $config = "crypto/err/openssl.ec" unless defined $config;
74*e0c4386eSCy Schubert    @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
75*e0c4386eSCy Schubert                glob('ssl/*.c'), glob('ssl/*/*.c'), glob('providers/*.c'),
76*e0c4386eSCy Schubert                glob('providers/*/*.c'), glob('providers/*/*/*.c') );
77*e0c4386eSCy Schubert} else {
78*e0c4386eSCy Schubert    die "Configuration file not given.\nSee '$0 -help' for information\n"
79*e0c4386eSCy Schubert        unless defined $config;
80*e0c4386eSCy Schubert    @source = @ARGV;
81*e0c4386eSCy Schubert}
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert# To detect if there is any error generation for a libcrypto/libssl libs
84*e0c4386eSCy Schubert# we don't know, we need to find out what libs we do know.  That list is
85*e0c4386eSCy Schubert# readily available in crypto/err/openssl.ec, in form of lines starting
86*e0c4386eSCy Schubert# with "L ".  Note that we always rely on the modules SYS and ERR to be
87*e0c4386eSCy Schubert# generally available.
88*e0c4386eSCy Schubertmy %libs       = ( SYS => 1, ERR => 1 );
89*e0c4386eSCy Schubertopen my $cfh, $config or die "Trying to read $config: $!\n";
90*e0c4386eSCy Schubertwhile (<$cfh>) {
91*e0c4386eSCy Schubert    s|\R$||;                    # Better chomp
92*e0c4386eSCy Schubert    next unless m|^L ([0-9A-Z_]+)\s|;
93*e0c4386eSCy Schubert    next if $1 eq "NONE";
94*e0c4386eSCy Schubert    $libs{$1} = 1;
95*e0c4386eSCy Schubert}
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubertmy $bad = 0;
98*e0c4386eSCy Schubertforeach my $file (@source) {
99*e0c4386eSCy Schubert    open( IN, "<$file" ) || die "Can't open $file, $!";
100*e0c4386eSCy Schubert    my $func = "";
101*e0c4386eSCy Schubert    while (<IN>) {
102*e0c4386eSCy Schubert        if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
103*e0c4386eSCy Schubert            /^([^()]*(\([^()]*\)[^()]*)*)\(/;
104*e0c4386eSCy Schubert            $1 =~ /([A-Za-z_0-9]*)$/;
105*e0c4386eSCy Schubert            $func = $1;
106*e0c4386eSCy Schubert            $func =~ tr/A-Z/a-z/;
107*e0c4386eSCy Schubert        }
108*e0c4386eSCy Schubert        if ( /([A-Z0-9_]+[A-Z0-9])err\(([^,]+)/ && !/ckerr_ignore/ ) {
109*e0c4386eSCy Schubert            my $errlib = $1;
110*e0c4386eSCy Schubert            my $n      = $2;
111*e0c4386eSCy Schubert
112*e0c4386eSCy Schubert            unless ( $libs{$errlib} ) {
113*e0c4386eSCy Schubert                print "$file:$.:$errlib not listed in $config\n";
114*e0c4386eSCy Schubert                $libs{$errlib} = 1; # To not display it again
115*e0c4386eSCy Schubert                $bad = 1;
116*e0c4386eSCy Schubert            }
117*e0c4386eSCy Schubert
118*e0c4386eSCy Schubert            if ( $func eq "" ) {
119*e0c4386eSCy Schubert                print "$file:$.:???:$n\n";
120*e0c4386eSCy Schubert                $bad = 1;
121*e0c4386eSCy Schubert                next;
122*e0c4386eSCy Schubert            }
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert            if ( $n !~ /^(.+)_F_(.+)$/ ) {
125*e0c4386eSCy Schubert                #print "check -$file:$.:$func:$n\n";
126*e0c4386eSCy Schubert                next;
127*e0c4386eSCy Schubert            }
128*e0c4386eSCy Schubert            my $lib = $1;
129*e0c4386eSCy Schubert            $n   = $2;
130*e0c4386eSCy Schubert
131*e0c4386eSCy Schubert            if ( $lib ne $errlib ) {
132*e0c4386eSCy Schubert                print "$file:$.:$func:$n [${errlib}err]\n";
133*e0c4386eSCy Schubert                $bad = 1;
134*e0c4386eSCy Schubert                next;
135*e0c4386eSCy Schubert            }
136*e0c4386eSCy Schubert
137*e0c4386eSCy Schubert            $n =~ tr/A-Z/a-z/;
138*e0c4386eSCy Schubert            if ( $n ne $func && $errlib ne "SYS" ) {
139*e0c4386eSCy Schubert                print "$file:$.:$func:$n\n";
140*e0c4386eSCy Schubert                $bad = 1;
141*e0c4386eSCy Schubert                next;
142*e0c4386eSCy Schubert            }
143*e0c4386eSCy Schubert
144*e0c4386eSCy Schubert            #		print "$func:$1\n";
145*e0c4386eSCy Schubert        }
146*e0c4386eSCy Schubert    }
147*e0c4386eSCy Schubert    close(IN);
148*e0c4386eSCy Schubert}
149*e0c4386eSCy Schubert
150*e0c4386eSCy Schubertif ( $bad && $err_strict ) {
151*e0c4386eSCy Schubert    print STDERR "FATAL: error discrepancy\n";
152*e0c4386eSCy Schubert    exit 1;
153*e0c4386eSCy Schubert}
154