Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeviceSanitizer] Support "-fsanitize-ignorelist=" to disable sanitizing on some of kernels #15294

Open
wants to merge 36 commits into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
30d18e1
wip
AllanZyne Sep 3, 2024
848587b
wip
AllanZyne Sep 5, 2024
ce5ef1f
wip
AllanZyne Sep 5, 2024
79d226c
Merge branch 'sycl' into review/yang/fix_kernel_filter
AllanZyne Sep 5, 2024
a522451
wip
AllanZyne Sep 5, 2024
f97c8b8
wip
AllanZyne Sep 6, 2024
8684ab7
Merge branch 'sycl' into review/yang/fix_kernel_filter
AllanZyne Oct 29, 2024
0f18783
wip
AllanZyne Oct 29, 2024
fd4feac
wip
AllanZyne Oct 29, 2024
cd1fe71
Merge branch 'sycl' into review/yang/fix_kernel_filter
AllanZyne Oct 29, 2024
6e06fed
wip
AllanZyne Oct 30, 2024
3c60ce4
wip
AllanZyne Oct 30, 2024
8473c58
fix crash
AllanZyne Oct 30, 2024
48716c0
fix crash
AllanZyne Oct 31, 2024
2ba2662
wip
AllanZyne Oct 31, 2024
20f08fe
wip
AllanZyne Nov 6, 2024
18eb60d
wip
AllanZyne Nov 7, 2024
6036d1e
wip
AllanZyne Nov 7, 2024
bba8dfe
Merge branch 'sycl' into review/yang/fix_kernel_filter
AllanZyne Nov 7, 2024
f614a47
wip
AllanZyne Nov 7, 2024
1aa7a67
fix comment
AllanZyne Nov 7, 2024
2e08e44
nit
AllanZyne Nov 7, 2024
7e8641a
nit
AllanZyne Nov 7, 2024
2e148cb
nit
AllanZyne Nov 7, 2024
20ebf54
address comments
AllanZyne Nov 8, 2024
acffb6c
address comments
AllanZyne Nov 8, 2024
dbae6c7
fix test fail
AllanZyne Nov 8, 2024
172aea7
fix test fail
AllanZyne Nov 8, 2024
8740fa6
Merge branch 'sycl' into review/yang/fix_kernel_filter
AllanZyne Nov 8, 2024
458c7ec
fix ESIMD
AllanZyne Nov 14, 2024
59b89ef
Merge branch 'sycl' into review/yang/fix_kernel_filter
AllanZyne Nov 14, 2024
bfd3384
fix lit test
AllanZyne Nov 14, 2024
79a7d21
fix comment
AllanZyne Nov 14, 2024
e9df350
fix sycl-device-global-size
AllanZyne Nov 14, 2024
9a64792
fix sycl-host-access
AllanZyne Nov 14, 2024
6d605ae
fix sycl-host-access
AllanZyne Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clang/lib/Driver/OffloadBundler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,11 @@ class ObjectFileHandler final : public FileHandler {
if (Error Err = Symbol.printName(NameOS))
return std::move(Err);

// If we are dealing with a bitcode file do not add special globals
// llvm.used and llvm.compiler.used to the list of defined symbols.
if (SF->isIR() && (Name == "llvm.used" || Name == "llvm.compiler.used"))
// If we are dealing with a bitcode file do not add special globals to
// the list of defined symbols.
if (SF->isIR() &&
AllanZyne marked this conversation as resolved.
Show resolved Hide resolved
(Name == "llvm.used" || Name == "llvm.compiler.used" ||
Name == "__AsanKernelMetadata"))
continue;

// Add symbol name with the target prefix to the buffer.
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Driver/SanitizerArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,9 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,

CmdArgs.push_back("-mllvm");
CmdArgs.push_back("-asan-mapping-scale=4");

addSpecialCaseListOpt(Args, CmdArgs,
"-fsanitize-ignorelist=", UserIgnorelistFiles);
}
return;
}
Expand Down
29 changes: 29 additions & 0 deletions libdevice/sanitizer_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,16 @@ constexpr size_t AlignMask(size_t n) { return n - 1; }
///
/// ASAN Load/Store Report Built-ins
///
/// NOTE:
/// if __AsanLaunchInfo equals 0, the sanitizer is disabled for this launch
///

