[PATCH] D149943: [hwasan] support hwasan-match-all-tag flag for hwasan meminstrinsic calls

Mingjie Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 5 03:13:28 PDT 2023


Enna1 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Enna1 added a comment.
Enna1 added reviewers: vitalybuka, eugenis, pcc, melver.
Enna1 added a subscriber: MTC.
Enna1 published this revision for review.
Herald added projects: Sanitizers, LLVM.
Herald added subscribers: llvm-commits, Sanitizers.

D149580 <https://reviews.llvm.org/D149580> and this patch implement hwasan-match-all-tag flag support for hwasan callback memaccess instrumentation and memset, memcpy, memmove meminstrinsic calls.
And with D149580 <https://reviews.llvm.org/D149580> and this patch, if hwasan-match-all-tag is set,  calls to `__hwasan_load`, `__hwasan_store`, `____hwasan_memset`, `__hwasan_memcpy` and `__hwasan_memmove` will be replaced with their match_all versions: `__hwasan_load_match_all`, `__hwasan_store_match_all`, `__hwasan_memset_match_all`, `__hwasan_memcpy_match_all` and `__hwasan_memmove_match_all`.
When HWAddressSanitizer compiling in kernel mode, hwasan-match-all-tag is implicitly set to 0xFF.
I'm not familiar with kernel mode HWASAN.
But from https://github.com/torvalds/linux/blob/master/mm/kasan/sw_tags.c#L88, I see pointers tagged with 0xff is already ignored in `__hwasan_load`, `__hwasan_store`.
So should we distinguish kernel mode HWASAN and user-space HWASAN for hwasan-match-all-tag flag ? :

- for user-space HWASAN, when hwasan-match-all-tag flag is set,  we emit calls to  `__hwasan_load_match_all`, `__hwasan_memset_match_all`, etc.
- for kernel mode HWASAN, hwasan-match-all-tag is implicitly set to 0xFF,  we emit calls to  `__hwasan_load`, `__hwasan_memset`, etc.

Or if hwasan-match-all-tag is set, no matter user-space HWASAN or kernel mode HWASAN, we always emit  calls to  `__hwasan_load_match_all`,`__hwasan_memset_match_all`, and implement `__hwasan_load_match_all`, `__hwasan_memset_match_all` in kernel HWASAN runtime ?



================
Comment at: llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:662-665
   const std::string MemIntrinCallbackPrefix =
       (CompileKernel && !ClKasanMemIntrinCallbackPrefix)
           ? std::string("")
           : ClMemoryAccessCallbackPrefix;
----------------
Introduced in https://reviews.llvm.org/D122724, for supporting HWAddressSanitizer compiling in kernel mode


================
Comment at: llvm/test/Instrumentation/HWAddressSanitizer/mem-intrinsics.ll:19-20
   call void @llvm.memset.p0.i64(ptr align 1 %Q, i8 0, i64 10, i1 false)
 ; CHECK-PREFIX: call ptr @__hwasan_memset
 ; CHECK-NOPREFIX: call ptr @memset
+; CHECK-MATCH-ALL-TAG: call ptr @__hwasan_memset_match_all(ptr %Q.hwasan, i32 0, i64 10, i8 0)
----------------
`-hwasan-kernel` implicit set match-all-tag to  0xff. 
With this change,  `opt -S -passes=hwasan -hwasan-kernel` will emit:
```
call ptr @__hwasan_memset_match_all
call ptr @memset_match_all
```
Just happen to pass these checks.


This patch implements `__hwasan_memset_match_all`, `__hwasan_memcpy_match_all` and `__hwasan_memmove_match_all`, making hwasan-match-all-tag flag working for hwasan versions of memset, memcpy and memmove.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149943

Files:
  compiler-rt/lib/hwasan/hwasan_interface_internal.h
  compiler-rt/lib/hwasan/hwasan_memintrinsics.cpp
  llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
  llvm/test/Instrumentation/HWAddressSanitizer/mem-intrinsics.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149943.519780.patch
Type: text/x-patch
Size: 9308 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230505/4d38762d/attachment.bin>


More information about the llvm-commits mailing list