Skip to content

Commit c9dd299

Browse files
committed
[GWP-ASan] Initial build files, implementation of PRNG [1].
Summary: See D60593 for further information. This patch slices off the PRNG implementation and the initial build files for GWP-ASan. Reviewers: vlad.tsyrklevich, morehouse, vitalybuka Reviewed By: morehouse Subscribers: srhines, kubamracek, mgorny, #sanitizers, llvm-commits, cryptoad, eugenis Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D61867 llvm-svn: 360710
1 parent b26592e commit c9dd299

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

compiler-rt/cmake/config-ix.cmake

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ else()
246246
set(ALL_FUZZER_SUPPORTED_ARCH ${X86_64} ${ARM64})
247247
endif()
248248

249+
set(ALL_GWP_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM64} ${ARM32})
249250
if(APPLE)
250251
set(ALL_LSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${MIPS64} ${ARM64})
251252
else()
@@ -445,6 +446,9 @@ if(APPLE)
445446
list_intersect(DFSAN_SUPPORTED_ARCH
446447
ALL_DFSAN_SUPPORTED_ARCH
447448
SANITIZER_COMMON_SUPPORTED_ARCH)
449+
list_intersect(GWP_ASAN_SUPPORTED_ARCH
450+
ALL_GWP_ASAN_SUPPORTED_ARCH
451+
SANITIZER_COMMON_SUPPORTED_ARCH)
448452
list_intersect(LSAN_SUPPORTED_ARCH
449453
ALL_LSAN_SUPPORTED_ARCH
450454
SANITIZER_COMMON_SUPPORTED_ARCH)
@@ -513,6 +517,7 @@ else()
513517
filter_available_targets(XRAY_SUPPORTED_ARCH ${ALL_XRAY_SUPPORTED_ARCH})
514518
filter_available_targets(SHADOWCALLSTACK_SUPPORTED_ARCH
515519
${ALL_SHADOWCALLSTACK_SUPPORTED_ARCH})
520+
filter_available_targets(GWP_ASAN_SUPPORTED_ARCH ${ALL_GWP_ASAN_SUPPORTED_ARCH})
516521
endif()
517522

518523
if (MSVC)
@@ -534,7 +539,7 @@ if(COMPILER_RT_SUPPORTED_ARCH)
534539
endif()
535540
message(STATUS "Compiler-RT supported architectures: ${COMPILER_RT_SUPPORTED_ARCH}")
536541

537-
set(ALL_SANITIZERS asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo;ubsan_minimal)
542+
set(ALL_SANITIZERS asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo;ubsan_minimal;gwp_asan)
538543
set(COMPILER_RT_SANITIZERS_TO_BUILD all CACHE STRING
539544
"sanitizers to build if supported on the target (all;${ALL_SANITIZERS})")
540545
list_replace(COMPILER_RT_SANITIZERS_TO_BUILD all "${ALL_SANITIZERS}")
@@ -678,3 +683,13 @@ if (COMPILER_RT_HAS_SANITIZER_COMMON AND SHADOWCALLSTACK_SUPPORTED_ARCH AND
678683
else()
679684
set(COMPILER_RT_HAS_SHADOWCALLSTACK FALSE)
680685
endif()
686+
687+
# Note: Fuchsia and Windows are not currently supported by GWP-ASan. Support
688+
# is planned for these platforms. Darwin is also not supported due to TLS
689+
# calling malloc on first use.
690+
if (GWP_ASAN_SUPPORTED_ARCH AND OS_NAME MATCHES "Android|Linux")
691+
set(COMPILER_RT_HAS_GWP_ASAN TRUE)
692+
else()
693+
set(COMPILER_RT_HAS_GWP_ASAN FALSE)
694+
endif()
695+
pythonize_bool(COMPILER_RT_HAS_GWP_ASAN)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
add_compiler_rt_component(gwp_asan)
2+
3+
include_directories(..)
4+
5+
set(GWP_ASAN_SOURCES
6+
random.cpp
7+
)
8+
9+
set(GWP_ASAN_HEADERS
10+
random.h
11+
)
12+
13+
# Disable RTTI and exception support, as we want these libraries to be
14+
# C-compatible. Regular C source files can be linked against the generated
15+
# GwpAsan libraries using the Clang C compiler.
16+
set(GWP_ASAN_CFLAGS -fno-rtti -fno-exceptions)
17+
18+
if (COMPILER_RT_HAS_GWP_ASAN)
19+
foreach(arch ${GWP_ASAN_SUPPORTED_ARCH})
20+
add_compiler_rt_runtime(
21+
clang_rt.gwp_asan
22+
STATIC
23+
ARCHS ${arch}
24+
SOURCES ${GWP_ASAN_SOURCES}
25+
ADDITIONAL_HEADERS ${GWP_ASAN_HEADERS}
26+
CFLAGS ${GWP_ASAN_CFLAGS}
27+
PARENT_TARGET gwp_asan
28+
)
29+
endforeach()
30+
31+
add_compiler_rt_object_libraries(RTGwpAsan
32+
ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
33+
SOURCES ${GWP_ASAN_SOURCES}
34+
ADDITIONAL_HEADERS ${GWP_ASAN_HEADERS}
35+
CFLAGS ${GWP_ASAN_CFLAGS})
36+
endif()

compiler-rt/lib/gwp_asan/random.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- random.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+
#include "gwp_asan/random.h"
10+
11+
#include <ctime>
12+
13+
namespace gwp_asan {
14+
uint32_t getRandomUnsigned32() {
15+
thread_local uint32_t RandomState = static_cast<uint64_t>(time(nullptr));
16+
RandomState ^= RandomState << 13;
17+
RandomState ^= RandomState >> 17;
18+
RandomState ^= RandomState << 5;
19+
return RandomState;
20+
}
21+
} // namespace gwp_asan

compiler-rt/lib/gwp_asan/random.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- random.h ------------------------------------------------*- 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+
#ifndef GWP_ASAN_RANDOM_H_
10+
#define GWP_ASAN_RANDOM_H_
11+
12+
#include <cstdint>
13+
14+
namespace gwp_asan {
15+
// xorshift (32-bit output), extremely fast PRNG that uses arithmetic operations
16+
// only. Seeded using walltime.
17+
uint32_t getRandomUnsigned32();
18+
} // namespace gwp_asan
19+
20+
#endif // GWP_ASAN_RANDOM_H_

compiler-rt/test/gwp_asan/CMakeLists.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)