Passed
Pull Request — master (#96)
by Shinji
01:40
created

PtraceX64::ptrace()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 16
nop 4
dl 0
loc 28
rs 8.8333
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler 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 PhpProfiler\Lib\Libc\Sys\Ptrace;
15
16
use FFI\CData;
17
use FFI\CInteger;
18
use PhpProfiler\Lib\Libc\Addressable;
19
20
class PtraceX64 implements Ptrace
21
{
22
    /** @var \FFI\Libc\ptrace_ffi */
23
    private \FFI $ffi;
24
25
    public function __construct()
26
    {
27
        /** @var \FFI\Libc\ptrace_ffi ffi */
28
        $this->ffi = \FFI::cdef('
29
           struct user_regs_struct {
30
               unsigned long r15;
31
               unsigned long r14;
32
               unsigned long r13;
33
               unsigned long r12;
34
               unsigned long bp;
35
               unsigned long bx;
36
               unsigned long r11;
37
               unsigned long r10;
38
               unsigned long r9;
39
               unsigned long r8;
40
               unsigned long ax;
41
               unsigned long cx;
42
               unsigned long dx;
43
               unsigned long si;
44
               unsigned long di;
45
               unsigned long orig_ax;
46
               unsigned long ip;
47
               unsigned long cs;
48
               unsigned long flags;
49
               unsigned long sp;
50
               unsigned long ss;
51
               unsigned long fs_base;
52
               unsigned long gs_base;
53
               unsigned long ds;
54
               unsigned long es;
55
               unsigned long fs;
56
               unsigned long gs;
57
           };
58
           typedef int pid_t;
59
           enum __ptrace_request
60
           {
61
               PTRACE_TRACEME = 0,
62
               PTRACE_PEEKTEXT = 1,
63
               PTRACE_PEEKDATA = 2,
64
               PTRACE_PEEKUSER = 3,
65
               PTRACE_POKETEXT = 4,
66
               PTRACE_POKEDATA = 5,
67
               PTRACE_POKEUSER = 6,
68
               PTRACE_CONT = 7,
69
               PTRACE_KILL = 8,
70
               PTRACE_SINGLESTEP = 9,
71
               PTRACE_GETREGS = 12,
72
               PTRACE_SETREGS = 13,
73
               PTRACE_GETFPREGS = 14,
74
               PTRACE_SETFPREGS = 15,
75
               PTRACE_ATTACH = 16,
76
               PTRACE_DETACH = 17,
77
               PTRACE_GETFPXREGS = 18,
78
               PTRACE_SETFPXREGS = 19,
79
               PTRACE_SYSCALL = 24,
80
               PTRACE_SETOPTIONS = 0x4200,
81
               PTRACE_GETEVENTMSG = 0x4201,
82
               PTRACE_GETSIGINFO = 0x4202,
83
               PTRACE_SETSIGINFO = 0x4203
84
           };
85
           long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data);
86
           int errno;
87
       ', 'libc.so.6');
88
    }
89
90
    public function ptrace(
91
        PtraceRequest $request,
92
        int $pid,
93
        Addressable|CData|null|int $addr,
0 ignored issues
show
Bug introduced by
The type PhpProfiler\Lib\Libc\Sys\Ptrace\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
        Addressable|CData|null|int $data,
95
    ): int {
96
        if (is_null($addr) or is_int($addr)) {
97
            /** @var CInteger */
98
            $addr_holder = \FFI::new('long');
99
            $addr_holder->cdata = (int)$addr;
100
            $addr = \FFI::cast('void *', $addr_holder);
101
        }
102
        if (is_null($data) or is_int($data)) {
103
            /** @var CInteger */
104
            $data_holder = \FFI::new('long');
105
            $data_holder->cdata = (int)$data;
106
            $data = \FFI::cast('void *', $data_holder);
107
        }
108
109
        $addr_pointer = $addr instanceof Addressable ? $addr->toVoidPointer() : $addr;
110
        $data_pointer = $data instanceof Addressable ? $data->toVoidPointer() : $data;
111
112
        /** @var int */
113
        return $this->ffi->ptrace(
0 ignored issues
show
Bug introduced by
The method ptrace() does not exist on FFI. It seems like you code against a sub-type of FFI such as FFI\Libc\ptrace_ffi. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        return $this->ffi->/** @scrutinizer ignore-call */ ptrace(
Loading history...
114
            $request->getValue(),
115
            $pid,
116
            $addr_pointer,
117
            $data_pointer,
118
        );
119
    }
120
}
121