File: vgdb-invoker-solaris.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (528 lines) | stat: -rw-r--r-- 16,614 bytes parent folder | download | duplicates (3)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*--------------------------------------------------------------------*/
/*--- Implementation of vgdb invoker subsystem on Solaris             */
/*                      via /proc filesystem and control messages. ---*/
/*--------------------------------------------------------------------*/

/*
   This file is part of Valgrind, a dynamic binary instrumentation
   framework.

   Copyright (C) 2014-2017 Ivo Raisr <ivosh@ivosh.net>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.

   The GNU General Public License is contained in the file COPYING.
*/

/* This module implements vgdb-invoker subsystem as per vgdb.h
   on Solaris. It differs significantly from the other ptrace-based
   implementation found in vgdb-invoker-ptrace.c. However the goal
   is the same - to work on the following scenario:

   - A valgrind process (referred to also as an inferior process)
     is remotely debugged with gdb.
   - All threads of the inferior process are stuck in blocking
     syscalls.
   - Therefore no thread can process packets received from gdb.

   When module vgdb.c detects this situation then it calls
   function invoker_invoke_gdbserver() within the context of
   invoke_gdbserver_in_valgrind_thread thread. The steps of
   interaction between vgdb and m_gdbserver module are as follows:

   1. Function invoker_invoke_gdbserver() attaches to the inferior
      process and stops all threads.
   2. It gets registers of the first thread and modifies them
      and the stack so that a call to "invoke_gdbserver" function
      is arranged along with a function parameter.
   3. Then it creates an agent thread within the inferior process
      with these modified registers and waits until the agent thread
      exits.
   4. Meanwhile in the inferior process function
      VG_(invoke_gdbserver)() is invoked within the context of the
      agent thread; all other threads are still stopped.
   5. The agent thread processes packets from gdb relayed by vgdb.
   6. Eventually processing is finished and the agent thread exits
      in function give_control_back_to_vgdb().
   7. vgdb then detaches from the inferior process and thus resumes
      all the stopped threads.
 */

#include "vgdb.h"

#include <assert.h>
#include <errno.h>
#include <string.h>

typedef Addr CORE_ADDR;

typedef struct {
   long cmd;
   union {
       long flags;
       prgregset_t regs;
   } arg;
} ctl_t;

/* Process control file /proc/<pid>/ctl.
   Once this file is closed, PR_RLC flag takes effect and
   inferior process resumes automatically. */
static int ctl_fd = -1;

/* Copy LEN bytes of data from vgdb memory at MYADDR
   to valgrind memory at MEMADDR.
   On failure (cannot write the valgrind memory)
   returns the value of errno. */
static int write_memory(pid_t pid, CORE_ADDR memaddr,
                        const void *myaddr, size_t len)
{
   char procname[PATH_MAX];
   snprintf(procname, sizeof(procname), "/proc/%d/as", pid);

   /* Open the process address-space file. */
   int as_fd = open(procname, O_WRONLY, 0);
   if (as_fd < 0) {
      int error = errno;
      ERROR(error, "Failed to open %s.\n", procname);
      return error;
   }

   if (debuglevel >= 1) {
      DEBUG(1, "Writing bytes '");
      size_t i;
      for (i = 0; i < len; i++)
         PDEBUG(1, "%02x", ((const unsigned char *) myaddr)[i]);
      PDEBUG(1, "' to address %#lx.\n", memaddr);
   }

   ssize_t written = pwrite(as_fd, myaddr, len, memaddr);
   if ((written < 0) || (written != len)) {
      int error = errno;
      ERROR(error, "Failed to write to file %s, memory block of %zu"
            " bytes at %#lx to %#lx.\n",
            procname, len, (Addr) myaddr, memaddr);
      close(as_fd);
      return error;
   }

   DEBUG(1, "Written ok.\n");
   close(as_fd);
   return 0;
}

