def from_line(cls, line, stage): res = cls.regexp.match(line.strip()) if res is None: raise Exception("%s is not a framac dst result") else: min_value = int(res.group(1), 0) max_value = int(res.group(2), 0) if max_value > 0xFFFFFFFF: max_value = 0xFFFFFFFF lvalue = res.group(3) path = res.group(4) # somewhat of a hack to get relative path root = Main.get_bootloader_cfg().software_cfg.root if path.startswith(root): path = os.path.relpath(path, root) path = os.path.join(Main.get_config("temp_bootloader_src_dir"), path) elif path.startswith( Main.test_suite_path): # not sure why this happens path = os.path.relpath(path, Main.test_suite_path) path = os.path.join(Main.get_config("temp_bootloader_src_dir"), path) elif path.startswith("/tmp/tmp"): path = "/".join(path.split("/")[3:]) path = os.path.join(Main.get_config("temp_bootloader_src_dir"), path) lineno = int(res.group(5)) callstack = res.group(6) # somewhat of a hack for muxconf return cls(path, lineno, lvalue, [intervaltree.Interval(min_value, max_value)], callstack=callstack, stage=stage)
def __init__(self, print_build, do_build): self.init_only = True if len(print_build) + len(do_build) == 0 else False self.print_build = print_build self.do_build = do_build self.builds = list(set(do_build + print_build)) ss = Main.config_class_lookup("Software") bootloader = Main.get_bootloader_cfg() self.code_tasks = [CodeTaskList(s, s.name not in self.do_build) for s in ss if hasattr(s, "build_required") and s.build_required and (s.name in self.builds)] # always need the bootloader if bootloader.software not in self.builds: self.code_tasks.extend(CodeTaskList(s, s.name not in self.do_build) for s in ss if s.name == bootloader.software) if self.init_only: for c in self.code_tasks: for t in c.tasks: t.uptodate = [True] else: for c in self.code_tasks: if c.basename in self.do_build: for t in c.tasks: t.uptodate = [False]
def instances(cls, stage, root=Main.get_bootloader_cfg().software_cfg.root, quick=False): files = cls.files fs = map(lambda s: os.path.join(root, s), files) fs = map(functools.partial(PreprocessedFileInstance, stage=stage), fs) return fs
def __init__(self, option_strings, dest, **kwargs): stages = list( Main.get_bootloader_cfg().supported_stages.itervalues()) self.stagenames = [s.stagename for s in stages] self.nargs = 2 defaults = {} kwargs['default'] = defaults super(SubstageNameAction, self).__init__(option_strings, dest, **kwargs)
def _calculate_current_id(self): (gitid, sha) = self.boot_task.get_gitinfo() cc = Main.cc cc_name = self.boot_task.build_cfg.compiler_name ccpath = "%s%s" % (cc, cc_name) defconfig = Main.get_bootloader_cfg().makecfg hwclass = Main.get_hardwareclass_config().name bootsoftware = self.boot_task.build_cfg.name ccinfo = pure_utils.file_md5(ccpath) gitinfo = {'local': self.boot_task.build_cfg.root, 'sha1': sha} return ("%s.%s.%s.%s.%s" % (hwclass, bootsoftware, defconfig, gitid, ccinfo), gitinfo)
def __init__(self, option_strings, dest, **kwargs): self.stages = list( Main.get_bootloader_cfg().supported_stages.itervalues()) self.stagenames = [s.stagename for s in self.stages] self.nargs = 3 defaultdir = os.path.join(Main.hw_info_path, Main.get_hardwareclass_config().name, Main.get_bootloader_cfg().software) self.sdefaults = { s.stagename: (os.path.join(defaultdir, s.stagename, "substages.yml"), os.path.join(defaultdir, s.stagename, "memory_map.yml")) for s in self.stages } if dest == "importpolicy": defaults = self.sdefaults else: defaults = {} kwargs['default'] = defaults super(SubstageFileAction, self).__init__(option_strings, dest, **kwargs)
def __init__(self, option_strings, dest, **kwargs): self.stages = list( Main.get_bootloader_cfg().supported_stages.itervalues()) hw_classes = Main.get_hardwareclass_config().hardware_type_cfgs self.hw_classes = list(hw_classes.iterkeys()) self.tracing_methods = { k: v.tracing_methods for k, v in hw_classes.iteritems() } self.nargs = 3 self.selected = False self.d = { 'stages': ["spl"], "traces": ["breakpoint", "calltrace"], "hw": "bbxmqemu", } kwargs['default'] = {} super(TraceAction, self).__init__(option_strings, dest, **kwargs)
def finalize(self, args): substage_names = { s.stagename: self._stages[s.stagename].substages for s in self.stage_order if self._stages[s.stagename].substages is not None } stages = [s.stagename for s in self.stage_order] doit_manager.TaskManager(False, False, stages, substage_names, False, None, self.test_trace_name, False, [], self.test_instance_name, hook=True) tmpdir = Main.get_config("temp_bootloader_src_dir") gdb.execute("dir %s" % tmpdir) gdb.execute("set substitute-path %s %s" % (Main.get_bootloader_cfg().software_cfg.root, tmpdir)) self.hw = Main.get_config("trace_hw") if self.hw.name == "bbxmqemu": self.isbaremetal = False else: self.isbaremetal = True if len(self.stage_order) == 0: self.stage_order = [ Main.stage_from_name(s) for s in list(self._stages.iterkeys()) ] for stage in self.stage_order: s = self._stages[stage.stagename] s.init_with_test_instance() # self.f_hooks = list(set(self.f_hooks)) for f in self.f_hooks: f(args)
def __init__(self, do_build, create_test, enabled_stages, policies, quick, run_trace, select_trace, import_policies, post_trace_processing=[], open_instance=None, run=True, print_cmds=False, hook=False, rm_dir=True): (print_build_cmd, build_source) = ([], []) bootloader = Main.get_bootloader_cfg() self.create_test = create_test self.print_cmds = print_cmds self.src_manager = external_source_manager.SourceLoader( print_build_cmd, build_source) self.loaders.append(self.src_manager) if (len(print_build_cmd) + len(build_source) > 0): return self.boot_task = [ s for s in self.src_manager.code_tasks if s.build_cfg.name == bootloader.software ][0] needs_build = False if create_test: if not self.boot_task.has_nothing_to_commit(): self.boot_task.commit_changes() needs_build = True (self.test_id, gitinfo) = self._calculate_current_id() current_id = self.test_id run = True else: if open_instance is None: self.test_id = self._get_newest_id() else: ids = self._get_all_ids() if open_instance not in ids: open_instance = difflib.get_close_matches( open_instance, ids, 1, 0)[0] open_instance = os.path.basename(open_instance) self.test_id = open_instance # resolve trace name to something that exists select_trace = instrumentation_results_manager.TraceTaskPrepLoader.get_trace_name( self.test_id, select_trace) config_path = os.path.join( instrumentation_results_manager.TraceTaskPrepLoader.test_path( self.test_id, select_trace), "config.yml") print config_path with open(config_path, 'r') as f: settings = yaml.load(f) enabled_stages = settings['stages'] self.boot_task.build.uptodate = [True] (current_id, gitinfo) = self._calculate_current_id() run = False self.ti = instrumentation_results_manager.InstrumentationTaskLoader( self.boot_task, self.test_id, enabled_stages, run, gitinfo, self, needs_build, rm_dir) if create_test: self.ppt = instrumentation_results_manager.PolicyTaskLoader( policies) self.loaders.append(instrumentation_results_manager.task_manager()) return self.tp = instrumentation_results_manager.TraceTaskPrepLoader( run_trace, select_trace, not hook and len(post_trace_processing) == 0, run, self.print_cmds, create_test, hook) self.pt = instrumentation_results_manager.PolicyTaskLoader(policies) run = run and (len(post_trace_processing) == 0) self.rt = instrumentation_results_manager.TraceTaskLoader( self.tp.stages, self.tp.hw, self.tp.tracenames, self.tp.trace_id, not self.print_cmds, quick, run, self.print_cmds, self.tp.new) run = (not create_test) or len(post_trace_processing) > 0 self.ppt = instrumentation_results_manager.PostTraceLoader( post_trace_processing, run) self.loaders.append(instrumentation_results_manager.task_manager())
def __init__(self): self.breakpoints = [] self.plugins = [] self.gone = False self.disabled_breakpoints = set() self.test_instance_name = None self.test_trace_name = None gdb.execute('set python print-stack full') self.cc = Main.cc self.name = "gdb_tools" self.command_handlers = [GDBBootCommandHandler(self.name, self, True)] self.parser = argparse.ArgumentParser(prog=self.name) self.subparser = self.parser.add_subparsers(title=self.name) # self.subparser_parsers = self.subparser.add_parser(self.name) # self.tool_subparsers = self.subparser_parsers.add_subparsers(title=self.name) self.cmds = [] self.subcommand_parsers = {} self.core_state = None self.current_substage = 0 self.current_substage_name = "" self.current_stage = None self._setup = False self.calculate_write_dst = False self.qemu_pid = None self.isbaremetal = False self.ia = staticanalysis.InstructionAnalyzer() self._kill = False self.bp_hooks = {} self.f_hooks = [] self.stage_hooks = [] self.exit_hooks = [] global breakpoint_classes for k in breakpoint_classes.iterkeys(): self.bp_hooks[k] = [] p = self.add_subcommand_parser('log') p.add_argument('logfile', default='', nargs='?') self.add_subcommand_parser('flushlog') p = self.add_subcommand_parser('go') p.add_argument('-p', "--prepare_only", action='store_true', default=False) p = self.add_subcommand_parser("startat") p.add_argument('-s', "--stage", action='store', default='') p.add_argument("pc", nargs='?') p = self.add_subcommand_parser('until') p.add_argument('-s', "--stage", action='store', default='') p.add_argument("pc", nargs='?') p = self.add_subcommand_parser('test_trace') p.add_argument('name') p = self.add_subcommand_parser('test_instance') p.add_argument('name') p = self.add_subcommand_parser('stages') p.add_argument('stage_name', nargs='*') p = self.add_subcommand_parser('substages') p.add_argument('stage_name') p.add_argument('substages_name') p = self.add_subcommand_parser('kill') p.add_argument('do_kill', nargs='?', default=False) p = self.add_subcommand_parser("setup_target") p.add_argument('do_setup', nargs='?', default=True) p = self.add_subcommand_parser('plugin') p.add_argument('module', nargs='?') self.stage_order = [Main.stage_from_name('spl') ] # default is spl stage only self._stages = { s.stagename: BootStageData(s, self.bp_hooks) for s in Main.get_bootloader_cfg().supported_stages.itervalues() } self.hw = None
sourcegroup = parser.add_mutually_exclusive_group(required=True) sourcegroup.add_argument('-S', '--Ssrc', action="store", help='name of assembly file to process') sourcegroup.add_argument('-c', '--csrc', action="store", help='name of c source file to process') parser.add_argument('-o', '--phaseoutputfile', type=argparse.FileType('a'), default=sys.stdout) parser.add_argument('-l', '--longwriteoutputfile', type=argparse.FileType('a'), default=sys.stdout) parser.add_argument('-L', '--printlongwrites', action="store_true", default=False, help='print longwrite labels') parser.add_argument('-P', '--printphases', action="store_false", default=True, help='print phase labels') parser.add_argument('-s', '--stage', action="store", choices=['spl', '.'], default='spl') parser.add_argument('-p', '--srcpath', action="store", default=Main.get_bootloader_cfg().software_cfg.root) sourcegroup.add_argument('--summarize', action="store", default="all", help="print out information on label type for source tree") args = parser.parse_args() if len(args.summarize) > 0: label = args.summarize if (not (label == "all")) and (label not in label_classes.keys()): print "label (%s) is not a valid label" % label sys.exit(1) label_class = None if not label == "all": label_class = label_classes[label] labels = SrcLabelTool.label_search(label_class, args.srcpath) label_by_file = {} for label in labels:
def __init__(self, stage, labels=None, execute=False, quick=False, more=False, verbose=False, patchdest=Main.get_bootloader_cfg().software_cfg.root, patch_symlink='', backupdir='', calltracefile=None, tee=None): self.frama_c = "frama-c" self.quick = quick self.execute = execute self.stage = stage self.verbose = verbose self.patchdest = patchdest if len(patch_symlink) == 0: patch_symlink = tempfile.mkdtemp() os.system("rm -r %s" % patch_symlink) if self.execute: atexit.register(lambda: os.system("rm %s" % patch_symlink)) self.shortdest = patch_symlink self.backupdir = backupdir self.tee = tee self.path = os.path.dirname(os.path.realpath(__file__)) verbosen = 3 if verbose else 3 # "-slevel-function printf:0 "\ # "-slevel-function puts:0 "\ # "-val-use-spec do_fat_read_at "\ self.frama_c_args = " -load-script %s -machdep arm " \ "-load-script %s " \ "-load-script %s " \ "-no-initialized-padding-locals " \ "-absolute-valid-range 0x10000000-0xffffffff "\ "-val-builtin malloc:Frama_C_malloc_fresh,free:Frama_C_free "\ "-val-initialization-padding-globals=no "\ "-constfold "\ "-kernel-verbose %d -value-verbose %d "\ "-big-ints-hex 0 "\ "-slevel 1 "\ "-val -then -dst -then -call" % (os.path.join(self.path, "machdep_arm.ml"), os.path.join(self.path, "dest_analysis.ml"), os.path.join(self.path, "call_analysis.ml"), verbosen, verbosen) if not self.quick: self.frama_c_args = "%s -slevel-function memset:1048576 " \ "-slevel-function malloc:2000 "\ "-slevel-function calloc:2000 " % self.frama_c_args if calltracefile: self.frama_c_args += " -call-output %s " % calltracefile self.frama_c_main_arg = " -main" self.more = more if more: self.frama_c_args += " -dst-more" self.results = {} if labels is None: self.labels = [ l for l in Main.get_config('labels')[labeltool.FramaCLabel] if (l.stagename == self.stage.stagename) ] else: self.labels = [ l for l in labels[labeltool.FramaCLabel] if l.stagename == self.stage.stagename ] self.entrypoints = [] self.preprocessed_files = []
if s is None: raise Exception("no such stage named %s" % args.stage) cc = Main.cc if not args.standalone: d = doit_manager.TaskManager(False, False, [args.stage], {args.stage: args.policy_id}, False, {}, args.test_id, False, [], hook=True, rm_dir=not args.keep_temp) labels = Main.get_config("labels") l = labels root = Main.get_config("temp_bootloader_src_dir") builder = d.build([Main.get_bootloader_cfg().software], False)[0] origdir = os.getcwd() os.chdir(root) for t in builder.tasks: for action in t.list_tasks()['actions']: if isinstance(action, CmdAction): do = action.expand_action() os.system(do) os.chdir(origdir) elf = Main.get_config("stage_elf", s) else: root = Main.get_bootloader_cfg().software.root labels = labeltool.get_all_labels(root) s.post_build_setup()