|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the reliforp/reli-prof package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) sji <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Reli\Lib\Process\MemoryReader; |
|
15
|
|
|
|
|
16
|
|
|
use FFI; |
|
17
|
|
|
use FFI\CData; |
|
18
|
|
|
use Reli\Lib\FFI\CannotAllocateBufferException; |
|
19
|
|
|
|
|
20
|
|
|
final class MemoryReader implements MemoryReaderInterface |
|
21
|
|
|
{ |
|
22
|
|
|
private FFI $ffi; |
|
23
|
|
|
private CData $local_iov; |
|
24
|
|
|
private CData $remote_iov; |
|
25
|
|
|
private CData $remote_base; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->ffi = FFI::cdef(' |
|
30
|
|
|
typedef int pid_t; |
|
31
|
|
|
struct iovec { |
|
32
|
|
|
void *iov_base; /* Starting address */ |
|
33
|
|
|
size_t iov_len; /* Number of bytes to transfer */ |
|
34
|
|
|
}; |
|
35
|
|
|
int errno; |
|
36
|
|
|
ssize_t process_vm_readv(pid_t pid, |
|
37
|
|
|
const struct iovec *local_iov, |
|
38
|
|
|
unsigned long liovcnt, |
|
39
|
|
|
const struct iovec *remote_iov, |
|
40
|
|
|
unsigned long riovcnt, |
|
41
|
|
|
unsigned long flags); |
|
42
|
|
|
', 'libc.so.6'); |
|
43
|
|
|
$this->local_iov = $this->ffi->new('struct iovec') |
|
44
|
|
|
?? throw new CannotAllocateBufferException('cannot allocate buffer'); |
|
45
|
|
|
$this->remote_iov = $this->ffi->new('struct iovec') |
|
46
|
|
|
?? throw new CannotAllocateBufferException('cannot allocate buffer'); |
|
47
|
|
|
$this->remote_base = $this->ffi->new('long') |
|
48
|
|
|
?? throw new CannotAllocateBufferException('cannot allocate buffer'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return \FFI\CArray<int> |
|
53
|
|
|
* @throws MemoryReaderException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function read(int $pid, int $remote_address, int $size): CData |
|
56
|
|
|
{ |
|
57
|
|
|
$buffer = $this->ffi->new("unsigned char[{$size}]") |
|
58
|
|
|
?? throw new CannotAllocateBufferException('cannot allocate buffer'); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var FFI\Libc\iovec $this->local_iov |
|
62
|
|
|
* @psalm-suppress PropertyTypeCoercion |
|
63
|
|
|
*/ |
|
64
|
|
|
$this->local_iov->iov_base = FFI::addr($buffer); |
|
65
|
|
|
$this->local_iov->iov_len = $size; |
|
66
|
|
|
|
|
67
|
|
|
/** @var FFI\Libc\iovec $this->remote_iov */ |
|
68
|
|
|
$this->remote_iov->iov_len = $size; |
|
69
|
|
|
/** @var FFI\CInteger $this->remote_base */ |
|
70
|
|
|
$this->remote_base->cdata = $remote_address; |
|
71
|
|
|
/** @psalm-suppress PropertyTypeCoercion */ |
|
72
|
|
|
$this->remote_iov->iov_base = FFI::cast('void *', $this->remote_base); |
|
73
|
|
|
|
|
74
|
|
|
/** @var FFI\Libc\process_vm_readv_ffi $this->ffi */ |
|
75
|
|
|
$read = $this->ffi->process_vm_readv( |
|
|
|
|
|
|
76
|
|
|
$pid, |
|
77
|
|
|
FFI::addr($this->local_iov), |
|
78
|
|
|
1, |
|
79
|
|
|
FFI::addr($this->remote_iov), |
|
80
|
|
|
1, |
|
81
|
|
|
0 |
|
82
|
|
|
); |
|
83
|
|
|
if ($read === -1) { |
|
84
|
|
|
/** @var int $errno */ |
|
85
|
|
|
$errno = $this->ffi->errno; |
|
86
|
|
|
$log_address = dechex($remote_address); |
|
87
|
|
|
throw new MemoryReaderException( |
|
88
|
|
|
"failed to read memory. target_pid={$pid}, remote_address=0x{$log_address}, errno={$errno}", |
|
89
|
|
|
$errno |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** @var \FFI\CArray<int> */ |
|
94
|
|
|
return $buffer; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|