HPCToolkit
x86-call.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 #include <stdbool.h>
48 
50 #include "x86-decoder.h"
51 #include "x86-interval-arg.h"
52 
54 
55 /******************************************************************************
56  * local operations
57  *****************************************************************************/
58 
59 //
60 // detect call instruction of the form:
61 // call NEXT
62 // NEXT: ....
63 //
64 // NOTE: ASSUME the incoming instruction is a 'call' instruction
65 //
66 static bool
67 call_is_push_next_addr_idiom(xed_decoded_inst_t* xptr, interval_arg_t* iarg)
68 {
69  void* ins = iarg->ins;
70  void* call_addr = x86_get_branch_target(ins, xptr);
71  void* next_addr = nextInsn(iarg, xptr);
72 
73  return (call_addr == next_addr);
74 }
75 
76 /******************************************************************************
77  * interface operations
78  *****************************************************************************/
79 
81 process_call(xed_decoded_inst_t *xptr, const xed_inst_t *xi, interval_arg_t *iarg)
82 {
83  unwind_interval *next = iarg->current;
84  highwatermark_t *hw_tmp = &(iarg->highwatermark);
85 
86  if (hw_tmp->state == HW_UNINITIALIZED) {
87  hw_tmp->uwi = iarg->current;
88  hw_tmp->state = HW_INITIALIZED;
89  }
90 
91  //
92  // Treat call instruction that looks like:
93  // call NEXT
94  // NEXT: ....
95  //
96  // As if it were a push
97  //
98  if (call_is_push_next_addr_idiom(xptr, iarg)) {
99  x86recipe_t *xr = UWI_RECIPE(iarg->current);
100  x86registers_t reg = xr->reg;
101  reg.sp_ra_pos += sizeof(void*);
102  reg.sp_bp_pos += sizeof(void*);
103  next = new_ui(nextInsn(iarg, xptr), xr->ra_status, &reg);
104  }
105 #ifdef USE_CALL_LOOKAHEAD
106  next = call_lookahead(xptr, iarg->current, iarg->ins);
107 #endif
108  return next;
109 }
110 
111 
112 
113 /******************************************************************************
114  * private operations
115  *****************************************************************************/
116 
117 #undef USE_CALL_LOOKAHEAD
118 #ifdef USE_CALL_LOOKAHEAD
120 call_lookahead(xed_decoded_inst_t *call_xedd, unwind_interval *current, char *ins)
121 {
122  // Assumes: 'ins' is pointing at the instruction from which
123  // lookahead is to occur (i.e, the instruction prior to the first
124  // lookahead).
125 
126  unwind_interval *next;
127  xed_error_enum_t xed_err;
128  int length = call_xedd->get_length();
129  xed_decoded_inst_t xeddobj;
130  xed_decoded_inst_t* xedd = &xeddobj;
131  char *jmp_ins_addr = ins + length;
132  char *jmp_target = NULL;
133  char *jmp_succ_addr = NULL;
134 
135  if (current->ra_status == RA_BP_FRAME) {
136  return current;
137  }
138 
139  // -------------------------------------------------------
140  // requirement 1: unconditional jump with known target within routine
141  // -------------------------------------------------------
142  xed_decoded_inst_zero_set_mode(xptr, xed_machine_state_ptr);
143  xed_err = xed_decode(xptr, reinterpret_cast<const uint8_t*>(jmp_ins_addr), 15);
144  if (xed_err != XED_ERROR_NONE) {
145  return current;
146  }
147 
148  if (iclass_eq(xptr, XED_ICLASS_JMP) ||
149  iclass_eq(xptr, XED_ICLASS_JMP_FAR)) {
150  if (xed_decoded_inst_number_of_memory_operands(xptr) == 0) {
151  const xed_immdis_t& disp = xptr->get_disp();
152  if (disp.is_present()) {
153  long long offset = disp.get_signed64();
154  jmp_succ_addr = jmp_ins_addr + xptr->get_length();
155  jmp_target = jmp_succ_addr + offset;
156  }
157  }
158  }
159  if (jmp_target == NULL) {
160  // jump of proper type not recognized
161  return current;
162  }
163  // FIXME: possibly test to ensure jmp_target is within routine
164 
165  // -------------------------------------------------------
166  // requirement 2: jump target affects stack
167  // -------------------------------------------------------
168  xed_decoded_inst_zero_set_mode(xptr, xed_machine_state_ptr);
169  xed_err = xed_decode(xptr, reinterpret_cast<const uint8_t*>(jmp_target), 15);
170  if (xed_err != XED_ERROR_NONE) {
171  return current;
172  }
173 
174  if (iclass_eq(xptr, XED_ICLASS_SUB) || iclass_eq(xptr, XED_ICLASS_ADD)) {
175  const xed_operand_t* op0 = xed_inst_operand(xi,0);
176  if ((xed_operand_name(op0) == XED_OPERAND_REG)
177  && isReg_x86_SP(xed_operand_reg(op0))) {
178  const xed_immdis_t& immed = xptr->get_immed();
179  if (immed.is_present()) {
180  int sign = (iclass_eq(xptr, XED_ICLASS_ADD)) ? -1 : 1;
181  long offset = sign * immed.get_signed64();
182  PMSG(INTV,"newinterval from ADD/SUB immediate");
183  next = newinterval(jmp_succ_addr,
184  current->ra_status,
185  current->sp_ra_pos + offset,
186  current->bp_ra_pos,
187  current->bp_status,
188  current->sp_bp_pos + offset,
189  current->bp_bp_pos,
190  current);
191  return next;
192  }
193  }
194  }
195  return current;
196 }
197 #endif
198 
#define HW_INITIALIZED
Definition: x86-canonical.c:54
static bool call_is_push_next_addr_idiom(xed_decoded_inst_t *xptr, interval_arg_t *iarg)
Definition: x86-call.c:67
#define PMSG(f,...)
Definition: messages.h:92
bitree_uwi_t * current
#define UWI_RECIPE(btuwi)
x86registers_t reg
unwind_interval * process_call(xed_decoded_inst_t *xptr, const xed_inst_t *xi, interval_arg_t *iarg)
Definition: x86-call.c:81
#define iclass_eq(xptr, class)
Definition: x86-decoder.h:71
static char * nextInsn(uint32_t *insn)
void * x86_get_branch_target(void *ins, xed_decoded_inst_t *xptr)
static xed_decoded_inst_t xedd
Definition: x86ISAXed.cpp:92
highwatermark_t highwatermark
unwind_interval * new_ui(char *startaddr, sp_ty_t sp_ty, ra_ty_t ra_ty, int sp_arg, int ra_arg)
#define HW_UNINITIALIZED
Definition: x86-canonical.c:58
#define NULL
Definition: ElfHelper.cpp:85
bitree_uwi_t unwind_interval