summary refs log tree commit diff
path: root/scout.cc
blob: 6c3ddb5ab4df603dc6622bc75ec4885a8d499c55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Patch's jump destinations searcher
// Copyright (C) 2024  Nguyễn Gia Phong
//
// This file is part of taosc.
//
// Taosc is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Taosc is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with taosc.  If not, see <https://www.gnu.org/licenses/>.

// Dyninst headers
#include <CFG.h>
#include <CodeObject.h>
#include <Graph.h>
#include <Instruction.h>
#include <InstructionDecoder.h>
#include <slicing.h>

using AbstractRegion = Dyninst::AbsRegion;
using Address = Dyninst::Address;
using AssignmentConverter = Dyninst::AssignmentConverter;
using AssignmentPtr = Dyninst::Assignment::Ptr;
using Block = Dyninst::ParseAPI::Block;
using CodeObject = Dyninst::ParseAPI::CodeObject;
using CodeRegion = Dyninst::ParseAPI::CodeRegion;
using CodeSource = Dyninst::ParseAPI::SymtabCodeSource;
using Edge = Dyninst::Edge;
using EdgeIterator = Dyninst::EdgeIterator;
using Function = Dyninst::ParseAPI::Function;
using Graph = Dyninst::Graph;
using Instruction = Dyninst::InstructionAPI::Instruction;
using InstructionCategory = Dyninst::InstructionAPI::InsnCategory;
using InstructionDecoder = Dyninst::InstructionAPI::InstructionDecoder;
using NodeIterator = Dyninst::NodeIterator;
using SliceNode = Dyninst::SliceNode;
using Slicer = Dyninst::Slicer;

#include <cassert>
#include <filesystem>
#include <functional>
#include <iostream>
#include <map>
#include <queue>

/// Collect elements from given iterator into a vector
template <class Element, class Iterator>
  std::vector <Element*>
  range (auto iter)
  {
    Iterator begin, end;
    std::vector <Element*> result;
    for (iter (begin, end); begin != end; ++begin)
      result.push_back (static_cast <Element*> ((*begin).get ()));
    return result;
  }

class SlicerHelper
  {
    CodeSource& cs;
    InstructionDecoder decoder;
    Slicer::Predicates predicates;
    AssignmentConverter ac {true, true};
    std::set <Address> seen;
    std::vector <std::vector <Address>> bfs_slices;

  public:
    SlicerHelper (CodeSource& cs)
    : cs {cs},
      decoder {(const void*) nullptr, 1, cs.getArch ()},
      ac {true, true} // enable caching and stack analysis
    {
      this->predicates.setSearchForControlFlowDep(true);
    }

    /// Decode instruction at given address
    Instruction
    decode (Address addr)
    {
      auto const& insn = this->decoder.decode ((const unsigned char*)
        this->cs.getPtrToInstruction (addr));
      assert (insn.size () > 0);
      return insn;
    }

    /// Collect the interprocedure backward slice at addr in BFS order
    void
    slice (Instruction const& insn, Address addr, Function* fun, Block* blk)
    {
      std::vector <AssignmentPtr> assignments;
      this->ac.convert (insn, addr, fun, blk, assignments);
      if (assignments.empty ())
        return;
      for (auto const& asgn : assignments)
        {
          this->bfs_slices.emplace_back ();
          Slicer s {asgn, blk, fun};
          auto const& slice = s.backwardSlice (this->predicates);
#define ITER(i, E, I, x, f) \
  for (auto const& i : range <E, I> ([x] (auto& b, auto& e) { x->f (b, e); }))
          ITER (node, SliceNode, NodeIterator, slice, exitNodes)
            {
              this->seen.insert (node->addr ());
              std::queue <SliceNode*> q; // breadth-first traversal
              q.push (node);
              while (!q.empty ())
                {
                  auto const& parent = q.front ();
                  q.pop ();
                  this->bfs_slices.back ().push_back (parent->addr ());
                  ITER (edge, Edge, EdgeIterator, parent, ins)
#undef ITER
                    {
                      auto const& child = edge->source ();
                      if (this->seen.count (child->addr ()) > 0)
                        continue;
                      this->seen.insert (child->addr ());
                      q.push (static_cast <SliceNode*> (child.get ()));
                    }
                }
            }
        }
    }

    /// Flatten stored BFS slices in round-robin order
    std::vector <Address> const
    slice_zip ()
    {
      std::vector <Address> result;
      size_t n = 0;
      for (auto const& v : this->bfs_slices)
        {
          if (result.empty () || v[0] != result.back ())
            result.push_back (v[0]);
          n = std::max (n, v.size ());
        }
      for (size_t i = 1; i < n; ++i)
        for (auto const& v : this->bfs_slices)
          if (i < v.size ())
            result.push_back (v[i]);
      return result;
    }
  };

