-
Notifications
You must be signed in to change notification settings - Fork 12
/
sm_util.c
65 lines (57 loc) · 1.47 KB
/
sm_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* This file is a part of SMalloc.
* SMalloc is MIT licensed.
* Copyright (c) 2017 Andrey Rys.
*/
#include "smalloc_i.h"
static int smalloc_check_bounds(struct smalloc_pool *spool, struct smalloc_hdr *shdr)
{
if (!spool) return 0;
if (CHAR_PTR(shdr) >= CHAR_PTR(spool->pool)
&& CHAR_PTR(shdr) <= (CHAR_PTR(spool->pool)+spool->pool_size))
return 1;
return 0;
}
static int smalloc_valid_tag(struct smalloc_hdr *shdr)
{
char *s;
uintptr_t r = smalloc_mktag(shdr);
size_t x;
if (shdr->tag == r) {
s = CHAR_PTR(HEADER_TO_USER(shdr));
s += shdr->usz;
for (x = 0; x < sizeof(struct smalloc_hdr); x += sizeof(uintptr_t)) {
r = smalloc_uinthash(r);
if (memcmp(s+x, &r, sizeof(uintptr_t)) != 0) return 0;
}
s += x; x = 0;
while (x < shdr->rsz - shdr->usz) {
if (s[x] != '\xFF') return 0;
x++;
}
return 1;
}
return 0;
}
static void smalloc_do_crash(struct smalloc_pool *spool, const void *p)
{
char *c = NULL;
*c = 'X';
}
smalloc_ub_handler smalloc_UB = smalloc_do_crash;
void sm_set_ub_handler(smalloc_ub_handler handler)
{
if (!handler) smalloc_UB = smalloc_do_crash;
else smalloc_UB = handler;
}
int smalloc_is_alloc(struct smalloc_pool *spool, struct smalloc_hdr *shdr)
{
if (!smalloc_check_bounds(spool, shdr)) return 0;
if (shdr->rsz == 0) return 0;
if (shdr->rsz > SIZE_MAX) return 0;
if (shdr->usz > SIZE_MAX) return 0;
if (shdr->usz > shdr->rsz) return 0;
if (shdr->rsz % HEADER_SZ) return 0;
if (!smalloc_valid_tag(shdr)) return 0;
return 1;
}