Skip to content

Commit 32a8dda

Browse files
committed
Single poison value
1 parent 1c42b53 commit 32a8dda

File tree

4 files changed

+14
-81
lines changed

4 files changed

+14
-81
lines changed

compiler-rt/lib/asan/asan_descriptions.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#define ASAN_DESCRIPTIONS_H
1616

1717
#include "asan_allocator.h"
18-
#include "asan_poisoning.h"
1918
#include "asan_thread.h"
2019
#include "sanitizer_common/sanitizer_common.h"
2120
#include "sanitizer_common/sanitizer_report_decorator.h"
@@ -47,9 +46,6 @@ class Decorator : public __sanitizer::SanitizerCommonDecorator {
4746
const char *Allocation() { return Magenta(); }
4847

4948
const char *ShadowByte(u8 byte) {
50-
if (IsPoisonTrackingMagic(byte))
51-
return Blue();
52-
5349
switch (byte) {
5450
case kAsanHeapLeftRedzoneMagic:
5551
case kAsanArrayCookieMagic:

compiler-rt/lib/asan/asan_errors.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -508,18 +508,6 @@ ErrorGeneric::ErrorGeneric(u32 tid, uptr pc_, uptr bp_, uptr sp_, uptr addr,
508508
break;
509509
}
510510

511-
if (flags()->track_poison > 0 && IsPoisonTrackingMagic(shadow_val)) {
512-
if (internal_strcmp(bug_descr, "unknown-crash") != 0) {
513-
Printf(
514-
"ERROR: use-after-poison tracking magic values overlap with "
515-
"other constants.\n");
516-
Printf("Please file a bug.\n");
517-
} else {
518-
bug_descr = "use-after-poison";
519-
bug_type_score = 20;
520-
}
521-
}
522-
523511
scariness.Scare(bug_type_score + read_after_free_bonus, bug_descr);
524512
if (far_from_bounds) scariness.Scare(10, "far-from-bounds");
525513
}
@@ -565,12 +553,8 @@ static void PrintLegend(InternalScopedString *str) {
565553
PrintShadowByte(str, " Global redzone: ", kAsanGlobalRedzoneMagic);
566554
PrintShadowByte(str, " Global init order: ",
567555
kAsanInitializationOrderMagic);
568-
// TODO: sync description with PoisonTrackingMagicValues
569-
PrintShadowByte(
570-
str, " Poisoned by user: ", kAsanUserPoisonedMemoryMagic,
571-
flags()->track_poison > 0 ? " with detailed tracking using {0x80-0x8F, "
572-
"0x90-0x9F, 0xD0-0xDF, 0xE0-0xEF}\n"
573-
: "\n");
556+
PrintShadowByte(str, " Poisoned by user: ",
557+
kAsanUserPoisonedMemoryMagic);
574558
PrintShadowByte(str, " Container overflow: ",
575559
kAsanContiguousContainerOOBMagic);
576560
PrintShadowByte(str, " Array cookie: ",
@@ -624,13 +608,11 @@ static void CheckPoisonRecords(uptr addr) {
624608
return;
625609
uptr shadow_addr = MemToShadow(addr);
626610
unsigned char poison_magic = *(reinterpret_cast<u8 *>(shadow_addr));
627-
int poison_index = PoisonTrackingMagicToIndex[poison_magic];
628611

629-
if (poison_index < 0 || poison_index >= NumPoisonTrackingMagicValues)
612+
if (poison_magic != kAsanUserPoisonedMemoryMagic)
630613
return;
631614

632-
PoisonRecordRingBuffer *PoisonRecord =
633-
reinterpret_cast<PoisonRecordRingBuffer *>(PoisonRecords[poison_index]);
615+
PoisonRecordRingBuffer *PoisonRecord = GetPoisonRecord();
634616
if (PoisonRecord) {
635617
bool FoundMatch = false;
636618

compiler-rt/lib/asan/asan_poisoning.cpp

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,17 @@ namespace __asan {
2626

2727
static atomic_uint8_t can_poison_memory;
2828

29-
PoisonRecordRingBuffer *PoisonRecords[NumPoisonTrackingMagicValues] = {0};
30-
int PoisonTrackingMagicToIndex[256] = {-1};
29+
static PoisonRecordRingBuffer *PoisonRecords = nullptr;
3130

3231
void InitializePoisonTracking() {
3332
if (flags()->track_poison <= 0)
3433
return;
3534

36-
for (unsigned int i = 0; i < sizeof(PoisonTrackingMagicToIndex) / sizeof(int);
37-
i++) {
38-
PoisonTrackingMagicToIndex[i] = -1;
39-
}
40-
41-
for (unsigned int i = 0; i < NumPoisonTrackingMagicValues; i++) {
42-
int magic = PoisonTrackingIndexToMagic[i];
43-
CHECK(magic > 0);
44-
CHECK((unsigned int)magic <
45-
sizeof(PoisonTrackingMagicToIndex) / sizeof(int));
46-
47-
// Necessary for AddressIsPoisoned calculations
48-
CHECK((char)magic < 0);
49-
50-
PoisonTrackingMagicToIndex[magic] = i;
51-
52-
PoisonRecords[i] = PoisonRecordRingBuffer::New(flags()->track_poison);
53-
}
35+
PoisonRecords = PoisonRecordRingBuffer::New(flags()->track_poison);
5436
}
5537

56-
bool IsPoisonTrackingMagic(int byte) {
57-
return (byte >= 0 &&
58-
(unsigned long)byte <
59-
(sizeof(PoisonTrackingMagicToIndex) / sizeof(int)) &&
60-
PoisonTrackingMagicToIndex[byte] >= 0 &&
61-
PoisonTrackingMagicToIndex[byte] < NumPoisonTrackingMagicValues &&
62-
PoisonTrackingIndexToMagic[PoisonTrackingMagicToIndex[byte]] == byte);
38+
PoisonRecordRingBuffer* GetPoisonRecord() {
39+
return PoisonRecords;
6340
}
6441

6542
void SetCanPoisonMemory(bool value) {
@@ -149,23 +126,18 @@ void __asan_poison_memory_region(void const volatile *addr, uptr size) {
149126

150127
if (flags()->track_poison > 0) {
151128
GET_STACK_TRACE(/*max_size=*/ 16, /*fast=*/ false);
129+
u32 current_tid = GetCurrentTidOrInvalid();
130+
152131
// TODO: garbage collect stacks once they fall off the ring buffer?
153132
// StackDepot doesn't currently have a way to delete stacks.
154133
u32 stack_id = StackDepotPut(stack);
155134

156-
u32 current_tid = GetCurrentTidOrInvalid();
157-
u32 poison_index = ((stack_id * 151157) ^ (current_tid * 733123)) %
158-
NumPoisonTrackingMagicValues;
159-
poison_magic = PoisonTrackingIndexToMagic[poison_index];
160135
PoisonRecord record{.stack_id = stack_id,
161136
.thread_id = current_tid,
162137
.begin = beg_addr,
163138
.end = end_addr};
164-
// This is a data race: with concurrent writes, some records may be lost,
165-
// but it's a sacrifice I am willing to make for speed.
166-
// The sharding across PoisonRecords reduces the likelihood of
167-
// concurrent writes.
168-
PoisonRecords[poison_index]->push(record);
139+
// TODO: mutex
140+
GetPoisonRecord()->push(record);
169141
}
170142

171143
ShadowSegmentEndpoint beg(beg_addr);

compiler-rt/lib/asan/asan_poisoning.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,19 @@
2525

2626
namespace __asan {
2727

28-
// These need to be negative chars (i.e., in the range [0x80 .. 0xff]) for
29-
// AddressIsPoisoned calculations.
30-
static const int PoisonTrackingIndexToMagic[] = {
31-
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a,
32-
0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
33-
0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xd0,
34-
0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
35-
0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
36-
0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
37-
};
38-
static const int NumPoisonTrackingMagicValues =
39-
sizeof(PoisonTrackingIndexToMagic) / sizeof(int);
40-
41-
extern int PoisonTrackingMagicToIndex[256];
42-
4328
struct PoisonRecord {
4429
unsigned int stack_id;
4530
unsigned int thread_id;
4631
uptr begin;
4732
uptr end;
4833
};
4934

50-
typedef RingBuffer<struct PoisonRecord> PoisonRecordRingBuffer;
51-
extern PoisonRecordRingBuffer* PoisonRecords[NumPoisonTrackingMagicValues];
35+
using PoisonRecordRingBuffer = RingBuffer<struct PoisonRecord>;
5236

5337
// Set up data structures for track_poison.
5438
void InitializePoisonTracking();
5539

56-
// Is this number a magic value used for poison tracking?
57-
bool IsPoisonTrackingMagic(int byte);
40+
PoisonRecordRingBuffer* GetPoisonRecord();
5841

5942
// Enable/disable memory poisoning.
6043
void SetCanPoisonMemory(bool value);

0 commit comments

Comments
 (0)