| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "path" |
| 5 | "regexp" |
| 6 | "sort" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "unicode/utf8" |
| 11 | ) |
| 12 | |
| 13 | // Why P2's srt run did not fully succeed: the step that failed, its |
| 14 | // exit, whether the run timed out, and the first error lines, bounded |
| 15 | // and with nothing secret in them. The probe's detail carries the short |
| 16 | // form; runner.log the excerpt, once per probe run. |
| 17 | |
| 18 | // p2Phases are the steps of P2's srt run in the order they run. The |
| 19 | // script build stands alone; each package step needs the one before. |
| 20 | var p2Phases = []struct{ key, label string }{ |
| 21 | {"script-build", "script build"}, |
| 22 | {"package-resolve", "package resolve"}, |
| 23 | {"test-build", "test build"}, |
| 24 | {"simulator-boot", "simulator boot"}, |
| 25 | {"test-run", "test run"}, |
| 26 | } |
| 27 | |
| 28 | // p2Succeeded are xcodebuild's own last lines for its three steps that |
| 29 | // build or test (build, build-for-testing, test-without-building). |
| 30 | var p2Succeeded = []string{"** BUILD SUCCEEDED **", "** TEST BUILD SUCCEEDED **", "** TEST EXECUTE SUCCEEDED **"} |
| 31 | |
| 32 | const ( |
| 33 | // p2AttemptLines bounds the report lines kept (15 are expected; the |
| 34 | // first for each attempt counts). |
| 35 | p2AttemptLines = 64 |
| 36 | // p2ErrorLines and p2ExcerptBytes bound the excerpt; p2ShortBytes its |
| 37 | // first line in the probe's detail. |
| 38 | p2ErrorLines = 5 |
| 39 | p2ExcerptBytes = 400 |
| 40 | p2ShortBytes = 120 |
| 41 | ) |
| 42 | |
| 43 | var ( |
| 44 | phaseLine = regexp.MustCompile(`^EQUIPO-PHASE (script-build|package-resolve|test-build|simulator-boot|test-run) ([0-9]{1,3})$`) |
| 45 | // failureLine: xcodebuild's and the compilers' error lines, srt's own |
| 46 | // ("Error: ...", a refused settings file), a nested sandbox that |
| 47 | // could not be applied, a denial a tool printed, a write Unix |
| 48 | // permissions refused, and the fixture finding no targets. |
| 49 | failureLine = regexp.MustCompile(`(?i)\berror:|^refusing to run|^failed to execute|sandbox_apply|sandbox-exec:|\bdeny\(\d+\)|operation not permitted|permission denied|^EQUIPO-PROBE \w+ targets missing`) |
| 50 | ansiEscape = regexp.MustCompile("\x1b\\[[0-9;?]*[ -/]*[@-~]") |
| 51 | ) |
| 52 | |
| 53 | // p2Watch reads the whole of P2's output as it comes, so what decides P2 |
| 54 | // does not depend on the gate's last 200 lines: a full build and test |
| 55 | // prints more than that, and the script build's lines come first. |
| 56 | type p2Watch struct { |
| 57 | mu sync.Mutex |
| 58 | attempts []string |
| 59 | phases map[string]int |
| 60 | succeeded map[string]bool |
| 61 | errors []string |
| 62 | } |
| 63 | |
| 64 | func newP2Watch() *p2Watch { |
| 65 | return &p2Watch{phases: map[string]int{}, succeeded: map[string]bool{}} |
| 66 | } |
| 67 | |
| 68 | func (w *p2Watch) line(l string) { |
| 69 | t := strings.TrimSpace(l) |
| 70 | w.mu.Lock() |
| 71 | defer w.mu.Unlock() |
| 72 | if attemptLine.MatchString(t) { |
| 73 | if len(w.attempts) < p2AttemptLines { |
| 74 | w.attempts = append(w.attempts, t) |
| 75 | } |
| 76 | return |
| 77 | } |
| 78 | if m := phaseLine.FindStringSubmatch(t); m != nil { |
| 79 | if _, seen := w.phases[m[1]]; !seen { |
| 80 | w.phases[m[1]], _ = strconv.Atoi(m[2]) |
| 81 | } |
| 82 | return |
| 83 | } |
| 84 | for _, s := range p2Succeeded { |
| 85 | if t == s { |
| 86 | w.succeeded[s] = true |
| 87 | } |
| 88 | } |
| 89 | if len(w.errors) < p2ErrorLines && failureLine.MatchString(t) { |
| 90 | for _, e := range w.errors { |
| 91 | if e == t { |
| 92 | return |
| 93 | } |
| 94 | } |
| 95 | w.errors = append(w.errors, t) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // attemptsSeen is the report lines, one per line, for ParseAttempts. |
| 100 | func (w *p2Watch) attemptsSeen() string { |
| 101 | w.mu.Lock() |
| 102 | defer w.mu.Unlock() |
| 103 | return strings.Join(w.attempts, "\n") |
| 104 | } |
| 105 | |
| 106 | // ran: the run exited 0, every step said it exited 0, and xcodebuild |
| 107 | // said each build and test succeeded. |
| 108 | func (w *p2Watch) ran(res Result) bool { |
| 109 | w.mu.Lock() |
| 110 | defer w.mu.Unlock() |
| 111 | if res.Code != 0 { |
| 112 | return false |
| 113 | } |
| 114 | for _, p := range p2Phases { |
| 115 | if c, ok := w.phases[p.key]; !ok || c != 0 { |
| 116 | return false |
| 117 | } |
| 118 | } |
| 119 | for _, s := range p2Succeeded { |
| 120 | if !w.succeeded[s] { |
| 121 | return false |
| 122 | } |
| 123 | } |
| 124 | return true |
| 125 | } |
| 126 | |
| 127 | // p2Failure is why the srt run did not fully succeed. |
| 128 | type p2Failure struct { |
| 129 | // Phase is the first step that failed, else the first that did not |
| 130 | // finish: "srt start" when no step reported, "gate start" when the |
| 131 | // gate could not start, "run end" when every step exited 0 and the |
| 132 | // run still did not succeed. Failed is every step that failed, with |
| 133 | // its exit ("script build exit 65"); Unfinished the step the run was |
| 134 | // stopped in ("test build timed out"). |
| 135 | Phase string |
| 136 | Failed []string |
| 137 | Unfinished string |
| 138 | Exit int |
| 139 | TimedOut bool |
| 140 | // Excerpt is up to p2ErrorLines redacted lines, at most |
| 141 | // p2ExcerptBytes; First the first of them, at most p2ShortBytes. |
| 142 | Excerpt, First string |
| 143 | } |
| 144 | |
| 145 | // failure says why a run that did not fully succeed failed. |
| 146 | func (w *p2Watch) failure(res Result, r redactor) p2Failure { |
| 147 | w.mu.Lock() |
| 148 | defer w.mu.Unlock() |
| 149 | f := p2Failure{Exit: res.Code, TimedOut: res.Reason == "checks_timeout"} |
| 150 | stopped := f.TimedOut || res.Reason == "cancelled" |
| 151 | missing := "" |
| 152 | for _, p := range p2Phases { |
| 153 | c, ok := w.phases[p.key] |
| 154 | if ok && c != 0 { |
| 155 | f.Failed = append(f.Failed, p.label+" exit "+strconv.Itoa(c)) |
| 156 | if f.Phase == "" { |
| 157 | f.Phase, f.Exit = p.label, c |
| 158 | } |
| 159 | } |
| 160 | if !ok && missing == "" { |
| 161 | missing = p.label |
| 162 | } |
| 163 | } |
| 164 | switch { |
| 165 | case len(w.phases) == 0 && res.Reason == "checks_unavailable": |
| 166 | missing = "gate start" |
| 167 | case len(w.phases) == 0 && !stopped: |
| 168 | missing = "srt start" |
| 169 | } |
| 170 | if stopped && missing != "" { |
| 171 | f.Unfinished = missing + map[bool]string{true: " timed out", false: " stopped"}[f.TimedOut] |
| 172 | } |
| 173 | if f.Phase == "" { |
| 174 | f.Phase = firstNonEmpty(missing, "run end") |
| 175 | } |
| 176 | var lines []string |
| 177 | size := 0 |
| 178 | for _, e := range w.errors { |
| 179 | if e = cut(r.line(e), p2ExcerptBytes-size); e == "" { |
| 180 | continue |
| 181 | } |
| 182 | lines = append(lines, e) |
| 183 | if size += len(e) + len(" | "); size >= p2ExcerptBytes { |
| 184 | break |
| 185 | } |
| 186 | } |
| 187 | f.Excerpt = cut(strings.Join(lines, " | "), p2ExcerptBytes) |
| 188 | if len(lines) > 0 { |
| 189 | f.First = cut(lines[0], p2ShortBytes) |
| 190 | } |
| 191 | return f |
| 192 | } |
| 193 | |
| 194 | // short is the failure for the probe's detail ("test build exit 65: |
| 195 | // error: ..."); clean then keeps it inside the capability string rules. |
| 196 | func (f p2Failure) short() string { |
| 197 | parts := append([]string{}, f.Failed...) |
| 198 | if f.Unfinished != "" { |
| 199 | parts = append(parts, f.Unfinished) |
| 200 | } |
| 201 | if len(parts) == 0 { |
| 202 | if f.Exit < 0 { |
| 203 | parts = append(parts, f.Phase+" failed") |
| 204 | } else { |
| 205 | parts = append(parts, f.Phase+" exit "+strconv.Itoa(f.Exit)) |
| 206 | } |
| 207 | } |
| 208 | s := strings.Join(parts, ", ") |
| 209 | if f.First != "" { |
| 210 | s += ": " + f.First |
| 211 | } |
| 212 | return s |
| 213 | } |
| 214 | |
| 215 | // redactor takes out of an output line what must never leave the Mac: |
| 216 | // the secrets it is given (the sentinel secret and its prefixes, the |
| 217 | // Keychain service, the targets file's lines and values) and every path |
| 218 | // under a home, kept as its last element only. |
| 219 | type redactor struct { |
| 220 | secrets []string |
| 221 | homes *regexp.Regexp |
| 222 | } |
| 223 | |
| 224 | func newRedactor(secrets, homes []string) redactor { |
| 225 | var s []string |
| 226 | for _, x := range secrets { |
| 227 | if x != "" { |
| 228 | s = append(s, x) |
| 229 | } |
| 230 | } |
| 231 | // Longest first: a prefix must not leave the rest of a longer one. |
| 232 | sort.SliceStable(s, func(i, j int) bool { return len(s[i]) > len(s[j]) }) |
| 233 | h := []string{regexp.QuoteMeta("/Users/")} |
| 234 | for _, x := range homes { |
| 235 | if x = strings.TrimRight(x, "/"); x != "" { |
| 236 | h = append(h, regexp.QuoteMeta(x)) |
| 237 | } |
| 238 | } |
| 239 | sort.SliceStable(h, func(i, j int) bool { return len(h[i]) > len(h[j]) }) |
| 240 | return redactor{secrets: s, homes: regexp.MustCompile("(?:/private)?(?:" + strings.Join(h, "|") + ")[^\\s'\"`:,;()<>\\[\\]]*")} |
| 241 | } |
| 242 | |
| 243 | func (r redactor) line(s string) string { |
| 244 | s = strings.ToValidUTF8(s, "") |
| 245 | s = ansiEscape.ReplaceAllString(s, "") |
| 246 | s = strings.Map(func(c rune) rune { |
| 247 | if c < 0x20 || c == 0x7f || c >= 0x80 && c < 0xa0 { |
| 248 | return ' ' |
| 249 | } |
| 250 | return c |
| 251 | }, s) |
| 252 | s = r.redact(s) |
| 253 | s = r.homes.ReplaceAllStringFunc(s, func(p string) string { |
| 254 | base := path.Base(strings.TrimRight(p, "/")) |
| 255 | if base == "." || base == "/" || base == "Users" { |
| 256 | return "[home]" |
| 257 | } |
| 258 | return "[home]..." + base |
| 259 | }) |
| 260 | // Again after the paths: a last element keeps no part of a secret. |
| 261 | s = r.redact(s) |
| 262 | return strings.Join(strings.Fields(s), " ") |
| 263 | } |
| 264 | |
| 265 | func (r redactor) redact(s string) string { |
| 266 | for _, x := range r.secrets { |
| 267 | s = strings.ReplaceAll(s, x, "[redacted]") |
| 268 | } |
| 269 | return s |
| 270 | } |
| 271 | |
| 272 | // cut keeps at most n bytes of s, whole runes only. |
| 273 | func cut(s string, n int) string { |
| 274 | if n <= 0 { |
| 275 | return "" |
| 276 | } |
| 277 | if len(s) <= n { |
| 278 | return s |
| 279 | } |
| 280 | s = s[:n] |
| 281 | for len(s) > 0 && !utf8.ValidString(s) { |
| 282 | s = s[:len(s)-1] |
| 283 | } |
| 284 | return s |
| 285 | } |
| 286 | |