Debian Codegraph @

Source/mmap.cpp

raw
1 /*
2 * mmap.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 */
16
17 #include "mmap.h"
18 #include <cstdio> // for f*
19 #include <cassert> // for assert
20 #include "tchar.h"
21 #include <limits.h>
22 #ifndef _WIN32
23 # include <sys/types.h> // for freebsd
24 # include <sys/mman.h>
25 # include <sys/stat.h>
26 # include <fcntl.h>
27 # include <unistd.h>
28 #endif
29 #include "util.h"
30
31
32 // =====
33 // IMMap
34 // =====
35
36 bool IMMap::write_to_external_file(FILE*file, UINT64 size)
37 {
38 if (~(size&0) == size) size = getsize();
39 if (getmaxoffset() < size)
40 return false;
41
42 UINT64 left = size, written = 0;
43 int offset = 0; // TODO: this will be removed after converting the mapping code to 64bit.
44 while (left > 0 && offset >= 0)
45 {
46 size_t chunksize = (size_t) STD_MIN(left, (UINT64) 1 << 20);
47 void *view = getmore(offset, chunksize);
48 written += fwrite(view, 1, chunksize, file);
49 release(view, chunksize);
50 left -= chunksize;
51 offset += (int)chunksize;
52 }
53 return written == size;
54 }
55
56
57 // ========
58 // MMapFile
59 // ========
60
61 int MMapFile::m_iAllocationGranularity = 0;
62
63 MMapFile::maxfilesizetype MMapFile::getmaxfilesize()
64 {
65 assert((~(maxfilesizetype)0) > 0);
66 return (maxfilesizetype) STD_MIN((UINT64) (~(maxfilesizetype)0), (UINT64) Platform_GetMaxFileSize());
67 }
68
69 MMapFile::MMapFile()
70 {
71 #ifdef _WIN32
72 m_hFile = INVALID_HANDLE_VALUE;
73 m_hFileMap = NULL;
74 #else
75 m_hFile = NULL;
76 m_hFileDesc = -1;
77 #endif
78
79 m_pView = NULL;
80 m_iSize = 0;
81 m_bReadOnly = FALSE;
82 m_bTempHandle = FALSE;
83
84 if (!m_iAllocationGranularity)
85 {
86 #ifdef _WIN32
87 SYSTEM_INFO si;
88 GetSystemInfo(&si);
89 m_iAllocationGranularity = (int) si.dwAllocationGranularity;
90 #else
91 m_iAllocationGranularity = getpagesize();
92 #endif
93 }
94 }
95
96 MMapFile::~MMapFile()
97 {
98 clear();
99 }
100
101 void MMapFile::clear()
102 {
103 release();
104
105 #ifdef _WIN32
106 if (m_hFileMap)
107 CloseHandle(m_hFileMap);
108 if (m_bTempHandle && m_hFile != INVALID_HANDLE_VALUE)
109 CloseHandle(m_hFile);
110
111 m_hFile = INVALID_HANDLE_VALUE;
112 m_hFileMap = 0;
113 #else
114 if (m_bTempHandle && m_hFile)
115 fclose(m_hFile);
116
117 m_hFile = NULL;
118 #endif
119 }
120
121 void MMapFile::setro(BOOL bRO)
122 {
123 m_bReadOnly = bRO;
124 }
125
126 // TODO: Convert to UINT64. Right now the mapping is still limited to 31 bits.
127 #ifdef _WIN32
128 int MMapFile::internalsetfile(HANDLE hFile, DWORD dwSize)
129 #else
130 int MMapFile::internalsetfile(int hFile, DWORD dwSize)
131 #endif
132 {
133 clear();
134
135 #ifdef _WIN32
136 m_hFile = hFile;
137 #else
138 m_hFileDesc = hFile;
139 #endif
140 m_bTempHandle = FALSE;
141
142 #ifdef _WIN32
143 if (m_hFile == INVALID_HANDLE_VALUE)
144 #else
145 if (m_hFileDesc == -1)
146 #endif
147 return 0;
148
149 m_iSize = (int) dwSize;
150
151 if (m_iSize <= 0)
152 return 0;
153
154 #ifdef _WIN32
155 m_hFileMap = CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, m_iSize, NULL);
156
157 if (!m_hFileMap)
158 return 0;
159 #endif
160
161 m_bReadOnly = TRUE;
162
163 return 1;
164 }
165
166 void MMapFile::resize(int newsize)
167 {
168 release();
169
170 if (newsize > m_iSize)
171 {
172 #ifdef _WIN32
173 if (m_hFileMap)
174 CloseHandle(m_hFileMap);
175
176 m_hFileMap = 0;
177 #endif
178
179 m_iSize = newsize;
180
181 #ifdef _WIN32
182 if (m_hFile == INVALID_HANDLE_VALUE)
183 {
184 TCHAR buf[MAX_PATH], buf2[MAX_PATH];
185
186 GetTempPath(MAX_PATH, buf);
187 GetTempFileName(buf, _T("nsd"), 0, buf2);
188
189 m_hFile = CreateFile(
190 buf2,
191 GENERIC_READ | GENERIC_WRITE,
192 0,
193 NULL,
194 CREATE_ALWAYS,
195 FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
196 NULL
197 );
198
199 m_bTempHandle = TRUE;
200 }
201
202 if (m_hFile != INVALID_HANDLE_VALUE)
203 {
204 m_hFileMap = CreateFileMapping(
205 m_hFile,
206 NULL,
207 m_bReadOnly ? PAGE_READONLY : PAGE_READWRITE,
208 0,
209 m_iSize,
210 NULL
211 );
212 }
213 #else
214 if (m_hFile == NULL)
215 {
216 m_hFile = tmpfile();
217 if (m_hFile != NULL)
218 {
219 m_hFileDesc = fileno(m_hFile);
220 m_bTempHandle = TRUE;
221 }
222 }
223
224 // resize
225 if (m_hFileDesc != -1)
226 {
227 unsigned char c = 0;
228
229 if (lseek(m_hFileDesc, m_iSize, SEEK_SET) != (off_t)-1)
230 {
231 if (read(m_hFileDesc, &c, 1) != -1)
232 {
233 if (lseek(m_hFileDesc, m_iSize, SEEK_SET) != (off_t)-1)
234 {
235 if (write(m_hFileDesc, &c, 1) != -1)
236 {
237 return; // no errors
238 }
239 }
240 }
241 }
242 }
243
244 m_hFileDesc = -1; // some error occurred, bail
245 #endif
246
247 #ifdef _WIN32
248 if (!m_hFileMap)
249 #else
250 if (m_hFileDesc == -1)
251 #endif
252 {
253 extern void quit(); extern int g_display_errors;
254 if (g_display_errors)
255 {
256 PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error creating mmap the size of %d.\n"), m_iSize);
257 }
258 quit();
259 }
260 }
261 }
262
263 #ifdef _WIN32
264 bool MMapFile::setfile(HANDLE hFile, UINT64 size)
265 {
266 maxfilesizetype maxsize = getmaxfilesize();
267 return size <= maxsize && internalsetfile(hFile, (maxfilesizetype) size);
268 }
269
270 HANDLE MMapFile::openfilehelper(const TCHAR*fpath, UINT64 &size)
271 {
272 HANDLE hFile = CreateFile(fpath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, NULL);
273 if (hFile != INVALID_HANDLE_VALUE)
274 {
275 ULARGE_INTEGER size64;
276 if (GetFileSize64(hFile, size64))
277 size = size64.QuadPart;
278 else
279 CloseHandle(hFile), hFile = INVALID_HANDLE_VALUE;
280 }
281 return hFile;
282 }
283 #else
284 bool MMapFile::setfile(FILE*hFile, UINT64 size)
285 {
286 maxfilesizetype maxsize = getmaxfilesize();
287 return size <= maxsize && internalsetfile(fileno(hFile), (maxfilesizetype) size);
288 }
289
290 FILE* MMapFile::openfilehelper(const TCHAR*fpath, UINT64 &size)
291 {
292 FILE *hFile = FOPEN(fpath, ("rb"));
293 if (hFile)
294 {
295 UINT64 size64 = get_file_size64(hFile);
296 if (size64 != invalid_file_size64)
297 size = size64;
298 else
299 fclose(hFile), hFile = 0;
300 }
301 return hFile;
302 }
303 #endif
304
305 UINT64 MMapFile::setfile(const TCHAR*fpath)
306 {
307 UINT64 size = 0;
308 #ifdef _WIN32
309 HANDLE hFile = openfilehelper(fpath, size);
310 if (hFile != INVALID_HANDLE_VALUE)
311 {
312 if (!setfile(hFile, size)) size = 0;
313 CloseHandle(hFile);
314 }
315 #else
316 FILE *hFile = openfilehelper(fpath, size);
317 if (hFile)
318 {
319 if (!setfile(hFile, size)) size = 0;
320 // NOTE: mmap() requires the file descriptor to stay open for get(),
321 // otherwise get() will fail with errno code EBADFD (bad file descriptor).
322 m_hFile = hFile, m_bTempHandle = TRUE;
323 }
324 #endif
325 return size;
326 }
327
328 int MMapFile::getsize() const
329 {
330 return m_iSize;
331 }
332
333 void *MMapFile::get(int offset, size_t size) const
334 {
335 return get(offset, &size);
336 }
337
338 void *MMapFile::get(int offset, size_t *sizep) const
339 {
340 if (!sizep)
341 return NULL;
342
343 assert(!m_pView);
344
345 size_t size = *sizep;
346
347 if (!m_iSize || offset + (int)size > m_iSize) // TODO: once m_iSize and offset is converted to unsigned 64bit, this typecast will disappear
348 {
349 extern void quit(); extern int g_display_errors;
350 if (g_display_errors)
351 {
352 PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error mmapping file (%d, %d) is out of range.\n"), offset, size);
353 }
354 quit();
355 }
356
357 // fix offset
358 int alignedoffset = offset - (offset % m_iAllocationGranularity);
359 size += offset - alignedoffset;
360
361 #ifdef _WIN32
362 m_pView = MapViewOfFile(m_hFileMap, m_bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, alignedoffset, size);
363 #else
364 m_pView = mmap(0, size, m_bReadOnly ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, m_hFileDesc, alignedoffset);
365 m_iMappedSize = *sizep = size;
366 #endif
367
368 #ifdef _WIN32
369 if (!m_pView)
370 #else
371 if (m_pView == MAP_FAILED)
372 #endif
373 {
374 extern void quit(); extern int g_display_errors;
375 if (g_display_errors)
376 {
377 PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error mmapping datablock to %d.\n"), size);
378 }
379 quit();
380 }
381
382 return (void *)((char *)m_pView + offset - alignedoffset);
383 }
384
385 void *MMapFile::getmore(int offset, size_t size) const
386 {
387 void *pView;
388 void *pViewBackup = m_pView;
389 #ifndef _WIN32
390 int iMappedSizeBackup = m_iMappedSize;
391 #endif
392 m_pView = 0;
393 pView = get(offset, size);
394 m_pView = pViewBackup;
395 #ifndef _WIN32
396 m_iMappedSize = iMappedSizeBackup;
397 #endif
398 return pView;
399 }
400
401 void MMapFile::release()
402 {
403 if (!m_pView)
404 return;
405
406 #ifdef _WIN32
407 UnmapViewOfFile(m_pView);
408 #else
409 munmap((char *)m_pView, m_iMappedSize);
410 #endif
411 m_pView = NULL;
412 }
413
414 void MMapFile::release(void *pView, size_t size)
415 {
416 if (!pView)
417 return;
418
419 unsigned int alignment = ((ULONG_PTR)pView) % m_iAllocationGranularity;
420 pView = (char *)pView - alignment;
421 size += alignment;
422 #ifdef _WIN32
423 UnmapViewOfFile(pView);
424 #else
425 munmap((char *)pView, size);
426 #endif
427 }
428
429 void MMapFile::flush(size_t num)
430 {
431 if (m_pView)
432 #ifdef _WIN32
433 {} // improving performance by commenting: FlushViewOfFile(m_pView, num);
434 #else
435 msync((char *)m_pView, num, MS_SYNC);
436 #endif
437 }
438
439 // ========
440 // MMapFake
441 // ========
442
443 MMapFake::MMapFake()
444 {
445 m_pMem = NULL;
446 m_iSize = 0;
447 }
448
449 void MMapFake::set(const char *pMem, int iSize)
450 {
451 m_pMem = pMem;
452 m_iSize = iSize;
453 }
454
455 int MMapFake::getsize() const
456 {
457 return m_iSize;
458 }
459
460 void *MMapFake::get(int offset, size_t size) const
461 {
462 return get(offset, &size);
463 }
464
465 void *MMapFake::get(int offset, size_t *size) const
466 {
467 if (!size || (offset + (int)*size > m_iSize)) // TODO: once m_iSize and offset is converted to unsigned 64bit, this typecast will disappear
468 return NULL;
469 return (void *)(m_pMem + offset);
470 }
471
472 void *MMapFake::getmore(int offset, size_t size) const
473 {
474 return get(offset, size);
475 }
476
477 void MMapFake::resize(int n) {}
478 void MMapFake::release() {}
479 void MMapFake::release(void *p, size_t size) {}
480 void MMapFake::clear() {}
481 void MMapFake::setro(BOOL b) {}
482 void MMapFake::flush(size_t b) {}
483
484 // =======
485 // MMapBuf
486 // =======
487
488 MMapBuf::MMapBuf()
489 {
490 m_gb_u=0;
491 m_alloc=m_used=0;
492 }
493
494 MMapBuf::~MMapBuf()
495 {
496 m_fm.release();
497 }
498
499 int MMapBuf::add(const void *data, int len)
500 {
501 if (len <= 0) return 0;
502 resize(getlen() + len);
503 memcpy((char*)get(getlen() - len, len), data, len);
504 release();
505 return getlen() - len;
506 }
507
508 void MMapBuf::setro(BOOL bRO)
509 {
510 m_fm.setro(bRO);
511 }
512
513 void MMapBuf::resize(int newlen)
514 {
515 if (!m_gb_u && newlen < getmodethreshold()) // still in db mode
516 {
517 m_gb.resize(newlen);
518 return;
519 }
520
521 // not in db mode
522 m_gb_u = 1;
523 m_used = newlen;
524
525 if (newlen > m_alloc)
526 {
527 if (!si_add(m_alloc, newlen, (16 << 20))) // add 16mb to top of mapping
528 m_alloc = INT_MAX; // we've hit a signed integer overflow
529
530 m_fm.resize(m_alloc);
531
532 if (m_gb.getlen())
533 {
534 memcpy(m_fm.get(0, m_gb.getlen()), m_gb.get(), m_gb.getlen());
535 m_fm.flush(m_gb.getlen());
536 m_fm.release();
537 m_gb.resize(0);
538 }
539 }
540 }
541
542 int MMapBuf::getsize() const
543 {
544 return getlen();
545 }
546
547 int MMapBuf::getlen() const
548 {
549 if (m_gb_u)
550 return m_used;
551 return m_gb.getlen();
552 }
553
554 void *MMapBuf::get() const
555 {
556 return get(0, m_alloc);
557 }
558
559 void *MMapBuf::get(int offset, size_t *sizep) const
560 {
561 if (!sizep)
562 return NULL;
563 size_t size = *sizep;
564 return get(offset, size);
565 }
566
567 void *MMapBuf::get(int offset, size_t size) const
568 {
569 if (m_gb_u)
570 return m_fm.get(offset, size);
571 return (void *) ((char *) m_gb.get() + offset);
572 }
573
574 void *MMapBuf::getmore(int offset, size_t size) const
575 {
576 if (m_gb_u)
577 return m_fm.getmore(offset, size);
578 return (void *) ((char *) m_gb.get() + offset);
579 }
580
581 void MMapBuf::release()
582 {
583 if (m_gb_u)
584 m_fm.release();
585 }
586
587 void MMapBuf::release(void *pView, size_t size)
588 {
589 if (m_gb_u)
590 m_fm.release(pView, size);
591 }
592
593 void MMapBuf::clear()
594 {
595 if (m_gb_u)
596 m_fm.clear();
597 }
598
599 void MMapBuf::flush(size_t num)
600 {
601 if (m_gb_u)
602 m_fm.flush(num);
603 }