Exemplo n.º 1
0
def main():
  if len(sys.argv[1:]) != 3:
    print('Usage:')
    print('   %s <rl file> <xml file> <output c file>' %
          os.path.basename(__file__))
    sys.exit(1)
  rl_filename, xml_filename, c_filename = sys.argv[1:]

  dfa = dfa_parser.ParseXml(xml_filename)

  with open(rl_filename) as rl_file:
    lines = list(rl_file)

  with open(c_filename, 'wt') as c_file:
    c_file.write(
      '/*\n'
      ' * THIS FILE IS AUTO-GENERATED. DO NOT EDIT.\n'
      ' * Generated by %s from %s and %s.\n'
      ' */\n\n' % (
          os.path.basename(__file__),
          os.path.basename(rl_filename),
          os.path.basename(xml_filename)))

    generator = Generator(dfa, c_file)

    in_ragel_block = False
    for line in lines:
      if line.strip() == '%%{':
        assert not in_ragel_block
        in_ragel_block = True
      elif line.strip() == '}%%':
        assert in_ragel_block
        in_ragel_block = False
      elif line.strip() == '%% write data;':
        assert not in_ragel_block
        generator.WriteData()
      elif line.strip() == '%% write init;':
        assert not in_ragel_block
        generator.WriteInit()
      elif line.strip() == '%% write exec;':
        assert not in_ragel_block
        generator.WriteExec()
      else:
        if not in_ragel_block:
          c_file.write(line)

    assert not in_ragel_block
Exemplo n.º 2
0
def main():
    # We keep these global to share state graph between workers spawned by
    # multiprocess. Passing it every time is slow.
    global options, xml_file
    global dfa
    global worker_validator
    options, xml_file = ParseOptions()
    dfa = dfa_parser.ParseXml(xml_file)
    worker_validator = validator.Validator(validator_dll=options.validator_dll,
                                           decoder_dll=options.decoder_dll)

    assert dfa.initial_state.is_accepting
    assert not dfa.initial_state.any_byte

    sys.stderr.write('%d states\n' % len(dfa.states))
    num_suffixes = dfa_traversal.GetNumSuffixes(dfa.initial_state)
    num_instructions = sum(
        num_suffixes[t.to_state]
        for t in dfa.initial_state.forward_transitions.values())
    sys.stderr.write('%d instructions\n' % num_instructions)
    tasks = dfa_traversal.CreateTraversalTasks(dfa.states, dfa.initial_state)
    sys.stderr.write('%d tasks\n' % len(tasks))

    pool = multiprocessing.Pool()
    results = pool.imap_unordered(Worker, tasks)

    total = 0
    num_valid = 0

    node_cache = trie.NodeCache()
    full_trie = node_cache.empty_node

    # The individual workers create subtries that we merge in and compress here.
    for count, valid_count, sub_trie, in results:
        total += count
        num_valid += valid_count
        full_trie = node_cache.Merge(full_trie, sub_trie)
        sys.stderr.write('%.2f%% completed\n' %
                         (total * 100.0 / num_instructions))
    sys.stderr.write('%d instructions were processed\n' % total)
    sys.stderr.write('%d valid instructions\n' % num_valid)

    trie.WriteToFile(options.trie_path, full_trie)
Exemplo n.º 3
0
def main():
    parser = optparse.OptionParser(__doc__)
    parser.add_option('-o',
                      '--out',
                      help='Output file name (instead of sys.stdout')

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error('need exactly one ragel XML file')

    xml_file = args[0]
    dfa = dfa_parser.ParseXml(xml_file)

    CheckForProperCleanup(dfa.states)

    collected_dangerous_paths = CheckDFAActions(dfa.initial_state, dfa.states)

    all_superinstructions = set()
    for dangerous_paths in collected_dangerous_paths:
        superinstructions = []
        CollectSandboxing(dfa.initial_state, dangerous_paths,
                          superinstructions)
        assert len(superinstructions) > 0
        for superinstruction in superinstructions:
            all_superinstructions.add('.byte ' + ', '.join([
                '0x{0:02x}'.format(transition.byte)
                for transition in superinstruction
            ]))

    if options.out is not None:
        with open(options.out, "w") as out_file:
            for superinstruction in sorted(all_superinstructions):
                print(superinstruction, file=out_file)
    else:
        for superinstruction in sorted(all_superinstructions):
            print(superinstruction)
Exemplo n.º 4
0
        parser.error('specify --bits 32 or --bits 64')
    if not (options.gas and options.objdump and options.decoder):
        parser.error('specify path to gas, objdump and decoder')

    if len(args) != 1:
        parser.error('specify one xml file')

    (xml_file, ) = args

    return options, xml_file


options, xml_file = ParseOptions()
# We are doing it here to share state graph between workers spawned by
# multiprocess. Passing it every time is slow.
dfa = dfa_parser.ParseXml(xml_file)


def main():
    # Decoder is supposed to have one accepting state.
    accepting_states = [state for state in dfa.states if state.is_accepting]
    assert accepting_states == [dfa.initial_state]

    assert not dfa.initial_state.any_byte

    num_suffixes = dfa_traversal.GetNumSuffixes(dfa.initial_state)
    # We can't just write 'num_suffixes[dfa.initial_state]' because
    # initial state is accepting.
    total_instructions = sum(
        num_suffixes[t.to_state]
        for t in dfa.initial_state.forward_transitions.values())