1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #include "ABI47_0_0JSBigString.h"
9 
10 #include <glog/logging.h>
11 
12 #include <folly/Memory.h>
13 #include <folly/ScopeGuard.h>
14 #include <folly/portability/Fcntl.h>
15 #include <folly/portability/SysMman.h>
16 #include <folly/portability/SysStat.h>
17 #include <folly/portability/Unistd.h>
18 
19 #include <memory>
20 
21 namespace ABI47_0_0facebook {
22 namespace ABI47_0_0React {
23 
JSBigFileString(int fd,size_t size,off_t offset)24 JSBigFileString::JSBigFileString(int fd, size_t size, off_t offset /*= 0*/)
25     : m_fd{-1}, m_data{nullptr} {
26   folly::checkUnixError(m_fd = dup(fd), "Could not duplicate file descriptor");
27 
28   // Offsets given to mmap must be page aligned. We abstract away that
29   // restriction by sending a page aligned offset to mmap, and keeping track
30   // of the offset within the page that we must alter the mmap pointer by to
31   // get the final desired offset.
32   if (offset != 0) {
33     const static auto ps = sysconf(_SC_PAGESIZE);
34     auto d = lldiv(offset, ps);
35 
36     m_mapOff = d.quot;
37     m_pageOff = d.rem;
38     m_size = size + m_pageOff;
39   } else {
40     m_mapOff = 0;
41     m_pageOff = 0;
42     m_size = size;
43   }
44 }
45 
~JSBigFileString()46 JSBigFileString::~JSBigFileString() {
47   if (m_data) {
48     munmap((void *)m_data, m_size);
49   }
50   close(m_fd);
51 }
52 
c_str() const53 const char *JSBigFileString::c_str() const {
54   if (m_size == 0) {
55     return "";
56   }
57   if (!m_data) {
58     m_data =
59         (const char *)mmap(0, m_size, PROT_READ, MAP_PRIVATE, m_fd, m_mapOff);
60     CHECK(m_data != MAP_FAILED)
61         << " fd: " << m_fd << " size: " << m_size << " offset: " << m_mapOff
62         << " error: " << std::strerror(errno);
63   }
64   static const size_t kMinPageSize = 4096;
65   CHECK(!(reinterpret_cast<uintptr_t>(m_data) & (kMinPageSize - 1)))
66       << "mmap address misaligned, likely corrupted"
67       << " m_data: " << (const void *)m_data;
68   CHECK(m_pageOff <= m_size)
69       << "offset impossibly large, likely corrupted"
70       << " m_pageOff: " << m_pageOff << " m_size: " << m_size;
71   return m_data + m_pageOff;
72 }
73 
size() const74 size_t JSBigFileString::size() const {
75   // Ensure mapping has been initialized.
76   c_str();
77   return m_size - m_pageOff;
78 }
79 
fd() const80 int JSBigFileString::fd() const {
81   return m_fd;
82 }
83 
fromPath(const std::string & sourceURL)84 std::unique_ptr<const JSBigFileString> JSBigFileString::fromPath(
85     const std::string &sourceURL) {
86   int fd = ::open(sourceURL.c_str(), O_RDONLY);
87   folly::checkUnixError(fd, "Could not open file", sourceURL);
88   SCOPE_EXIT {
89     CHECK(::close(fd) == 0);
90   };
91 
92   struct stat fileInfo;
93   folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on bundle failed.");
94 
95   return std::make_unique<const JSBigFileString>(fd, fileInfo.st_size);
96 }
97 
98 } // namespace ABI47_0_0React
99 } // namespace ABI47_0_0facebook
100