summaryrefslogtreecommitdiff
path: root/erts/emulator/asan/asan_logs_to_html
blob: 14c9b7fcde02441b0e6c8f4c7415db0247a9a29c (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env escript
%% -*- erlang -*-

%% Parse address sanitizer log files generated from test runs with
%% with environment variables ASAN_LOG_DIR and TS_RUN_EMU=asan set.

%% Repeated leak reports are ignored and additional leaks of same type
%% as seen before are identified as such.

-mode(compile).

main([]) ->
    help();
main(["--help"]) ->
    help();
main([OutDir]) ->
    case os:getenv("ASAN_LOG_DIR") of
	false ->
	    io:format(standard_error,
		      "\nMissing asan log directory argument and environment\n"
		      "variable ASAN_LOG_DIR is not set.\n\n",[]),
	    help();
	InDir ->
	    run(OutDir, InDir)
    end;
main([OutDir, InDir]) ->
    run(OutDir, InDir).


help() ->
    io:format("\nSyntax: asan_log_to_html OutDir [InDir]\n"
	      "\nParses all address-sanetizer log files in InDir\n"
	      "and generates a summary file OutDir/asan_summary.html.\n"
	      "Environment variable ASAN_LOG_DIR is used if InDir\n"
	      "is not specified\n\n", []).

-record(logacc, {srcfile,            % full path of current log file
                 did_output = false, % output contribution from srcfile
                 obuf = [],          % output buffer
                 app = none,         % current application
                 app_err = 0,        % nr of reports from application
                 tc_err = 0,         % nr of reports from srcfile (test case)
                 app_stat_bytes = 0,    % total leaked bytes from app
                 app_stat_blocks = 0,   % total leaked blocks from app
                 app_stat_errors = 0}). % total errors from app

run(OutDir, InDir) ->
    StartTime = erlang:monotonic_time(millisecond),

    {ok, InFilesUS} = file:list_dir(InDir),
    InFiles = lists:sort(InFilesUS),

    OutFile = filename:join(OutDir, "asan_summary.html"),
    {ok, Out} = file:open(OutFile, [write]),

    ok = file:write(Out, <<"<!DOCTYPE html>\n"
                          "<html>\n"
                          "<head><title>Address Sanitizer</title>\n">>),
    ok = file:write(Out, style_block()),
    ok = file:write(Out, <<"</head><body>\n"
		          "<h1>Address Sanitizer</h1>\n">>),

    {_, _, LogAcc2} =
        lists:foldl(fun(File, {LM, RegEx, LogAcc}) ->
                            analyze_log_file(Out, filename:join(InDir,File),
                                             {LM, RegEx, LogAcc})
                    end,
                    {#{}, none, #logacc{}},
                    InFiles),

    LogAcc3 = app_end(Out, LogAcc2),
    try_delete_srcfile(LogAcc3),

    Time = calendar:system_time_to_rfc3339(erlang:system_time(second),
                                           [{time_designator, 32}]),
    %%{_, _, ThisFile} = code:get_object_code(?MODULE),
    ThisFile = escript:script_name(),
    User = string:trim(os:cmd("whoami")),
    {ok, Host} = inet:gethostname(),
    Seconds = (erlang:monotonic_time(millisecond) - StartTime) / 1000,
    ok = io:format(Out, "\n<hr><p><small>This page was generated ~s\n"
                   " by <tt>~s</tt>\n"
                   " run by ~s@~s in ~.1f seconds.</small></p>\n",
                   [Time, ThisFile, User, Host, Seconds]),

    ok = file:write(Out, script_block()),
    ok = file:write(Out, <<"</body>\n</html>\n">>),
    ok = file:close(Out),
    io:format("Generated file ~s\n", [OutFile]),
    ok.

analyze_log_file(Out, SrcFile, {LeakMap0, RegEx0, LogAcc0}=Acc) ->

    #logacc{app=PrevApp} = LogAcc0,

    case filelib:is_regular(SrcFile) of
        false ->
            Acc;
        true ->
            FileName = filename:basename(SrcFile),
            %%io:format("analyze ~s\n", [FileName]),

            %% Is it a new application?
            LogAcc2 = case string:lexemes(FileName, "-") of
                          [_Exe, PrevApp | _] ->
                              try_delete_srcfile(LogAcc0),
                              LogAcc0#logacc{srcfile=SrcFile,
                                             tc_err=0,
                                             did_output=false};
                          [_Exe, NewApp | _] ->
                              LogAcc1 = app_end(Out, LogAcc0),
                              try_delete_srcfile(LogAcc1),
                              LogAcc1#logacc{srcfile=SrcFile,
                                             obuf=[],
                                             app=NewApp,
                                             app_err=0,
                                             tc_err=0,
                                             did_output=false,
                                             app_stat_bytes=0,
                                             app_stat_blocks=0,
                                             app_stat_errors=0}
                      end,

            case LogAcc2#logacc.app_err of
                truncated ->
                    {LeakMap0, RegEx0, LogAcc2};
                _ ->
                    {ok, Bin} = file:read_file(SrcFile),
                    match_loop(Out, Bin, RegEx0, LogAcc2, 0, [], LeakMap0)
            end
    end.

-define(APP_ERR_LIMIT, 200).

match_loop(Out, _, RegEx, #logacc{app_err=AppErr}=LogAcc0, _, _, LM)
  when AppErr >= ?APP_ERR_LIMIT ->

    Txt = [io_format("<h2>WARNING!!! Log truncated for application ~p,"
                     " more than ~p errors found.</h2>\n",
                     [LogAcc0#logacc.app, ?APP_ERR_LIMIT])],
    LogAcc1 = log_error(Out, LogAcc0, Txt),
    {LM, RegEx, LogAcc1#logacc{app_err=truncated}};

match_loop(Out, Bin, RegEx0, LogAcc0, PrevEnd, Unmatched0, LM0) ->
    {Match, RegEx1} =
	run_regex(Bin, RegEx0,
		  %% LeakReport
		  "(?:(Direct|Indirect) leak of ([0-9]+) byte\\(s\\) "
		  "in ([0-9]+) object\\(s\\) allocated from:\n"
		  "((?:[ \t]*#[0-9]+.+\n)+))" % Call stack
		  "|"
		  %% ErrorReport
		  "(?:(==ERROR: AddressSanitizer:.*\n"
		  "(?:.*\n)+?)"   % any lines (non-greedy)
		  "^(?:==|--))"   % stop at line begining with == or --
		  "|"
		  %% Skipped
		  "(?:^[=-]+$)"  % skip lines consisting only of = or -
                  "|"
                  "Objects leaked above:\n" % if LSAN_OPTIONS="report_objects=1"
                  "(?:0x.+\n)+"
                  "|"
                  "^\n", % empty lines
		  [multiline],
		  [{offset, PrevEnd}, {capture, all, index}]),


    BP = fun(PartIx) -> binary:part(Bin, PartIx) end,

    case Match of
        [ErrorReport, {-1,0}, {-1,0}, {-1,0}, {-1,0}, Captured] ->
            {Start,MatchLen} = ErrorReport,
            Txt = [io_format("<pre~s>\n", [style(error)]),
                   file_write(BP(Captured)),
                   io_format("</pre>\n", [])],
            LogAcc1 = log_error(Out, LogAcc0, Txt),
            Unmatched1 = [BP({PrevEnd, Start-PrevEnd}) | Unmatched0],
            End = Start + MatchLen,
            match_loop(Out, Bin, RegEx1, app_stats(LogAcc1,0,0,1),
                       End, Unmatched1, LM0);

        [LeakReport, TypeIx, BytesIx, BlocksIx, StackIx | _] ->
            {Start, MatchLen} = LeakReport,
            Bytes = binary_to_integer(BP(BytesIx)),
            Blocks = binary_to_integer(BP(BlocksIx)),
            End = Start + MatchLen,
            Unmatched1 = [BP({PrevEnd, Start-PrevEnd})|Unmatched0],
            TypeBin = BP(TypeIx),

            %% We indentify a leak by its type (direct or indirect)
            %% and its full call stack.
            Key = {TypeBin, BP(StackIx)},
            {LogAcc2, LM2} =
                case lookup_leak(LM0, Key) of
                    undefined ->
                        %% A new leak
                        LM1 = insert_leak(LM0, Key, Bytes, Blocks),
                        Txt = [io_format("<pre~s>\n", [style(new, TypeBin)]),
                               file_write(BP(LeakReport)),
                               io_format("</pre>\n", [])],
                        LogAcc1 = log_error(Out, LogAcc0, Txt),
                        {app_stats(LogAcc1,Bytes,Blocks,0), LM1};

                    {Bytes, Blocks} ->
                        %% Exact same leak(s) repeated, ignore
                        {LogAcc0, LM0};

                    {OldBytes, OldBlocks} ->
                        %% More leaked bytes/blocks of same type&stack as before
                        LM1 = insert_leak(LM0, Key, Bytes, Blocks),
                        ByteDiff = Bytes - OldBytes,
                        BlockDiff = Blocks - OldBlocks,
                        Txt = [io_format("<pre~s>\n", [style(more, TypeBin)]),
                               io_format("More ~s leak of ~w(~w) byte(s) "
                                         "in ~w(~w) object(s) allocated from:\n",
                                         [TypeBin, ByteDiff, Bytes, BlockDiff, Blocks]),
                              file_write(BP(StackIx)),
                              io_format("</pre>\n", [])],
                        LogAcc1 = log_error(Out, LogAcc0, Txt),
                        {app_stats(LogAcc1, ByteDiff, BlockDiff, 0), LM1}
                end,
            match_loop(Out, Bin, RegEx1, LogAcc2, End, Unmatched1, LM2);

        [SkipLine] ->
            {Start, MatchLen} = SkipLine,
            %%nomatch = binary:match(BP(SkipLine), <<"\n">>), % Assert single line
            End = Start + MatchLen,
            Unmatched1 = [BP({PrevEnd, Start-PrevEnd})|Unmatched0],
            match_loop(Out, Bin, RegEx1, LogAcc0, End, Unmatched1, LM0);

        nomatch ->
            Unmatched1 = [BP({PrevEnd, byte_size(Bin)-PrevEnd}) | Unmatched0],

            LogAcc1 =
                case iolist_size(Unmatched1) > 500 of
                    true ->
                        Txt = [io_format("<h2>WARNING!!! May be unmatched error reports"
                                         " in file ~s:</h2>\n<pre>~s</pre>",
                                         [LogAcc0#logacc.srcfile, Unmatched1])],
                        log_error(Out, LogAcc0, Txt);
                    false ->
                        LogAcc0
                end,
            {LM0, RegEx1, LogAcc1}
    end.

lookup_leak(LeakMap, Key) ->
    maps:get(Key, LeakMap, undefined).

insert_leak(LeakMap, Key, Bytes, Blocks) ->
    LeakMap#{Key => {Bytes, Blocks}}.

log_error(_Out, #logacc{app_err=AppErr, tc_err=TcErr}=LogAcc, Txt0) ->
    {DidTc, Txt1} =
        case TcErr of
            0 ->
                %% First error in test case, print test case header
                SrcFile = LogAcc#logacc.srcfile,
                TcFile = filename:basename(SrcFile),
                Hdr = case string:lexemes(TcFile, "-") of
                          [_Exe, App, _Rest] ->
                              io_format("<h3>Before first test case of ~s</h3>\n",
                                        [App]);
                          [_Exe, _App, "tc", Num, Mod, Rest] ->
                              [Func | _] = string:lexemes(Rest, "."),
                              io_format("<h3>Test case #~s ~s:~s</h3>\n",
                                        [Num, Mod, Func]);
                          _ ->
                              io_format("<h3>Strange log file name '~s'</h3>\n",
                                        [SrcFile])
                      end,
                {true, [Hdr | Txt0]};
            _ ->
                {false, Txt0}
        end,
    LogAcc#logacc{app_err=AppErr+1, tc_err=TcErr+1,
                  obuf = [Txt1 | LogAcc#logacc.obuf],
                  did_output = (LogAcc#logacc.did_output or DidTc)}.

app_stats(#logacc{}=LogAcc, Bytes, Blocks, Errors) ->
    LogAcc#logacc{app_stat_bytes = LogAcc#logacc.app_stat_bytes + Bytes,
                  app_stat_blocks = LogAcc#logacc.app_stat_blocks + Blocks,
                  app_stat_errors = LogAcc#logacc.app_stat_errors + Errors}.


app_end(Out, LogAcc) ->
    case LogAcc of
        #logacc{app=none} ->
            LogAcc;
        #logacc{app_err=0} ->
            ok = io:format(Out, "<button class=\"app_ok\" disabled>~s</button>\n",
                           [LogAcc#logacc.app]),
            LogAcc#logacc{did_output = true};
        #logacc{} ->
            %% Print red clickable app button with stats
            %% and all the buffered logs.
            ok = io:format(Out, "<button type=\"button\" class=\"app_err\">~s"
                           "<span class=\"stats\">Leaks: ~p bytes in ~p blocks</span>",
                           [LogAcc#logacc.app,
                            LogAcc#logacc.app_stat_bytes,
                            LogAcc#logacc.app_stat_blocks]),
            case LogAcc#logacc.app_stat_errors of
                0 -> ignore;
                _ ->
                    ok = io:format(Out, "<span class=\"stats\">Errors: ~p</span>",
                                   [LogAcc#logacc.app_stat_errors])
            end,
            ok = io:format(Out, "</button>\n"
                           "<div class=\"content\">", []),

            flush_obuf(Out, LogAcc#logacc.obuf),

            ok = io:format(Out, "<button type=\"button\" "
                           "class=\"app_err_end\">"
                           "end of ~s</button>\n", [LogAcc#logacc.app]),
            ok = io:format(Out, "</div>", []),
            LogAcc
    end.

flush_obuf(Out, Obuf) ->
    flush_obuf_rev(Out, lists:reverse(Obuf)).

flush_obuf_rev(_Out, []) -> ok;
flush_obuf_rev(Out, [Txt | T]) ->
    [OutFun(Out) || OutFun <- Txt],
    flush_obuf_rev(Out, T).

io_format(Frmt, List) ->
    fun(Out) -> io:format(Out, Frmt, List) end.

file_write(Bin) ->
    fun(Out) -> file:write(Out, Bin) end.

style(error) ->
    " style=\"background-color:Tomato;\"".

style(new, <<"Direct">>) ->
    " style=\"background-color:orange;\"";
style(new, <<"Indirect">>) ->
    "";
style(more, _) ->
    " style=\"background-color:yellow;\"".


run_regex(Bin, none, RegExString, CompileOpts, RunOpts) ->
    {ok, RegEx} = re:compile(RegExString, CompileOpts),
    run_regex(Bin, RegEx, none, none, RunOpts);
run_regex(Bin, RegEx, _, _, RunOpts) ->
    case re:run(Bin, RegEx, RunOpts) of
	nomatch ->
	    {nomatch, RegEx};
	{match, Match} ->
	    {Match, RegEx}
    end.

try_delete_srcfile(LogAcc) ->
    case LogAcc of
        #logacc{srcfile=undefined} ->
            ignore;
        #logacc{did_output=false} ->
            %% This file did not contribute any output.
            %% Optimize future script invokations by removing it.
            delete_file(LogAcc#logacc.srcfile);
        _ ->
            keep
    end.

delete_file(File) ->
    case filelib:is_regular(File) of
        true ->
            io:format("Delete file ~p\n", [File]),
            Dir = filename:dirname(File),
            Name = filename:basename(File),
            Trash = filename:join([Dir, "DELETED", Name]),
            ok = filelib:ensure_dir(Trash),
            ok = file:rename(File, Trash);
        false ->
            ignore
    end.

style_block() ->
    <<"<style>

.app_err, .app_err_end, .app_ok {
  color: white;
  padding: 10px;
  /*border: none;*/
  text-align: left;
  /*outline: none;*/
  font-size: 15px;
}

.app_err {
  width: 100%;
  background-color: #D11;
  cursor: pointer;
}
.app_err:hover {
  background-color: #F11;
}
.app_err_end {
  background-color: #D11;
  cursor: pointer;
}
.app_err_end:hover {
  background-color: #F11;
}

.app_ok {
  width: 100%;
  background-color: #292;
}

.stats {
  font-style: italic;
  margin-left: 50px;
}

.content {
  padding: 0 18px;
  display: none;
  overflow: hidden;
  background-color: #f1f1f1;
}
</style>
">>.

script_block() ->
    <<"<script>
var app_err = document.getElementsByClassName(\"app_err\");
var i;

for (i = 0; i < app_err.length; i++) {
  app_err[i].addEventListener(\"click\", function() {
    var content = this.nextElementSibling;
    if (content.style.display === \"block\") {
      content.style.display = \"none\";
    } else {
      content.style.display = \"block\";
    }
  });
}

var app_err_end = document.getElementsByClassName(\"app_err_end\");
for (i = 0; i < app_err_end.length; i++) {
  app_err_end[i].addEventListener(\"click\", function() {
    var content = this.parentElement;
    content.style.display = \"none\";
  });
}

</script>
">>.