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
|
#ifdef _MSC_VER
#include <windows.h>
#else
#include <sys/mman.h>
#include <fcntl.h>
#include <cstdlib>
#include <sys/resource.h>
#endif
#include "MappedFile.h"
#include "binaryninjaapi.h"
#ifndef _MSC_VER
uint64_t AdjustFileDescriptorLimit()
{
// The soft file descriptor limit on Linux and Mac is a lot lower than
// on Windows (1024 for Linux, 256 for Mac). Recent iOS shared caches
// have 60+ files which may not leave much headroom if a user opens
// more than one at a time. Attempt to increase the file descriptor
// limit to 1024, and limit ourselves to caching half of them as a
// memory vs performance trade-off (closing and re-opening a file
// requires parsing and applying the slide information again).
uint64_t maxFPLimit = 1024;
// check for BN_SHAREDCACHE_FP_MAX
// if it exists, set maxFPLimit to that value
if (auto env = getenv("BN_SHAREDCACHE_FP_MAX"); env)
{
// FIXME behav on 0 here is unintuitive, '0123' will interpret as octal and be 83 according to manpage. meh.
maxFPLimit = strtoull(env, nullptr, 0);
if (maxFPLimit < 10)
{
BinaryNinja::LogWarn(
"BN_SHAREDCACHE_FP_MAX set to below 10. A value of at least 10 is recommended for performant analysis "
"on SharedCache Binaries.");
}
if (maxFPLimit == 0)
{
BinaryNinja::LogError("BN_SHAREDCACHE_FP_MAX set to 0. Adjusting to 1");
maxFPLimit = 1;
}
}
rlimit rlim {};
getrlimit(RLIMIT_NOFILE, &rlim);
uint64_t previousLimit = rlim.rlim_cur;
uint64_t targetLimit = std::min(maxFPLimit, rlim.rlim_max);
if (rlim.rlim_cur < targetLimit)
{
rlim.rlim_cur = targetLimit;
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
{
perror("setrlimit(RLIMIT_NOFILE)");
rlim.rlim_cur = previousLimit;
}
}
maxFPLimit = rlim.rlim_cur / 2;
return maxFPLimit;
}
MappedFile::~MappedFile()
{
Unmap();
if (fd)
fclose(fd);
}
std::optional<MappedFile> MappedFile::OpenFile(const std::string& path)
{
MappedFile file;
file._mmap = nullptr;
file.fd = fopen(path.c_str(), "r");
if (file.fd == nullptr)
return std::nullopt;
fseek(file.fd, 0L, SEEK_END);
file.len = ftell(file.fd);
fseek(file.fd, 0L, SEEK_SET);
return file;
}
MapStatus MappedFile::Map()
{
if (_mmap)
return MapStatus::Success;
void* result = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(fd), 0u);
if (result == MAP_FAILED)
{
BinaryNinja::LogErrorF("mmap failed: {}", strerror(errno)); // Use errno to log the reason
return MapStatus::Error;
}
_mmap = static_cast<uint8_t*>(result);
return MapStatus::Success;
}
MapStatus MappedFile::Unmap()
{
if (_mmap)
{
munmap(_mmap, len);
_mmap = nullptr;
}
return MapStatus::Success;
}
#else
uint64_t AdjustFileDescriptorLimit()
{
return 0x1000000;
}
MappedFile::~MappedFile()
{
Unmap();
if (hFile)
CloseHandle(hFile);
}
std::optional<MappedFile> MappedFile::OpenFile(const std::string& path)
{
MappedFile file;
file._mmap = nullptr;
file.hFile = CreateFile(path.c_str(), // file name
GENERIC_READ, // desired access (read-only)
FILE_SHARE_READ, // share mode
NULL, // security attributes
OPEN_EXISTING, // creation disposition
FILE_ATTRIBUTE_NORMAL, // flags and attributes
NULL); // template file
if (file.hFile == INVALID_HANDLE_VALUE)
return std::nullopt;
LARGE_INTEGER fileSize;
if (!GetFileSizeEx(file.hFile, &fileSize))
{
CloseHandle(file.hFile);
return std::nullopt;
}
file.len = static_cast<size_t>(fileSize.QuadPart);
return file;
}
MapStatus MappedFile::Map()
{
if (_mmap)
return MapStatus::Success;
HANDLE hMapping = CreateFileMapping(hFile, // file handle
NULL, // security attributes
PAGE_WRITECOPY, // protection
0, // maximum size (high-order DWORD)
0, // maximum size (low-order DWORD)
NULL); // name of the mapping object
if (hMapping == NULL)
{
CloseHandle(hFile);
return MapStatus::Error;
}
_mmap = static_cast<uint8_t*>(MapViewOfFile(hMapping, // handle to the file mapping object
FILE_MAP_COPY, // desired access
0, // file offset (high-order DWORD)
0, // file offset (low-order DWORD)
0)); // number of bytes to map (0 = entire file)
if (_mmap == nullptr)
{
CloseHandle(hMapping);
CloseHandle(hFile);
return MapStatus::Error;
}
CloseHandle(hMapping);
CloseHandle(hFile);
return MapStatus::Success;
}
MapStatus MappedFile::Unmap()
{
if (_mmap)
{
UnmapViewOfFile(_mmap);
}
return MapStatus::Success;
}
#endif
|