def test_find_param_var(): """ Test finding the name of a variable corresponding to a module parameter. This is necessary since for parameters defined with module_param_named, names of the parameter and of the variable differ. """ source = SourceTree("kernel/linux-3.10", KernelLlvmSourceBuilder) mod = source.get_module_for_symbol("rfkill_init") assert mod.find_param_var("default_state").name == "rfkill_default_state"
def _from_yaml(self, yaml_file): """ Load the snaphot from its YAML representation. Paths are assumed to be relative to the root directory. :param yaml_file: Contents of the YAML file. """ yaml_loader = (yaml.CSafeLoader if "CSafeLoader" in yaml.__dict__ else yaml.SafeLoader) yaml_file = yaml.load(yaml_file, Loader=yaml_loader) yaml_dict = yaml_file[0] self.created_time = yaml_dict["created_time"] self.created_time = self.created_time.replace( tzinfo=datetime.timezone.utc) self.llvm_version = yaml_dict["llvm_version"] llvm_finder_cls = None llvm_finder_path = None if yaml_dict["llvm_source_finder"]["kind"] == "kernel_with_builder": llvm_finder_cls = KernelLlvmSourceBuilder if llvm_finder_cls is not None: self.snapshot_tree.create_source_finder(llvm_finder_cls, llvm_finder_path) if os.path.isdir(yaml_dict["source_dir"]): self.source_tree = SourceTree(yaml_dict["source_dir"], llvm_finder_cls, llvm_finder_path) else: sys.stderr.write( "Warning: snapshot in {} has missing or invalid source_dir, " "some comparison features may not be available\n".format( self.snapshot_tree.source_dir)) if "sysctl" in yaml_dict["list"][0]: self.kind = "sysctl" groups = yaml_dict["list"] else: groups = [yaml_dict["list"]] for g in groups: if "sysctl" in g: self.kind = "sysctl" group = g["sysctl"] functions = g["functions"] else: group = None functions = g self.fun_groups[group] = self.FunctionGroup() for f in functions: self.add_fun( f["name"], LlvmModule( os.path.join( os.path.relpath(self.snapshot_tree.source_dir), f["llvm"])) if f["llvm"] else None, f["glob_var"], f["tag"], group)
def test_syntax_diff(): f = "dio_iodone2_helper" diff = ('*************** static void dio_iodone2_helper(struct dio *dio, l' 'off_t offset,\n*** 246,250 ***\n {\n! \tif (dio->end_io && dio->' 'result)\n! \t\tdio->end_io(dio->iocb, offset,\n! \t\t\t\ttransfer' 'red, dio->private, ret, is_async);\n \n--- 246,249 ---\n {\n! ' '\tif (dio->end_io)\n! \t\tdio->end_io(dio->iocb, offset, ret, dio' '->private, 0, 0);\n \n') source_first = SourceTree("kernel/linux-3.10.0-862.el7", KernelLlvmSourceBuilder) source_second = SourceTree("kernel/linux-3.10.0-957.el7", KernelLlvmSourceBuilder) config = Config(source_first, source_second, show_diff=True, output_llvm_ir=False, pattern_config=None, control_flow_only=True, print_asm_diffs=False, verbosity=False, use_ffi=False, semdiff_tool=None) first = source_first.get_module_for_symbol(f) second = source_second.get_module_for_symbol(f) fun_result = functions_diff(mod_first=first, mod_second=second, fun_first=f, fun_second=f, glob_var=None, config=config) assert fun_result.inner[f].diff == diff
def load_from_dir(cls, snapshot_dir, config_file="snapshot.yaml"): """ Loads a snapshot from its directory. :param snapshot_dir: Target snapshot directory. :param config_file: Name of the snapshot configuration file. :return: Desired instance of Snapshot. """ snapshot_tree = SourceTree(snapshot_dir) loaded_snapshot = cls(None, snapshot_tree) with open(os.path.join(snapshot_dir, config_file), "r") as \ snapshot_yaml: loaded_snapshot._from_yaml(snapshot_yaml.read()) return loaded_snapshot
def test_copy_source_files(source): """Test copying source files into other root kernel directory.""" m1 = source.get_module_for_symbol("net_ratelimit") m2 = source.get_module_for_symbol("__alloc_workqueue_key") tmp_source = SourceTree(tempfile.mkdtemp()) source.copy_source_files([m1, m2], tmp_source) # Check that necessary directories were created. for d in ["net", "net/core", "kernel", "include", "include/linux"]: assert os.path.isdir(os.path.join(tmp_source.source_dir, d)) # Check that files were successfully copied. for f in [ "net/core/utils.c", "net/core/utils.ll", "kernel/workqueue.c", "kernel/workqueue.ll", "include/linux/module.h", "include/linux/kernel.h" ]: assert os.path.isfile(os.path.join(tmp_source.source_dir, f)) shutil.rmtree(tmp_source.source_dir)
def source(): """Create KernelSource shared among multiple tests.""" s = SourceTree("kernel/linux-3.10.0-957.el7", KernelLlvmSourceBuilder) yield s s.finalize()
def source(): # If a new class extending LlvmSourceFinder is implemented, it should be # added here for testing that it provides correct LLVM IR files. s = SourceTree("kernel/linux-3.10.0-957.el7", KernelLlvmSourceBuilder) yield s s.finalize()
def __init__(self, source_dir, source_finder_cls=None, source_finder_path=None): SourceTree.__init__(self, source_dir, source_finder_cls, source_finder_path)