StdLib.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Checked.h>
  28. #include <AK/Forward.h>
  29. #include <AK/Time.h>
  30. #include <AK/Userspace.h>
  31. #include <Kernel/UnixTypes.h>
  32. namespace Syscall {
  33. struct StringArgument;
  34. }
  35. [[nodiscard]] String copy_string_from_user(const char*, size_t);
  36. [[nodiscard]] String copy_string_from_user(Userspace<const char*>, size_t);
  37. [[nodiscard]] Optional<Time> copy_time_from_user(const timespec*);
  38. [[nodiscard]] Optional<Time> copy_time_from_user(const timeval*);
  39. template<typename T>
  40. [[nodiscard]] Optional<Time> copy_time_from_user(Userspace<T*> src);
  41. [[nodiscard]] Optional<u32> user_atomic_fetch_add_relaxed(volatile u32* var, u32 val);
  42. [[nodiscard]] Optional<u32> user_atomic_exchange_relaxed(volatile u32* var, u32 val);
  43. [[nodiscard]] Optional<u32> user_atomic_load_relaxed(volatile u32* var);
  44. [[nodiscard]] bool user_atomic_store_relaxed(volatile u32* var, u32 val);
  45. [[nodiscard]] Optional<bool> user_atomic_compare_exchange_relaxed(volatile u32* var, u32& expected, u32 val);
  46. [[nodiscard]] Optional<u32> user_atomic_fetch_and_relaxed(volatile u32* var, u32 val);
  47. [[nodiscard]] Optional<u32> user_atomic_fetch_and_not_relaxed(volatile u32* var, u32 val);
  48. [[nodiscard]] Optional<u32> user_atomic_fetch_or_relaxed(volatile u32* var, u32 val);
  49. [[nodiscard]] Optional<u32> user_atomic_fetch_xor_relaxed(volatile u32* var, u32 val);
  50. extern "C" {
  51. [[nodiscard]] bool copy_to_user(void*, const void*, size_t);
  52. [[nodiscard]] bool copy_from_user(void*, const void*, size_t);
  53. [[nodiscard]] bool memset_user(void*, int, size_t);
  54. void* memcpy(void*, const void*, size_t);
  55. [[nodiscard]] int strncmp(const char* s1, const char* s2, size_t n);
  56. [[nodiscard]] char* strstr(const char* haystack, const char* needle);
  57. [[nodiscard]] int strcmp(char const*, const char*);
  58. [[nodiscard]] size_t strlen(const char*);
  59. [[nodiscard]] size_t strnlen(const char*, size_t);
  60. void* memset(void*, int, size_t);
  61. [[nodiscard]] int memcmp(const void*, const void*, size_t);
  62. void* memmove(void* dest, const void* src, size_t n);
  63. const void* memmem(const void* haystack, size_t, const void* needle, size_t);
  64. [[nodiscard]] inline u16 ntohs(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); }
  65. [[nodiscard]] inline u16 htons(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); }
  66. }
  67. template<typename T>
  68. [[nodiscard]] inline bool copy_from_user(T* dest, const T* src)
  69. {
  70. static_assert(is_trivially_copyable<T>());
  71. return copy_from_user(dest, src, sizeof(T));
  72. }
  73. template<typename T>
  74. [[nodiscard]] inline bool copy_to_user(T* dest, const T* src)
  75. {
  76. static_assert(is_trivially_copyable<T>());
  77. return copy_to_user(dest, src, sizeof(T));
  78. }
  79. template<typename T>
  80. [[nodiscard]] inline bool copy_from_user(T* dest, Userspace<const T*> src)
  81. {
  82. static_assert(is_trivially_copyable<T>());
  83. return copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T));
  84. }
  85. template<typename T>
  86. [[nodiscard]] inline bool copy_from_user(T* dest, Userspace<T*> src)
  87. {
  88. static_assert(is_trivially_copyable<T>());
  89. return copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T));
  90. }
  91. #define DEPRECATE_COPY_FROM_USER_TYPE(T, REPLACEMENT) \
  92. template<> \
  93. [[nodiscard]] inline __attribute__((deprecated("use " #REPLACEMENT " instead"))) bool copy_from_user<T>(T*, const T*) \
  94. { \
  95. VERIFY_NOT_REACHED(); \
  96. } \
  97. template<> \
  98. [[nodiscard]] inline __attribute__((deprecated("use " #REPLACEMENT " instead"))) bool copy_from_user<T>(T*, Userspace<const T*>) \
  99. { \
  100. VERIFY_NOT_REACHED(); \
  101. } \
  102. template<> \
  103. [[nodiscard]] inline __attribute__((deprecated("use " #REPLACEMENT " instead"))) bool copy_from_user<T>(T*, Userspace<T*>) \
  104. { \
  105. VERIFY_NOT_REACHED(); \
  106. }
  107. DEPRECATE_COPY_FROM_USER_TYPE(timespec, copy_time_from_user)
  108. DEPRECATE_COPY_FROM_USER_TYPE(timeval, copy_time_from_user)
  109. template<typename T>
  110. [[nodiscard]] inline bool copy_to_user(Userspace<T*> dest, const T* src)
  111. {
  112. static_assert(is_trivially_copyable<T>());
  113. return copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T));
  114. }
  115. template<typename T>
  116. [[nodiscard]] inline bool copy_to_user(Userspace<T*> dest, const void* src, size_t size)
  117. {
  118. static_assert(is_trivially_copyable<T>());
  119. return copy_to_user(dest.unsafe_userspace_ptr(), src, size);
  120. }
  121. template<typename T>
  122. [[nodiscard]] inline bool copy_from_user(void* dest, Userspace<const T*> src, size_t size)
  123. {
  124. static_assert(is_trivially_copyable<T>());
  125. return copy_from_user(dest, src.unsafe_userspace_ptr(), size);
  126. }
  127. template<typename T>
  128. [[nodiscard]] inline bool copy_n_from_user(T* dest, const T* src, size_t count)
  129. {
  130. static_assert(is_trivially_copyable<T>());
  131. Checked size = sizeof(T);
  132. size *= count;
  133. if (size.has_overflow())
  134. return false;
  135. return copy_from_user(dest, src, sizeof(T) * count);
  136. }
  137. template<typename T>
  138. [[nodiscard]] inline bool copy_n_to_user(T* dest, const T* src, size_t count)
  139. {
  140. static_assert(is_trivially_copyable<T>());
  141. Checked size = sizeof(T);
  142. size *= count;
  143. if (size.has_overflow())
  144. return false;
  145. return copy_to_user(dest, src, sizeof(T) * count);
  146. }
  147. template<typename T>
  148. [[nodiscard]] inline bool copy_n_from_user(T* dest, Userspace<const T*> src, size_t count)
  149. {
  150. static_assert(is_trivially_copyable<T>());
  151. Checked size = sizeof(T);
  152. size *= count;
  153. if (size.has_overflow())
  154. return false;
  155. return copy_from_user(dest, src.unsafe_userspace_ptr(), sizeof(T) * count);
  156. }
  157. template<typename T>
  158. [[nodiscard]] inline bool copy_n_to_user(Userspace<T*> dest, const T* src, size_t count)
  159. {
  160. static_assert(is_trivially_copyable<T>());
  161. Checked size = sizeof(T);
  162. size *= count;
  163. if (size.has_overflow())
  164. return false;
  165. return copy_to_user(dest.unsafe_userspace_ptr(), src, sizeof(T) * count);
  166. }