#define ASAN_REPORT_ERROR_BASE(type, is_write, size, as) \
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size##_as##as( \
uptr addr, const char __SYCL_CONSTANT__ *file, uint32_t line, \
const char __SYCL_CONSTANT__ *func) { \
if (!__AsanLaunchInfo) \
return; \
if (addr & AlignMask(size)) { \
__asan_report_misalign_error(addr, as, size, is_write, addr, file, line, \
func); \
Expand All @@ -692,6 +697,8 @@ constexpr size_t AlignMask(size_t n) { return n - 1; }
DEVICE_EXTERN_C_NOINLINE void __asan_##type##size##_as##as##_noabort( \
uptr addr, const char __SYCL_CONSTANT__ *file, uint32_t line, \
const char __SYCL_CONSTANT__ *func) { \
if (!__AsanLaunchInfo) \
return; \
if (addr & AlignMask(size)) { \
__asan_report_misalign_error(addr, as, size, is_write, addr, file, line, \
func, true); \
Expand Down Expand Up @@ -724,6 +731,8 @@ ASAN_REPORT_ERROR(store, true, 16)
DEVICE_EXTERN_C_NOINLINE void __asan_##type##N_as##as( \
uptr addr, size_t size, const char __SYCL_CONSTANT__ *file, \
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
if (!__AsanLaunchInfo) \
return; \
if (auto poisoned_addr = __asan_region_is_poisoned(addr, as, size)) { \
__asan_report_access_error(addr, as, size, is_write, poisoned_addr, \
file, line, func); \
Expand All @@ -732,6 +741,8 @@ ASAN_REPORT_ERROR(store, true, 16)
DEVICE_EXTERN_C_NOINLINE void __asan_##type##N_as##as##_noabort( \
uptr addr, size_t size, const char __SYCL_CONSTANT__ *file, \
uint32_t line, const char __SYCL_CONSTANT__ *func) { \
if (!__AsanLaunchInfo) \
return; \
if (auto poisoned_addr = __asan_region_is_poisoned(addr, as, size)) { \
__asan_report_access_error(addr, as, size, is_write, poisoned_addr, \
file, line, func, true); \
Expand All @@ -753,6 +764,9 @@ ASAN_REPORT_ERROR_N(store, true)
///

DEVICE_EXTERN_C_NOINLINE uptr __asan_mem_to_shadow(uptr ptr, uint32_t as) {
if (!__AsanLaunchInfo)
return 0;

return MemToShadow(ptr, as);
}

Expand All @@ -766,6 +780,9 @@ static __SYCL_CONSTANT__ const char __mem_set_shadow_local[] =
DEVICE_EXTERN_C_NOINLINE void
__asan_set_shadow_static_local(uptr ptr, size_t size,
size_t size_with_redzone) {
if (!__AsanLaunchInfo)
return;

// Since ptr is aligned to ASAN_SHADOW_GRANULARITY,
// if size != aligned_size, then the buffer tail of ptr is not aligned
uptr aligned_size = RoundUpTo(size, ASAN_SHADOW_GRANULARITY);
Expand Down Expand Up @@ -805,6 +822,9 @@ static __SYCL_CONSTANT__ const char __mem_unpoison_shadow_static_local_end[] =
DEVICE_EXTERN_C_NOINLINE void
__asan_unpoison_shadow_static_local(uptr ptr, size_t size,
size_t size_with_redzone) {
if (!__AsanLaunchInfo)
return;

ASAN_DEBUG(__spirv_ocl_printf(__mem_unpoison_shadow_static_local_begin));

auto shadow_begin = MemToShadow(ptr + size, ADDRESS_SPACE_LOCAL);
Expand Down Expand Up @@ -838,6 +858,9 @@ static __SYCL_CONSTANT__ const char __mem_report_arg_count_incorrect[] =

DEVICE_EXTERN_C_NOINLINE void
__asan_set_shadow_dynamic_local(uptr ptr, uint32_t num_args) {
if (!__AsanLaunchInfo)
return;

ASAN_DEBUG(__spirv_ocl_printf(__mem_set_shadow_dynamic_local_begin));

auto *launch_info = (__SYCL_GLOBAL__ const LaunchInfo *)__AsanLaunchInfo;
Expand Down Expand Up @@ -869,6 +892,9 @@ static __SYCL_CONSTANT__ const char __mem_unpoison_shadow_dynamic_local_end[] =

DEVICE_EXTERN_C_NOINLINE void
__asan_unpoison_shadow_dynamic_local(uptr ptr, uint32_t num_args) {
if (!__AsanLaunchInfo)
return;

ASAN_DEBUG(__spirv_ocl_printf(__mem_unpoison_shadow_dynamic_local_begin));

auto *launch_info = (__SYCL_GLOBAL__ const LaunchInfo *)__AsanLaunchInfo;
Expand Down Expand Up @@ -905,6 +931,9 @@ static __SYCL_CONSTANT__ const char __mem_set_shadow_private[] =

DEVICE_EXTERN_C_NOINLINE void __asan_set_shadow_private(uptr begin, uptr size,
char val) {
if (!__AsanLaunchInfo)
return;

ASAN_DEBUG(__spirv_ocl_printf(__mem_set_shadow_private_begin));

auto *launch_info = (__SYCL_GLOBAL__ const LaunchInfo *)__AsanLaunchInfo;
Expand Down
23 changes: 23 additions & 0 deletions llvm/include/llvm/SYCLLowerIR/AsanKernelMetadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- AsanKernelMetadata.h - fix kernel medatadata for sanitizer ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// This pass fixes attributes and metadata of the global variable
// "__AsanKernelMetadata"
//===----------------------------------------------------------------------===//

#pragma once

#include "llvm/IR/PassManager.h"
AllanZyne marked this conversation as resolved.
Show resolved Hide resolved

namespace llvm {

class AsanKernelMetadataPass : public PassInfoMixin<AsanKernelMetadataPass> {
public:
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
};

} // namespace llvm
63 changes: 63 additions & 0 deletions llvm/lib/SYCLLowerIR/AsanKernelMetadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===-- AsanKernelMetadata.cpp - fix kernel medatadata for sanitizer -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// This pass fixes attributes and metadata of global variable
// "__AsanKernelMetadata".
// We treat "__AsanKernelMetadata" as a device global variable, so that it can
// be read by runtime.
// "spirv.Decorations" is removed by llvm-link, so we add it here again.
//===----------------------------------------------------------------------===//

#include "llvm/SYCLLowerIR/AsanKernelMetadata.h"

#include "llvm/IR/IRBuilder.h"

#define DEBUG_TYPE "AsanKernelMetadata"

using namespace llvm;

namespace llvm {

constexpr StringRef SPIRV_DECOR_MD_KIND = "spirv.Decorations";
constexpr uint32_t SPIRV_HOST_ACCESS_DECOR = 6147;

PreservedAnalyses AsanKernelMetadataPass::run(Module &M,
ModuleAnalysisManager &MAM) {
auto *KernelMetadata = M.getNamedGlobal("__AsanKernelMetadata");
if (!KernelMetadata) {
return PreservedAnalyses::all();
}

auto &DL = M.getDataLayout();
auto &Ctx = M.getContext();

// Fix attributes
KernelMetadata->addAttribute(
"sycl-device-global-size",
std::to_string(DL.getTypeAllocSize(KernelMetadata->getValueType())));

// Fix metadata
unsigned MDKindID = Ctx.getMDKindID(SPIRV_DECOR_MD_KIND);

SmallVector<Metadata *, 1> MDOps;

SmallVector<Metadata *, 3> MD;
auto *Ty = Type::getInt32Ty(Ctx);
MD.push_back(ConstantAsMetadata::get(
Constant::getIntegerValue(Ty, APInt(32, SPIRV_HOST_ACCESS_DECOR))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

host access mode should be read only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! done.

MD.push_back(
ConstantAsMetadata::get(Constant::getIntegerValue(Ty, APInt(32, 0))));
MD.push_back(MDString::get(Ctx, "_Z20__AsanKernelMetadata"));

MDOps.push_back(MDNode::get(Ctx, MD));

KernelMetadata->addMetadata(MDKindID, *MDNode::get(Ctx, MDOps));

return PreservedAnalyses::none();
}

} // namespace llvm
1 change: 1 addition & 0 deletions llvm/lib/SYCLLowerIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ add_llvm_component_library(LLVMSYCLLowerIR
SYCLVirtualFunctionsAnalysis.cpp
SYCLUtils.cpp
SanitizeDeviceGlobal.cpp
AsanKernelMetadata.cpp

LocalAccessorToSharedMemory.cpp
GlobalOffset.cpp
Expand Down
11 changes: 1 addition & 10 deletions llvm/lib/SYCLLowerIR/ComputeModuleRuntimeInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,7 @@ getSYCLESIMDSplitStatusFromMetadata(const Module &M) {
} // namespace

bool isModuleUsingAsan(const Module &M) {
for (const auto &F : M) {
if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
continue;
if (F.arg_size() == 0)
continue;
const auto *LastArg = F.getArg(F.arg_size() - 1);
if (LastArg->getName() == "__asan_launch")
return true;
}
return false;
return M.getNamedGlobal("__AsanKernelMetadata");
}

// This function traverses over reversed call graph by BFS algorithm.
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/SYCLLowerIR/SanitizeDeviceGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ static bool instrumentDeviceGlobal(Module &M) {
if (!isDeviceGlobalVariable(G) || !hasDeviceImageScopeProperty(G))
continue;

// Skip instrumenting on "__AsanKernelMetadata" etc.
if (G.getName().starts_with("__Asan"))
sarnex marked this conversation as resolved.
Show resolved Hide resolved
continue;

Type *Ty = G.getValueType();
const uint64_t SizeInBytes = DL.getTypeAllocSize(Ty);
const uint64_t RightRedzoneSize = [&] {
Expand Down
Loading
Loading