<commit_before>// Copyright 2013-2015 Stanford University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STOKE_TOOLS_GADGETS_CFG_H
#define STOKE_TOOLS_GADGETS_CFG_H

#include <vector>

#include "src/ext/x64asm/include/x64asm.h"

#include "src/cfg/cfg.h"
#include "src/sandbox/sandbox.h"
#include "src/target/cpu_info.h"
#include "src/tunit/tunit.h"
#include "tools/args/in_out.h"
#include "tools/ui/console.h"

namespace stoke {

class CfgGadget : public Cfg {
public:
  CfgGadget(const x64asm::Code& code, const std::vector<TUnit>& aux_fxns)
    : Cfg(fix_code(code), def_in(live_out()), live_out()) {
    // Emit warning if register values were guessed
    reg_warning();

    // Check for unsupported instructions and cpu flags
    flag_check(get_code());
    sandbox_check(get_code());

    // Check that this function can link against auxiliary functions
    linker_check(aux_fxns);

    // Add summaries for auxiliary functions
    summarize_functions(aux_fxns);
  }

private:
  void reg_warning() const {
    // The static guard here to prevent this warning from being emitted more than once
    // once for the target, and once for current, best_cost, best_correct, etc...
    static auto once = false;
    if (!once) {
      once = true;
      if (!live_out_arg.has_been_provided()) {
        Console::warn() << "No live out values provided, assuming " << live_out() << std::endl;
      }
      if (!def_in_arg.has_been_provided()) {
        Console::warn() << "No def in values provided; assuming " << def_in(live_out()) << std::endl;
      }
    }
  }

  x64asm::Code fix_code(const x64asm::Code& code) const {
    for (const auto& instr : code) {
      if (instr.is_ret()) {
        return code;
      }
    }

    Console::warn() << "Adding a missing retq instruction to target/rewrite" << std::endl;
    auto ret = code;<commit_msg>Fixes #507 Merge branch 'issue-507' into develop<commit_after>// Copyright 2013-2015 Stanford University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STOKE_TOOLS_GADGETS_CFG_H
#define STOKE_TOOLS_GADGETS_CFG_H

#include <vector>

#include "src/ext/x64asm/include/x64asm.h"

#include "src/cfg/cfg.h"
#include "src/sandbox/sandbox.h"
#include "src/target/cpu_info.h"
#include "src/tunit/tunit.h"
#include "tools/args/in_out.h"
#include "tools/ui/console.h"

namespace stoke {

class CfgGadget : public Cfg {
public:
  CfgGadget(const x64asm::Code& code, const std::vector<TUnit>& aux_fxns)
    : Cfg(code, def_in(live_out()), live_out()) {
    // Emit warning if register values were guessed
    reg_warning();

    // Check for unsupported instructions and cpu flags
    flag_check(get_code());
    sandbox_check(get_code());

    // Check that this function can link against auxiliary functions
    linker_check(aux_fxns);

    // Add summaries for auxiliary functions
    summarize_functions(aux_fxns);
  }

private:
  void reg_warning() const {
    // The static guard here to prevent this warning from being emitted more than once
    // once for the target, and once for current, best_cost, best_correct, etc...
    static auto once = false;
    if (!once) {
      once = true;
      if (!live_out_arg.has_been_provided()) {
        Console::warn() << "No live out values provided, assuming " << live_out() << std::endl;
      }
      if (!def_in_arg.has_been_provided()) {
        Console::warn() << "No def in values provided; assuming " << def_in(live_out()) << std::endl;
      }
    }
  }

  x64asm::Code fix_code(const x64asm::Code& code) const {
    // This function adds a retq to a code that doesn't have any
    // It's causing endless warnings for the time being for search states which are
    // default constructed to empty functions.
    // I tried changing that, but ran into segfaults, so for the time being, this is
    // deactivated.
    for (const auto& instr : code) {
      if (instr.is_ret()) {
        return code;
      }
    }

    Console::warn() << "Adding a missing retq instruction to target/rewrite" << std::endl;
    auto ret = code;
{'fraction_non_alphanumeric': 0.1132858837485172, 'fraction_numerical': 0.009885330170027679, 'mean_word_length': 3.349957007738607, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2715766', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1538, 'n_words': 525}