diff_asm.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "os"
  19. "os/exec"
  20. "path/filepath"
  21. "strings"
  22. "syscall"
  23. )
  24. var (
  25. boringsslDir = flag.String("boringssl", ".", "The path to the BoringSSL checkout.")
  26. opensslDir = flag.String("openssl", filepath.Join("..", "openssl"), "The path to the OpenSSL checkout.")
  27. )
  28. func mapName(path string) string {
  29. path = strings.Replace(path, filepath.FromSlash("/fipsmodule/"), string(filepath.Separator), 1)
  30. switch filepath.ToSlash(path) {
  31. case "crypto/cipher_extra/asm/aes128gcmsiv-x86_64.pl", "crypto/cipher_extra/asm/chacha20_poly1305_x86_64.pl", "crypto/rand/asm/rdrand-x86_64.pl":
  32. return ""
  33. case "crypto/ec/asm/p256-x86_64-asm.pl":
  34. return filepath.FromSlash("crypto/ec/asm/ecp_nistz256-x86_64.pl")
  35. }
  36. return path
  37. }
  38. func diff(from, to string) error {
  39. cmd := exec.Command("diff", "-u", "--", from, to)
  40. cmd.Stdout = os.Stdout
  41. cmd.Stderr = os.Stderr
  42. err := cmd.Run()
  43. // diff returns exit code 1 if the files differ but it was otherwise
  44. // successful.
  45. if exitError, ok := err.(*exec.ExitError); ok && exitError.Sys().(syscall.WaitStatus).ExitStatus() == 1 {
  46. return nil
  47. }
  48. return err
  49. }
  50. func main() {
  51. flag.Usage = func() {
  52. fmt.Fprintf(os.Stderr, "Usage: diff_asm [flag...] [filter...]\n")
  53. fmt.Fprintf(os.Stderr, "Filter arguments limit to assembly files which match arguments.\n")
  54. fmt.Fprintf(os.Stderr, "If not using a filter, piping to `diffstat` may be useful.\n\n")
  55. flag.PrintDefaults()
  56. }
  57. flag.Parse()
  58. // Find all the assembly files.
  59. var files []string
  60. err := filepath.Walk(*boringsslDir, func(path string, info os.FileInfo, err error) error {
  61. if err != nil {
  62. return nil
  63. }
  64. path, err = filepath.Rel(*boringsslDir, path)
  65. if err != nil {
  66. return err
  67. }
  68. dir := filepath.Base(filepath.Dir(path))
  69. if !info.IsDir() && (dir == "asm" || dir == "perlasm") && strings.HasSuffix(filepath.Base(path), ".pl") {
  70. files = append(files, path)
  71. }
  72. return nil
  73. })
  74. if err != nil {
  75. fmt.Fprintf(os.Stderr, "Error finding assembly: %s\n", err)
  76. os.Exit(1)
  77. }
  78. for _, file := range files {
  79. opensslFile := mapName(file)
  80. if len(opensslFile) == 0 {
  81. continue
  82. }
  83. if flag.NArg() > 0 {
  84. var found bool
  85. for _, arg := range flag.Args() {
  86. if strings.Contains(file, arg) {
  87. found = true
  88. break
  89. }
  90. }
  91. if !found {
  92. continue
  93. }
  94. }
  95. if err := diff(filepath.Join(*opensslDir, opensslFile), filepath.Join(*boringsslDir, file)); err != nil {
  96. fmt.Fprintf(os.Stderr, "Error comparing %s: %s\n", file, err)
  97. os.Exit(1)
  98. }
  99. }
  100. }