1dc2b01b3SSiva Chandra Reddy //===-- Linux implementation of read --------------------------------------===// 2dc2b01b3SSiva Chandra Reddy // 3dc2b01b3SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4dc2b01b3SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information. 5dc2b01b3SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6dc2b01b3SSiva Chandra Reddy // 7dc2b01b3SSiva Chandra Reddy //===----------------------------------------------------------------------===// 8dc2b01b3SSiva Chandra Reddy 9dc2b01b3SSiva Chandra Reddy #include "src/unistd/read.h" 10dc2b01b3SSiva Chandra Reddy 11dc2b01b3SSiva Chandra Reddy #include "src/__support/OSUtil/syscall.h" // For internal syscall function. 12dc2b01b3SSiva Chandra Reddy #include "src/__support/common.h" 13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 140e8eb445Smichaelrj-google #include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON 15803437dbSMichael Jones #include "src/errno/libc_errno.h" 16dc2b01b3SSiva Chandra Reddy #include <sys/syscall.h> // For syscall numbers. 17dc2b01b3SSiva Chandra Reddy 18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 19dc2b01b3SSiva Chandra Reddy 20dc2b01b3SSiva Chandra Reddy LLVM_LIBC_FUNCTION(ssize_t, read, (int fd, void *buf, size_t count)) { 21b6bc9d72SGuillaume Chatelet ssize_t ret = LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_read, fd, buf, count); 22dc2b01b3SSiva Chandra Reddy if (ret < 0) { 23f0a3954eSMichael Jones libc_errno = static_cast<int>(-ret); 24dc2b01b3SSiva Chandra Reddy return -1; 25dc2b01b3SSiva Chandra Reddy } 260e8eb445Smichaelrj-google // The cast is important since there is a check that dereferences the pointer 270e8eb445Smichaelrj-google // which fails on void*. 280e8eb445Smichaelrj-google MSAN_UNPOISON(reinterpret_cast<char *>(buf), count); 29dc2b01b3SSiva Chandra Reddy return ret; 30dc2b01b3SSiva Chandra Reddy } 31dc2b01b3SSiva Chandra Reddy 32*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 33