Skip to content

Commit

Permalink
Merge pull request #672 from ohmtech-rdi/erb-fuzz-target
Browse files Browse the repository at this point in the history
Add fuzz testing system
  • Loading branch information
ohmtech-rdi authored Mar 7, 2024
2 parents 4554769 + 3c7fb7a commit a9cb1aa
Show file tree
Hide file tree
Showing 12 changed files with 1,360 additions and 3 deletions.
10 changes: 10 additions & 0 deletions build-system/build-system.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
'erbb/generators/action/action_daisy_template.py',
'erbb/generators/action/action_data_template.py',
'erbb/generators/action/action_faust_template.py',
'erbb/generators/action/action_fuzz_template.py',
'erbb/generators/action/action_max_template.py',
'erbb/generators/action/action_perf_template.py',
'erbb/generators/action/action_ui_template.py',
'erbb/generators/action/action_vcvrack_install_template.py',
'erbb/generators/action/action_vcvrack_template.py',
Expand Down Expand Up @@ -98,6 +100,10 @@
'erbb/generators/perf/Makefile_template',
'erbb/generators/perf/make.py',

# generators/fuzz
'erbb/generators/fuzz/Makefile_template',
'erbb/generators/fuzz/make.py',

# generators/vscode
'erbb/generators/vscode/c_cpp_properties_template.json',
'erbb/generators/vscode/c_cpp_properties.py',
Expand Down Expand Up @@ -161,6 +167,10 @@
'erbui/generators/perf/code_template.cpp',
'erbui/generators/perf/code.py',

# generators/fuzz
'erbui/generators/fuzz/code_template.cpp',
'erbui/generators/fuzz/code.py',

# generators/tayda
'erbui/generators/tayda/drill.py',
'erbui/generators/tayda/drill_template.py',
Expand Down
10 changes: 7 additions & 3 deletions build-system/completion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ def with_apt (): return '--with-apt'
def build_simulator (): return 'simulator', ZeroOrMore ([configuration, xcode])
def build_firmware (): return 'firmware', ZeroOrMore ([configuration, semihosting])
def build_performance (): return 'performance', ZeroOrMore ([logger])
def build_fuzz (): return 'fuzz', ZeroOrMore ([logger])
def build_hardware (): return 'hardware', ZeroOrMore ([only_gerber])

def install_firmware (): return 'firmware', ZeroOrMore ([configuration, programmer])
def install_performance (): return 'performance'
def install_fuzz (): return 'fuzz'
def install_bootloader (): return 'bootloader', ZeroOrMore ([fast_boot])
def install_simulator (): return 'simulator', ZeroOrMore ([configuration])

def run_performance (): return 'performance'
def run_fuzz (): return 'fuzz'

def setup ():
if platform.system () == 'Darwin':
Expand All @@ -51,9 +54,9 @@ def setup ():

def init (): return 'init', ZeroOrMore ([name, language])
def configure (): return 'configure'
def build (): return 'build', [build_simulator, build_firmware, build_performance, build_hardware]
def install (): return 'install', [install_firmware, install_performance, install_bootloader, install_simulator]
def run (): return 'run ', run_performance
def build (): return 'build', [build_simulator, build_firmware, build_performance, build_fuzz, build_hardware]
def install (): return 'install', [install_firmware, install_performance, install_fuzz, install_bootloader, install_simulator]
def run (): return 'run ', [run_performance, run_fuzz]

def commands (): return [setup, init, configure, build, install, run]

Expand Down Expand Up @@ -86,6 +89,7 @@ def erbb_cli (): return 'erbb', commands
'install': 'install the firmware or bootloader',
'--programmer': 'the programmer to use, defaults to automatic selection',
'performance': 'the performance analysis firmware',
'fuzz': 'the fuzz testing firmware',
'--logger': 'the logger to use, defaults to automatic selection',
'bootloader': 'the bootloader',
'debug': 'for debugging',
Expand Down
156 changes: 156 additions & 0 deletions build-system/erbb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from .generators.simulator.make import Make as simulatorMake
from .generators.daisy.make import Make as daisyMake
from .generators.perf.make import Make as perfMake
from .generators.fuzz.make import Make as fuzzMake
from .generators.vcvrack.project import Project as vcvrackProject
from .generators.vscode.c_cpp_properties import CCppProperties as vscodeCCppProperties
from .generators.vscode.extensions import Extensions as vscodeExtensions
Expand Down Expand Up @@ -232,6 +233,7 @@ def configure (path, ast):
configure_simulator_make (path, ast)
configure_daisy (path, ast)
configure_perf (path, ast)
configure_fuzz (path, ast)
configure_vscode (path, ast)


Expand Down Expand Up @@ -285,6 +287,18 @@ def configure_perf (path, ast):



"""
==============================================================================
Name: configure_fuzz
==============================================================================
"""

def configure_fuzz (path, ast):
generator = fuzzMake ()
generator.generate (path, ast)



