out-of-tree kernel {module, exploit} development tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2019 Mikhail Klementev. All rights reserved.
  2. // Use of this source code is governed by a AGPLv3 license
  3. // (or later) that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "database/sql"
  7. "encoding/json"
  8. "fmt"
  9. "log"
  10. "math"
  11. "os"
  12. "github.com/olekukonko/tablewriter"
  13. "gopkg.in/logrusorgru/aurora.v2"
  14. "code.dumpstack.io/tools/out-of-tree/config"
  15. )
  16. func logLogEntry(l logEntry) {
  17. distroInfo := fmt.Sprintf("%s-%s {%s}", l.DistroType,
  18. l.DistroRelease, l.KernelRelease)
  19. artifactInfo := fmt.Sprintf("{[%s] %s}", l.Type, l.Name)
  20. colored := ""
  21. if l.Type == config.KernelExploit {
  22. colored = aurora.Sprintf("[%4d %4s] [%s] %40s %40s: %s %s",
  23. l.ID, l.Tag, l.Timestamp, artifactInfo, distroInfo,
  24. genOkFail("BUILD", l.Build.Ok),
  25. genOkFail("LPE", l.Test.Ok))
  26. } else {
  27. colored = aurora.Sprintf("[%4d %4s] [%s] %40s %40s: %s %s %s",
  28. l.ID, l.Tag, l.Timestamp, artifactInfo, distroInfo,
  29. genOkFail("BUILD", l.Build.Ok),
  30. genOkFail("INSMOD", l.Run.Ok),
  31. genOkFail("TEST", l.Test.Ok))
  32. }
  33. additional := ""
  34. if l.KernelPanic {
  35. additional = "(panic)"
  36. } else if l.KilledByTimeout {
  37. additional = "(timeout)"
  38. }
  39. if additional != "" {
  40. fmt.Println(colored, additional)
  41. } else {
  42. fmt.Println(colored)
  43. }
  44. }
  45. func logHandler(db *sql.DB, path, tag string, num int, rate bool) (err error) {
  46. var les []logEntry
  47. ka, kaErr := config.ReadArtifactConfig(path + "/.out-of-tree.toml")
  48. if kaErr == nil {
  49. log.Println(".out-of-tree.toml found, filter by artifact name")
  50. les, err = getAllArtifactLogs(db, tag, num, ka)
  51. } else {
  52. les, err = getAllLogs(db, tag, num)
  53. }
  54. if err != nil {
  55. return
  56. }
  57. s := "\nS"
  58. if rate {
  59. if kaErr != nil {
  60. err = kaErr
  61. return
  62. }
  63. s = fmt.Sprintf("{[%s] %s} Overall s", ka.Type, ka.Name)
  64. les, err = getAllArtifactLogs(db, tag, math.MaxInt64, ka)
  65. if err != nil {
  66. return
  67. }
  68. } else {
  69. for _, l := range les {
  70. logLogEntry(l)
  71. }
  72. }
  73. success := 0
  74. for _, l := range les {
  75. if l.Test.Ok {
  76. success++
  77. }
  78. }
  79. overall := float64(success) / float64(len(les))
  80. fmt.Printf("%success rate: %.04f (~%.0f%%)\n",
  81. s, overall, overall*100)
  82. return
  83. }
  84. func logDumpHandler(db *sql.DB, id int) (err error) {
  85. l, err := getLogByID(db, id)
  86. if err != nil {
  87. return
  88. }
  89. fmt.Println("ID:", l.ID)
  90. fmt.Println("Date:", l.Timestamp)
  91. fmt.Println("Tag:", l.Tag)
  92. fmt.Println()
  93. fmt.Println("Type:", l.Type.String())
  94. fmt.Println("Name:", l.Name)
  95. fmt.Println()
  96. fmt.Println("Distro:", l.DistroType.String(), l.DistroRelease)
  97. fmt.Println("Kernel:", l.KernelRelease)
  98. fmt.Println()
  99. fmt.Println("Build ok:", l.Build.Ok)
  100. if l.Type == config.KernelModule {
  101. fmt.Println("Insmod ok:", l.Run.Ok)
  102. }
  103. fmt.Println("Test ok:", l.Test.Ok)
  104. fmt.Println()
  105. fmt.Printf("Build output:\n%s\n", l.Build.Output)
  106. fmt.Println()
  107. if l.Type == config.KernelModule {
  108. fmt.Printf("Insmod output:\n%s\n", l.Run.Output)
  109. fmt.Println()
  110. }
  111. fmt.Printf("Test output:\n%s\n", l.Test.Output)
  112. fmt.Println()
  113. fmt.Printf("Qemu stdout:\n%s\n", l.Stdout)
  114. fmt.Println()
  115. fmt.Printf("Qemu stderr:\n%s\n", l.Stderr)
  116. fmt.Println()
  117. return
  118. }
  119. type runstat struct {
  120. All, BuildOK, RunOK, TestOK, Timeout, Panic int
  121. }
  122. func getStats(db *sql.DB, path, tag string) (
  123. distros map[string]map[string]map[string]runstat, err error) {
  124. var les []logEntry
  125. ka, kaErr := config.ReadArtifactConfig(path + "/.out-of-tree.toml")
  126. if kaErr == nil {
  127. les, err = getAllArtifactLogs(db, tag, -1, ka)
  128. } else {
  129. les, err = getAllLogs(db, tag, -1)
  130. }
  131. if err != nil {
  132. return
  133. }
  134. distros = make(map[string]map[string]map[string]runstat)
  135. for _, l := range les {
  136. _, ok := distros[l.DistroType.String()]
  137. if !ok {
  138. distros[l.DistroType.String()] = make(map[string]map[string]runstat)
  139. }
  140. _, ok = distros[l.DistroType.String()][l.DistroRelease]
  141. if !ok {
  142. distros[l.DistroType.String()][l.DistroRelease] = make(map[string]runstat)
  143. }
  144. rs := distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease]
  145. rs.All++
  146. if l.Build.Ok {
  147. rs.BuildOK++
  148. }
  149. if l.Run.Ok {
  150. rs.RunOK++
  151. }
  152. if l.Test.Ok {
  153. rs.TestOK++
  154. }
  155. if l.KernelPanic {
  156. rs.Panic++
  157. }
  158. if l.KilledByTimeout {
  159. rs.Timeout++
  160. }
  161. distros[l.DistroType.String()][l.DistroRelease][l.KernelRelease] = rs
  162. }
  163. return
  164. }
  165. func logJSONHandler(db *sql.DB, path, tag string) (err error) {
  166. distros, err := getStats(db, path, tag)
  167. if err != nil {
  168. return
  169. }
  170. bytes, err := json.Marshal(&distros)
  171. if err != nil {
  172. return
  173. }
  174. fmt.Println(string(bytes))
  175. return
  176. }
  177. func logMarkdownHandler(db *sql.DB, path, tag string) (err error) {
  178. distros, err := getStats(db, path, tag)
  179. if err != nil {
  180. return
  181. }
  182. table := tablewriter.NewWriter(os.Stdout)
  183. table.SetHeader([]string{"Distro", "Release", "Kernel", "Reliability"})
  184. table.SetBorders(tablewriter.Border{
  185. Left: true, Top: false, Right: true, Bottom: false})
  186. table.SetCenterSeparator("|")
  187. for distro, releases := range distros {
  188. for release, kernels := range releases {
  189. for kernel, stats := range kernels {
  190. all := float64(stats.All)
  191. ok := float64(stats.TestOK)
  192. r := fmt.Sprintf("%6.02f%%", (ok/all)*100)
  193. table.Append([]string{distro, release, kernel, r})
  194. }
  195. }
  196. }
  197. table.Render()
  198. return
  199. }