Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csmith #294

Merged
merged 7 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
- name: Unpack gcc-c-torture
run: tar -xvf gcc.c-torture.tar.gz -C . > /dev/null

- name: Parsing
- name: Transpilation
run: |
cd ./project/scripts
python3 transpile.py ../../gcc.c-torture -s gcc -n
Expand Down Expand Up @@ -158,6 +158,44 @@ jobs:
cd ./project/scripts
python3 test.py -s test

csmith:
needs:
- cpp-lint
- clang-format
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Cache LLVM
id: cache-llvm
uses: actions/cache@v3
with:
path: llvm-clang.tar.gz
key: ${{ runner.os }}-llvm-clang

- name: Unpack LLVM
run: |
sudo chmod ugo+rwx llvm-clang.tar.gz
tar -xvf llvm-clang.tar.gz -C . > /dev/null

- name: Install csmith
run: |
cd ..
git clone https://github.com/csmith-project/csmith.git
cd csmith
cmake -DCMAKE_INSTALL_PREFIX=csmith .
make

- name: Generating tests
run: |
cd ./project/scripts
python3 csmith.py ../tests/main/csmith -c 1000

- name: Transpilation
run: |
cd ./project/scripts
python3 transpile.py ../tests/main/csmith -n

codecov:
needs:
- clang-tidy
Expand Down
94 changes: 94 additions & 0 deletions project/scripts/csmith.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#! /usr/bin/python3

"""
The MIT License (MIT)

Copyright (c) 2021-2022 c2eo team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

import os
import sys
import shutil
import argparse
import subprocess

# Our scripts
import tools
import settings


class Csmith(object):

def __init__(self, path_to_generate, files_count):
self.csmith_args = ' '.join([f'--{arg}' for arg in settings.get_setting('csmith_args')])
self.path_to_csmith = os.path.abspath(os.path.join(settings.get_setting('path_to_csmith'), 'csmith'))
self.path_to_csmith_runtime = os.path.join(settings.get_setting('path_to_csmith_runtime'))
self.path_to_generate = path_to_generate
self.files_count = files_count
self.generated_files_count = 0

def generate(self):
tools.pprint('\nMaking the dir:', slowly=True)
if os.path.exists(self.path_to_generate):
tools.clear_dir_by_patterns(self.path_to_generate, ['*.c', '*.h'])
else:
os.makedirs(self.path_to_generate, exist_ok=True)
tools.pprint('\nCopying runtime files into the dir:', slowly=True)
for file in tools.search_files_by_patterns(self.path_to_csmith_runtime, ['*.h'], print_files=True):
shutil.copy(file, self.path_to_generate)
os.chdir(self.path_to_generate)
tools.pprint('\nRunning generating files:\n', slowly=True)
tools.print_progress_bar(0, self.files_count)
with tools.thread_pool() as threads:
results = threads.map(self.generate_file, range(self.files_count))
print()
tools.pprint('\nGenerated files: ')
for file_name, code in sorted(results, key=lambda x: x[0]):
print(f'{file_name}:\n')
print(code)

def generate_file(self, number):
result = subprocess.run(f'{self.path_to_csmith} {self.csmith_args}', shell=True, text=True, capture_output=True)
file_name = f'{number + 1:0{len(str(self.files_count))}}.c'
with open(file_name, 'w') as f:
f.write(result.stdout)
self.generated_files_count += 1
tools.print_progress_bar(self.generated_files_count, self.files_count)
return file_name, result.stderr if result.returncode else result.stdout


def create_parser():
_parser = argparse.ArgumentParser(description='the script for generating csmith testsuite for c2eo transpiler')

_parser.add_argument('path_to_generate', metavar='PATH',
help='the relative path from the scripts folder to the generating folder')

_parser.add_argument('-c', '--count_of_files', metavar='COUNT', default=1, type=int,
help='the count of generating files')

return _parser


if __name__ == '__main__':
tools.move_to_script_dir(sys.argv[0])
parser = create_parser()
namespace = parser.parse_args()
Csmith(namespace.path_to_generate, namespace.count_of_files).generate()
10 changes: 10 additions & 0 deletions project/scripts/data/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ clang_tidy_checks:
code_file_patterns:
- '*.cpp'
- '*.h'
csmith_args:
- 'max-block-depth 2'
- 'max-array-len-per-dim 4'
- 'max-block-size 2'
- 'max-expr-complexity 3'
- 'max-funcs 2'
- 'max-struct-fields 3'
- 'max-union-fields 3'
skips_for_test: test
skips_for_compile: compile
skips_for_transpile: transpile
Expand All @@ -45,6 +53,8 @@ path_to_c2eo_build: ../build/
path_to_c2eo_transpiler: ../bin/
path_to_code_files: ../src/transpiler/
path_to_skips: data/skips/
path_to_csmith: ../../../csmith/src/
path_to_csmith_runtime: ../../../csmith/runtime/
path_to_eo: eo/
path_to_eo_external: ../../result/eo/c2eo/external/
path_to_eo_parse: target/01-parse/
Expand Down
Empty file removed project/tests/compilation/.gitkeep
Empty file.
Empty file.
Empty file.
Loading