Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/utils/ExecutableMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,20 @@ void ExecutableMemory::init(uint8_t const *const data) {
return;
}

MemUtils::MmapMemory const mmapMemory{MemUtils::allocPagedMemory(size_)};
uint8_t *const readWriteMemory{mmapMemory.ptr};
MemUtils::MmapMemory const mmapMemory{MemUtils::allocPagedMemory(size_, data)};
uint8_t *const readWriteOrExecuteMemory{mmapMemory.ptr};

if (readWriteMemory != nullptr) {
if (readWriteOrExecuteMemory != nullptr) {
fd_ = mmapMemory.fd;
MemUtils::memcpyAndClearInstrCache(readWriteMemory, data, size_);
data_ = readWriteOrExecuteMemory;
Comment thread
mafone marked this conversation as resolved.
} else {
throw std::bad_alloc(); // GCOVR_EXCL_LINE
}

#ifdef __linux__
uint8_t *const readExecuteMemory{MemUtils::mapRXMemory(size_, fd_)};
data_ = readExecuteMemory;
MemUtils::freePagedMemory(readWriteMemory, size_);
#if defined(__linux__) || defined(__QNX__)
MemUtils::clearInstructionCache(data_, size_);
#else
data_ = readWriteMemory;
MemUtils::memcpyAndClearInstrCache(data_, data, size_);
MemUtils::setPermissionRX(data_, size_);
#endif
}
Expand Down
53 changes: 32 additions & 21 deletions src/utils/MemUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ static void *alignedReduce(void *const ptr, size_t const size, size_t const alig
/// @brief wrapper of mmap, throw std::bad_alloc when mmap failed
/// @param addr bypass to mmap
/// @param length bypass to mmap
/// @param port bypass to mmap
/// @param prot bypass to mmap
/// @param flags bypass to mmap
/// @param fd bypass to mmap
/// @param offset bypass to mmap
/// @return result of mmap when mmap success
/// @throw throw std::bad_alloc when mmap failed
static uint8_t *wrapperMmap(void *const addr, size_t const length, int32_t const port, int32_t const flags, int32_t const fd, off_t const offset) {
static uint8_t *wrapperMmap(void *const addr, size_t const length, int32_t const prot, int32_t const flags, int32_t const fd, off_t const offset) {
// coverity[autosar_cpp14_a16_2_3_violation]
void *const ptr{mmap(addr, length, port, flags, fd, offset)};
void *const ptr{mmap(addr, length, prot, flags, fd, offset)};
// coverity[autosar_cpp14_a16_2_3_violation]
// coverity[autosar_cpp14_a5_2_2_violation]
// coverity[autosar_cpp14_m5_2_9_violation]
Expand Down Expand Up @@ -381,45 +381,56 @@ void memcpyAndClearInstrCache(uint8_t *const dest, uint8_t const *const source,
clearInstructionCache(dest, size);
}

MmapMemory allocPagedMemory(size_t const size) {
MmapMemory allocPagedMemory(size_t const size, uint8_t const *const data) {
MmapMemory mmapMemory{nullptr, -1};
#ifdef VB_WIN32
mmapMemory.ptr = allocAlignedMemory(size, getOSMemoryPageSize());
return mmapMemory;
#else
size_t const alignedSize{roundUpToOSMemoryPageSize(size)};
#ifdef __linux__
static std::atomic<uint64_t> fileCounter{0U}; ///< counter of mmap files
// coverity[autosar_cpp14_a16_2_3_violation]
std::stringstream ss{};
uint64_t const localCounter{fileCounter};
fileCounter++;
ss << getpid() << "_vb_wasm_mem" << localCounter;
int32_t const jitCodeMapFile{
#if defined(__linux__) || defined(__QNX__)
#if defined(__linux__)
static std::atomic<uint64_t> fileCounter{0U}; ///< counter of mmap files
// coverity[autosar_cpp14_a16_2_3_violation]
std::stringstream ss{};
uint64_t const localCounter{fileCounter};
fileCounter++;
ss << getpid() << "_vb_wasm_mem" << localCounter;
int32_t const jitCodeMapFile{
// coverity[autosar_cpp14_a16_2_3_violation]
static_cast<int32_t>(syscall(__NR_memfd_create, ss.str().c_str(), MFD_CLOEXEC))}; // NOLINT(cppcoreguidelines-pro-type-vararg)
#elif defined __QNX__
int32_t const jitCodeMapFile{SHM_ANON, O_RDWR | O_CREAT, 0600)};
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int32_t const jitCodeMapFile{SHM_ANON, O_RDWR | O_CREAT, 0600)};
int32_t const jitCodeMapFile{shm_open(SHM_ANON, O_RDWR | O_CREAT, 0600)};

#endif

if (jitCodeMapFile == -1) {
return mmapMemory;
}

int32_t error{ftruncate(jitCodeMapFile, static_cast<off_t>(alignedSize))};

if (error != 0) {
if ((error != 0) || (data == nullptr) || (write(jitCodeMapFile, data, size) == -1)) {
error = close(jitCodeMapFile);
static_cast<void>(error);
assert(error == 0 && "close file failed");
return mmapMemory;
}

// coverity[autosar_cpp14_a16_2_3_violation]
constexpr int32_t flag{MAP_SHARED};
constexpr int32_t flag{MAP_PRIVATE};
mmapMemory.fd = jitCodeMapFile;
#else
mmapMemory.fd = -1;
constexpr uint32_t uflag = static_cast<uint32_t>(MAP_ANONYMOUS) | static_cast<uint32_t>(MAP_PRIVATE);
constexpr int32_t flag = static_cast<int32_t>(uflag);
#endif

#else
mmapMemory.fd = -1;
constexpr uint32_t uflag = static_cast<uint32_t>(MAP_ANONYMOUS) | static_cast<uint32_t>(MAP_PRIVATE);
constexpr int32_t flag = static_cast<int32_t>(uflag);
#endif

// coverity[autosar_cpp14_a16_2_3_violation]
constexpr uint32_t prot{static_cast<uint32_t>(PROT_READ) | static_cast<uint32_t>(PROT_WRITE)};
uint32_t prot{static_cast<uint32_t>(PROT_READ) | static_cast<uint32_t>(PROT_WRITE)};
if(data != nullptr)
{
prot = static_cast<uint32_t>(PROT_READ) | static_cast<uint32_t>(PROT_EXEC);
}
mmapMemory.ptr = wrapperMmap(nullptr, alignedSize, static_cast<int32_t>(prot), static_cast<int32_t>(flag), mmapMemory.fd, 0);

return mmapMemory;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/MemUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ struct MmapMemory final {
/// @brief Allocate page aligned memory
///
/// @param size size of to be allocated memory
/// @param data pointer to be mapped as an anonymous file
Comment thread
mafone marked this conversation as resolved.
/// @throw std::bad_alloc memory allocation failed
/// @return MmapMemory
///
MmapMemory allocPagedMemory(size_t const size);
MmapMemory allocPagedMemory(size_t const size, uint8_t const *const data = nullptr);
///
/// @brief Free page aligned memory
///
Expand Down
Loading