client/runtime/linux_file.c (10366B)
1 #define _GNU_SOURCE 2 #include "browser_profile.h" 3 #include "linux_path.h" 4 #include <pthread.h> 5 #include "linux_file.h" 6 #ifdef __EMSCRIPTEN__ 7 #include "browser_asset_cache.h" 8 #include <emscripten/wasmfs.h> 9 #endif 10 #include <errno.h> 11 #include <fcntl.h> 12 #include <sys/file.h> 13 #include <sys/stat.h> 14 #include <unistd.h> 15 16 /* Guest handles identify host-owned descriptors, never host pointers. */ 17 struct guest_file { 18 uint32_t id; 19 int descriptor, unbuffered; 20 #ifdef __EMSCRIPTEN__ 21 dev_t device; 22 ino_t inode; 23 int exclusive; 24 off_t asset_size; 25 #endif 26 struct guest_file *next; 27 }; 28 static struct guest_file *handles; 29 /* game/hhr.dll/lifted-0007.c: sub_31015A7C rejects negative file handles. */ 30 static uint32_t next_handle = 0x05000000u; 31 32 static struct guest_file *find_handle(uint32_t id) 33 { 34 for (struct guest_file *handle = handles; handle; handle = handle->next) 35 if (handle->id == id) return handle; 36 MEM32(g_fs_base + 0x34) = 6; /* ERROR_INVALID_HANDLE */ 37 return NULL; 38 } 39 40 static uint32_t file_error(int error) 41 { 42 switch (error) { 43 case ENOENT: return 2; 44 case ENOTDIR: return 3; 45 case EMFILE: case ENFILE: return 4; 46 case EACCES: case EPERM: case EISDIR: return 5; 47 case EBADF: return 6; 48 case EINVAL: return 87; 49 case ENOMEM: return 8; 50 case ENAMETOOLONG: return 206; 51 case EFAULT: return 998; 52 case EIO: return 1117; 53 default: 54 fprintf(stderr, "Unhandled native file error: %s\n", strerror(error)); 55 abort(); 56 } 57 } 58 59 static void create_file_locked(void) 60 { 61 const char *name = (const char *)ADDR(MEM32(g_esp + 4)); 62 int buffered = MEM32(g_esp + 12) == 1 && MEM32(g_esp + 20) == 3 && MEM32(g_esp + 24) == 0x80; 63 int unbuffered = MEM32(g_esp + 12) == 3 && MEM32(g_esp + 20) == 4 && MEM32(g_esp + 24) == 0x30000000; 64 int exclusive = MEM32(g_esp + 12) == 0 && MEM32(g_esp + 20) == 3 && MEM32(g_esp + 24) == 0x80000000; 65 if (MEM32(g_esp + 8) != 0x80000000u || MEM32(g_esp + 16) || 66 (!buffered && !unbuffered && !exclusive) || MEM32(g_esp + 28)) { 67 fprintf(stderr, "Unsupported CreateFileA mode for %s: %08x %x %x %x %x %x\n", 68 name, MEM32(g_esp + 8), MEM32(g_esp + 12), MEM32(g_esp + 16), 69 MEM32(g_esp + 20), MEM32(g_esp + 24), MEM32(g_esp + 28)); 70 abort(); 71 } 72 if (strchr(name, ':') || !strncmp(name, "\\\\", 2)) { 73 fprintf(stderr, "Windows drive/UNC file paths are not implemented: %s\n", name); 74 abort(); 75 } 76 77 /* OPEN_ALWAYS reports whether it created a file without truncating existing data. */ 78 char *path = linux_path(name); 79 int options = O_RDONLY | O_CLOEXEC | O_NONBLOCK; 80 #ifndef __EMSCRIPTEN__ 81 /* WasmFS has no host disk cache; guest alignment and sharing are checked below. */ 82 options |= (unbuffered ? O_DIRECT : 0) | (exclusive ? O_SYNC : 0); 83 #endif 84 int descriptor = open(path, options | (unbuffered ? O_CREAT | O_EXCL : 0), 0666); 85 int created = unbuffered && descriptor >= 0; 86 if (unbuffered && descriptor < 0 && errno == EEXIST) descriptor = open(path, options); 87 uint32_t error = descriptor < 0 ? file_error(errno) : 0; 88 if (error == 2) { 89 char *slash = strrchr(path, '/'); 90 if (slash && slash != path) { 91 *slash = 0; 92 struct stat parent; 93 if (stat(path, &parent) || !S_ISDIR(parent.st_mode)) error = 3; 94 } 95 } 96 free(path); 97 struct stat info; 98 if (descriptor >= 0) { 99 if (fstat(descriptor, &info)) error = file_error(errno); 100 else if (!S_ISREG(info.st_mode)) error = 5; 101 /* All supported modes are read-only; shared/exclusive locks enforce sharing. */ 102 #ifdef __EMSCRIPTEN__ 103 else for (struct guest_file *other = handles; other; other = other->next) 104 if (other->device == info.st_dev && other->inode == info.st_ino && (exclusive || other->exclusive)) { 105 error = 32; 106 break; 107 } 108 #else 109 else if (flock(descriptor, (exclusive ? LOCK_EX : LOCK_SH) | LOCK_NB)) 110 error = errno == EWOULDBLOCK ? 32 : file_error(errno); 111 #endif 112 if (error) close(descriptor); 113 } 114 115 g_eax = UINT32_MAX; 116 if (!error) { 117 if (next_handle >= 0x06000000u) abort(); 118 struct guest_file *handle = malloc(sizeof(*handle)); 119 if (!handle) abort(); 120 handle->id = next_handle; 121 next_handle += 4; 122 handle->descriptor = descriptor; 123 handle->unbuffered = unbuffered; 124 #ifdef __EMSCRIPTEN__ 125 handle->device = info.st_dev; 126 handle->inode = info.st_ino; 127 handle->exclusive = exclusive; 128 handle->asset_size = wasmfs_get_backend_by_fd(descriptor) == 129 wasmfs_get_backend_by_path("/remote") ? info.st_size : -1; 130 #endif 131 handle->next = handles; 132 handles = handle; 133 g_eax = handle->id; 134 } 135 MEM32(g_fs_base + 0x34) = error ? error : (unbuffered && !created ? 183 : 0); 136 g_esp += 32; 137 } 138 139 static void get_file_size_locked(void) 140 { 141 struct guest_file *handle = find_handle(MEM32(g_esp + 4)); 142 uint32_t high = MEM32(g_esp + 8); 143 g_eax = UINT32_MAX; 144 g_esp += 12; 145 if (!handle) return; 146 struct stat info; 147 if (fstat(handle->descriptor, &info)) { 148 MEM32(g_fs_base + 0x34) = file_error(errno); 149 return; 150 } 151 g_eax = (uint64_t)info.st_size; 152 if (high) MEM32(high) = (uint64_t)info.st_size >> 32; 153 if (g_eax == UINT32_MAX) MEM32(g_fs_base + 0x34) = 0; 154 } 155 156 static void read_file_locked(void) 157 { 158 uint32_t output = MEM32(g_esp + 8), size = MEM32(g_esp + 12); 159 uint32_t count = MEM32(g_esp + 16); 160 if (!count || MEM32(g_esp + 20)) { 161 fprintf(stderr, "Overlapped/uncounted ReadFile is not implemented\n"); 162 abort(); 163 } 164 MEM32(count) = 0; 165 struct guest_file *handle = find_handle(MEM32(g_esp + 4)); 166 g_eax = 0; 167 g_esp += 24; 168 if (!handle) return; 169 if (!output && size) { MEM32(g_fs_base + 0x34) = 998; return; } 170 171 /* Enforce guest sector alignment; only native O_DIRECT needs an aligned buffer. */ 172 void *buffer = output ? (void *)ADDR(output) : NULL; 173 if (handle->unbuffered && size) { 174 off_t position = lseek(handle->descriptor, 0, SEEK_CUR); 175 if (position < 0) { MEM32(g_fs_base + 0x34) = file_error(errno); return; } 176 if ((output | size | position) & 511) { MEM32(g_fs_base + 0x34) = 87; return; } 177 #ifndef __EMSCRIPTEN__ 178 if (posix_memalign(&buffer, 4096, size)) { MEM32(g_fs_base + 0x34) = 8; return; } 179 #endif 180 } 181 double profile_start = ao_profile_begin("file read"); 182 ssize_t result; 183 do { 184 #ifdef __EMSCRIPTEN__ 185 if (handle->asset_size >= 0) 186 result = browser_asset_read_locked(handle->descriptor, handle->inode, handle->asset_size, buffer, size); 187 else 188 #endif 189 result = read(handle->descriptor, buffer, size); 190 } while (result < 0 && errno == EINTR); 191 ao_profile_end("file read", profile_start, NULL); 192 if (result < 0) MEM32(g_fs_base + 0x34) = file_error(errno); 193 #ifndef __EMSCRIPTEN__ 194 if (handle->unbuffered && size) { 195 if (result > 0) memcpy((void *)ADDR(output), buffer, result); 196 free(buffer); 197 } 198 #endif 199 if (result >= 0) { MEM32(count) = result; g_eax = 1; } 200 } 201 202 static void set_file_pointer_locked(void) 203 { 204 struct guest_file *handle = find_handle(MEM32(g_esp + 4)); 205 uint32_t low = MEM32(g_esp + 8), high = MEM32(g_esp + 12); 206 uint32_t method = MEM32(g_esp + 16); 207 g_eax = UINT32_MAX; 208 if (handle) { 209 int64_t distance = high ? (int64_t)(int32_t)MEM32(high) * 0x100000000ll + low : (int32_t)low; 210 int64_t base = 0; 211 uint32_t error = 0; 212 if (method == 1) { 213 base = lseek(handle->descriptor, 0, SEEK_CUR); 214 if (base < 0) error = file_error(errno); 215 } else if (method == 2) { 216 struct stat info; 217 if (fstat(handle->descriptor, &info)) error = file_error(errno); 218 else base = info.st_size; 219 } else if (method != 0) error = 87; /* ERROR_INVALID_PARAMETER */ 220 221 /* Validate before moving, so failed seeks leave the cursor and high word intact. */ 222 if (!error) { 223 if (distance < -base) error = 131; /* ERROR_NEGATIVE_SEEK */ 224 else if (distance > INT64_MAX - base || (!high && base + distance > UINT32_MAX)) error = 87; 225 else if (handle->unbuffered && ((base + distance) & 511)) error = 87; 226 else { 227 int64_t position = lseek(handle->descriptor, base + distance, SEEK_SET); 228 if (position < 0) error = file_error(errno); 229 else { 230 g_eax = (uint32_t)position; 231 if (high) MEM32(high) = (uint64_t)position >> 32; 232 if (g_eax == UINT32_MAX) MEM32(g_fs_base + 0x34) = 0; 233 } 234 } 235 } 236 if (error) MEM32(g_fs_base + 0x34) = error; 237 } 238 g_esp += 20; 239 } 240 241 static void close_file_locked(void) 242 { 243 struct guest_file *handle = find_handle(MEM32(g_esp + 4)); 244 g_eax = 0; 245 if (handle) { 246 struct guest_file **link = &handles; 247 while (*link != handle) link = &(*link)->next; 248 *link = handle->next; 249 g_eax = close(handle->descriptor) == 0; 250 if (!g_eax) MEM32(g_fs_base + 0x34) = file_error(errno); 251 free(handle); 252 } 253 g_esp += 8; 254 } 255 256 /* API calls serialize shared stream/handle state and synchronous file positions. */ 257 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 258 static void call_serialized(recomp_func_t function) 259 { 260 if (pthread_mutex_lock(&mutex)) abort(); 261 function(); 262 if (pthread_mutex_unlock(&mutex)) abort(); 263 } 264 265 static void create_file(void) { call_serialized(create_file_locked); } 266 static void get_file_size(void) { call_serialized(get_file_size_locked); } 267 static void read_file(void) { call_serialized(read_file_locked); } 268 static void set_file_pointer(void) { call_serialized(set_file_pointer_locked); } 269 static void close_file(void) { call_serialized(close_file_locked); } 270 271 recomp_func_t linux_file_lookup(const char *name) 272 { 273 if (!strcmp(name, "CreateFileA")) return create_file; 274 if (!strcmp(name, "GetFileSize")) return get_file_size; 275 if (!strcmp(name, "ReadFile")) return read_file; 276 if (!strcmp(name, "SetFilePointer")) return set_file_pointer; 277 if (!strcmp(name, "CloseHandle")) return close_file; 278 return NULL; 279 }