Commit 99c04341 authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

base: Add ASAN and MSAN support

- Move ASAN and MSAN support to base/
- Eval arguments for builds without sanitizer to preserve same
  semantics
- IWYU fixes

Bug: chromium:1056170
Change-Id: I212becf205e03b155c188ffd13cf5629df6f2cb8
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2851887Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74225}
parent 6083aeff
......@@ -2999,8 +2999,6 @@ v8_header_set("v8_internal_headers") {
"src/roots/roots.h",
"src/runtime/runtime-utils.h",
"src/runtime/runtime.h",
"src/sanitizer/asan.h",
"src/sanitizer/msan.h",
"src/sanitizer/tsan.h",
"src/snapshot/code-serializer.h",
"src/snapshot/context-deserializer.h",
......@@ -4617,8 +4615,10 @@ v8_component("v8_libbase") {
"src/base/safe_conversions.h",
"src/base/safe_conversions_arm_impl.h",
"src/base/safe_conversions_impl.h",
"src/base/sanitizer/asan.h",
"src/base/sanitizer/lsan-page-allocator.cc",
"src/base/sanitizer/lsan-page-allocator.h",
"src/base/sanitizer/msan.h",
"src/base/small-vector.h",
"src/base/sys-info.cc",
"src/base/sys-info.h",
......
......@@ -4,25 +4,34 @@
// AddressSanitizer support.
#ifndef V8_SANITIZER_ASAN_H_
#define V8_SANITIZER_ASAN_H_
#ifndef V8_BASE_SANITIZER_ASAN_H_
#define V8_BASE_SANITIZER_ASAN_H_
#include <type_traits>
#include "src/base/macros.h"
#include "src/common/globals.h"
#ifdef V8_USE_ADDRESS_SANITIZER
#include <sanitizer/asan_interface.h>
#if !defined(ASAN_POISON_MEMORY_REGION) || !defined(ASAN_UNPOISON_MEMORY_REGION)
#error \
"ASAN_POISON_MEMORY_REGION and ASAN_UNPOISON_MEMORY_REGION must be defined"
#endif
#else // !V8_USE_ADDRESS_SANITIZER
#define ASAN_POISON_MEMORY_REGION(start, size) \
static_assert(std::is_pointer<decltype(start)>::value && \
std::is_convertible<decltype(size), size_t>::value, \
"static type violation")
#define ASAN_POISON_MEMORY_REGION(start, size) \
static_assert(std::is_pointer<decltype(start)>::value, \
"static type violation"); \
static_assert(std::is_convertible<decltype(size), size_t>::value, \
"static type violation"); \
USE(start, size)
#define ASAN_UNPOISON_MEMORY_REGION(start, size) \
ASAN_POISON_MEMORY_REGION(start, size)
#endif // V8_USE_ADDRESS_SANITIZER
#endif // !V8_USE_ADDRESS_SANITIZER
#endif // V8_SANITIZER_ASAN_H_
#endif // V8_BASE_SANITIZER_ASAN_H_
......@@ -4,33 +4,37 @@
// MemorySanitizer support.
#ifndef V8_SANITIZER_MSAN_H_
#define V8_SANITIZER_MSAN_H_
#ifndef V8_BASE_SANITIZER_MSAN_H_
#define V8_BASE_SANITIZER_MSAN_H_
#include "src/base/macros.h"
#include "src/common/globals.h"
#include "src/base/memory.h"
#ifdef V8_USE_MEMORY_SANITIZER
#include <sanitizer/msan_interface.h>
// Marks a memory range as uninitialized, as if it was allocated here.
#define MSAN_ALLOCATED_UNINITIALIZED_MEMORY(p, s) \
__msan_allocated_memory(reinterpret_cast<const void*>(p), (s))
#define MSAN_ALLOCATED_UNINITIALIZED_MEMORY(start, size) \
__msan_allocated_memory(reinterpret_cast<const void*>(start), (size))
// Marks a memory range as initialized.
#define MSAN_MEMORY_IS_INITIALIZED(p, s) \
__msan_unpoison(reinterpret_cast<const void*>(p), (s))
#define MSAN_MEMORY_IS_INITIALIZED(start, size) \
__msan_unpoison(reinterpret_cast<const void*>(start), (size))
#else // !V8_USE_MEMORY_SANITIZER
#define MSAN_ALLOCATED_UNINITIALIZED_MEMORY(p, s) \
static_assert((std::is_pointer<decltype(p)>::value || \
std::is_same<v8::internal::Address, decltype(p)>::value) && \
std::is_convertible<decltype(s), size_t>::value, \
"static type violation")
#define MSAN_MEMORY_IS_INITIALIZED(p, s) \
MSAN_ALLOCATED_UNINITIALIZED_MEMORY(p, s)
#define MSAN_ALLOCATED_UNINITIALIZED_MEMORY(start, size) \
static_assert((std::is_pointer<decltype(start)>::value || \
std::is_same<v8::base::Address, decltype(start)>::value), \
"static type violation"); \
static_assert(std::is_convertible<decltype(size), size_t>::value, \
"static type violation"); \
USE(start, size)
#define MSAN_MEMORY_IS_INITIALIZED(start, size) \
MSAN_ALLOCATED_UNINITIALIZED_MEMORY(start, size)
#endif // V8_USE_MEMORY_SANITIZER
#endif // V8_SANITIZER_MSAN_H_
#endif // V8_BASE_SANITIZER_MSAN_H_
......@@ -32,6 +32,7 @@
#include "src/base/platform/platform.h"
#include "src/base/platform/time.h"
#include "src/base/platform/wrappers.h"
#include "src/base/sanitizer/msan.h"
#include "src/base/sys-info.h"
#include "src/d8/d8-console.h"
#include "src/d8/d8-platforms.h"
......@@ -53,7 +54,6 @@
#include "src/parsing/parsing.h"
#include "src/parsing/scanner-character-streams.h"
#include "src/profiler/profile-generator.h"
#include "src/sanitizer/msan.h"
#include "src/snapshot/snapshot.h"
#include "src/tasks/cancelable-task.h"
#include "src/trap-handler/trap-handler.h"
......
......@@ -11,6 +11,7 @@
#include "include/v8.h"
#include "src/api/api-inl.h"
#include "src/base/compiler-specific.h"
#include "src/base/sanitizer/asan.h"
#include "src/execution/vm-state-inl.h"
#include "src/heap/embedder-tracing.h"
#include "src/heap/heap-write-barrier-inl.h"
......@@ -19,7 +20,6 @@
#include "src/objects/objects-inl.h"
#include "src/objects/slots.h"
#include "src/objects/visitors.h"
#include "src/sanitizer/asan.h"
#include "src/tasks/cancelable-task.h"
#include "src/tasks/task-utils.h"
#include "src/utils/utils.h"
......
......@@ -5,12 +5,12 @@
#ifndef V8_HANDLES_HANDLES_INL_H_
#define V8_HANDLES_HANDLES_INL_H_
#include "src/base/sanitizer/msan.h"
#include "src/execution/isolate.h"
#include "src/execution/local-isolate.h"
#include "src/handles/handles.h"
#include "src/handles/local-handles-inl.h"
#include "src/objects/objects.h"
#include "src/sanitizer/msan.h"
namespace v8 {
namespace internal {
......
......@@ -5,10 +5,10 @@
#ifndef V8_HANDLES_LOCAL_HANDLES_INL_H_
#define V8_HANDLES_LOCAL_HANDLES_INL_H_
#include "src/base/sanitizer/msan.h"
#include "src/execution/isolate.h"
#include "src/execution/local-isolate.h"
#include "src/handles/local-handles.h"
#include "src/sanitizer/msan.h"
namespace v8 {
namespace internal {
......
......@@ -6,6 +6,7 @@
#include <limits>
#include "src/base/macros.h"
#include "src/base/platform/platform.h"
#include "src/heap/cppgc/globals.h"
#include "src/heap/cppgc/sanitizers.h"
......@@ -41,7 +42,7 @@ namespace {
// No ASAN support as accessing fake frames otherwise results in
// "stack-use-after-scope" warnings.
NO_SANITIZE_ADDRESS
DISABLE_ASAN
void IterateAsanFakeFrameIfNecessary(StackVisitor* visitor,
void* asan_fake_stack,
const void* stack_start,
......@@ -101,7 +102,7 @@ void IterateSafeStackIfNecessary(StackVisitor* visitor) {
// any data that needs to be scanned.
V8_NOINLINE
// No ASAN support as method accesses redzones while walking the stack.
NO_SANITIZE_ADDRESS
DISABLE_ASAN
void IteratePointersImpl(const Stack* stack, StackVisitor* visitor,
intptr_t* stack_end) {
#ifdef V8_USE_ADDRESS_SANITIZER
......@@ -116,7 +117,7 @@ void IteratePointersImpl(const Stack* stack, StackVisitor* visitor,
// MSAN: Instead of unpoisoning the whole stack, the slot's value is copied
// into a local which is unpoisoned.
void* address = *current;
MSAN_UNPOISON(&address, sizeof(address));
MSAN_MEMORY_IS_INITIALIZED(&address, sizeof(address));
if (address == nullptr) continue;
visitor->VisitPointer(address);
#ifdef V8_USE_ADDRESS_SANITIZER
......
......@@ -9,48 +9,16 @@
#include <string.h>
#include "src/base/macros.h"
//
// TODO(chromium:1056170): Find a place in base for sanitizer support.
//
#ifdef V8_USE_ADDRESS_SANITIZER
#include <sanitizer/asan_interface.h>
#define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
#if !defined(ASAN_POISON_MEMORY_REGION) || !defined(ASAN_UNPOISON_MEMORY_REGION)
#error "ASAN_POISON_MEMORY_REGION must be defined"
#endif
#else // !V8_USE_ADDRESS_SANITIZER
#define NO_SANITIZE_ADDRESS
#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#endif // V8_USE_ADDRESS_SANITIZER
#ifdef V8_USE_MEMORY_SANITIZER
#include <sanitizer/msan_interface.h>
#define MSAN_POISON(addr, size) __msan_allocated_memory(addr, size)
#define MSAN_UNPOISON(addr, size) __msan_unpoison(addr, size)
#else // !V8_USE_MEMORY_SANITIZER
#define MSAN_POISON(addr, size) ((void)(addr), (void)(size))
#define MSAN_UNPOISON(addr, size) ((void)(addr), (void)(size))
#endif // V8_USE_MEMORY_SANITIZER
#include "src/base/sanitizer/asan.h"
#include "src/base/sanitizer/msan.h"
// API for newly allocated or reclaimed memory.
#if defined(V8_USE_MEMORY_SANITIZER)
#define SET_MEMORY_ACCESSIBLE(address, size) MSAN_UNPOISON(address, size);
#define SET_MEMORY_ACCESSIBLE(address, size) \
MSAN_MEMORY_IS_INITIALIZED(address, size);
#define SET_MEMORY_INACCESSIBLE(address, size) \
memset((address), 0, (size)); \
MSAN_POISON((address), (size))
MSAN_ALLOCATED_UNINITIALIZED_MEMORY((address), (size))
#elif defined(V8_USE_ADDRESS_SANITIZER)
#define SET_MEMORY_ACCESSIBLE(address, size) \
ASAN_UNPOISON_MEMORY_REGION(address, size);
......
......@@ -4,11 +4,11 @@
#include "src/heap/cppgc/visitor.h"
#include "src/base/sanitizer/msan.h"
#include "src/heap/cppgc/gc-info-table.h"
#include "src/heap/cppgc/heap-object-header.h"
#include "src/heap/cppgc/heap-page.h"
#include "src/heap/cppgc/page-memory.h"
#include "src/heap/cppgc/sanitizers.h"
namespace cppgc {
......@@ -37,7 +37,7 @@ void TraceConservatively(ConservativeTracingVisitor* conservative_visitor,
// |payload| may be uninitialized by design or just contain padding bytes.
// Copy into a local variable that is not poisoned for conservative marking.
// Copy into a temporary variable to maintain the original MSAN state.
MSAN_UNPOISON(&maybe_ptr, sizeof(maybe_ptr));
MSAN_MEMORY_IS_INITIALIZED(&maybe_ptr, sizeof(maybe_ptr));
#endif
if (maybe_ptr) {
conservative_visitor->TraceConservativelyIfNeeded(maybe_ptr);
......
......@@ -8,22 +8,17 @@
#include <cmath>
// Clients of this interface shouldn't depend on lots of heap internals.
// Do not include anything from src/heap other than src/heap/heap.h and its
// write barrier here!
// Avoid including anything but `heap.h` from `src/heap` where possible.
#include "src/base/atomic-utils.h"
#include "src/base/atomicops.h"
#include "src/base/platform/platform.h"
#include "src/base/sanitizer/msan.h"
#include "src/common/assert-scope.h"
#include "src/heap/heap-write-barrier.h"
#include "src/heap/heap.h"
#include "src/heap/third-party/heap-api.h"
#include "src/objects/feedback-vector.h"
// TODO(gc): There is one more include to remove in order to no longer
// leak heap internals to users of this interface!
#include "src/execution/isolate-data.h"
#include "src/execution/isolate.h"
#include "src/heap/code-object-registry.h"
#include "src/heap/heap-write-barrier.h"
#include "src/heap/heap.h"
#include "src/heap/large-spaces.h"
#include "src/heap/memory-allocator.h"
#include "src/heap/memory-chunk.h"
......@@ -31,11 +26,13 @@
#include "src/heap/paged-spaces-inl.h"
#include "src/heap/read-only-spaces.h"
#include "src/heap/spaces-inl.h"
#include "src/heap/third-party/heap-api.h"
#include "src/objects/allocation-site-inl.h"
#include "src/objects/api-callbacks-inl.h"
#include "src/objects/cell-inl.h"
#include "src/objects/descriptor-array.h"
#include "src/objects/feedback-cell-inl.h"
#include "src/objects/feedback-vector.h"
#include "src/objects/literal-objects-inl.h"
#include "src/objects/objects-inl.h"
#include "src/objects/oddball.h"
......@@ -45,7 +42,6 @@
#include "src/objects/slots-inl.h"
#include "src/objects/struct-inl.h"
#include "src/profiler/heap-profiler.h"
#include "src/sanitizer/msan.h"
#include "src/strings/string-hasher.h"
#include "src/zone/zone-list-inl.h"
......
......@@ -5,6 +5,7 @@
#include "src/heap/large-spaces.h"
#include "src/base/platform/mutex.h"
#include "src/base/sanitizer/msan.h"
#include "src/common/globals.h"
#include "src/execution/isolate.h"
#include "src/heap/combined-heap.h"
......@@ -18,7 +19,6 @@
#include "src/heap/spaces-inl.h"
#include "src/logging/log.h"
#include "src/objects/objects-inl.h"
#include "src/sanitizer/msan.h"
#include "src/utils/ostreams.h"
namespace v8 {
......
......@@ -5,12 +5,12 @@
#ifndef V8_HEAP_NEW_SPACES_INL_H_
#define V8_HEAP_NEW_SPACES_INL_H_
#include "src/base/sanitizer/msan.h"
#include "src/common/globals.h"
#include "src/heap/heap.h"
#include "src/heap/new-spaces.h"
#include "src/heap/spaces-inl.h"
#include "src/objects/tagged-impl.h"
#include "src/sanitizer/msan.h"
namespace v8 {
namespace internal {
......
......@@ -11,6 +11,7 @@
#include "src/base/bits.h"
#include "src/base/bounded-page-allocator.h"
#include "src/base/macros.h"
#include "src/base/sanitizer/msan.h"
#include "src/common/globals.h"
#include "src/heap/combined-heap.h"
#include "src/heap/concurrent-marking.h"
......@@ -31,7 +32,6 @@
#include "src/objects/heap-object.h"
#include "src/objects/js-array-buffer-inl.h"
#include "src/objects/objects-inl.h"
#include "src/sanitizer/msan.h"
#include "src/snapshot/snapshot.h"
#include "src/utils/ostreams.h"
......
......@@ -7,14 +7,14 @@
#include <cinttypes>
#include "include/v8-profiler.h"
#include "src/base/sanitizer/asan.h"
#include "src/base/sanitizer/msan.h"
#include "src/execution/frames-inl.h"
#include "src/execution/simulator.h"
#include "src/execution/vm-state-inl.h"
#include "src/heap/heap-inl.h" // For Heap::code_range.
#include "src/logging/counters.h"
#include "src/profiler/profiler-stats.h"
#include "src/sanitizer/asan.h"
#include "src/sanitizer/msan.h"
namespace v8 {
namespace internal {
......
......@@ -11,10 +11,10 @@
#include "include/libplatform/libplatform.h"
#include "src/base/platform/platform.h"
#include "src/base/platform/wrappers.h"
#include "src/base/sanitizer/msan.h"
#include "src/codegen/assembler-arch.h"
#include "src/codegen/source-position-table.h"
#include "src/flags/flags.h"
#include "src/sanitizer/msan.h"
#include "src/snapshot/context-serializer.h"
#include "src/snapshot/embedded/embedded-file-writer.h"
#include "src/snapshot/snapshot.h"
......
......@@ -4,7 +4,7 @@
#include "src/snapshot/snapshot-utils.h"
#include "src/sanitizer/msan.h"
#include "src/base/sanitizer/msan.h"
#include "third_party/zlib/zlib.h"
namespace v8 {
......
......@@ -4,7 +4,7 @@
#include "src/zone/zone-segment.h"
#include "src/sanitizer/msan.h"
#include "src/base/sanitizer/msan.h"
namespace v8 {
namespace internal {
......
......@@ -7,8 +7,8 @@
#include <cstring>
#include <memory>
#include "src/base/sanitizer/asan.h"
#include "src/init/v8.h"
#include "src/sanitizer/asan.h"
#include "src/utils/utils.h"
#include "src/zone/type-stats.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment