-
Notifications
You must be signed in to change notification settings - Fork 6
Allocate executable JIT code to read+execute memory directly #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mafone
wants to merge
2
commits into
wasm-ecosystem:develop
Choose a base branch
from
mafone:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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] | ||||||
|
|
@@ -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)}; | ||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| #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; | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.