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 # ===------------------------------------------------------------------------===#
23 def PrintStack(line
, stack
):
27 print("Unbalanced " + line
.rstrip())
32 def ProcessStack(line
, f
):
34 while line
and line
.startswith(" #"):
40 def ProcessFree(line
, f
, allocs
):
41 if not line
.startswith("FREE["):
44 addr
= int(line
.split()[1], 16)
45 next_line
, stack
= ProcessStack(f
.readline(), f
)
49 PrintStack(line
, stack
)
53 def ProcessMalloc(line
, f
, allocs
):
54 if not line
.startswith("MALLOC["):
55 return ProcessFree(line
, f
, allocs
)
57 addr
= int(line
.split()[1], 16)
58 assert not addr
in allocs
60 next_line
, stack
= ProcessStack(f
.readline(), f
)
61 allocs
[addr
] = (line
, stack
)
65 def ProcessRun(line
, f
):
66 if not line
.startswith("MallocFreeTracer: START"):
67 return ProcessMalloc(line
, f
, {})
73 if line
.startswith("MallocFreeTracer: STOP"):
76 for _
, (l
, s
) in allocs
.items():
80 line
= ProcessMalloc(line
, f
, allocs
)
87 line
= ProcessRun(line
, f
)
91 parser
= argparse
.ArgumentParser()
92 parser
.add_argument("--skip", default
=0, help="number of runs to ignore")
93 args
= parser
.parse_args()
95 _skip
= int(args
.skip
) + 1
96 ProcessFile(sys
.stdin
)
99 if __name__
== "__main__":