Пример #1
0
def prettyPrintRule(kRule, symbolTable):
    newRule = pyk.KRule(pyk.pushDownRewrites(kRule['body']), requires = kRule['requires'], ensures = kRule['ensures'], att = kRule['att'])
    minRule = pyk.minimizeRule(newRule)
    ruleBody = minRule['body']
    if pyk.isKApply(ruleBody) and ruleBody['label'] == '<generatedTop>':
        ruleBody = ruleBody['args'][0]
    minRule['body'] = insertDots(ruleBody)
    return pyk.prettyPrintKast(minRule, symbolTable)
Пример #2
0
def pykPrettyPrint(module):
    WASM_definition_llvm_no_coverage_dir = '.build/defn/llvm'
    WASM_definition_main_file = 'test'
    WASM_definition_llvm_no_coverage = pyk.readKastTerm(
        WASM_definition_llvm_no_coverage_dir + '/' +
        WASM_definition_main_file + '-kompiled/compiled.json')
    WASM_symbols_llvm_no_coverage = pyk.buildSymbolTable(
        WASM_definition_llvm_no_coverage)
    print(pyk.prettyPrintKast(module, WASM_symbols_llvm_no_coverage))
def run_test_file(template_wasm_config, test_file_path, output_dir, cmd_args):
    global args
    test_name = os.path.basename(test_file_path)
    k_steps = get_steps_as_kseq(test_file_path, output_dir)
    final_config = None

    if cmd_args.log_level == 'none' or cmd_args.log_level == 'per-file':
        # Flatten the list of k_steps, just run them all in one go.
        k_steps = [('full', [y for (_, x) in k_steps for y in x])]

    (symbolic_config, init_subst) = pyk.splitConfigFrom(template_wasm_config)

    for i in range(len(k_steps)):
        step_name, curr_step = k_steps[i]
        if args.verbose:
            print('Executing step %s' % step_name)
        init_subst['K_CELL'] = KSequence(curr_step)

        init_config = pyk.substitute(symbolic_config, init_subst)

        input_json = config_to_kast_term(init_config)
        krun_args = ['--term', '--debug']

        # Run: generate a new JSON as a temporary file, then read that as the new wasm state.
        if cmd_args.log_level != 'none':
            log_intermediate_state("%s_%d_%s.pre" % (test_name, i, step_name),
                                   init_config, output_dir)
        (rc, new_wasm_config,
         err) = pyk.krunJSONLegacy(WASM_definition_llvm_no_coverage_dir,
                                   input_json,
                                   krunArgs=krun_args,
                                   teeOutput=True)
        if rc != 0:
            print('output:\n%s' % new_wasm_config, file=sys.stderr)
            print(
                pyk.prettyPrintKast(new_wasm_config,
                                    WASM_symbols_llvm_no_coverage))
            raise Exception("Received error while running: " + err)

        final_config = new_wasm_config

        if cmd_args.log_level != 'none':
            log_intermediate_state("%s_%d_%s" % (test_name, i, step_name),
                                   new_wasm_config, output_dir)

        # Check if the k cell is empty
        (symbolic_config, init_subst) = pyk.splitConfigFrom(new_wasm_config)
        k_cell = init_subst['K_CELL']
        assert k_cell['node'] == 'KSequence' and k_cell[
            'arity'] == 0, "k cell not empty, contains a sequence of %d items.\nSee %s" % (
                k_cell['arity'], output_dir)

    return final_config
Пример #4
0
def printMCD(k):
    return pyk.prettyPrintKast(k, MCD_definition_llvm_symbols)
Пример #5
0
    files = os.listdir(path)
    return [ path + '/' + f for f in files if f.endswith('_coverage.txt') ]

def krunCoverage(inputJson, *krunArgs):
    for f in findCoverageFiles(WASM_definition_llvm_coverage_dir + '/' + WASM_definition_main_file + '-kompiled'):
        os.remove(f)
    ret = pyk.krunJSON(WASM_definition_llvm_coverage_dir, inputJson, krunArgs = list(krunArgs))
    [ coverageFile ] = findCoverageFiles(WASM_definition_llvm_coverage_dir + '/' + WASM_definition_main_file + '-kompiled')
    return coverageFile

def kprove(inputJson, *krunArgs):
    return pyk.kproveJSON(WASM_definition_llvm_no_coverage, inputJson, kproveArgs = list(kproveArgs))

################################################################################
# Main Functionality                                                           #
################################################################################

def get_init_config():
    init_term = { 'format': 'KAST', 'version': 1, 'term': KConstant('.List{"___WASM_Stmts_Stmt_Stmts"}_Stmts') }
    (_, simple_config, _) = krun(init_term)
    return pyk.splitConfigFrom(simple_config)

if __name__ == '__main__':
    (generatedTop, initSubst) = get_init_config()
    initial_configuration = pyk.substitute(generatedTop, initSubst)
    kast_json = { 'format': 'KAST', 'version': 1, 'term': initial_configuration }

    fastPrinted = pyk.prettyPrintKast(initial_configuration['args'][0], WASM_symbols_llvm_no_coverage)
    _notif('fastPrinted output')
    print(fastPrinted)
Пример #6
0
    if pyk.isKApply(k):
        if not (k['label'] == '_+Int_'):
            return pyk.onChildren(k, lambda x: simplifyPlusInt(x))
        (c, vs) = gatherConstInts(k)
        if not (c == 0):
            vs.append(KToken(str(c), 'Int'))
        return buildPlusInt(vs)
    return k

simplified_json = pyk.simplifyBool(gas_exp)
simplified_json = simplifyPlusInt(simplified_json)

rewrites = [ (KApply('_==K_', [KVariable('I1'), KVariable('I2')]),  KApply('_==Int_', [KVariable('I1'), KVariable('I2')]))
           , (KApply('_=/=K_', [KVariable('I1'), KVariable('I2')]), KApply('_=/=Int_', [KVariable('I1'), KVariable('I2')]))
           , ( KApply(ite_label, [KVariable('C'), KApply('_+Int_', [KVariable('I'), KVariable('I1')]), KApply('_+Int_', [KVariable('I'), KVariable('I2')])])
             , KApply('_+Int_', [KVariable('I'), KApply(ite_label, [KVariable('C'), KVariable('I1'), KVariable('I2')])])
             )
           , ( KApply(ite_label, [KVariable('C'), KApply('_+Int_', [KVariable('I1'), KVariable('I')]), KApply('_+Int_', [KVariable('I2'), KVariable('I')])])
             , KApply('_+Int_', [KVariable('I'), KApply(ite_label, [KVariable('C'), KVariable('I1'), KVariable('I2')])])
             )
           ]

for r in rewrites:
    simplified_json = pyk.rewriteAnywhereWith(r, simplified_json)

for c in constraint:
    rule = (c['args'][0], c['args'][1])
    simplified_json = pyk.replaceAnywhereWith(rule, simplified_json)

print(pyk.prettyPrintKast(simplified_json, symbolTable))
def log_intermediate_state(name, config, output_dir):
    with open('%s/%s' % (output_dir, name), 'w') as f:
        f.write(json.dumps(config_to_kast_term(config)))
    with open('%s/%s.pretty.k' % (output_dir, name), 'w') as f:
        pretty = pyk.prettyPrintKast(config, WASM_symbols_llvm_no_coverage)
        f.write(pretty)
Пример #8
0
def prettyPrintRule(kRule, symbolTable):
    kRule['body'] = pyk.pushDownRewrites(kRule['body'])
    return pyk.prettyPrintKast(pyk.minimizeRule(kRule), symbolTable)