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 | p: reinterpret_cast<uptr>(block), sz: size); |
27 | return memset(s: block, c: c, n: size); |
28 | } |
29 | |
30 | void *__hwasan_memcpy(void *to, const void *from, uptr size) { |
31 | CheckAddressSized<ErrorAction::Recover, AccessType::Store>( |
32 | p: reinterpret_cast<uptr>(to), sz: size); |
33 | CheckAddressSized<ErrorAction::Recover, AccessType::Load>( |
34 | p: reinterpret_cast<uptr>(from), sz: size); |
35 | return memcpy(dest: to, src: from, n: size); |
36 | } |
37 | |
38 | void *__hwasan_memmove(void *to, const void *from, uptr size) { |
39 | CheckAddressSized<ErrorAction::Recover, AccessType::Store>( |
40 | p: reinterpret_cast<uptr>(to), sz: size); |
41 | CheckAddressSized<ErrorAction::Recover, AccessType::Load>( |
42 | p: reinterpret_cast<uptr>(from), sz: size); |
43 | return memmove(dest: to, src: from, n: size); |
44 | } |
45 | |
46 | void *__hwasan_memset_match_all(void *block, int c, uptr size, |
47 | u8 match_all_tag) { |
48 | if (GetTagFromPointer(p: reinterpret_cast<uptr>(block)) != match_all_tag) |
49 | CheckAddressSized<ErrorAction::Recover, AccessType::Store>( |
50 | p: reinterpret_cast<uptr>(block), sz: size); |
51 | return memset(s: block, c: c, n: size); |
52 | } |
53 | |
54 | void *__hwasan_memcpy_match_all(void *to, const void *from, uptr size, |
55 | u8 match_all_tag) { |
56 | if (GetTagFromPointer(p: reinterpret_cast<uptr>(to)) != match_all_tag) |
57 | CheckAddressSized<ErrorAction::Recover, AccessType::Store>( |
58 | p: reinterpret_cast<uptr>(to), sz: size); |
59 | if (GetTagFromPointer(p: reinterpret_cast<uptr>(from)) != match_all_tag) |
60 | CheckAddressSized<ErrorAction::Recover, AccessType::Load>( |
61 | p: reinterpret_cast<uptr>(from), sz: size); |
62 | return memcpy(dest: to, src: from, n: size); |
63 | } |
64 | |
65 | void *__hwasan_memmove_match_all(void *to, const void *from, uptr size, |
66 | u8 match_all_tag) { |
67 | if (GetTagFromPointer(p: reinterpret_cast<uptr>(to)) != match_all_tag) |
68 | CheckAddressSized<ErrorAction::Recover, AccessType::Store>( |
69 | p: reinterpret_cast<uptr>(to), sz: size); |
70 | if (GetTagFromPointer(p: reinterpret_cast<uptr>(from)) != match_all_tag) |
71 | CheckAddressSized<ErrorAction::Recover, AccessType::Load>( |
72 | p: reinterpret_cast<uptr>(from), sz: size); |
73 | return memmove(dest: to, src: from, n: size); |
74 | } |
75 | |