xref: /aosp_15_r20/external/boringssl/src/util/diff_asm.go (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1// Copyright (c) 2016, Google Inc.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15//go:build ignore
16
17package main
18
19import (
20	"flag"
21	"fmt"
22	"os"
23	"os/exec"
24	"path/filepath"
25	"strings"
26	"syscall"
27)
28
29var (
30	boringsslDir = flag.String("boringssl", ".", "The path to the BoringSSL checkout.")
31	opensslDir   = flag.String("openssl", filepath.Join("..", "openssl"), "The path to the OpenSSL checkout.")
32)
33
34func mapName(path string) string {
35	path = strings.Replace(path, filepath.FromSlash("/fipsmodule/"), string(filepath.Separator), 1)
36	pathSlash := filepath.ToSlash(path)
37	if strings.HasPrefix(pathSlash, "crypto/test/") {
38		return ""
39	}
40	switch pathSlash {
41	case "crypto/aes/asm/vpaes-armv7.pl",
42		"crypto/cipher_extra/asm/aes128gcmsiv-x86_64.pl",
43		"crypto/cipher_extra/asm/chacha20_poly1305_x86_64.pl",
44		"crypto/ec/asm/p256_beeu-x86_64-asm.pl",
45		"crypto/modes/asm/ghash-neon-armv8.pl",
46		"crypto/modes/asm/ghash-ssse3-x86.pl",
47		"crypto/modes/asm/ghash-ssse3-x86_64.pl",
48		"crypto/rand/asm/rdrand-x86_64.pl":
49		return ""
50	case "crypto/ec/asm/p256-x86_64-asm.pl":
51		return filepath.FromSlash("crypto/ec/asm/ecp_nistz256-x86_64.pl")
52	}
53	return path
54}
55
56func diff(from, to string) error {
57	cmd := exec.Command("diff", "-u", "--", from, to)
58	cmd.Stdout = os.Stdout
59	cmd.Stderr = os.Stderr
60	err := cmd.Run()
61	// diff returns exit code 1 if the files differ but it was otherwise
62	// successful.
63	if exitError, ok := err.(*exec.ExitError); ok && exitError.Sys().(syscall.WaitStatus).ExitStatus() == 1 {
64		return nil
65	}
66	return err
67}
68
69func main() {
70	flag.Usage = func() {
71		fmt.Fprintf(os.Stderr, "Usage: diff_asm [flag...] [filter...]\n")
72		fmt.Fprintf(os.Stderr, "Filter arguments limit to assembly files which match arguments.\n")
73		fmt.Fprintf(os.Stderr, "If not using a filter, piping to `diffstat` may be useful.\n\n")
74		flag.PrintDefaults()
75	}
76	flag.Parse()
77
78	// Find all the assembly files.
79	var files []string
80	err := filepath.Walk(*boringsslDir, func(path string, info os.FileInfo, err error) error {
81		if err != nil {
82			return nil
83		}
84
85		path, err = filepath.Rel(*boringsslDir, path)
86		if err != nil {
87			return err
88		}
89
90		dir := filepath.Base(filepath.Dir(path))
91		if !info.IsDir() && (dir == "asm" || dir == "perlasm") && strings.HasSuffix(filepath.Base(path), ".pl") {
92			files = append(files, path)
93		}
94
95		return nil
96	})
97	if err != nil {
98		fmt.Fprintf(os.Stderr, "Error finding assembly: %s\n", err)
99		os.Exit(1)
100	}
101
102	for _, file := range files {
103		opensslFile := mapName(file)
104		if len(opensslFile) == 0 {
105			continue
106		}
107
108		if flag.NArg() > 0 {
109			var found bool
110			for _, arg := range flag.Args() {
111				if strings.Contains(file, arg) {
112					found = true
113					break
114				}
115			}
116			if !found {
117				continue
118			}
119		}
120
121		if err := diff(filepath.Join(*opensslDir, opensslFile), filepath.Join(*boringsslDir, file)); err != nil {
122			fmt.Fprintf(os.Stderr, "Error comparing %s: %s\n", file, err)
123			os.Exit(1)
124		}
125	}
126}
127