| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
########################################################################## |
| 4 |
# # |
| 5 |
# This file is part of Frama-C. # |
| 6 |
# # |
| 7 |
# Copyright (C) 2007-2022 # |
| 8 |
# CEA (Commissariat à l'énergie atomique et aux énergies # |
| 9 |
# alternatives) # |
| 10 |
# # |
| 11 |
# you can redistribute it and/or modify it under the terms of the GNU # |
| 12 |
# Lesser General Public License as published by the Free Software # |
| 13 |
# Foundation, version 2.1. # |
| 14 |
# # |
| 15 |
# It is distributed in the hope that it will be useful, # |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 18 |
# GNU Lesser General Public License for more details. # |
| 19 |
# # |
| 20 |
# See the GNU Lesser General Public License version 2.1 # |
| 21 |
# for more details (enclosed in the file licenses/LGPLv2.1). # |
| 22 |
# # |
| 23 |
########################################################################## |
| 24 |
|
| 25 |
"""This script serves as wrapper to GNU make (when using the analysis-scripts |
| 26 |
GNUmakefile template): it parses the output and suggests useful commands |
| 27 |
whenever it can, by calling frama-c-script itself.""" |
| 28 |
|
| 29 |
import argparse |
| 30 |
import os |
| 31 |
import re |
| 32 |
import subprocess |
| 33 |
import sys |
| 34 |
from functools import partial |
| 35 |
|
| 36 |
# Check if GNU make is available and has the minimal required version |
| 37 |
# (4.0). Otherwise, this script will fail. |
| 38 |
# We first test with 'gmake', then 'make', then fail. |
| 39 |
make_cmd = "gmake" |
| 40 |
get_make_major_version_args = ( |
| 41 |
r" --version | grep 'GNU Make\s\+\([0-9]\+\)\..*$' | sed -E 's|GNU Make +([0-9]+)\..*|\1|'" |
| 42 |
) |
| 43 |
|
| 44 |
cmd = ( |
| 45 |
f"command -v {make_cmd} >{os.devnull} && " |
| 46 |
f'test "$({make_cmd} {get_make_major_version_args})" -ge 4 2>{os.devnull}' |
| 47 |
) |
| 48 |
if os.system(cmd) != 0: |
| 49 |
make_cmd = "make" |
| 50 |
cmd = ( |
| 51 |
f"command -v {make_cmd} >{os.devnull} && " |
| 52 |
f'test "$({make_cmd} {get_make_major_version_args})" -ge 4 2>{os.devnull}' |
| 53 |
) |
| 54 |
if os.system(cmd) != 0: |
| 55 |
sys.exit("error: could not find GNU make >= 4.0 (tried 'gmake' and 'make')") |
| 56 |
|
| 57 |
parser = argparse.ArgumentParser( |
| 58 |
description=""" |
| 59 |
Builds the specified target, parsing the output to identify and recommend |
| 60 |
actions in case of failure.""" |
| 61 |
) |
| 62 |
parser.add_argument( |
| 63 |
"--make-dir", |
| 64 |
metavar="DIR", |
| 65 |
default=".frama-c", |
| 66 |
help="directory containing the makefile (default: .frama-c)", |
| 67 |
) |
| 68 |
|
| 69 |
(make_dir_arg, args) = parser.parse_known_args() |
| 70 |
make_dir = vars(make_dir_arg)["make_dir"] |
| 71 |
args = args[1:] |
| 72 |
|
| 73 |
framac_bin = os.getenv("FRAMAC_BIN") |
| 74 |
if not framac_bin: |
| 75 |
sys.exit("error: FRAMAC_BIN not in environment (set by frama-c-script)") |
| 76 |
framac_script = f"{framac_bin}/frama-c-script" |
| 77 |
|
| 78 |
output_lines = [] |
| 79 |
cmd_list = [make_cmd, "-C", make_dir] + args |
| 80 |
with subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc: |
| 81 |
while True: |
| 82 |
line = proc.stdout.readline() |
| 83 |
if line: |
| 84 |
sys.stdout.buffer.write(line) |
| 85 |
sys.stdout.flush() |
| 86 |
output_lines.append(line.decode("utf-8")) |
| 87 |
else: |
| 88 |
break |
| 89 |
|
| 90 |
re_missing_spec = re.compile("Neither code nor specification for function ([^,]+),") |
| 91 |
re_recursive_call_start = re.compile("detected recursive call") |
| 92 |
re_recursive_call_stack_start = re.compile(r"^\s+stack:") |
| 93 |
re_recursive_call_stack_end = re.compile(r"^\[") |
| 94 |
|
| 95 |
tips = [] |
| 96 |
|
| 97 |
lines = iter(output_lines) |
| 98 |
for line in lines: |
| 99 |
match = re_recursive_call_start.search(line) |
| 100 |
if match: |
| 101 |
|
| 102 |
def _action(): |
| 103 |
print( |
| 104 |
"Consider patching, stubbing or adding an ACSL " |
| 105 |
+ "specification to the recursive call, " |
| 106 |
+ "then re-run the analysis." |
| 107 |
) |
| 108 |
|
| 109 |
while True: |
| 110 |
msg_lines = [] |
| 111 |
match = re_recursive_call_start.search(line) |
| 112 |
try: |
| 113 |
while not match: |
| 114 |
line = next(lines) |
| 115 |
match = re_recursive_call_start.search(line) |
| 116 |
match = None |
| 117 |
while not match: |
| 118 |
line = next(lines) |
| 119 |
match = re_recursive_call_stack_start.search(line) |
| 120 |
match = None |
| 121 |
while not match: |
| 122 |
msg_lines.append(line) |
| 123 |
line = next(lines) |
| 124 |
match = re_recursive_call_stack_end.search(line) |
| 125 |
# note: this ending line can also match re_missing_spec |
| 126 |
tip = { |
| 127 |
"message": "Found recursive call at:\n" + "".join(msg_lines), |
| 128 |
"action": _action, |
| 129 |
} |
| 130 |
tips.append(tip) |
| 131 |
break |
| 132 |
except StopIteration: |
| 133 |
print("** Error: did not match expected regex before EOF") |
| 134 |
assert False |
| 135 |
match = re_missing_spec.search(line) |
| 136 |
if match: |
| 137 |
fname = match.group(1) |
| 138 |
|
| 139 |
def _action(fname): |
| 140 |
out = subprocess.Popen( |
| 141 |
[framac_script, "find-fun", "-C", make_dir, fname], |
| 142 |
stdout=subprocess.PIPE, |
| 143 |
stderr=subprocess.STDOUT, |
| 144 |
) |
| 145 |
output = out.communicate()[0].decode("utf-8") |
| 146 |
re_possible_definers = re.compile("Possible definitions for function") |
| 147 |
find_fun_lines = iter(output.splitlines()) |
| 148 |
for find_fun_line in find_fun_lines: |
| 149 |
if re_possible_definers.match(find_fun_line): |
| 150 |
found_files = [next(find_fun_lines)] |
| 151 |
while True: |
| 152 |
try: |
| 153 |
found_files.append(next(find_fun_lines)) |
| 154 |
except StopIteration: |
| 155 |
if len(found_files) > 1: |
| 156 |
print( |
| 157 |
"Found several files defining function '" |
| 158 |
+ fname |
| 159 |
+ "', cannot recommend automatically." |
| 160 |
) |
| 161 |
print( |
| 162 |
"Check which one is appropriate and add it " |
| 163 |
+ "to the list of sources to be parsed:" |
| 164 |
) |
| 165 |
print("\n".join(found_files)) |
| 166 |
else: |
| 167 |
print( |
| 168 |
"Add the following file to the list of " |
| 169 |
+ "sources to be parsed:\n" |
| 170 |
+ found_files[0] |
| 171 |
) |
| 172 |
return |
| 173 |
print("Could not find any files defining " + fname + ".") |
| 174 |
print("Find the sources defining it and add them, " + "or provide a stub.") |
| 175 |
|
| 176 |
tip = { |
| 177 |
"message": "Found function with missing spec: " |
| 178 |
+ fname |
| 179 |
+ "\n" |
| 180 |
+ " Looking for files defining it...", |
| 181 |
"action": partial(_action, fname), |
| 182 |
} |
| 183 |
tips.append(tip) |
| 184 |
|
| 185 |
if tips != []: |
| 186 |
print("") |
| 187 |
print("***** make-wrapper recommendations *****") |
| 188 |
print("") |
| 189 |
counter = 1 |
| 190 |
print("*** recommendation #" + str(counter) + " ***") |
| 191 |
print("") |
| 192 |
for tip in tips: |
| 193 |
if counter > 1: |
| 194 |
print("") |
| 195 |
print("*** recommendation #" + str(counter) + " ***") |
| 196 |
print(str(counter) + ". " + tip["message"]) |
| 197 |
counter += 1 |
| 198 |
tip["action"]() |