/// Find next basic block's entry after given address, reparsing if necessary
Block*
next_block (CodeObject& co, CodeRegion* region, Address address)
{
  auto blk = co.findBlockByEntry (region, address);
  if (blk != nullptr)
    return blk;
  co.parse (address, true);
  blk = co.findBlockByEntry (region, address);
  return (blk != nullptr) ? blk : co.findNextBlock (region, address);
}

/// Find block containing given address
Block*
find_block (CodeSource& cs, CodeObject& co, Address target_addr)
{
  if (!cs.isCode (target_addr))
    {
      std::cerr << std::hex << target_addr
        << " does not point to an instruction\n";
      return nullptr;
    }
  std::set <CodeRegion*> regions;
  if (cs.findRegions (target_addr, regions) != 1)
    {
      std::cerr << "expected 1 region containing instruction, found "
        << regions.size () << '\n';
      return nullptr;
    }
  for (auto const& region : regions)
    {
      std::set <Block*> blocks;
      if (co.findBlocks (region, target_addr, blocks) > 0)
        for (auto const& blk : blocks) // TODO: choose the best block
          return blk;

      auto* blk = next_block (co, region, region->low ());
      while (blk != nullptr && target_addr > blk->last ())
        blk = next_block (co, region, blk->end ());
      if (blk == nullptr)
        return nullptr;
      assert (target_addr >= blk->start () && target_addr < blk->end ());
      return blk;
    }
#if defined(__cpp_lib_unreachable) && (__cpp_lib_unreachable >= 202202L)
  std::unreachable ();
#else
  __builtin_unreachable(); // GCC or Clang
#endif
}

/// Slice backward from return instructions
std::vector <Address> const
returns_slice (CodeSource& cs, Function* fun, Address target_addr)
{
  SlicerHelper helper {cs};
  for (auto const& blk : fun->blocks ())
    for (auto [addr, step] = std::tuple {blk->start (), 0ul};
         addr < blk->end ();
         addr += step)
      {
        auto const& insn = helper.decode (addr);
        step = insn.size ();
        if (insn.getCategory () == InstructionCategory::c_ReturnInsn)
          helper.slice (insn, addr, fun, blk);
      }
  return helper.slice_zip ();
}

int
main (int argc, char** argv)
{
  if (argc != 3)
    {
      std::cerr << "Usage: " << std::filesystem::path (argv[0]).filename ()
        << " binary instruction-address\n";
      return -1;
    }
  CodeSource cs {argv[1]};
  Address target_addr;
  {
    std::stringstream ss;
    ss << std::hex << argv[2];
    ss >> target_addr;
  }
  CodeObject co {&cs};
  co.parse (); // parsed functions have same lifetime as co
  auto const& block = find_block (cs, co, target_addr);
  if (block == nullptr)
    {
      std::cerr << "block containing instruction not found\n";
      return -1;
    }
  std::vector<Function*> functions;
  block->getFuncs (functions);
  if (functions.size () != 1)
    {
      std::cerr << "0 or multiple functions containing instruction found\n";
      return -1;
    }
  for (auto* fun : functions)
    for (auto const& addr : returns_slice (cs, fun, target_addr))
      std::cout << std::hex << addr << '\n';
  return 0;
}