72 changes: 72 additions & 0 deletions compiler-rt/lib/esan/esan_interface_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===-- esan_interface_internal.h -------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of EfficiencySanitizer, a family of performance tuners.
//
// Calls to the functions declared in this header will be inserted by
// the instrumentation module.
//===----------------------------------------------------------------------===//

#ifndef ESAN_INTERFACE_INTERNAL_H
#define ESAN_INTERFACE_INTERNAL_H

#include <sanitizer_common/sanitizer_internal_defs.h>

// This header should NOT include any other headers.
// All functions in this header are extern "C" and start with __esan_.

extern "C" {

// This should be kept consistent with LLVM's EfficiencySanitizerOptions.
// The value is passed as a 32-bit integer by the compiler.
typedef enum Type : u32 {
ESAN_None = 0,
ESAN_CacheFrag,
} ToolType;

// This function should be called at the very beginning of the process,
// before any instrumented code is executed and before any call to malloc.
SANITIZER_INTERFACE_ATTRIBUTE void __esan_init(ToolType Tool);

// The instrumentation module will insert a call to one of these routines prior
// to each load and store instruction for which we do not have "fastpath"
// inlined instrumentation. These calls constitute the "slowpath" for our
// tools. We have separate routines for each type of memory access to enable
// targeted optimization.
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_load1(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_load2(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_load4(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_load8(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_load16(void *Addr);

SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_store1(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_store2(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_store4(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_store8(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_aligned_store16(void *Addr);

SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_load2(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_load4(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_load8(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_load16(void *Addr);

SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_store2(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_store4(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_store8(void *Addr);
SANITIZER_INTERFACE_ATTRIBUTE void __esan_unaligned_store16(void *Addr);

// These cover unusually-sized accesses.
SANITIZER_INTERFACE_ATTRIBUTE
void __esan_unaligned_loadN(void *Addr, uptr Size);
SANITIZER_INTERFACE_ATTRIBUTE
void __esan_unaligned_storeN(void *Addr, uptr Size);

} // extern "C"

#endif // ESAN_INTERFACE_INTERNAL_H
3 changes: 3 additions & 0 deletions compiler-rt/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS)
if(COMPILER_RT_HAS_SAFESTACK)
add_subdirectory(safestack)
endif()
if(COMPILER_RT_HAS_ESAN)
add_subdirectory(esan)
endif()
endif()

if(COMPILER_RT_STANDALONE_BUILD)
Expand Down
32 changes: 32 additions & 0 deletions compiler-rt/test/esan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
set(ESAN_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
if(NOT COMPILER_RT_STANDALONE_BUILD)
list(APPEND ESAN_TEST_DEPS esan)
endif()

set(ESAN_TESTSUITES)

set(ESAN_TEST_ARCH ${ESAN_SUPPORTED_ARCH})

set(ESAN_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

foreach(arch ${ESAN_TEST_ARCH})
set(ESAN_TEST_TARGET_ARCH ${arch})
string(TOLOWER "-${arch}" ESAN_TEST_CONFIG_SUFFIX)
get_target_flags_for_arch(${arch} ESAN_TEST_TARGET_CFLAGS)
string(REPLACE ";" " " ESAN_TEST_TARGET_CFLAGS "${ESAN_TEST_TARGET_CFLAGS}")

string(TOUPPER ${arch} ARCH_UPPER_CASE)
set(CONFIG_NAME ${ARCH_UPPER_CASE}Config)

configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg)
list(APPEND ESAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
endforeach()

# TODO(bruening): add Unit/ tests as well

add_lit_testsuite(check-esan "Running EfficiencySanitizer tests"
${ESAN_TESTSUITES}
DEPENDS ${ESAN_TEST_DEPS})
set_target_properties(check-esan PROPERTIES FOLDER "Esan tests")
11 changes: 11 additions & 0 deletions compiler-rt/test/esan/TestCases/verbose-simple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_esan_frag -O0 %s -o %t 2>&1
// RUN: %env_esan_opts=verbosity=1 %run %t 2>&1 | FileCheck %s

#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
// CHECK: in esan::initializeLibrary
// CHECK-NEXT: in esan::finalizeLibrary
// CHECK-NEXT: {{.*}}EfficiencySanitizer is not finished: nothing yet to report
return 0;
}
32 changes: 32 additions & 0 deletions compiler-rt/test/esan/lit.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- Python -*-

import os

# Setup config name.
config.name = 'EfficiencySanitizer' + config.name_suffix

# Setup source root.
config.test_source_root = os.path.dirname(__file__)

# Setup default compiler flags used with -fsanitize=efficiency option.
base_cflags = ([config.target_cflags] + config.debug_info_flags)
base_cxxflags = config.cxx_mode_flags + base_cflags

frag_cflags = (["-fsanitize=efficiency-cache-frag"] + base_cflags)

def build_invocation(compile_flags):
return " " + " ".join([config.clang] + compile_flags) + " "

config.substitutions.append( ("%clang_esan_frag ",
build_invocation(frag_cflags)) )

default_esan_opts = ''
config.substitutions.append(('%env_esan_opts=',
'env ESAN_OPTIONS=' + default_esan_opts))

# Default test suffixes.
config.suffixes = ['.c', '.cpp']

# EfficiencySanitizer tests are currently supported on Linux x86-64 only.
if config.host_os not in ['Linux'] or config.target_arch != 'x86_64':
config.unsupported = True
14 changes: 14 additions & 0 deletions compiler-rt/test/esan/lit.site.cfg.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Autogenerated by LLVM/Clang configuration.
# Do not edit!

# Tool-specific config options.
config.name_suffix = "@ESAN_TEST_CONFIG_SUFFIX@"
config.esan_lit_source_dir = "@ESAN_LIT_SOURCE_DIR@"
config.target_cflags = "@ESAN_TEST_TARGET_CFLAGS@"
config.target_arch = "@ESAN_TEST_TARGET_ARCH@"

# Load common config for all compiler-rt lit tests.
lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")

# Load tool-specific config that would do the real work.
lit_config.load_config(config, "@ESAN_LIT_SOURCE_DIR@/lit.cfg")