"""
==============================================================================
Name: configure_vscode
Expand Down Expand Up @@ -435,6 +449,38 @@ def build_performance_target (target, path, logger):



"""
==============================================================================
Name : build_fuzz_target
==============================================================================
"""

def build_fuzz_target (target, path, logger):
path_artifacts = os.path.join (path, 'artifacts')

build_libdaisy ()

os.environ ['CONFIGURATION'] = 'Release'

if logger == 'auto':
if stlink_plugged ():
logger = 'semihosting'
else:
logger = 'usb'

if logger == 'semihosting':
os.environ ['SEMIHOSTING'] = '1'

cmd = [
MAKE_CMD,
'--jobs',
'--directory=%s' % os.path.join (path_artifacts, 'fuzz')
]

subprocess.check_call (cmd)



"""
==============================================================================
Name : build_libdaisy
Expand Down Expand Up @@ -686,6 +732,38 @@ def deploy_performance (name, section, path, programmer):



"""
==============================================================================
Name : deploy_fuzz
==============================================================================
"""

def deploy_fuzz (name, section, path, programmer):
path_artifacts = os.path.join (path, 'artifacts')

if programmer == 'auto':
if section != 'flash':
programmer = 'dfu'
elif stlink_plugged ():
programmer = 'stlink'
else:
programmer = 'dfu'

if programmer == 'stlink':
if section != 'flash':
print ('Install option \'stlink\' doesn\'t support programming to %s.' % section)
print ('Please use option \'dfu\' instead.')
sys.exit ()

file_elf = os.path.join (path_artifacts, 'fuzz', 'Release', '%s.elf' % name)
deploy_openocd (name, file_elf)

elif programmer == 'dfu':
file_bin = os.path.join (path_artifacts, 'fuzz', 'Release', '%s.bin' % name)
deploy_dfu_util (name, section, file_bin)



"""
==============================================================================
Name : deploy_bootloader
Expand Down Expand Up @@ -929,3 +1007,81 @@ def run_performance_usb ():
while True:
nbr = daisy.in_waiting
print (daisy.read (nbr).decode ('utf-8'), end='')



"""
==============================================================================
Name : run_fuzz
==============================================================================
"""

def run_fuzz ():
if stlink_plugged ():
run_fuzz_semihosting ()
else:
run_fuzz_usb ()



"""
==============================================================================
Name : run_fuzz_semihosting
==============================================================================
"""

def run_fuzz_semihosting ():
openocd_cmd = [
OPENOCD_CMD,
'--search', OPENOCD_SCRIPTS,
'--file', 'interface/stlink.cfg',
'--file', 'target/stm32h7x.cfg',
]

gdb_cmd = [
GDB_CMD,
'--ex', 'target extended-remote localhost:3333', # connect to openocd gdb-server
'--ex', 'monitor reset halt', # restart device and stop execution
'--ex', 'monitor arm semihosting enable', # activate semihosting
'--ex', 'continue', # run
]

proc_openocd = subprocess.Popen (openocd_cmd)
proc_gdb = subprocess.Popen (gdb_cmd)

try:
while True:
pass
except KeyboardInterrupt:
proc_gdb.kill ()
proc_openocd.kill ()



"""
==============================================================================
Name : run_fuzz_usb
==============================================================================
"""

def run_fuzz_usb ():
print ('Waiting for Daisy...')
print ('(Press the RESET button if it doesn\'t connect)')

while not daisy_plugged ():
print ('.', end='', flush=True)
time.sleep (0.5)

print ('')

device = None
for port in serial.tools.list_ports.comports ():
if (port.vid, port.pid) == (0x0483, 0x5740): # Daisy Seed Built In
device = port.device

assert device
daisy = serial.Serial (device, 115200, timeout=0)

while True:
nbr = daisy.in_waiting
print (daisy.read (nbr).decode ('utf-8'), end='')
1 change: 1 addition & 0 deletions build-system/erbb/generators/action/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def generate (self, path, root):
def generate_module (self, path, module):
self.generate_module_action (path, module, 'daisy')
self.generate_module_action (path, module, 'perf')
self.generate_module_action (path, module, 'fuzz')
self.generate_module_action (path, module, 'ui')
self.generate_module_action (path, module, 'vcvrack')
self.generate_module_action (path, module, 'data')
Expand Down
52 changes: 52 additions & 0 deletions build-system/erbb/generators/action/action_fuzz_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
#
# action_daisy.py
# Copyright (c) 2020 Raphael Dinge
#
#Tab=3########################################################################


##### IMPORT #################################################################

from __future__ import print_function
import os
import subprocess
import sys

PATH_ROOT = '%PATH_ROOT%'
PATH_PROJECT = '%PATH_PROJECT%'

sys.path.insert (0, os.path.join (PATH_ROOT, 'build-system'))
import erbui



##############################################################################

if sys.version_info < (3, 7):
print ('This script requires python 3.7 or greater.', file = sys.stderr)
sys.exit (1)



##############################################################################

def find_erbui ():
files = os.listdir (PATH_PROJECT)
for file in files:
if file.endswith ('.erbui'):
return os.path.join (PATH_PROJECT, file)

return None

if __name__ == '__main__':
try:
project_path = os.path.abspath (PATH_PROJECT)
artifacts_path = os.path.join (project_path, 'artifacts')

ast = erbui.parse (find_erbui ())
erbui.generate_fuzz (artifacts_path, ast)

except subprocess.CalledProcessError as error:
print ('Action exited with %d' % error.returncode, file = sys.stderr)
sys.exit (1)
Loading

0 comments on commit a9cb1aa

Please sign in to comment.