The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /* |
| 2 | * Copyright 2018 WebAssembly Community Group participants |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "wasm-rt-impl.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #ifdef _WIN32 |
| 23 | #include <windows.h> |
| 24 | #else |
| 25 | #include <sys/mman.h> |
| 26 | #endif |
| 27 | |
| 28 | #ifdef WASM_RT_GROW_FAILED_HANDLER |
| 29 | extern void WASM_RT_GROW_FAILED_HANDLER(); |
| 30 | #endif |
| 31 | |
| 32 | #define PTHREAD_MEMORY_LOCK_VAR_INIT(name) \ |
| 33 | if (pthread_mutex_init(&(name), NULL) != 0) { \ |
| 34 | fprintf(stderr, "Lock init failed\n"); \ |
| 35 | abort(); \ |
| 36 | } |
| 37 | #define PTHREAD_MEMORY_LOCK_AQUIRE(name) \ |
| 38 | if (pthread_mutex_lock(&(name)) != 0) { \ |
| 39 | fprintf(stderr, "Lock acquire failed\n"); \ |
| 40 | abort(); \ |
| 41 | } |
| 42 | #define PTHREAD_MEMORY_LOCK_RELEASE(name) \ |
| 43 | if (pthread_mutex_unlock(&(name)) != 0) { \ |
| 44 | fprintf(stderr, "Lock release failed\n"); \ |
| 45 | abort(); \ |
| 46 | } |
| 47 | |
| 48 | #define WIN_MEMORY_LOCK_VAR_INIT(name) InitializeCriticalSection(&(name)) |
| 49 | #define WIN_MEMORY_LOCK_AQUIRE(name) EnterCriticalSection(&(name)) |
| 50 | #define WIN_MEMORY_LOCK_RELEASE(name) LeaveCriticalSection(&(name)) |
| 51 | |
| 52 | #if WASM_RT_USE_MMAP |
| 53 | |
| 54 | #ifdef _WIN32 |
| 55 | static void* os_mmap(size_t size) { |
| 56 | void* ret = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | static int os_munmap(void* addr, size_t size) { |
| 61 | // Windows can only unmap the whole mapping |
| 62 | (void)size; /* unused */ |
| 63 | BOOL succeeded = VirtualFree(addr, 0, MEM_RELEASE); |
| 64 | return succeeded ? 0 : -1; |
| 65 | } |
| 66 | |
| 67 | static int os_mprotect(void* addr, size_t size) { |
| 68 | if (size == 0) { |
| 69 | return 0; |
| 70 | } |
| 71 | void* ret = VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE); |
| 72 | if (ret == addr) { |
| 73 | return 0; |
| 74 | } |
| 75 | VirtualFree(addr, 0, MEM_RELEASE); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | static void os_print_last_error(const char* msg) { |
| 80 | DWORD errorMessageID = GetLastError(); |
| 81 | if (errorMessageID != 0) { |
| 82 | LPSTR messageBuffer = 0; |
| 83 | // The api creates the buffer that holds the message |
| 84 | size_t size = FormatMessageA( |
| 85 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 86 | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 87 | NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 88 | (LPSTR)&messageBuffer, 0, NULL); |
| 89 | (void)size; |
| 90 | printf("%s. %s\n", msg, messageBuffer); |
| 91 | LocalFree(messageBuffer); |
| 92 | } else { |
| 93 | printf("%s. No error code.\n", msg); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | #else /* !_WIN32 */ |
| 98 | static void* os_mmap(size_t size) { |
| 99 | int map_prot = PROT_NONE; |
| 100 | int map_flags = MAP_ANONYMOUS | MAP_PRIVATE; |
| 101 | uint8_t* addr = mmap(NULL, size, map_prot, map_flags, -1, 0); |
| 102 | if (addr == MAP_FAILED) |
| 103 | return NULL; |
| 104 | return addr; |
| 105 | } |
| 106 | |
| 107 | static int os_munmap(void* addr, size_t size) { |
| 108 | return munmap(addr, size); |
| 109 | } |
| 110 | |
| 111 | static int os_mprotect(void* addr, size_t size) { |
| 112 | if (size == 0) { |
| 113 | return 0; |
| 114 | } |
| 115 | return mprotect(addr, size, PROT_READ | PROT_WRITE); |
| 116 | } |
| 117 | |
| 118 | static void os_print_last_error(const char* msg) { |
| 119 | perror(msg); |
| 120 | } |
| 121 | |
| 122 | #endif /* _WIN32 */ |
| 123 | |
| 124 | static uint64_t get_alloc_size_for_mmap_default32(uint64_t max_pages) { |
| 125 | #if WASM_RT_MEMCHECK_GUARD_PAGES |
| 126 | /* Reserve 8GiB. */ |
| 127 | const uint64_t max_size = 0x200000000ul; |
| 128 | return max_size; |
| 129 | #else |
| 130 | if (max_pages != 0) { |
| 131 | const uint64_t max_size = max_pages * WASM_DEFAULT_PAGE_SIZE; |
| 132 | return max_size; |
| 133 | } |
| 134 | |
| 135 | /* Reserve 4GiB. */ |
| 136 | const uint64_t max_size = 0x100000000ul; |
| 137 | return max_size; |
| 138 | #endif |
| 139 | } |
| 140 | |
| 141 | #endif /* WASM_RT_USE_MMAP */ |
| 142 | |
| 143 | static bool u64_mult_overflow(uint64_t a, uint64_t b, uint64_t* result) { |
| 144 | uint64_t x = a * b; |
| 145 | *result = x; |
| 146 | if (a != 0 && x / a != b) { |
| 147 | // overflow |
| 148 | return true; |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // Include operations for memory |
| 154 | #define WASM_RT_MEM_OPS |
| 155 | #include "wasm-rt-mem-impl-helper.inc" |
| 156 | #undef WASM_RT_MEM_OPS |
| 157 | |
| 158 | // Include operations for shared memory |
| 159 | #define WASM_RT_MEM_OPS_SHARED |
| 160 | #include "wasm-rt-mem-impl-helper.inc" |
| 161 | #undef WASM_RT_MEM_OPS_SHARED |
| 162 | |
| 163 | #undef C11_MEMORY_LOCK_VAR_INIT |
| 164 | #undef C11_MEMORY_LOCK_AQUIRE |
| 165 | #undef C11_MEMORY_LOCK_RELEASE |
| 166 | #undef PTHREAD_MEMORY_LOCK_VAR_INIT |
| 167 | #undef PTHREAD_MEMORY_LOCK_AQUIRE |
| 168 | #undef PTHREAD_MEMORY_LOCK_RELEASE |
| 169 | #undef WIN_MEMORY_LOCK_VAR_INIT |
| 170 | #undef WIN_MEMORY_LOCK_AQUIRE |
| 171 | #undef WIN_MEMORY_LOCK_RELEASE |