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
|
# reference counting checker, for libreswan
#
# Copyright (C) 2017,2019 Andrew Cagney
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
# Script for checking reference counted pointers; notably symkeys
# which libreswan doesn't directly manage.
#
# The script is looking for debug-log entries of the form:
#
# [|:] newref .*@0xADDRESS[(NNN)].*
# [|:] addref .*@0xADDRESS[(NNN)].*
# [|:] delref .*@0xADDRESS[(NNN)].*
#
# and optionally:
#
# [|:] freeref .*@0xADDRESS[(NNN)].*
#
BEGIN {
status = 0
}
{
key = ""
count = -1
op = ""
}
func debug(what) {
if (DEBUG) {
print $0 " DEBUG: " what
}
}
func error(key, message) {
print "ERROR: " op ": '" key "' " message
if (key in history) {
print history[key]
}
if (what) {
print NR ": " $0 "\n"
}
status = 1
}
/@0x/ {
key = gensub(/.*@(0x[0-9a-fA-F]*).*/, "\\1", 1)
debug("key='" key "'")
}
/@0x[0-9a-fA-F]*\([0-9]*->[0-9]*\)/ {
count = gensub(/.*@0x[0-9a-fA-F]*\([0-9]*->([0-9]*)\).*/, "\\1", 1)
debug("count='" count "'")
}
/[|:] [a-z]*ref .*@0x/ {
op = gensub(/.*[|:] ([a-z]*)ref .*/, "\\1", 1)
debug("op='" op "'")
}
op == "new" {
debug("new")
if ((key in counts) && counts[key] != 0) {
history[key] = history[key] "\n" NR ": " $0
error(key, "already in use")
} else {
counts[key] = 1
history[key] = NR ": " $0
if (count >= 0 && count != counts[key]) {
error(key, "has wrong count " count "; expecting" counts[key])
}
}
next
}
op == "add" {
debug("add")
if (!(key in counts)) {
error(key, "unknown")
} else {
history[key] = history[key] "\n" NR ": " $0
if (counts[key] == 0) {
error(key, "already released")
} else {
counts[key]++
}
if (count >= 0 && count != counts[key]) {
error(key, "has wrong count")
}
}
next
}
op == "del" {
debug("del")
if (!(key in counts)) {
error(key, "unknown")
} else {
history[key] = history[key] "\n" NR ": " $0
if (counts[key] == 0) {
error(key, "already released")
} else {
counts[key]--
}
if (count >= 0 && count != counts[key]) {
error(key, "has wrong count")
}
}
next
}
op == "free" {
debug("free")
if (!(key in counts)) {
error(key, "unknown")
} else {
history[key] = history[key] "\n" NR ": " $0
if (counts[key] != 0) {
error(key, "already released")
}
if (count >= 0 && count != counts[key]) {
error(key, "has wrong count")
}
}
next
}
op == "peek" {
debug("peek")
if (!(key in counts)) {
error(key, "unknown")
} else {
history[key] = history[key] "\n" NR ": " $0
# gets by delete with 0 so checking is meaningless
}
next
}
op != "" {
error(key, "operation unknown")
}
/@0x/ {
op = "use"
if (key in counts) {
if (counts[key] == 0) {
history[key] = history[key] "\n" NR ": " $0
error(key, "use after free")
}
}
}
END {
# this is in random order, oops
op = ""
for (key in counts) {
if (counts[key]) {
error(key, "has count " counts[key])
}
}
exit status
}
|