Compare commits
5 commits
master
...
csmith_com
Author | SHA1 | Date | |
---|---|---|---|
Théophile Bastian | fd8478552a | ||
Théophile Bastian | 6bdeda910f | ||
Théophile Bastian | 11a218b6ab | ||
Théophile Bastian | f872119189 | ||
Théophile Bastian | 2f91f732cb |
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -33,3 +33,4 @@
|
||||||
|
|
||||||
dwarf-assembly
|
dwarf-assembly
|
||||||
__pycache__
|
__pycache__
|
||||||
|
platform.info
|
||||||
|
|
0
__init__.py
Normal file
0
__init__.py
Normal file
0
benching/__init__.py
Normal file
0
benching/__init__.py
Normal file
103
benching/benchlib/DwBenchmark.cpp
Normal file
103
benching/benchlib/DwBenchmark.cpp
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
#include "DwBenchmark.hpp"
|
||||||
|
|
||||||
|
#include <ostream>
|
||||||
|
#include <cassert>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
#ifdef UNWIND_EH_ELF
|
||||||
|
#include "../../stack_walker/stack_walker.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef UNWIND_LIBUNWIND
|
||||||
|
#include <libunwind.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
unique_ptr<DwBenchmark> DwBenchmark::instance = nullptr;
|
||||||
|
|
||||||
|
DwBenchmark::DwBenchmark() {
|
||||||
|
#ifdef UNWIND_EH_ELF
|
||||||
|
stack_walker_init();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
DwBenchmark& DwBenchmark::get_instance() {
|
||||||
|
if(!DwBenchmark::instance)
|
||||||
|
instance = unique_ptr<DwBenchmark>(new DwBenchmark);
|
||||||
|
return *instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DwBenchmark::unwind_measure() {
|
||||||
|
#ifdef UNWIND_EH_ELF
|
||||||
|
unwind_context_t context = get_context();
|
||||||
|
SingleUnwind this_measure;
|
||||||
|
this_measure.nb_frames = 0;
|
||||||
|
|
||||||
|
auto start_time = chrono::high_resolution_clock::now();
|
||||||
|
while(unwind_context(context)) {
|
||||||
|
this_measure.nb_frames++;
|
||||||
|
}
|
||||||
|
auto end_time = chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
|
this_measure.nanoseconds = chrono::duration_cast<chrono::nanoseconds>(
|
||||||
|
end_time - start_time).count();
|
||||||
|
|
||||||
|
add_measure(this_measure);
|
||||||
|
#elif UNWIND_LIBUNWIND
|
||||||
|
unw_context_t context;
|
||||||
|
int rc = unw_getcontext(&context);
|
||||||
|
if(rc < 0)
|
||||||
|
assert(false);
|
||||||
|
unw_cursor_t cursor;
|
||||||
|
rc = unw_init_local(&cursor, &context);
|
||||||
|
if(rc < 0)
|
||||||
|
assert(false);
|
||||||
|
|
||||||
|
SingleUnwind this_measure;
|
||||||
|
this_measure.nb_frames = 0;
|
||||||
|
|
||||||
|
auto start_time = chrono::high_resolution_clock::now();
|
||||||
|
while(unw_step(&cursor) > 0) {
|
||||||
|
this_measure.nb_frames++;
|
||||||
|
}
|
||||||
|
auto end_time = chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
|
this_measure.nanoseconds = chrono::duration_cast<chrono::nanoseconds>(
|
||||||
|
end_time - start_time).count();
|
||||||
|
|
||||||
|
add_measure(this_measure);
|
||||||
|
#else
|
||||||
|
assert(false);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void DwBenchmark::add_measure(const SingleUnwind& measure) {
|
||||||
|
measures.push_back(measure);
|
||||||
|
}
|
||||||
|
void DwBenchmark::add_measure(int nb_frames, size_t microseconds) {
|
||||||
|
add_measure(SingleUnwind({nb_frames, microseconds}));
|
||||||
|
}
|
||||||
|
|
||||||
|
void DwBenchmark::format_output(std::ostream& os) const {
|
||||||
|
size_t nb_unwind_frames = 0;
|
||||||
|
size_t total_nanoseconds = 0;
|
||||||
|
|
||||||
|
for(const auto& measure: measures) {
|
||||||
|
nb_unwind_frames += measure.nb_frames;
|
||||||
|
total_nanoseconds += measure.nanoseconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
double clock_precision_ns =
|
||||||
|
((double)
|
||||||
|
(1000*1000*1000 * std::chrono::high_resolution_clock::period::num))
|
||||||
|
/ ((double) std::chrono::high_resolution_clock::period::den);
|
||||||
|
|
||||||
|
os << "Total time: " << total_nanoseconds << "ns" << endl
|
||||||
|
<< "Total frames: " << nb_unwind_frames << endl
|
||||||
|
<< "Avg frames/unwind: "
|
||||||
|
<< (double)nb_unwind_frames / (double)measures.size() << endl
|
||||||
|
<< "Avg time/frame: "
|
||||||
|
<< (double)total_nanoseconds / nb_unwind_frames << "ns" << endl
|
||||||
|
<< "Clock precision: " << clock_precision_ns << "ns" << endl;
|
||||||
|
}
|
32
benching/benchlib/DwBenchmark.hpp
Normal file
32
benching/benchlib/DwBenchmark.hpp
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class DwBenchmark {
|
||||||
|
/** Singleton class - keeps track of benchmarks performed during a run */
|
||||||
|
|
||||||
|
public:
|
||||||
|
struct SingleUnwind {
|
||||||
|
int nb_frames;
|
||||||
|
size_t nanoseconds;
|
||||||
|
};
|
||||||
|
|
||||||
|
static DwBenchmark& get_instance();
|
||||||
|
|
||||||
|
void unwind_measure(); ///< Unwind from here, and add the measure
|
||||||
|
|
||||||
|
void add_measure(const SingleUnwind& measure); ///< Add this measure
|
||||||
|
void add_measure(int nb_frames, size_t microseconds);
|
||||||
|
|
||||||
|
/** Dump formatted output on `os` displaying stats about this benchmark
|
||||||
|
* run */
|
||||||
|
void format_output(std::ostream& os) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
DwBenchmark();
|
||||||
|
|
||||||
|
static std::unique_ptr<DwBenchmark> instance;
|
||||||
|
|
||||||
|
std::vector<SingleUnwind> measures;
|
||||||
|
};
|
32
benching/benchlib/Makefile
Normal file
32
benching/benchlib/Makefile
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
TARGETS=libbench.eh_elf.so libbench.unwind.so
|
||||||
|
COMMON_OBJS=bench.o
|
||||||
|
EH_ELF_OBJS=DwBenchmark.eh_elf.o
|
||||||
|
UNWIND_OBJS=DwBenchmark.unwind.o
|
||||||
|
CXX=g++
|
||||||
|
CXXFLAGS=-Wall -Wextra -O2 -std=c++14
|
||||||
|
CXXLIBS=
|
||||||
|
|
||||||
|
|
||||||
|
all: $(TARGETS)
|
||||||
|
|
||||||
|
libbench.eh_elf.so: $(EH_ELF_OBJS) $(COMMON_OBJS)
|
||||||
|
$(CXX) $(CXXFLAGS) -shared -o $@ $^ $(CXXLIBS) \
|
||||||
|
-L../../stack_walker -lstack_walker.global
|
||||||
|
|
||||||
|
libbench.unwind.so: $(UNWIND_OBJS) $(COMMON_OBJS)
|
||||||
|
$(CXX) $(CXXFLAGS) -shared -o $@ $^ $(CXXLIBS) \
|
||||||
|
-lunwind -lunwind-x86_64
|
||||||
|
|
||||||
|
%.eh_elf.o: %.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) -fPIC -DUNWIND_EH_ELF -o $@ -c $<
|
||||||
|
|
||||||
|
%.unwind.o: %.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) -fPIC -DUNWIND_LIBUNWIND -o $@ -c $<
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) -fPIC -o $@ -c $<
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm *.o $(TARGETS)
|
12
benching/benchlib/bench.cpp
Normal file
12
benching/benchlib/bench.cpp
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#include "bench.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include "DwBenchmark.hpp"
|
||||||
|
|
||||||
|
void bench_unwinding() {
|
||||||
|
DwBenchmark::get_instance().unwind_measure();
|
||||||
|
}
|
||||||
|
|
||||||
|
void bench_dump_data() {
|
||||||
|
DwBenchmark::get_instance().format_output(std::cout);
|
||||||
|
}
|
16
benching/benchlib/bench.h
Normal file
16
benching/benchlib/bench.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
/** Benchmarking library: a collection of functions that can be called to
|
||||||
|
* benchmark dwarf-assembly
|
||||||
|
**/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void bench_unwinding();
|
||||||
|
void bench_dump_data();
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // END extern "C"
|
||||||
|
#endif
|
1
benching/csmith/.gitignore
vendored
Normal file
1
benching/csmith/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
tests/
|
0
benching/csmith/__init__.py
Normal file
0
benching/csmith/__init__.py
Normal file
5
benching/csmith/csmith-bench.sh
Executable file
5
benching/csmith/csmith-bench.sh
Executable file
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
csmith "$@" | \
|
||||||
|
sed 's/#include "csmith.h"/#include <bench.h>\n#include <csmith.h>/g' |\
|
||||||
|
sed 's/return /bench_unwinding(); return /g'
|
235
benching/csmith/csmith_compile.py
Executable file
235
benching/csmith/csmith_compile.py
Executable file
|
@ -0,0 +1,235 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
""" Helper file to generate Csmith test cases for benchmarking purposes """
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import copy
|
||||||
|
|
||||||
|
sys.path.append('../..')
|
||||||
|
|
||||||
|
from shared_python import do_remote
|
||||||
|
from generate_eh_elf import gen_eh_elfs
|
||||||
|
|
||||||
|
|
||||||
|
def get_base_id(out_dir, base_name):
|
||||||
|
''' Find the smallest xx such that
|
||||||
|
∀y >= x, `[out_dir]/[base_name]0*[yy].*` does not exist '''
|
||||||
|
|
||||||
|
direntry_id_re = re.compile(r'0*(\d+)\..*')
|
||||||
|
|
||||||
|
out = -1
|
||||||
|
|
||||||
|
for entry in os.scandir(out_dir):
|
||||||
|
fname = entry.name
|
||||||
|
if not fname.startswith(base_name):
|
||||||
|
continue
|
||||||
|
fname = fname[len(base_name):] # Truncate base_name from fname
|
||||||
|
match = direntry_id_re.match(fname)
|
||||||
|
if match is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
out = max(out, int(match.group(1)))
|
||||||
|
|
||||||
|
return out + 1
|
||||||
|
|
||||||
|
|
||||||
|
def csmith_generate(name, args):
|
||||||
|
''' Generate a C file using Csmith (possibly on --remote) '''
|
||||||
|
|
||||||
|
command = [
|
||||||
|
args.csmith_binary,
|
||||||
|
]
|
||||||
|
|
||||||
|
if args.fast_csmith:
|
||||||
|
command += ['--max-funcs', '15']
|
||||||
|
else:
|
||||||
|
command += ['--max-funcs', '100']
|
||||||
|
|
||||||
|
def write_file(content):
|
||||||
|
with open(name, 'w') as handle:
|
||||||
|
handle.write(content)
|
||||||
|
|
||||||
|
def postprocess(content):
|
||||||
|
csmith_h_re = re.compile(r'#include "csmith.h"')
|
||||||
|
return_re = re.compile(r'\breturn\b')
|
||||||
|
content = csmith_h_re.sub(
|
||||||
|
'#include <bench.h>\n#include <csmith.h>',
|
||||||
|
return_re.sub(
|
||||||
|
'bench_unwinding(); return',
|
||||||
|
content))
|
||||||
|
write_file(content)
|
||||||
|
|
||||||
|
if args.remote:
|
||||||
|
out = do_remote(args.remote, command)
|
||||||
|
else:
|
||||||
|
out = subprocess.check_output(command).decode('utf-8')
|
||||||
|
|
||||||
|
if out is None:
|
||||||
|
print("Generating {} failed".format(name), file=sys.stderr)
|
||||||
|
return False
|
||||||
|
|
||||||
|
postprocess(out)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def csmith_compile_libunwind(name, src, args):
|
||||||
|
''' Compile a Csmith-generated file, using libunwind as an unwinding
|
||||||
|
mechanism for benchmarking '''
|
||||||
|
|
||||||
|
base_dir = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
benchlib_path = os.path.normpath(os.path.join(base_dir, '../benchlib/'))
|
||||||
|
env = copy.copy(os.environ)
|
||||||
|
env.update({
|
||||||
|
'LD_RUN_PATH': benchlib_path,
|
||||||
|
})
|
||||||
|
command = [
|
||||||
|
'gcc', '-O3',
|
||||||
|
'-L' + benchlib_path, '-lbench.unwind',
|
||||||
|
'-I' + benchlib_path,
|
||||||
|
'-I' + '/usr/include/csmith-2.3.0',
|
||||||
|
'-o', name,
|
||||||
|
src,
|
||||||
|
]
|
||||||
|
print('LD_RUN_PATH={} {}'.format(env['LD_RUN_PATH'], ' '.join(command)))
|
||||||
|
|
||||||
|
try:
|
||||||
|
subprocess.run(' '.join(command), env=env, check=True, shell=True)
|
||||||
|
except subprocess.CalledProcessError as exn:
|
||||||
|
print("Compiling {}: failed with return code {}".format(
|
||||||
|
name, exn.returncode), file=sys.stderr)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def csmith_compile_eh_elf(name, src, args):
|
||||||
|
''' Compile a Csmith-generated file, using eh_elf and stack_walker as a
|
||||||
|
mechanism for benchmarking '''
|
||||||
|
|
||||||
|
base_dir = os.path.realpath(__file__)
|
||||||
|
env = {
|
||||||
|
'LD_RUN_PATH': ':'.join(
|
||||||
|
[
|
||||||
|
os.path.join(base_dir, '../benchlib'),
|
||||||
|
os.path.join(base_dir, '../../stack_walker'),
|
||||||
|
'eh_elfs'
|
||||||
|
]),
|
||||||
|
}
|
||||||
|
command = [
|
||||||
|
'gcc', '-O3',
|
||||||
|
src,
|
||||||
|
'-L', os.path.join(base_dir, '../benchlib/'), '-lbench.eh_elf',
|
||||||
|
'-L', os.path.join(base_dir, '../../stack_walker'),
|
||||||
|
'-lstack_walker.global',
|
||||||
|
'-o', name,
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
subprocess.run(command, env=env, check=True)
|
||||||
|
except subprocess.CalledProcessError as exn:
|
||||||
|
print("Compiling {}: failed with return code {}".format(
|
||||||
|
name, exn.returncode), file=sys.stderr)
|
||||||
|
return False
|
||||||
|
|
||||||
|
eh_elf_path = os.path.join(args.output, 'eh_elfs')
|
||||||
|
os.makedirs(eh_elf_path, exist_ok=True)
|
||||||
|
gen_eh_elfs(name,
|
||||||
|
eh_elf_path,
|
||||||
|
global_switch=True,
|
||||||
|
deps=True,
|
||||||
|
remote=args.remote)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def generate_and_compile(name_base, args):
|
||||||
|
c_name = name_base + '.c'
|
||||||
|
unwind_bin_name = name_base + '.unwind.bin'
|
||||||
|
eh_elf_bin_name = name_base + '.eh_elf.bin'
|
||||||
|
|
||||||
|
print('\tGenerating C file…')
|
||||||
|
if not csmith_generate(c_name, args):
|
||||||
|
return False
|
||||||
|
print('\tCompiling with libunwind…')
|
||||||
|
if not csmith_compile_libunwind(unwind_bin_name, c_name, args):
|
||||||
|
return False
|
||||||
|
print('\tCompiling with eh_elf…')
|
||||||
|
if not csmith_compile_eh_elf(eh_elf_bin_name, c_name, args):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def process_args():
|
||||||
|
''' Process `sys.argv` arguments '''
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(prog="csmith_compile")
|
||||||
|
|
||||||
|
parser.add_argument('--remote',
|
||||||
|
help=("Execute the heavyweight steps of the "
|
||||||
|
"computation on the remote machine "
|
||||||
|
"indicated, formatted for ssh"))
|
||||||
|
subparsers = parser.add_subparsers()
|
||||||
|
|
||||||
|
gc_parser = subparsers.add_parser(
|
||||||
|
'gen-test',
|
||||||
|
help=("Generate one or more Csmith test cases for benchmarking, and "
|
||||||
|
"compile them into the corresponding binaries linked against "
|
||||||
|
"libunwind and eh_elfs")
|
||||||
|
)
|
||||||
|
|
||||||
|
gc_parser.add_argument('--csmith-binary', default='csmith',
|
||||||
|
help=("Use a different csmith binary path"))
|
||||||
|
gc_parser.add_argument('--output', '-o', required=True,
|
||||||
|
help=("Store the produced tests in this directory"))
|
||||||
|
gc_parser.add_argument('--name', '-t', default='',
|
||||||
|
help=("Define the naming scheme for the output "
|
||||||
|
"files. The files will be named "
|
||||||
|
"[name]xx.ext, where xx is the id of the "
|
||||||
|
"generated test. By default, [name] is empty,"
|
||||||
|
" and xx is initialized as the smallest id"
|
||||||
|
"such that no [name]yy.ext exists, with "
|
||||||
|
"yy >= xx."))
|
||||||
|
gc_parser.add_argument('--fast-csmith', action='store_true',
|
||||||
|
help=("Quickly generate a small (and probably "
|
||||||
|
"uninterresting) Csmith source file. Useful "
|
||||||
|
"for fast setup testing."))
|
||||||
|
gc_parser.add_argument('--number', '-n', default='1',
|
||||||
|
help=("The number of test cases to generate. "
|
||||||
|
"Defaults to 1."))
|
||||||
|
gc_parser.set_defaults(main_func=main_gen_compile)
|
||||||
|
|
||||||
|
parsed = parser.parse_args()
|
||||||
|
if 'main_func' not in parsed: # No subcommand
|
||||||
|
parser.print_help()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
|
def main_gen_compile(args):
|
||||||
|
""" Main function handling gen-test """
|
||||||
|
|
||||||
|
base_id = get_base_id(args.output, args.name)
|
||||||
|
|
||||||
|
def naming_scheme(test_id):
|
||||||
|
return os.path.join(
|
||||||
|
args.output,
|
||||||
|
"{}{:03}".format(args.name, test_id))
|
||||||
|
|
||||||
|
for zeroed_test_id in range(int(args.number)):
|
||||||
|
test_id = base_id + zeroed_test_id
|
||||||
|
name_base = naming_scheme(test_id)
|
||||||
|
print('> {}…'.format(name_base))
|
||||||
|
generate_and_compile(name_base, args=args)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = process_args()
|
||||||
|
args.main_func(args)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
124
benching/csmith/gen_call_graph.py
Executable file
124
benching/csmith/gen_call_graph.py
Executable file
|
@ -0,0 +1,124 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
""" Generates the call graph (in dot format) of a C code generated by CSmith.
|
||||||
|
|
||||||
|
This does not parse C code, but performs only string lookups. In particular, it
|
||||||
|
assumes functions are named `func_[0-9]+` or `main`, and that a function
|
||||||
|
implementation is of the form (whitespaces meaningful)
|
||||||
|
|
||||||
|
(static)? RETURN_TYPE func_[0-9]+(.*)
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def build_graph(prog):
|
||||||
|
func_declare_re = re.compile(r'(?:static )?\S.* (func_\d+|main) ?\(.*\)$')
|
||||||
|
func_call_re = re.compile(r'func_\d+')
|
||||||
|
|
||||||
|
graph = {}
|
||||||
|
cur_function = None
|
||||||
|
|
||||||
|
for line in prog:
|
||||||
|
func_declare_groups = func_declare_re.match(line)
|
||||||
|
if func_declare_groups:
|
||||||
|
func_name = func_declare_groups.group(1)
|
||||||
|
cur_function = func_name
|
||||||
|
graph[func_name] = []
|
||||||
|
|
||||||
|
elif line == '}':
|
||||||
|
cur_function = None
|
||||||
|
|
||||||
|
else:
|
||||||
|
if cur_function is None:
|
||||||
|
continue # Not interresting outside of functions
|
||||||
|
|
||||||
|
last_find_pos = 0
|
||||||
|
call_match = func_call_re.search(line, pos=last_find_pos)
|
||||||
|
|
||||||
|
while call_match is not None:
|
||||||
|
graph[cur_function].append(call_match.group(0))
|
||||||
|
last_find_pos = call_match.end()
|
||||||
|
call_match = func_call_re.search(line, pos=last_find_pos)
|
||||||
|
|
||||||
|
reachable = set()
|
||||||
|
def mark_reachable(node):
|
||||||
|
nonlocal reachable, graph
|
||||||
|
if node in reachable:
|
||||||
|
return
|
||||||
|
reachable.add(node)
|
||||||
|
|
||||||
|
for child in graph[node]:
|
||||||
|
mark_reachable(child)
|
||||||
|
mark_reachable('main')
|
||||||
|
|
||||||
|
delete = []
|
||||||
|
for node in graph:
|
||||||
|
if node not in reachable:
|
||||||
|
delete.append(node)
|
||||||
|
for node in delete:
|
||||||
|
print('> Deleted: {}'.format(node), file=sys.stderr)
|
||||||
|
graph.pop(node)
|
||||||
|
|
||||||
|
return graph
|
||||||
|
|
||||||
|
|
||||||
|
def dump_graph(graph):
|
||||||
|
print('digraph prog {')
|
||||||
|
|
||||||
|
for node in graph:
|
||||||
|
for call in graph[node]:
|
||||||
|
if call in graph:
|
||||||
|
print('\t{} -> {}'.format(node, call))
|
||||||
|
else:
|
||||||
|
print('Wtf is {} (called from {})?'.format(node, call),
|
||||||
|
file=sys.stderr)
|
||||||
|
print('}')
|
||||||
|
|
||||||
|
|
||||||
|
def dump_stats(graph, out_file):
|
||||||
|
entry_point = 'main'
|
||||||
|
|
||||||
|
depth_of = {}
|
||||||
|
def find_depth(node):
|
||||||
|
nonlocal depth_of
|
||||||
|
|
||||||
|
if node in depth_of:
|
||||||
|
return depth_of[node]
|
||||||
|
|
||||||
|
callees = graph[node]
|
||||||
|
if callees:
|
||||||
|
depth = max(map(find_depth, callees)) + 1
|
||||||
|
else:
|
||||||
|
depth = 1
|
||||||
|
depth_of[node] = depth
|
||||||
|
return depth
|
||||||
|
|
||||||
|
print("Call chain max depth: {}".format(find_depth(entry_point)),
|
||||||
|
file=out_file)
|
||||||
|
|
||||||
|
|
||||||
|
def get_prog_lines():
|
||||||
|
def do_read(handle):
|
||||||
|
return handle.readlines()
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
with open(sys.argv[1], 'r') as handle:
|
||||||
|
return do_read(handle)
|
||||||
|
else:
|
||||||
|
return do_read(sys.stdin)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
prog = get_prog_lines()
|
||||||
|
graph = build_graph(prog)
|
||||||
|
dump_graph(graph)
|
||||||
|
dump_stats(graph, out_file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
112
benching/csmith/validate_csmith.py
Executable file
112
benching/csmith/validate_csmith.py
Executable file
|
@ -0,0 +1,112 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
""" Checks whether a Csmith-generated file is complicated enough
|
||||||
|
|
||||||
|
This does not parse C code, but performs only string lookups. In particular, it
|
||||||
|
assumes functions are named `func_[0-9]+` or `main`, and that a function
|
||||||
|
implementation is of the form (whitespaces meaningful)
|
||||||
|
|
||||||
|
(static)? RETURN_TYPE func_[0-9]+(.*)
|
||||||
|
{
|
||||||
|
...
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def build_graph(prog):
|
||||||
|
func_declare_re = re.compile(r'(?:static )?\S.* (func_\d+|main) ?\(.*\)$')
|
||||||
|
func_call_re = re.compile(r'func_\d+')
|
||||||
|
|
||||||
|
graph = {}
|
||||||
|
cur_function = None
|
||||||
|
|
||||||
|
for line in prog:
|
||||||
|
func_declare_groups = func_declare_re.match(line)
|
||||||
|
if func_declare_groups:
|
||||||
|
func_name = func_declare_groups.group(1)
|
||||||
|
cur_function = func_name
|
||||||
|
graph[func_name] = []
|
||||||
|
|
||||||
|
elif line == '}':
|
||||||
|
cur_function = None
|
||||||
|
|
||||||
|
else:
|
||||||
|
if cur_function is None:
|
||||||
|
continue # Not interresting outside of functions
|
||||||
|
|
||||||
|
last_find_pos = 0
|
||||||
|
call_match = func_call_re.search(line, pos=last_find_pos)
|
||||||
|
|
||||||
|
while call_match is not None:
|
||||||
|
graph[cur_function].append(call_match.group(0))
|
||||||
|
last_find_pos = call_match.end()
|
||||||
|
call_match = func_call_re.search(line, pos=last_find_pos)
|
||||||
|
|
||||||
|
reachable = set()
|
||||||
|
def mark_reachable(node):
|
||||||
|
nonlocal reachable, graph
|
||||||
|
if node in reachable:
|
||||||
|
return
|
||||||
|
reachable.add(node)
|
||||||
|
|
||||||
|
for child in graph[node]:
|
||||||
|
mark_reachable(child)
|
||||||
|
mark_reachable('main')
|
||||||
|
|
||||||
|
delete = []
|
||||||
|
for node in graph:
|
||||||
|
if node not in reachable:
|
||||||
|
delete.append(node)
|
||||||
|
for node in delete:
|
||||||
|
graph.pop(node)
|
||||||
|
|
||||||
|
return graph
|
||||||
|
|
||||||
|
|
||||||
|
def get_depth(graph):
|
||||||
|
entry_point = 'main'
|
||||||
|
|
||||||
|
depth_of = {}
|
||||||
|
def find_depth(node):
|
||||||
|
nonlocal depth_of
|
||||||
|
|
||||||
|
if node in depth_of:
|
||||||
|
return depth_of[node]
|
||||||
|
|
||||||
|
callees = graph[node]
|
||||||
|
if callees:
|
||||||
|
depth = max(map(find_depth, callees)) + 1
|
||||||
|
else:
|
||||||
|
depth = 1
|
||||||
|
depth_of[node] = depth
|
||||||
|
return depth
|
||||||
|
|
||||||
|
return find_depth(entry_point)
|
||||||
|
|
||||||
|
|
||||||
|
def get_prog_lines():
|
||||||
|
def do_read(handle):
|
||||||
|
return handle.readlines()
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
with open(sys.argv[1], 'r') as handle:
|
||||||
|
return do_read(handle)
|
||||||
|
else:
|
||||||
|
return do_read(sys.stdin)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
prog = get_prog_lines()
|
||||||
|
graph = build_graph(prog)
|
||||||
|
|
||||||
|
if len(graph) < 5 or get_depth(graph) < 5:
|
||||||
|
print("Graph is too simple.", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -11,6 +11,7 @@ import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
import argparse
|
import argparse
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
from shared_python import elf_so_deps, do_remote, is_newer
|
from shared_python import elf_so_deps, do_remote, is_newer
|
||||||
from extract_pc import generate_pc_list
|
from extract_pc import generate_pc_list
|
||||||
|
@ -24,15 +25,53 @@ C_BIN = (
|
||||||
else os.environ['C'])
|
else os.environ['C'])
|
||||||
|
|
||||||
|
|
||||||
def gen_dw_asm_c(obj_path, out_path, dwarf_assembly_args):
|
class SwitchGenPolicy(Enum):
|
||||||
|
''' The various switch generation policies possible '''
|
||||||
|
SWITCH_PER_FUNC = '--switch-per-func'
|
||||||
|
GLOBAL_SWITCH = '--global-switch'
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
''' Holds the run's settings '''
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
output,
|
||||||
|
objects,
|
||||||
|
sw_gen_policy=SwitchGenPolicy.GLOBAL_SWITCH,
|
||||||
|
force=False,
|
||||||
|
use_pc_list=False,
|
||||||
|
c_opt_level='3',
|
||||||
|
remote=None):
|
||||||
|
self.output = output
|
||||||
|
self.objects = objects
|
||||||
|
self.sw_gen_policy = sw_gen_policy
|
||||||
|
self.force = force
|
||||||
|
self.use_pc_list = use_pc_list
|
||||||
|
self.c_opt_level = c_opt_level
|
||||||
|
self.remote = remote
|
||||||
|
|
||||||
|
def dwarf_assembly_args(self):
|
||||||
|
''' Arguments to `dwarf_assembly` '''
|
||||||
|
return [self.sw_gen_policy.value]
|
||||||
|
|
||||||
|
def opt_level(self):
|
||||||
|
''' The optimization level to pass to gcc '''
|
||||||
|
return '-O{}'.format(self.c_opt_level)
|
||||||
|
|
||||||
|
|
||||||
|
def gen_dw_asm_c(obj_path, out_path, config, pc_list_path=None):
|
||||||
''' Generate the C code produced by dwarf-assembly from `obj_path`, saving
|
''' Generate the C code produced by dwarf-assembly from `obj_path`, saving
|
||||||
it as `out_path` '''
|
it as `out_path` '''
|
||||||
|
|
||||||
|
dw_assembly_args = config.dwarf_assembly_args()
|
||||||
|
if pc_list_path is not None:
|
||||||
|
dw_assembly_args += ['--pc-list', pc_list_path]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(out_path, 'w') as out_handle:
|
with open(out_path, 'w') as out_handle:
|
||||||
# TODO enhance error handling
|
# TODO enhance error handling
|
||||||
dw_asm_output = subprocess.check_output(
|
dw_asm_output = subprocess.check_output(
|
||||||
[DWARF_ASSEMBLY_BIN, obj_path] + dwarf_assembly_args) \
|
[DWARF_ASSEMBLY_BIN, obj_path] + dw_assembly_args) \
|
||||||
.decode('utf-8')
|
.decode('utf-8')
|
||||||
out_handle.write(dw_asm_output)
|
out_handle.write(dw_asm_output)
|
||||||
except subprocess.CalledProcessError as exn:
|
except subprocess.CalledProcessError as exn:
|
||||||
|
@ -44,18 +83,15 @@ def gen_dw_asm_c(obj_path, out_path, dwarf_assembly_args):
|
||||||
exn.returncode))
|
exn.returncode))
|
||||||
|
|
||||||
|
|
||||||
def gen_eh_elf(obj_path, args, dwarf_assembly_args=None):
|
def gen_eh_elf(obj_path, config):
|
||||||
''' Generate the eh_elf corresponding to `obj_path`, saving it as
|
''' Generate the eh_elf corresponding to `obj_path`, saving it as
|
||||||
`out_dir/$(basename obj_path).eh_elf.so` (or in the current working
|
`out_dir/$(basename obj_path).eh_elf.so` (or in the current working
|
||||||
directory if out_dir is None) '''
|
directory if out_dir is None) '''
|
||||||
|
|
||||||
if args.output is None:
|
if config.output is None:
|
||||||
out_dir = '.'
|
out_dir = '.'
|
||||||
else:
|
else:
|
||||||
out_dir = args.output
|
out_dir = config.output
|
||||||
|
|
||||||
if dwarf_assembly_args is None:
|
|
||||||
dwarf_assembly_args = []
|
|
||||||
|
|
||||||
print("> {}...".format(os.path.basename(obj_path)))
|
print("> {}...".format(os.path.basename(obj_path)))
|
||||||
|
|
||||||
|
@ -63,43 +99,41 @@ def gen_eh_elf(obj_path, args, dwarf_assembly_args=None):
|
||||||
out_so_path = os.path.join(out_dir, (out_base_name + '.so'))
|
out_so_path = os.path.join(out_dir, (out_base_name + '.so'))
|
||||||
pc_list_dir = os.path.join(out_dir, 'pc_list')
|
pc_list_dir = os.path.join(out_dir, 'pc_list')
|
||||||
|
|
||||||
if is_newer(out_so_path, obj_path) and not args.force:
|
if is_newer(out_so_path, obj_path) and not config.force:
|
||||||
return # The object is recent enough, no need to recreate it
|
return # The object is recent enough, no need to recreate it
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as compile_dir:
|
with tempfile.TemporaryDirectory() as compile_dir:
|
||||||
# Generate PC list
|
# Generate PC list
|
||||||
if args.use_pc_list:
|
pc_list_path = None
|
||||||
|
if config.use_pc_list:
|
||||||
pc_list_path = \
|
pc_list_path = \
|
||||||
os.path.join(pc_list_dir, out_base_name + '.pc_list')
|
os.path.join(pc_list_dir, out_base_name + '.pc_list')
|
||||||
os.makedirs(pc_list_dir, exist_ok=True)
|
os.makedirs(pc_list_dir, exist_ok=True)
|
||||||
print('\tGenerating PC list…')
|
print('\tGenerating PC list…')
|
||||||
generate_pc_list(obj_path, pc_list_path)
|
generate_pc_list(obj_path, pc_list_path)
|
||||||
dwarf_assembly_args += ['--pc-list', pc_list_path]
|
|
||||||
|
|
||||||
# Generate the C source file
|
# Generate the C source file
|
||||||
print("\tGenerating C…")
|
print("\tGenerating C…")
|
||||||
c_path = os.path.join(compile_dir, (out_base_name + '.c'))
|
c_path = os.path.join(compile_dir, (out_base_name + '.c'))
|
||||||
gen_dw_asm_c(obj_path, c_path, dwarf_assembly_args)
|
gen_dw_asm_c(obj_path, c_path, config, pc_list_path)
|
||||||
|
|
||||||
# Compile it into a .o
|
# Compile it into a .o
|
||||||
print("\tCompiling into .o…")
|
print("\tCompiling into .o…")
|
||||||
o_path = os.path.join(compile_dir, (out_base_name + '.o'))
|
o_path = os.path.join(compile_dir, (out_base_name + '.o'))
|
||||||
opt_level = args.c_opt_level
|
if config.remote:
|
||||||
if opt_level is None:
|
|
||||||
opt_level = '-O3'
|
|
||||||
if args.remote:
|
|
||||||
remote_out = do_remote(
|
remote_out = do_remote(
|
||||||
args.remote,
|
config.remote,
|
||||||
[C_BIN,
|
[C_BIN,
|
||||||
'-o', out_base_name + '.o',
|
'-o', out_base_name + '.o',
|
||||||
'-c', out_base_name + '.c',
|
'-c', out_base_name + '.c',
|
||||||
opt_level, '-fPIC'],
|
config.opt_level(), '-fPIC'],
|
||||||
send_files=[c_path],
|
send_files=[c_path],
|
||||||
retr_files=[(out_base_name + '.o', o_path)])
|
retr_files=[(out_base_name + '.o', o_path)])
|
||||||
call_rc = 1 if remote_out is None else 0
|
call_rc = 1 if remote_out is None else 0
|
||||||
else:
|
else:
|
||||||
call_rc = subprocess.call(
|
call_rc = subprocess.call(
|
||||||
[C_BIN, '-o', o_path, '-c', c_path, opt_level, '-fPIC'])
|
[C_BIN, '-o', o_path, '-c', c_path,
|
||||||
|
config.opt_level(), '-fPIC'])
|
||||||
if call_rc != 0:
|
if call_rc != 0:
|
||||||
raise Exception("Failed to compile to a .o file")
|
raise Exception("Failed to compile to a .o file")
|
||||||
|
|
||||||
|
@ -111,15 +145,38 @@ def gen_eh_elf(obj_path, args, dwarf_assembly_args=None):
|
||||||
raise Exception("Failed to compile to a .so file")
|
raise Exception("Failed to compile to a .so file")
|
||||||
|
|
||||||
|
|
||||||
def gen_all_eh_elf(obj_path, args, dwarf_assembly_args=None):
|
def gen_all_eh_elf(obj_path, config):
|
||||||
''' Call `gen_eh_elf` on obj_path and all its dependencies '''
|
''' Call `gen_eh_elf` on obj_path and all its dependencies '''
|
||||||
if dwarf_assembly_args is None:
|
|
||||||
dwarf_assembly_args = []
|
|
||||||
|
|
||||||
deps = elf_so_deps(obj_path)
|
deps = elf_so_deps(obj_path)
|
||||||
deps.append(obj_path)
|
deps.append(obj_path)
|
||||||
for dep in deps:
|
for dep in deps:
|
||||||
gen_eh_elf(dep, args, dwarf_assembly_args)
|
gen_eh_elf(dep, config)
|
||||||
|
|
||||||
|
|
||||||
|
def gen_eh_elfs(obj_path,
|
||||||
|
out_dir,
|
||||||
|
global_switch=True,
|
||||||
|
deps=True,
|
||||||
|
remote=None):
|
||||||
|
''' Call gen{_all,}_eh_elf with args setup accordingly with the given
|
||||||
|
options '''
|
||||||
|
|
||||||
|
switch_gen_policy = (
|
||||||
|
SwitchGenPolicy.GLOBAL_SWITCH if global_switch
|
||||||
|
else SwitchGenPolicy.SWITCH_PER_FUNC
|
||||||
|
)
|
||||||
|
|
||||||
|
config = Config(
|
||||||
|
out_dir,
|
||||||
|
[obj_path],
|
||||||
|
sw_gen_policy=switch_gen_policy,
|
||||||
|
remote=remote,
|
||||||
|
)
|
||||||
|
|
||||||
|
if deps:
|
||||||
|
return gen_all_eh_elf([obj_path], config)
|
||||||
|
else:
|
||||||
|
return gen_eh_elf([obj_path], config)
|
||||||
|
|
||||||
|
|
||||||
def process_args():
|
def process_args():
|
||||||
|
@ -150,35 +207,40 @@ def process_args():
|
||||||
"ELF."))
|
"ELF."))
|
||||||
# c_opt_level
|
# c_opt_level
|
||||||
opt_level_grp = parser.add_mutually_exclusive_group()
|
opt_level_grp = parser.add_mutually_exclusive_group()
|
||||||
opt_level_grp.add_argument('-O0', action='store_const', const='-O0',
|
opt_level_grp.add_argument('-O0', action='store_const', const='0',
|
||||||
dest='c_opt_level',
|
dest='c_opt_level',
|
||||||
help=("Compile C file with this optimization "
|
help=("Compile C file with this optimization "
|
||||||
"level."))
|
"level."))
|
||||||
opt_level_grp.add_argument('-O1', action='store_const', const='-O1',
|
opt_level_grp.add_argument('-O1', action='store_const', const='1',
|
||||||
dest='c_opt_level',
|
dest='c_opt_level',
|
||||||
help=("Compile C file with this optimization "
|
help=("Compile C file with this optimization "
|
||||||
"level."))
|
"level."))
|
||||||
opt_level_grp.add_argument('-O2', action='store_const', const='-O2',
|
opt_level_grp.add_argument('-O2', action='store_const', const='2',
|
||||||
dest='c_opt_level',
|
dest='c_opt_level',
|
||||||
help=("Compile C file with this optimization "
|
help=("Compile C file with this optimization "
|
||||||
"level."))
|
"level."))
|
||||||
opt_level_grp.add_argument('-O3', action='store_const', const='-O3',
|
opt_level_grp.add_argument('-O3', action='store_const', const='3',
|
||||||
dest='c_opt_level',
|
dest='c_opt_level',
|
||||||
help=("Compile C file with this optimization "
|
help=("Compile C file with this optimization "
|
||||||
"level."))
|
"level."))
|
||||||
opt_level_grp.add_argument('-Os', action='store_const', const='-Os',
|
opt_level_grp.add_argument('-Os', action='store_const', const='s',
|
||||||
dest='c_opt_level',
|
dest='c_opt_level',
|
||||||
help=("Compile C file with this optimization "
|
help=("Compile C file with this optimization "
|
||||||
"level."))
|
"level."))
|
||||||
|
opt_level_grp.set_defaults(c_opt_level='3')
|
||||||
|
|
||||||
switch_generation_policy = \
|
switch_gen_policy = \
|
||||||
parser.add_mutually_exclusive_group(required=True)
|
parser.add_mutually_exclusive_group(required=True)
|
||||||
switch_generation_policy.add_argument('--switch-per-func',
|
switch_gen_policy.add_argument('--switch-per-func',
|
||||||
action='store_const', const='',
|
dest='sw_gen_policy',
|
||||||
help=("Passed to dwarf-assembly."))
|
action='store_const',
|
||||||
switch_generation_policy.add_argument('--global-switch',
|
const=SwitchGenPolicy.SWITCH_PER_FUNC,
|
||||||
action='store_const', const='',
|
help=("Passed to dwarf-assembly."))
|
||||||
help=("Passed to dwarf-assembly."))
|
switch_gen_policy.add_argument('--global-switch',
|
||||||
|
dest='sw_gen_policy',
|
||||||
|
action='store_const',
|
||||||
|
const=SwitchGenPolicy.GLOBAL_SWITCH,
|
||||||
|
help=("Passed to dwarf-assembly."))
|
||||||
parser.add_argument('object', nargs='+',
|
parser.add_argument('object', nargs='+',
|
||||||
help="The ELF object(s) to process")
|
help="The ELF object(s) to process")
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
@ -186,20 +248,18 @@ def process_args():
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = process_args()
|
args = process_args()
|
||||||
|
config = Config(
|
||||||
DW_ASSEMBLY_OPTS = {
|
args.output,
|
||||||
'switch_per_func': '--switch-per-func',
|
args.object,
|
||||||
'global_switch': '--global-switch',
|
args.sw_gen_policy,
|
||||||
}
|
args.force,
|
||||||
|
args.use_pc_list,
|
||||||
dwarf_assembly_opts = []
|
args.c_opt_level,
|
||||||
args_dict = vars(args)
|
args.remote,
|
||||||
for opt in DW_ASSEMBLY_OPTS:
|
)
|
||||||
if opt in args and args_dict[opt] is not None:
|
|
||||||
dwarf_assembly_opts.append(DW_ASSEMBLY_OPTS[opt])
|
|
||||||
|
|
||||||
for obj in args.object:
|
for obj in args.object:
|
||||||
args.gen_func(obj, args, dwarf_assembly_opts)
|
args.gen_func(obj, config)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -52,6 +52,9 @@ def do_remote(remote, command, send_files=None, retr_files=None):
|
||||||
|
|
||||||
The command is executed on the machine described by `remote` (see ssh(1)).
|
The command is executed on the machine described by `remote` (see ssh(1)).
|
||||||
|
|
||||||
|
If `preload` is set, then the remote file at this path will be sourced
|
||||||
|
before running any command, allowing to set PATH and other variables.
|
||||||
|
|
||||||
send_files is a list of file paths that must be first copied at the root of
|
send_files is a list of file paths that must be first copied at the root of
|
||||||
a temporary directory on `remote` before running the command. Consider
|
a temporary directory on `remote` before running the command. Consider
|
||||||
yourself jailed in that directory.
|
yourself jailed in that directory.
|
||||||
|
@ -64,6 +67,11 @@ def do_remote(remote, command, send_files=None, retr_files=None):
|
||||||
otherwise, on the local machine.
|
otherwise, on the local machine.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
if send_files is None:
|
||||||
|
send_files = []
|
||||||
|
if retr_files is None:
|
||||||
|
retr_files = []
|
||||||
|
|
||||||
def ssh_do(cmd_args, working_directory=None):
|
def ssh_do(cmd_args, working_directory=None):
|
||||||
try:
|
try:
|
||||||
cmd = ['ssh', remote]
|
cmd = ['ssh', remote]
|
||||||
|
|
Loading…
Reference in a new issue