1 //===-- Linux implementation of pread -------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "src/unistd/pread.h" 10 11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function. 12 #include "src/__support/common.h" 13 #include "src/__support/macros/config.h" 14 #include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON 15 #include "src/errno/libc_errno.h" 16 #include <stdint.h> // For uint64_t. 17 #include <sys/syscall.h> // For syscall numbers. 18 19 namespace LIBC_NAMESPACE_DECL { 20 21 LLVM_LIBC_FUNCTION(ssize_t, pread, 22 (int fd, void *buf, size_t count, off_t offset)) { 23 ssize_t ret; 24 if constexpr (sizeof(long) == sizeof(uint32_t) && 25 sizeof(off_t) == sizeof(uint64_t)) { 26 // This is a 32-bit system with a 64-bit offset, offset must be split. 27 const uint64_t bits = cpp::bit_cast<uint64_t>(offset); 28 const uint32_t lo = bits & UINT32_MAX; 29 const uint32_t hi = bits >> 32; 30 const long offset_low = cpp::bit_cast<long>(static_cast<long>(lo)); 31 const long offset_high = cpp::bit_cast<long>(static_cast<long>(hi)); 32 ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pread64, fd, buf, count, 33 offset_low, offset_high); 34 } else { 35 ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_pread64, fd, buf, count, 36 offset); 37 } 38 // The cast is important since there is a check that dereferences the pointer 39 // which fails on void*. 40 MSAN_UNPOISON(reinterpret_cast<char *>(buf), count); 41 if (ret < 0) { 42 libc_errno = static_cast<int>(-ret); 43 return -1; 44 } 45 return ret; 46 } 47 48 } // namespace LIBC_NAMESPACE_DECL 49