/* Attaches to a process identified by pid and stops all threads. */
static Bool attach(pid_t pid)
{
   char procname[PATH_MAX];
   snprintf(procname, sizeof(procname), "/proc/%d/ctl", pid);

   DEBUG(1, "Attaching to pid %d.\n", pid);

   /* Open the process control file. */
   ctl_fd = open(procname, O_WRONLY, 0);
   if (ctl_fd < 0) {
      ERROR(errno, "Failed to open %s.\n", procname);
      return False;
   }

   DEBUG(1, "Setting run-on-last-close-flag (PR_RLC) to pid %d.\n", pid);

   /* Set run-on-last-close flag. */
   ctl_t ctl;
   ctl.cmd = PCSET;
   ctl.arg.flags = PR_RLC;
   size_t bytes = sizeof(ctl.cmd) + sizeof(ctl.arg.flags);
   ssize_t written = write(ctl_fd, (void *) &ctl, bytes);
   if ((written < 0) || (written != bytes)) {
      ERROR(errno, "Failed to write to ctl_fd: PCSET + PR_RLC.\n");
      return False;
   }

   DEBUG(1, "Stopping process %d.\n", pid);

   /* Stop the whole process - all threads. */
   ctl.cmd = PCSTOP;
   bytes = sizeof(ctl.cmd);
   written = write(ctl_fd, (void *) &ctl, bytes);
   if ((written < 0) || (written != bytes)) {
      ERROR(errno, "Failed to write to ctl_fd: PCSTOP.\n");
      return False;
   }

   DEBUG(1, "Process %d stopped.\n", pid);

   /* Now confirm it is actually the case. */
   snprintf(procname, sizeof(procname), "/proc/%d/status", pid);
   int status_fd = open(procname, O_RDONLY, 0);
   if (status_fd < 0) {
      ERROR(errno, "Failed to open %s.\n", procname);
      return False;
   }

   pstatus_t pstatus;
   ssize_t nread = read(status_fd, &pstatus, sizeof(pstatus));
   if ((nread < 0) || (nread != sizeof(pstatus))) {
      ERROR(errno, "Failed to read from %s.\n", procname);
      close(status_fd);
      return False;
   }

   if (pstatus.pr_flags & PR_RLC) {
      DEBUG(2, "Process %d has run-on-last-close flag set. Good.\n", pid);
   } else {
      ERROR(0, "Process %d does not have run-on-last-close flag set!\n", pid);
      close(status_fd);
      return False;
   }

   if (pstatus.pr_lwp.pr_flags & PR_STOPPED) {
      DEBUG(3, "Process %d seems to be stopped. Good.\n", pid);
   } else {
      ERROR(0, "Process %d is not stopped!\n", pid);
      close(status_fd);
      return False;
   }

   close(status_fd);
   return True;
}

static void detach(pid_t pid)
{
   if (ctl_fd != -1) {
      close(ctl_fd);
      ctl_fd = -1;
   }

   DEBUG(1, "Detached from pid %d.\n", pid);
}

/* Gets the registers of the first thread. */
static Bool get_regs(pid_t pid, prgregset_t *regs)
{
   char procname[PATH_MAX];
   snprintf(procname, sizeof(procname), "/proc/%d/lwp/1/lwpstatus", pid);

   DEBUG(1, "Getting registers from the first thread of process %d.\n", pid);

   /* Open the first thread's status file. */
   int status_fd = open(procname, O_RDONLY, 0);
   if (status_fd < 0) {
      ERROR(errno, "Failed to open file %s.\n", procname);
      return False;
   }

   lwpstatus_t status;
   ssize_t bytes = read(status_fd, &status, sizeof(status));
   if ((bytes < 0) || (bytes != sizeof(status))) {
      ERROR(errno, "Failed to read from %s.\n", procname);
      close(status_fd);
      return False;
   }

   DEBUG(3, "Registers of thread %d from process %d: ", status.pr_lwpid, pid);
   unsigned int i;
   for (i = 0; i < _NGREG; i++) {
      PDEBUG(3, "%u: %#lx, ", i, (unsigned long) status.pr_reg[i]);
   }
   PDEBUG(3, "\n");

   memcpy(regs, &status.pr_reg, sizeof(prgregset_t));
   close(status_fd);
   return True;
}

/* Modifies the register set so that a new stack frame is created
   for "invoke_gdbserver" function with an extra argument.
   The argument is written to the stack of the first thread.
 */
