-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzero_page.cpp
More file actions
180 lines (167 loc) · 5.68 KB
/
zero_page.cpp
File metadata and controls
180 lines (167 loc) · 5.68 KB
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
// Copyright 2023 The Division Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author dietoad@gmail.com && firedtoad@gmail.com
#include "utils/rss.h"
#include <fstream>
#include <sstream>
#include <sys/mman.h>
#include <unistd.h>
const int PAGE_SIZE = sysconf(_SC_PAGESIZE);
inline std::string &trim_(std::string &str)
{
const char *spaces = " \n\r\t";
str.erase(str.find_last_not_of(spaces) + 1);
str.erase(0, str.find_first_not_of(spaces));
return str;
}
static inline void StringSplit(const std::string &str, const std::string &delimiters, std::vector<std::string> &elems)
{
std::string::size_type pos, prev = 0;
while ((pos = str.find_first_of(delimiters, prev)) != std::string::npos)
{
if (pos > prev)
{
elems.emplace_back(str, prev, pos - prev);
}
prev = pos + 1;
}
if (prev < str.size())
elems.emplace_back(str, prev, str.size() - prev);
}
struct stat_m
{
size_t vmSize;
size_t vmRss;
size_t vmShare;
size_t textSize;
size_t libSize;
size_t dataSize;
size_t dirtyPages;
};
std::string GetStatMRaw()
{
std::ostringstream oss;
std::fstream fs("/proc/self/statm", std::ios_base::in | std::ios_base::binary);
oss << fs.rdbuf();
return oss.str();
}
stat_m GetStatM()
{
std::ostringstream oss;
std::fstream fs("/proc/self/statm", std::ios_base::in | std::ios_base::binary);
oss << fs.rdbuf();
std::vector<std::string> vec;
StringSplit(oss.str(), " ", vec);
stat_m ret;
if (vec.size() >= 7)
{
size_t *p = &ret.vmSize;
for (const auto &it : vec)
{
if (p <= &ret.dirtyPages)
{
*p++ = std::stoul(it) * PAGE_SIZE;
}
}
}
return ret;
}
void PrintStatM(const stat_m &m)
{
std::cout << "vmSize : " << m.vmSize << " B / " << m.vmSize / 1024 << " KB /" << m.vmSize / 1024.0 / 1024.0 << " MB" << '\n';
std::cout << "vmRss : " << m.vmRss << " B / " << m.vmRss / 1024 << " KB /" << m.vmRss / 1024.0 / 1024.0 << " MB" << '\n';
std::cout << "vmShare : " << m.vmShare << " B / " << m.vmShare / 1024 << " KB /" << m.vmShare / 1024.0 / 1024.0 << " MB" << '\n';
std::cout << "textSize : " << m.textSize << " B / " << m.textSize / 1024 << " KB /" << m.textSize / 1024.0 / 1024.0 << " MB" << '\n';
std::cout << "libSize : " << m.libSize << " B / " << m.libSize / 1024 << " KB /" << m.libSize / 1024.0 / 1024.0 << " MB" << '\n';
std::cout << "dataSize : " << m.dataSize << " B / " << m.dataSize / 1024 << " KB /" << m.dataSize / 1024.0 / 1024.0 << " MB" << '\n';
std::cout << "dirtySize : " << m.dirtyPages << " B / " << m.dirtyPages / 1024 << " KB /" << m.dirtyPages / 1024.0 / 1024.0 << " MB" << '\n';
std::cout << '\n';
std::cout.flush();
}
void PrintMMap()
{
std::fstream fs("/proc/self/maps", std::ios::binary | std::ios::in);
std::stringstream ss;
ss << fs.rdbuf();
std::string line;
std::cout << ss.str() << '\n';
}
#include <csignal>
#include <setjmp.h>
#include <ucontext.h>
thread_local jmp_buf buf{};
int first = 1;
void sigsegv(int signo, siginfo_t *info, void *context)
{
auto uctx = (ucontext_t *)context;
// siglongjmp(buf, 1);
// ;
// std::cerr << signo << '\n';
// std::cerr << info->si_code << '\n';
// std::cerr << info->si_errno << '\n';
if (first)
{
auto addr = (void *)((uintptr_t)info->si_addr & (-4096));
std::cerr << info->si_addr << ' ' << addr << '\n';
mmap(addr, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
first = 0;
// uctx->uc_mcontext.gregs[REG_RIP]+=4;
return;
}
// uctx->uc_mcontext.gregs[REG_RIP]+=4;
// exit(0);
}
char *pChar=0;
// echo 0 > /proc/sys/vm/mmap_min_addr
int main(int argc, char **argv)
{
// struct sigaction act
// {
// };
// act.sa_flags = SA_SIGINFO | SA_NODEFER;
// act.sa_sigaction = sigsegv;
// sigemptyset(&act.sa_mask);
// // sigaddset(&act.sa_mask, SIGSEGV);
// // sigaddset(&act.sa_mask, SIGTRAP);
// sigaction(SIGSEGV, &act, nullptr);
// sigaction(SIGFPE, &act, nullptr);
// sigaction(SIGTRAP, &act, nullptr);
//
// PrintMMap();
// // void *p = mmap(0, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
// // (void *)p;
//
//// std::cout << strerror(errno) << '\n' << '\n';
// char *pChar = (char *)0;
// *pChar = 'a';
// std::cout << *pChar << '\n';
// PrintMMap();
//
struct sigaction act
{
};
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = sigsegv;
// act.sa_sigaction = sigsegvMMap;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGSEGV);
sigaction(SIGSEGV, &act, nullptr);
void *p = mmap(0, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE|PROT_EXEC, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
// std::cout << p << ' ' << strerror(errno) << '\n' << '\n';
// sleep(10000000);
*pChar = 'a';
std::cout << *pChar << '\n';
PrintMMap();
return 0;
}