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 
15 #include <string.h>
16 #include "hwasan.h"
17 #include "hwasan_checks.h"
18 #include "hwasan_flags.h"
19 #include "hwasan_interface_internal.h"
20 #include "sanitizer_common/sanitizer_libc.h"
21 
22 using namespace __hwasan;
23 
24 void *__hwasan_memset(void *block, int c, uptr size) {
25   CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
26       reinterpret_cast<uptr>(block), size);
27   return memset(block, c, size);
28 }
29 
30 void *__hwasan_memcpy(void *to, const void *from, uptr size) {
31   CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
32       reinterpret_cast<uptr>(to), size);
33   CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
34       reinterpret_cast<uptr>(from), size);
35   return memcpy(to, from, size);
36 }
37 
38 void *__hwasan_memmove(void *to, const void *from, uptr size) {
39   CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
40       reinterpret_cast<uptr>(to), size);
41   CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
42       reinterpret_cast<uptr>(from), size);
43   return memmove(to, from, size);
44 }
45 
46 void *__hwasan_memset_match_all(void *block, int c, uptr size,
47                                 u8 match_all_tag) {
48   if (GetTagFromPointer(reinterpret_cast<uptr>(block)) != match_all_tag)
49     CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
50         reinterpret_cast<uptr>(block), size);
51   return memset(block, c, size);
52 }
53 
54 void *__hwasan_memcpy_match_all(void *to, const void *from, uptr size,
55                                 u8 match_all_tag) {
56   if (GetTagFromPointer(reinterpret_cast<uptr>(to)) != match_all_tag)
57     CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
58         reinterpret_cast<uptr>(to), size);
59   if (GetTagFromPointer(reinterpret_cast<uptr>(from)) != match_all_tag)
60     CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
61         reinterpret_cast<uptr>(from), size);
62   return memcpy(to, from, size);
63 }
64 
65 void *__hwasan_memmove_match_all(void *to, const void *from, uptr size,
66                                  u8 match_all_tag) {
67   if (GetTagFromPointer(reinterpret_cast<uptr>(to)) != match_all_tag)
68     CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
69         reinterpret_cast<uptr>(to), size);
70   if (GetTagFromPointer(reinterpret_cast<uptr>(from)) != match_all_tag)
71     CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
72         reinterpret_cast<uptr>(from), size);
73   return memmove(to, from, size);
74 }
75