Skip to content

Commit a96efa8

Browse files
authored
Add user poison functions (#494)
2 parents fb71c30 + 08c7d5f commit a96efa8

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

amd/device-libs/asanrtl/src/shadow_mapping.cl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "shadow_mapping.h"
99

1010
static const __constant u8 kAsanHeapLeftRedzoneMagic = (u8)0xfa;
11+
static const __constant u8 kAsanUserPoisonedMemoryMagic = (u8)0xf7;
1112

1213
NO_SANITIZE_ADDR
1314
static uptr
@@ -69,3 +70,84 @@ __asan_poison_region(ulong beg, ulong size)
6970
__builtin_memset(shadow_ptr, kAsanHeapLeftRedzoneMagic, shadow_size);
7071
}
7172
}
73+
74+
USED NO_SANITIZE_ADDR
75+
void
76+
__asan_poison_memory_region(const void *addr, uptr size)
77+
{
78+
if (size == 0)
79+
return;
80+
81+
uptr beg_addr = (uptr)addr;
82+
uptr end_addr = beg_addr + size;
83+
84+
__global s8 *beg_sp = (__global s8 *)MEM_TO_SHADOW(beg_addr);
85+
s8 beg_off = (s8)(beg_addr & (SHADOW_GRANULARITY - 1));
86+
s8 beg_val = *beg_sp;
87+
88+
__global s8 *end_sp = (__global s8 *)MEM_TO_SHADOW(end_addr);
89+
s8 end_off = (s8)(end_addr & (SHADOW_GRANULARITY - 1));
90+
s8 end_val = *end_sp;
91+
92+
if (beg_sp == end_sp) {
93+
s8 val = beg_val;
94+
if (val > 0 && val <= end_off) {
95+
if (beg_off > 0)
96+
*beg_sp = (val < beg_off) ? val : beg_off;
97+
else
98+
*beg_sp = (s8)kAsanUserPoisonedMemoryMagic;
99+
}
100+
return;
101+
}
102+
103+
if (beg_off > 0) {
104+
if (beg_val == 0)
105+
*beg_sp = beg_off;
106+
else
107+
*beg_sp = (beg_val < beg_off) ? beg_val : beg_off;
108+
beg_sp++;
109+
}
110+
111+
__builtin_memset(beg_sp, kAsanUserPoisonedMemoryMagic, end_sp - beg_sp);
112+
113+
if (end_val > 0 && end_val <= end_off)
114+
*end_sp = (s8)kAsanUserPoisonedMemoryMagic;
115+
}
116+
117+
USED NO_SANITIZE_ADDR
118+
void
119+
__asan_unpoison_memory_region(const void *addr, uptr size)
120+
{
121+
if (size == 0)
122+
return;
123+
124+
uptr beg_addr = (uptr)addr;
125+
uptr end_addr = beg_addr + size;
126+
127+
__global s8 *beg_sp = (__global s8 *)MEM_TO_SHADOW(beg_addr);
128+
s8 beg_off = (s8)(beg_addr & (SHADOW_GRANULARITY - 1));
129+
s8 beg_val = *beg_sp;
130+
131+
__global s8 *end_sp = (__global s8 *)MEM_TO_SHADOW(end_addr);
132+
s8 end_off = (s8)(end_addr & (SHADOW_GRANULARITY - 1));
133+
s8 end_val = *end_sp;
134+
135+
if (beg_sp == end_sp) {
136+
s8 val = beg_val;
137+
if (val != 0)
138+
*beg_sp = (val > end_off) ? val : end_off;
139+
return;
140+
}
141+
142+
__builtin_memset(beg_sp, 0, end_sp - beg_sp);
143+
144+
if (end_off > 0 && end_val != 0)
145+
*end_sp = (end_val > end_off) ? end_val : end_off;
146+
}
147+
148+
USED NO_SANITIZE_ADDR
149+
int
150+
__asan_address_is_poisoned(const void *addr)
151+
{
152+
return is_address_poisoned((uptr)addr);
153+
}

0 commit comments

Comments
 (0)