summaryrefslogtreecommitdiffstats
path: root/scripts/update-crash-signatures.pl
blob: 324495f0efd3c85b2990e14c889c1168c9c0c3b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/perl

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use strict;
use warnings;
use lib qw(. lib local/lib/perl5);
$| = 1;


use constant BATCH_SIZE => 100;

use Bugzilla;
use Bugzilla::Bug;
use Bugzilla::Constants;
use Bugzilla::Util qw( trim );
use List::MoreUtils qw( any );
use Text::Balanced qw( extract_bracketed extract_multiple );

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);

my $user = Bugzilla::User->check({name => 'automation@bmo.tld'});
$user->{groups}       = [Bugzilla::Group->get_all];
$user->{bless_groups} = [Bugzilla::Group->get_all];
Bugzilla->set_user($user);

my $dbh = Bugzilla->dbh;

# find the bugs

my $bugs = $dbh->selectall_arrayref(
  "SELECT bug_id,cf_crash_signature FROM bugs WHERE resolution = '' AND cf_crash_signature != ''",
  {Slice => {}}
);
my $count = scalar @$bugs;

# update

die "No bugs found\n" unless $count;
print
  "Found $count open bug(s) with crash signatures\nPress <Ctrl-C> to stop or <Enter> to continue..\n";
getc();

my $updated = 0;
foreach my $rh_bug (@$bugs) {
  my $bug_id    = $rh_bug->{bug_id};
  my $signature = $rh_bug->{cf_crash_signature};

  # check for updated signature
  my $collapsed = collapse($signature);
  next if is_same($signature, $collapsed);

 # ignore signatures malformed in a way that would result in updating on each pass
  next if $collapsed ne collapse($collapsed);

  # update the bug, preventing bugmail
  print "$bug_id\n";
  $dbh->bz_start_transaction;
  my $bug = Bugzilla::Bug->check($bug_id);
  $bug->set_all({cf_crash_signature => $collapsed});
  $bug->update();
  $dbh->do("UPDATE bugs SET lastdiffed = delta_ts WHERE bug_id = ?",
    undef, $bug_id);
  $dbh->bz_commit_transaction;

  # object caching causes us to consume a lot of memory
  # process in batches
  last if ++$updated == BATCH_SIZE;
}
print "Updated $updated bugs(s)\n";

sub is_same {
  my ($old, $new) = @_;
  $old =~ s/[\015\012]+/ /g;
  $new =~ s/[\015\012]+/ /g;
  return trim($old) eq trim($new);
}

sub collapse {
  my ($crash_signature) = @_;

  # ignore completely invalid signatures
  return $crash_signature
    unless $crash_signature =~ /\[/ && $crash_signature =~ /\]/;

  # split
  my @signatures = grep {/\S/}
    extract_multiple($crash_signature, [sub { extract_bracketed($_[0], '[]') }]);
  my @unbracketed = map { unbracketed($_) } @signatures;

  foreach my $signature (@signatures) {

    # ignore invalid signatures
    next unless $signature =~ /^\s*\[/;
    next if unbracketed($signature) =~ /\.\.\.$/;

    # collpase
    my $collapsed = collapse_crash_sig({
      signature         => $signature,
      open              => '<',
      replacement_open  => '<',
      close             => '>',
      replacement_close => 'T>',
      exceptions        => [],
    });
    $collapsed = collapse_crash_sig({
      signature         => $collapsed,
      open              => '(',
      replacement_open  => '',
      close             => ')',
      replacement_close => '',
      exceptions        => ['anonymous namespace', 'operator'],
    });
    $collapsed =~ s/\s+/ /g;

    # ignore sigs that collapse down to nothing
    next if $collapsed eq '[@ ]';

    # don't create duplicates
    my $unbracketed = unbracketed($collapsed);
    next if any { $unbracketed eq $_ } @unbracketed;

    push @signatures,  $collapsed;
    push @unbracketed, $unbracketed;
  }

  return join("\015\012", map { trim($_) } @signatures);
}

sub unbracketed {
  my ($signature) = @_;
  $signature =~ s/(^\s*\[\s*|\s*\]\s*$)//g;
  return $signature;
}

# collapsing code lifted from socorro:
# https://github.com/mozilla/socorro/blob/master/socorro/processor/signature_utilities.py#L110

my ($target_counter, $exception_mode, @collapsed);

sub append_if_not_in_collapse_mode {
  my ($character) = @_;
  if (!$target_counter) {
    push @collapsed, $character;
  }
}

sub is_exception {
  my ($exceptions, $remaining_original_line, $line_up_to_current_position) = @_;
  foreach my $exception (@$exceptions) {
    if (substr($remaining_original_line, 0, length($exception)) eq $exception) {
      return 1;
    }
    if (substr($line_up_to_current_position, -length($exception)) eq $exception) {
      return 1;
    }
  }
  return 0;
}

sub collapse_crash_sig {
  my ($params) = @_;

  $target_counter = 0;
  @collapsed      = ();
  $exception_mode = 0;
  my $signature = $params->{signature};

  for (my $i = 0; $i < length($signature); $i++) {
    my $character = substr($signature, $i, 1);
    if ($character eq $params->{open}) {
      if (is_exception(
        $params->{exceptions},
        substr($signature, $i + 1),
        substr($signature, 0, $i)
      ))
      {
        $exception_mode = 1;
        append_if_not_in_collapse_mode($character);
        next;
      }
      append_if_not_in_collapse_mode($params->{replacement_open});
      $target_counter++;
    }
    elsif ($character eq $params->{close}) {
      if ($exception_mode) {
        append_if_not_in_collapse_mode($character);
        $exception_mode = 0;
      }
      else {
        $target_counter--;
        append_if_not_in_collapse_mode($params->{replacement_close});
      }
    }
    else {
      append_if_not_in_collapse_mode($character);
    }
  }

  return join '', @collapsed;
}