static Bool setup_stack_frame(pid_t pid, prgregset_t *regs)
{
   DEBUG(1, "Setting up new stack frame of process %d.\n", pid);

   /* A specific int value is passed to invoke_gdbserver(), to check
      everything goes according to the plan. */
   const int check = 0x8BADF00D; // ate bad food.

   /* A bad return address will be pushed on the stack.
      Function invoke_gdbserver() cannot return. If it ever returns,
      a NULL address pushed on the stack should ensure this is
      detected. */
   const Addr bad_return = 0;

#if defined(VGA_x86)
   Addr sp = (*regs)[UESP];
#elif defined(VGA_amd64)
   Addr sp = (*regs)[REG_RSP];
#else
   I_die_here : (sp) architecture missing in vgdb-invoker-solaris.c
#endif

   if (shared32 != NULL) {
      /* vgdb speaking with a 32bit executable. */
#if   defined(VGA_x86) || defined(VGA_amd64)
      const size_t regsize = 4;

      /* Push check argument on the stack - according to C/ia32 ABI. */
      sp = sp - regsize;
      DEBUG(1, "Pushing check argument to process %d memory.\n", pid);
      assert(regsize == sizeof(check));
      int error = write_memory(pid, sp, &check, regsize);
      if (error != 0) {
         ERROR(error, "Failed to push check argument to process %d memory.\n",
                      pid);
         detach(pid);
         return False;
      }

      sp = sp - regsize;
      DEBUG(1, "Pushing bad_return return address to process %d memory.\n",
               pid);
      /* Note that even for a 64 bits vgdb, only 4 bytes
         of NULL bad_return are written. */
      error = write_memory(pid, sp, &bad_return, regsize);
      if (error != 0) {
         ERROR(error, "Failed to push bad_return return address to process %d "
                      "memory.\n", pid);
         detach(pid);
         return False;
      }

#if   defined(VGA_x86)
      /* Set EBP, ESP, EIP to invoke gdbserver.
         vgdb is 32bits, speaking with a 32bits process. */
      (*regs)[EBP] = sp; // bp set to sp
      (*regs)[UESP] = sp;
      (*regs)[EIP] = shared32->invoke_gdbserver;
#elif defined(VGA_amd64)
      /* Set RBP, RSP, RIP to invoke gdbserver.
         vgdb is 64bits, speaking with a 32bits process. */
      (*regs)[REG_RBP] = sp; // bp set to sp
      (*regs)[REG_RSP] = sp;
      (*regs)[REG_RIP] = shared32->invoke_gdbserver;
#else
      I_die_here : not x86 or amd64 in x86/amd64 section/
#endif

#else
      I_die_here : architecture missing in vgdb-invoker-solaris.c
#endif

   } else if (shared64 != NULL) {
#if defined(VGA_x86)
      assert(0); /* 64bits process with a 32bits vgdb - no way */
#elif defined(VGA_amd64)
      /* 64bits vgdb speaking with a 64 bit process. */
      const int regsize = 8;

      /* Give check argument in rdi - according to C/amd64 ABI. */
      (*regs)[REG_RDI] = check;

      /* Push return address on stack: return to breakaddr. */
      sp = sp - regsize;
      DEBUG(1, "Pushing bad_return return address to process %d memory.\n",
               pid);
      int error = write_memory(pid, sp, &bad_return,
                               sizeof(bad_return));
      if (error != 0) {
         ERROR(error, "Failed to push bad_return return address to process %d "
                      "memory.\n", pid);
         detach(pid);
         return False;
      }

      /* set RBP, RSP, RIP to invoke gdbserver */
      (*regs)[REG_RBP] = sp; // bp set to sp
      (*regs)[REG_RSP] = sp;
      (*regs)[REG_RIP] = shared64->invoke_gdbserver;
#else
      I_die_here: architecture missing in vgdb-invoker-solaris.c
#endif
   } else {
      assert(0);
   }

   DEBUG(1, "New stack frame set up for process %d.\n", pid);
   return True;
}

/* Creates and starts an agent thread within the inferior process.
   The agent thread is created stopped and with its held signal set
   (the signal mask) having all signals except SIGKILL and SIGSTOP
   blocked. All these signals need to remain blocked while the agent
   thread is running because valgrind syscall/signal machinery expects
   that (remember: all valgrind threads are blocked in VgTs_WaitSys
   - that is the reason why we are invoking the agent, after all).
   It is necessary to resume the agent thread afterwards.
 */
