1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "partition_alloc/pointers/raw_ptr_asan_unowned_impl.h"
6
7 #include <sanitizer/asan_interface.h>
8 #include <cstdint>
9
10 #include "partition_alloc/partition_alloc_base/compiler_specific.h"
11
12 namespace base::internal {
13
14 PA_NO_SANITIZE("address")
EndOfAliveAllocation(const volatile void * ptr,bool is_adjustable_ptr)15 bool EndOfAliveAllocation(const volatile void* ptr, bool is_adjustable_ptr) {
16 uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
17
18 // Normally, we probe the first byte of an object, but in cases of pointer
19 // arithmetic, we may be probing subsequent bytes, including the legal
20 // "end + 1" position.
21 //
22 // Alas, ASAN will claim an unmapped page is unpoisoned, so willfully ignore
23 // the fist address of a page, since "end + 1" of an object allocated exactly
24 // up to a page boundary will SEGV on probe. This will cause false negatives
25 // for pointers that happen to be page aligned, which is undesirable but
26 // necessary for now.
27 //
28 // We minimize the consequences by using the pointer arithmetic flag in
29 // higher levels to conditionalize this suppression.
30 //
31 // TODO(tsepez): this may still fail for a non-accessible but non-null
32 // return from, say, malloc(0) which happens to be page-aligned.
33 //
34 // TODO(tsepez): enforce the pointer arithmetic flag. Until then, we
35 // may fail here if a pointer requires the flag but is lacking it.
36 return is_adjustable_ptr &&
37 ((address & 0x0fff) == 0 ||
38 __asan_region_is_poisoned(reinterpret_cast<void*>(address), 1)) &&
39 !__asan_region_is_poisoned(reinterpret_cast<void*>(address - 1), 1);
40 }
41
LikelySmuggledScalar(const volatile void * ptr)42 bool LikelySmuggledScalar(const volatile void* ptr) {
43 intptr_t address = reinterpret_cast<intptr_t>(ptr);
44 return address < 0x4000; // Negative or small positive.
45 }
46
47 } // namespace base::internal
48