2 #===- lib/fuzzer/scripts/unbalanced_allocs.py ------------------------------===#
4 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 # See https://llvm.org/LICENSE.txt for license information.
6 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 #===------------------------------------------------------------------------===#
10 # Post-process -trace_malloc=2 output and printout only allocations and frees
11 # unbalanced inside of fuzzer runs.
13 # my_fuzzer -trace_malloc=2 -runs=10 2>&1 | unbalanced_allocs.py -skip=5
15 #===------------------------------------------------------------------------===#
22 def PrintStack(line
, stack
):
26 print('Unbalanced ' + line
.rstrip());
30 def ProcessStack(line
, f
):
32 while line
and line
.startswith(' #'):
37 def ProcessFree(line
, f
, allocs
):
38 if not line
.startswith('FREE['):
41 addr
= int(line
.split()[1], 16)
42 next_line
, stack
= ProcessStack(f
.readline(), f
)
46 PrintStack(line
, stack
)
49 def ProcessMalloc(line
, f
, allocs
):
50 if not line
.startswith('MALLOC['):
51 return ProcessFree(line
, f
, allocs
)
53 addr
= int(line
.split()[1], 16)
54 assert not addr
in allocs
56 next_line
, stack
= ProcessStack(f
.readline(), f
)
57 allocs
[addr
] = (line
, stack
)
60 def ProcessRun(line
, f
):
61 if not line
.startswith('MallocFreeTracer: START'):
62 return ProcessMalloc(line
, f
, {})
68 if line
.startswith('MallocFreeTracer: STOP'):
71 for _
, (l
, s
) in allocs
.items():
75 line
= ProcessMalloc(line
, f
, allocs
)
81 line
= ProcessRun(line
, f
);
84 parser
= argparse
.ArgumentParser()
85 parser
.add_argument('--skip', default
=0, help='number of runs to ignore')
86 args
= parser
.parse_args()
88 _skip
= int(args
.skip
) + 1
89 ProcessFile(sys
.stdin
)
91 if __name__
== '__main__':