hwasan_memintrinsics.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //===-- hwasan_memintrinsics.cpp --------------------------------*- C++ -*-===//
  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. /// \file
  10. /// This file is a part of HWAddressSanitizer and contains HWASAN versions of
  11. /// memset, memcpy and memmove
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include <string.h>
  15. #include "hwasan.h"
  16. #include "hwasan_checks.h"
  17. #include "hwasan_flags.h"
  18. #include "hwasan_interface_internal.h"
  19. #include "sanitizer_common/sanitizer_libc.h"
  20. using namespace __hwasan;
  21. void *__hwasan_memset(void *block, int c, uptr size) {
  22. CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
  23. reinterpret_cast<uptr>(block), size);
  24. return memset(block, c, size);
  25. }
  26. void *__hwasan_memcpy(void *to, const void *from, uptr size) {
  27. CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
  28. reinterpret_cast<uptr>(to), size);
  29. CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
  30. reinterpret_cast<uptr>(from), size);
  31. return memcpy(to, from, size);
  32. }
  33. void *__hwasan_memmove(void *to, const void *from, uptr size) {
  34. CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
  35. reinterpret_cast<uptr>(to), size);
  36. CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
  37. reinterpret_cast<uptr>(from), size);
  38. return memmove(to, from, size);
  39. }
  40. void *__hwasan_memset_match_all(void *block, int c, uptr size,
  41. u8 match_all_tag) {
  42. if (GetTagFromPointer(reinterpret_cast<uptr>(block)) != match_all_tag)
  43. CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
  44. reinterpret_cast<uptr>(block), size);
  45. return memset(block, c, size);
  46. }
  47. void *__hwasan_memcpy_match_all(void *to, const void *from, uptr size,
  48. u8 match_all_tag) {
  49. if (GetTagFromPointer(reinterpret_cast<uptr>(to)) != match_all_tag)
  50. CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
  51. reinterpret_cast<uptr>(to), size);
  52. if (GetTagFromPointer(reinterpret_cast<uptr>(from)) != match_all_tag)
  53. CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
  54. reinterpret_cast<uptr>(from), size);
  55. return memcpy(to, from, size);
  56. }
  57. void *__hwasan_memmove_match_all(void *to, const void *from, uptr size,
  58. u8 match_all_tag) {
  59. if (GetTagFromPointer(reinterpret_cast<uptr>(to)) != match_all_tag)
  60. CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
  61. reinterpret_cast<uptr>(to), size);
  62. if (GetTagFromPointer(reinterpret_cast<uptr>(from)) != match_all_tag)
  63. CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
  64. reinterpret_cast<uptr>(from), size);
  65. return memmove(to, from, size);
  66. }