1e3b6d110SNico Weber //===-- hwasan_memintrinsics.cpp --------------------------------*- C++ -*-===//
2e3b6d110SNico Weber //
3e3b6d110SNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e3b6d110SNico Weber // See https://llvm.org/LICENSE.txt for license information.
5e3b6d110SNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e3b6d110SNico Weber //
7e3b6d110SNico Weber //===----------------------------------------------------------------------===//
8e3b6d110SNico Weber ///
9e3b6d110SNico Weber /// \file
10e3b6d110SNico Weber /// This file is a part of HWAddressSanitizer and contains HWASAN versions of
11e3b6d110SNico Weber /// memset, memcpy and memmove
12e3b6d110SNico Weber ///
13e3b6d110SNico Weber //===----------------------------------------------------------------------===//
14e3b6d110SNico Weber
15e3b6d110SNico Weber #include <string.h>
16e3b6d110SNico Weber #include "hwasan.h"
17e3b6d110SNico Weber #include "hwasan_checks.h"
18e3b6d110SNico Weber #include "hwasan_flags.h"
19e3b6d110SNico Weber #include "hwasan_interface_internal.h"
20e3b6d110SNico Weber #include "sanitizer_common/sanitizer_libc.h"
21e3b6d110SNico Weber
22e3b6d110SNico Weber using namespace __hwasan;
23e3b6d110SNico Weber
__hwasan_memset(void * block,int c,uptr size)24e3b6d110SNico Weber void *__hwasan_memset(void *block, int c, uptr size) {
25e3b6d110SNico Weber CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
26e3b6d110SNico Weber reinterpret_cast<uptr>(block), size);
2796a4167bSMatt Morehouse return memset(block, c, size);
28e3b6d110SNico Weber }
29e3b6d110SNico Weber
__hwasan_memcpy(void * to,const void * from,uptr size)30e3b6d110SNico Weber void *__hwasan_memcpy(void *to, const void *from, uptr size) {
31e3b6d110SNico Weber CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
32e3b6d110SNico Weber reinterpret_cast<uptr>(to), size);
33e3b6d110SNico Weber CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
34e3b6d110SNico Weber reinterpret_cast<uptr>(from), size);
3596a4167bSMatt Morehouse return memcpy(to, from, size);
36e3b6d110SNico Weber }
37e3b6d110SNico Weber
__hwasan_memmove(void * to,const void * from,uptr size)38e3b6d110SNico Weber void *__hwasan_memmove(void *to, const void *from, uptr size) {
39e3b6d110SNico Weber CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
40e3b6d110SNico Weber reinterpret_cast<uptr>(to), size);
41e3b6d110SNico Weber CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
42e3b6d110SNico Weber reinterpret_cast<uptr>(from), size);
43f7c28332SMatt Morehouse return memmove(to, from, size);
44e3b6d110SNico Weber }
45*ba13e1b4SEnna1
__hwasan_memset_match_all(void * block,int c,uptr size,u8 match_all_tag)46*ba13e1b4SEnna1 void *__hwasan_memset_match_all(void *block, int c, uptr size,
47*ba13e1b4SEnna1 u8 match_all_tag) {
48*ba13e1b4SEnna1 if (GetTagFromPointer(reinterpret_cast<uptr>(block)) != match_all_tag)
49*ba13e1b4SEnna1 CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
50*ba13e1b4SEnna1 reinterpret_cast<uptr>(block), size);
51*ba13e1b4SEnna1 return memset(block, c, size);
52*ba13e1b4SEnna1 }
53*ba13e1b4SEnna1
__hwasan_memcpy_match_all(void * to,const void * from,uptr size,u8 match_all_tag)54*ba13e1b4SEnna1 void *__hwasan_memcpy_match_all(void *to, const void *from, uptr size,
55*ba13e1b4SEnna1 u8 match_all_tag) {
56*ba13e1b4SEnna1 if (GetTagFromPointer(reinterpret_cast<uptr>(to)) != match_all_tag)
57*ba13e1b4SEnna1 CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
58*ba13e1b4SEnna1 reinterpret_cast<uptr>(to), size);
59*ba13e1b4SEnna1 if (GetTagFromPointer(reinterpret_cast<uptr>(from)) != match_all_tag)
60*ba13e1b4SEnna1 CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
61*ba13e1b4SEnna1 reinterpret_cast<uptr>(from), size);
62*ba13e1b4SEnna1 return memcpy(to, from, size);
63*ba13e1b4SEnna1 }
64*ba13e1b4SEnna1
__hwasan_memmove_match_all(void * to,const void * from,uptr size,u8 match_all_tag)65*ba13e1b4SEnna1 void *__hwasan_memmove_match_all(void *to, const void *from, uptr size,
66*ba13e1b4SEnna1 u8 match_all_tag) {
67*ba13e1b4SEnna1 if (GetTagFromPointer(reinterpret_cast<uptr>(to)) != match_all_tag)
68*ba13e1b4SEnna1 CheckAddressSized<ErrorAction::Recover, AccessType::Store>(
69*ba13e1b4SEnna1 reinterpret_cast<uptr>(to), size);
70*ba13e1b4SEnna1 if (GetTagFromPointer(reinterpret_cast<uptr>(from)) != match_all_tag)
71*ba13e1b4SEnna1 CheckAddressSized<ErrorAction::Recover, AccessType::Load>(
72*ba13e1b4SEnna1 reinterpret_cast<uptr>(from), size);
73*ba13e1b4SEnna1 return memmove(to, from, size);
74*ba13e1b4SEnna1 }
75