|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/backends/aoti/temp_library.h> |
| 10 | +#include <atomic> |
| 11 | +#include <string> |
| 12 | + |
| 13 | +#ifdef _WIN32 |
| 14 | +#include <windows.h> |
| 15 | +#include <cstdio> |
| 16 | +#include <random> |
| 17 | +#else // Posix |
| 18 | +#include <fcntl.h> |
| 19 | +#include <unistd.h> |
| 20 | +#include <cerrno> |
| 21 | +#include <cstdlib> |
| 22 | +#include <cstring> |
| 23 | +#endif |
| 24 | + |
| 25 | +namespace executorch { |
| 26 | +namespace backends { |
| 27 | +namespace aoti { |
| 28 | + |
| 29 | +executorch::runtime::Result<std::filesystem::path> write_temp_library( |
| 30 | + const void* data, |
| 31 | + size_t size) { |
| 32 | + std::error_code temp_dir_error; |
| 33 | + const std::filesystem::path temp_dir = |
| 34 | + std::filesystem::temp_directory_path(temp_dir_error); |
| 35 | + if (temp_dir_error) { |
| 36 | + ET_LOG( |
| 37 | + Error, |
| 38 | + "Failed to find the temporary directory: %s", |
| 39 | + temp_dir_error.message().c_str()); |
| 40 | + return executorch::runtime::Error::AccessFailed; |
| 41 | + } |
| 42 | + // The loader hands back an already loaded library when given the same path |
| 43 | + // again, and libraries are never unloaded, so a path must not repeat within |
| 44 | + // a process. |
| 45 | + static std::atomic<uint64_t> counter{0}; |
| 46 | + const std::string prefix = "executorch_aoti_" + |
| 47 | + std::to_string(counter.fetch_add(1, std::memory_order_relaxed)) + "_"; |
| 48 | + const char* bytes = static_cast<const char*>(data); |
| 49 | + size_t remaining = size; |
| 50 | +#ifdef _WIN32 |
| 51 | + std::random_device random; |
| 52 | + char suffix[24]; |
| 53 | + snprintf(suffix, sizeof(suffix), "%08x%08x.so", random(), random()); |
| 54 | + std::filesystem::path path = temp_dir / (prefix + suffix); |
| 55 | + const auto u8 = path.u8string(); |
| 56 | + const std::string utf8(u8.begin(), u8.end()); |
| 57 | + HANDLE file = CreateFileW( |
| 58 | + path.c_str(), |
| 59 | + GENERIC_WRITE, |
| 60 | + 0, |
| 61 | + nullptr, |
| 62 | + CREATE_NEW, |
| 63 | + FILE_ATTRIBUTE_NORMAL, |
| 64 | + nullptr); |
| 65 | + if (file == INVALID_HANDLE_VALUE) { |
| 66 | + ET_UNUSED const DWORD err = GetLastError(); |
| 67 | + ET_LOG(Error, "Failed to create %s with error %lu", utf8.c_str(), err); |
| 68 | + return executorch::runtime::Error::AccessFailed; |
| 69 | + } |
| 70 | + // WriteFile takes a 32-bit length, so write in chunks. |
| 71 | + constexpr DWORD kMaxChunk = 1u << 30; |
| 72 | + DWORD err = ERROR_SUCCESS; |
| 73 | + while (remaining > 0 && err == ERROR_SUCCESS) { |
| 74 | + const DWORD chunk = |
| 75 | + remaining > kMaxChunk ? kMaxChunk : static_cast<DWORD>(remaining); |
| 76 | + DWORD written = 0; |
| 77 | + if (!WriteFile(file, bytes, chunk, &written, nullptr)) { |
| 78 | + err = GetLastError(); |
| 79 | + } else if (written == 0) { |
| 80 | + err = ERROR_WRITE_FAULT; |
| 81 | + } else { |
| 82 | + bytes += written; |
| 83 | + remaining -= written; |
| 84 | + } |
| 85 | + } |
| 86 | + if (!CloseHandle(file) && err == ERROR_SUCCESS) { |
| 87 | + err = GetLastError(); |
| 88 | + } |
| 89 | + if (err != ERROR_SUCCESS) { |
| 90 | + DeleteFileW(path.c_str()); |
| 91 | + ET_LOG(Error, "Failed to write %s with error %lu", utf8.c_str(), err); |
| 92 | + return executorch::runtime::Error::AccessFailed; |
| 93 | + } |
| 94 | + ET_LOG(Info, "Wrote %zu bytes to %s", size, utf8.c_str()); |
| 95 | + return path; |
| 96 | +#else |
| 97 | + // mkostemps replaces the Xs with random characters and creates the file with |
| 98 | + // O_EXCL and mode 0600, retrying if the name is taken. |
| 99 | + std::string path = (temp_dir / (prefix + "XXXXXX.so")).string(); |
| 100 | + const int fd = mkostemps(path.data(), /*suffixlen=*/3, O_CLOEXEC); |
| 101 | + if (fd == -1) { |
| 102 | + ET_UNUSED const int err = errno; |
| 103 | + ET_LOG(Error, "Failed to create %s: %s", path.c_str(), strerror(err)); |
| 104 | + return executorch::runtime::Error::AccessFailed; |
| 105 | + } |
| 106 | + int err = 0; |
| 107 | + while (remaining > 0 && err == 0) { |
| 108 | + const ssize_t written = write(fd, bytes, remaining); |
| 109 | + if (written > 0) { |
| 110 | + bytes += written; |
| 111 | + remaining -= static_cast<size_t>(written); |
| 112 | + } else if (written == 0) { |
| 113 | + err = EIO; |
| 114 | + } else if (errno != EINTR) { |
| 115 | + err = errno; |
| 116 | + } |
| 117 | + } |
| 118 | + if (close(fd) != 0 && err == 0) { |
| 119 | + err = errno; |
| 120 | + } |
| 121 | + if (err != 0) { |
| 122 | + ET_LOG(Error, "Failed to write %s: %s", path.c_str(), strerror(err)); |
| 123 | + unlink(path.c_str()); |
| 124 | + return executorch::runtime::Error::AccessFailed; |
| 125 | + } |
| 126 | + ET_LOG(Info, "Wrote %zu bytes to %s", size, path.c_str()); |
| 127 | + // The caller only takes over the file once this returns, so remove it here |
| 128 | + // if building the path throws. |
| 129 | + try { |
| 130 | + return std::filesystem::path(path); |
| 131 | + } catch (...) { |
| 132 | + unlink(path.c_str()); |
| 133 | + throw; |
| 134 | + } |
| 135 | +#endif |
| 136 | +} |
| 137 | + |
| 138 | +} // namespace aoti |
| 139 | +} // namespace backends |
| 140 | +} // namespace executorch |
0 commit comments