about summary refs log tree commit diff
path: root/measure-stack.cc
blob: 51517722523ccdc901bf21622e9739351449ebe1 (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
// Function stack size counter
// Copyright (C) 2025  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/>.

#include "helpers.hh"

// Dyninst headers
#include <Architecture.h>
#include <CFG.h>
#include <CodeObject.h>
#include <InstructionDecoder.h>
#include <Register.h>

using Architecture = Dyninst::Architecture;
using Address = Dyninst::Address;
using Function = Dyninst::ParseAPI::Function;
using CodeObject = Dyninst::ParseAPI::CodeObject;
using CodeSource = Dyninst::ParseAPI::SymtabCodeSource;
using Instruction = Dyninst::InstructionAPI::Instruction;
using InstructionDecoder = Dyninst::InstructionAPI::InstructionDecoder;
using MachRegister = Dyninst::MachRegister;
using Operand = Dyninst::InstructionAPI::Operand;
using RegisterAST = Dyninst::InstructionAPI::RegisterAST;

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
#include <utility>

static int64_t
immediate (Operand const& operand)
{
  auto const& result = operand.getValue ().get ()->eval ();
  assert (result.defined);
  switch (result.type)
    {
    case Dyninst::InstructionAPI::s8:
    case Dyninst::InstructionAPI::u8:
      return result.val.s8val;
    case Dyninst::InstructionAPI::s16:
    case Dyninst::InstructionAPI::u16:
      return result.val.s16val;
    case Dyninst::InstructionAPI::s32:
    case Dyninst::InstructionAPI::u32:
      return result.val.s32val;
    case Dyninst::InstructionAPI::s64:
    case Dyninst::InstructionAPI::u64:
      return result.val.s64val;
    default:
      std::unreachable ();
    }
}

static int64_t
stack_offset (Instruction const& instruction,
              Architecture architecture)
{
  switch (instruction.getOperation ().getID ())
    {
    case e_push:
      return Dyninst::getArchAddressWidth (architecture);
    case e_pop:
      return -Dyninst::getArchAddressWidth (architecture);
    case e_sub:
      return immediate (instruction.getOperand (1));
    case e_add:
      return -immediate (instruction.getOperand (1));
    default:
      std::unreachable ();
    }
}

int
main (int argc, char** argv)
{
  CodeSource cs {parse_args (argc, argv)};
  auto const architecture = cs.getArch ();
  RegisterAST::Ptr const stack_pointer
    {new RegisterAST(MachRegister::getStackPointer (architecture))};
  CodeObject co {&cs};
  co.parse (); // parsed functions have same lifetime as co
  while (!std::cin.eof ())
    {
      Address address;
      std::cin >> std::hex >> address;
      if (std::cin.fail ())
        break;
      auto* const block = find_block (cs, co, address);
      if (block->containingFuncs () < 1)
        die_for (address, "no function found containing instruction at");
      std::vector <Function*> functions;
      block->getFuncs (functions);
      size_t stack_size = 0;
      for (auto* const fun : functions)
        {
          auto const entry = fun->addr ();
          auto const* buffer = (char*) cs.getPtrToInstruction (entry);
          auto const length = fun->entry ()->end () - entry;
          InstructionDecoder decoder {buffer, length, architecture};
          size_t s = 0;
          for (auto insn = decoder.decode ();
               insn.isValid ();
               insn = decoder.decode ())
            if (insn.isWritten (stack_pointer))
              s += stack_offset (insn, architecture);
          if (s == 0)
            continue;
          if (stack_size == 0)
            stack_size = s;
          else if (s != stack_size)
            die_for (address, "functions with different stack sizes spanning");
        }
      std::cout << stack_size << '\n';
    }
  if (std::cin.eof ())
    return 0;
  std::cerr << "invalid input\n";
  return -1;
}