|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// ReportFormat specifies the output format for reports. |
| 11 | +type ReportFormat string |
| 12 | + |
| 13 | +const ( |
| 14 | + // FormatText outputs a human-readable text report. |
| 15 | + FormatText ReportFormat = "text" |
| 16 | + |
| 17 | + // FormatJSON outputs a JSON report. |
| 18 | + FormatJSON ReportFormat = "json" |
| 19 | + |
| 20 | + // FormatGitHubActions outputs annotations for GitHub Actions. |
| 21 | + FormatGitHubActions ReportFormat = "github" |
| 22 | +) |
| 23 | + |
| 24 | +// WriteReport writes a report to the given writer in the specified format. |
| 25 | +func WriteReport(w io.Writer, report *Report, format ReportFormat) error { |
| 26 | + switch format { |
| 27 | + case FormatJSON: |
| 28 | + return writeJSONReport(w, report) |
| 29 | + case FormatGitHubActions: |
| 30 | + return writeGitHubActionsReport(w, report) |
| 31 | + default: |
| 32 | + return writeTextReport(w, report) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// writeTextReport writes a human-readable text report. |
| 37 | +func writeTextReport(w io.Writer, report *Report) error { |
| 38 | + fmt.Fprintf(w, "Constant-Time Analysis Report\n") |
| 39 | + fmt.Fprintf(w, "==============================\n\n") |
| 40 | + fmt.Fprintf(w, "Architecture: %s (%s)\n", report.Architecture, report.GOARCH) |
| 41 | + fmt.Fprintf(w, "Functions analyzed: %d\n", report.TotalFunctions) |
| 42 | + fmt.Fprintf(w, "Instructions analyzed: %d\n\n", report.TotalInstructions) |
| 43 | + |
| 44 | + if len(report.Violations) == 0 { |
| 45 | + fmt.Fprintf(w, "No violations found.\n\n") |
| 46 | + } else { |
| 47 | + // Group violations by function |
| 48 | + byFunction := make(map[string][]Violation) |
| 49 | + for _, v := range report.Violations { |
| 50 | + byFunction[v.Function] = append(byFunction[v.Function], v) |
| 51 | + } |
| 52 | + |
| 53 | + for fn, violations := range byFunction { |
| 54 | + fmt.Fprintf(w, "Function: %s\n", fn) |
| 55 | + if violations[0].File != "" { |
| 56 | + fmt.Fprintf(w, " File: %s\n", violations[0].File) |
| 57 | + } |
| 58 | + fmt.Fprintf(w, "\n") |
| 59 | + |
| 60 | + for _, v := range violations { |
| 61 | + severityStr := strings.ToUpper(v.Severity) |
| 62 | + fmt.Fprintf(w, " [%s] 0x%x: %s\n", severityStr, v.Address, v.Instruction) |
| 63 | + fmt.Fprintf(w, " %s\n\n", v.Reason) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fmt.Fprintf(w, "Summary\n") |
| 69 | + fmt.Fprintf(w, "-------\n") |
| 70 | + fmt.Fprintf(w, "Errors: %d\n", report.ErrorCount) |
| 71 | + fmt.Fprintf(w, "Warnings: %d\n", report.WarningCount) |
| 72 | + |
| 73 | + if report.Passed { |
| 74 | + fmt.Fprintf(w, "\nPASSED\n") |
| 75 | + } else { |
| 76 | + fmt.Fprintf(w, "\nFAILED\n") |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// JSONReport is the JSON-serializable report structure. |
| 83 | +type JSONReport struct { |
| 84 | + Architecture string `json:"architecture"` |
| 85 | + GOARCH string `json:"goarch"` |
| 86 | + GOOS string `json:"goos,omitempty"` |
| 87 | + TotalFunctions int `json:"total_functions"` |
| 88 | + TotalInstructions int `json:"total_instructions"` |
| 89 | + ErrorCount int `json:"error_count"` |
| 90 | + WarningCount int `json:"warning_count"` |
| 91 | + Passed bool `json:"passed"` |
| 92 | + Violations []JSONViolation `json:"violations"` |
| 93 | +} |
| 94 | + |
| 95 | +// JSONViolation is the JSON-serializable violation structure. |
| 96 | +type JSONViolation struct { |
| 97 | + Function string `json:"function"` |
| 98 | + File string `json:"file,omitempty"` |
| 99 | + Address string `json:"address"` |
| 100 | + Instruction string `json:"instruction"` |
| 101 | + Mnemonic string `json:"mnemonic"` |
| 102 | + Reason string `json:"reason"` |
| 103 | + Severity string `json:"severity"` |
| 104 | +} |
| 105 | + |
| 106 | +// writeJSONReport writes a JSON report. |
| 107 | +func writeJSONReport(w io.Writer, report *Report) error { |
| 108 | + jsonReport := JSONReport{ |
| 109 | + Architecture: report.Architecture, |
| 110 | + GOARCH: report.GOARCH, |
| 111 | + GOOS: report.GOOS, |
| 112 | + TotalFunctions: report.TotalFunctions, |
| 113 | + TotalInstructions: report.TotalInstructions, |
| 114 | + ErrorCount: report.ErrorCount, |
| 115 | + WarningCount: report.WarningCount, |
| 116 | + Passed: report.Passed, |
| 117 | + Violations: make([]JSONViolation, len(report.Violations)), |
| 118 | + } |
| 119 | + |
| 120 | + for i, v := range report.Violations { |
| 121 | + jsonReport.Violations[i] = JSONViolation{ |
| 122 | + Function: v.Function, |
| 123 | + File: v.File, |
| 124 | + Address: fmt.Sprintf("0x%x", v.Address), |
| 125 | + Instruction: v.Instruction, |
| 126 | + Mnemonic: v.Mnemonic, |
| 127 | + Reason: v.Reason, |
| 128 | + Severity: v.Severity, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + encoder := json.NewEncoder(w) |
| 133 | + encoder.SetIndent("", " ") |
| 134 | + return encoder.Encode(jsonReport) |
| 135 | +} |
| 136 | + |
| 137 | +// writeGitHubActionsReport writes GitHub Actions workflow commands. |
| 138 | +func writeGitHubActionsReport(w io.Writer, report *Report) error { |
| 139 | + for _, v := range report.Violations { |
| 140 | + // GitHub Actions annotation format: |
| 141 | + // ::error file={name},line={line},endLine={endLine},title={title}::{message} |
| 142 | + // ::warning file={name},line={line},endLine={endLine},title={title}::{message} |
| 143 | + |
| 144 | + level := "error" |
| 145 | + if v.Severity == "warning" { |
| 146 | + level = "warning" |
| 147 | + } |
| 148 | + |
| 149 | + title := fmt.Sprintf("Constant-time violation: %s", v.Mnemonic) |
| 150 | + message := fmt.Sprintf("%s in %s: %s", v.Instruction, v.ShortFunction, v.Reason) |
| 151 | + |
| 152 | + if v.File != "" { |
| 153 | + fmt.Fprintf(w, "::%s file=%s,title=%s::%s\n", level, v.File, title, message) |
| 154 | + } else { |
| 155 | + fmt.Fprintf(w, "::%s title=%s::%s\n", level, title, message) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Write summary |
| 160 | + if report.Passed { |
| 161 | + fmt.Fprintf(w, "::notice::Constant-time analysis passed. %d functions, %d instructions analyzed.\n", |
| 162 | + report.TotalFunctions, report.TotalInstructions) |
| 163 | + } else { |
| 164 | + fmt.Fprintf(w, "::error::Constant-time analysis failed. %d error(s) found.\n", report.ErrorCount) |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
0 commit comments