static Bool invoke_agent(pid_t pid, prgregset_t *regs, id_t *agent_lwpid)
{
   assert(ctl_fd != -1);

   DEBUG(1, "Creating an agent thread within process %d.\n", pid);

   /* Create the agent thread. */
   ctl_t ctl;
   ctl.cmd = PCAGENT;
   memcpy(&ctl.arg.regs, regs, sizeof(prgregset_t));
   size_t bytes = sizeof(ctl.cmd) + sizeof(ctl.arg.regs);
   ssize_t written = write(ctl_fd, (void *) &ctl, bytes);
   if ((written < 0) || (written != bytes)) {
      ERROR(errno, "Failed to write to ctl_fd: PCAGENT.\n");
      return False;
   }

   DEBUG(1, "Obtaining agent thread lwpid for process %d.\n", pid);

   char procname[PATH_MAX];
   snprintf(procname, sizeof(procname),
            "/proc/%d/lwp/agent/lwpstatus", pid);

   int status_fd = open(procname, O_RDONLY, 0);
   if (status_fd < 0) {
      /* Operation failed but there is no way to get rid of the agent
         thread from outside. We are doomed... */
      ERROR(errno, "Failed to open file %s.\n", procname);
      return False;
   }

   lwpstatus_t status;
   ssize_t nread = read(status_fd, &status, sizeof(status));
   if ((nread < 0) || (nread != sizeof(status))) {
      ERROR(errno, "Failed to read from %s.\n", procname);
      close(status_fd);
      return False;
   }

   close(status_fd);
   *agent_lwpid = status.pr_lwpid;

   snprintf(procname, sizeof(procname),
            "/proc/%d/lwp/agent/lwpctl", pid);

   int agent_ctl_fd = open(procname, O_WRONLY, 0);
   if (agent_ctl_fd < 0) {
      /* Resuming failed but there is no way to get rid of the agent
         thread from outside. We are doomed... */
      ERROR(errno, "Failed to open file %s.\n", procname);
      return False;
   }

   DEBUG(1, "Resuming the agent thread for process %d.\n", pid);

   /* Resume the agent thread. */
   ctl.cmd = PCRUN;
   ctl.arg.flags = 0;
   bytes = sizeof(ctl.cmd) + sizeof(ctl.arg.flags);
   written = write(agent_ctl_fd, (void *) &ctl, bytes);
   if ((written < 0) || (written != bytes)) {
      /* Resuming failed but there is no way to get rid of the agent
         thread from outside. We are doomed... */
      ERROR(errno, "Failed to write to agent_ctl_fd: PCRUN 0.\n");
      close(agent_ctl_fd);
      return False;
   }

   DEBUG(1, "Agent thread lwpid %d now running within process %d.\n",
         *agent_lwpid, pid);
   close(agent_ctl_fd);
   return True;
}

/* Waits until the agent thread running inside the inferior
   process exits. */
static Bool wait_for_agent_exit(pid_t pid, id_t agent_lwpid)
{
   char procname[PATH_MAX];
   snprintf(procname, sizeof(procname), "/proc/%d/lwp/agent/lwpctl", pid);

   int agent_ctl_fd = open(procname, O_WRONLY, 0);
   if (agent_ctl_fd < 0) {
      if (errno == ENOENT) {
         DEBUG(1, "Agent control file %s no longer exists. This means "
               "agent thread %d exited meanwhile.\n",
               procname, agent_lwpid);
         return True;
      }
      ERROR(errno, "Failed to open agent control file %s.\n", procname);
      return False;
   }

   DEBUG(1, "Waiting for agent thread %d to exit.\n", agent_lwpid);

   /* Wait until the agent thread stops. This covers also the case
      when the thread exited. */
   ctl_t ctl;
   ctl.cmd = PCWSTOP;
   size_t bytes = sizeof(ctl.cmd);
   ssize_t written = write(agent_ctl_fd, (void *) &ctl, bytes);
   if ((written < 0) || (written != bytes)) {
      if (errno == ENOENT) {
         DEBUG(1, "Agent thread lwpid %d has now exited in process %d.\n",
                  agent_lwpid, pid);
      } else {
         ERROR(errno, "Failed to write to agent_ctl_fd: PCWSTOP.\n");
         close(agent_ctl_fd);
         return False;
      }
   }

   close(agent_ctl_fd);
   return True;
}

Bool invoker_invoke_gdbserver(pid_t pid)
{
   if (attach(pid) != True) {
      return False;
   }

   prgregset_t regs;
   if (get_regs(pid, &regs) != True) {
      detach(pid);
      return False;
   }

   if (setup_stack_frame(pid, &regs) != True) {
      detach(pid);
      return False;
   }

   id_t agent_lwpid;
   if (invoke_agent(pid, &regs, &agent_lwpid) != True) {
      detach(pid);
      return False;
   }

   if (wait_for_agent_exit(pid, agent_lwpid) != True) {
      detach(pid);
      return False;
   }

   detach(pid);
   return True;
}

void invoker_cleanup_restore_and_detach(void *v_pid)
{
   detach(*(int *) v_pid);
}

void invoker_restrictions_msg(void)
{
}

void invoker_valgrind_dying(void)
{
}