Skip to content

Commit 773c106

Browse files
committed
librasan: Use musl implementations of some libc functions
1 parent 9771891 commit 773c106

File tree

8 files changed

+456
-148
lines changed

8 files changed

+456
-148
lines changed

libafl_qemu/librasan/asan/build.rs

+15-38
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,26 @@
1-
fn main() {
2-
println!("cargo:rerun-if-changed=cc/include/hooks.h");
3-
println!("cargo:rerun-if-changed=cc/include/trace.h");
4-
println!("cargo:rerun-if-changed=cc/include/printf.h");
5-
println!("cargo:rerun-if-changed=cc/src/asprintf.c");
6-
println!("cargo:rerun-if-changed=cc/src/log.c");
7-
println!("cargo:rerun-if-changed=cc/src/printf.c");
8-
println!("cargo:rerun-if-changed=cc/src/vasprintf.c");
9-
1+
fn compile(file: &str, output: &str) {
102
cc::Build::new()
113
.define("_GNU_SOURCE", None)
124
.opt_level(3)
135
.flag("-Werror")
146
.flag("-fno-stack-protector")
157
.flag("-ffunction-sections")
168
.include("cc/include/")
17-
.file("cc/src/asprintf.c")
18-
.compile("asprintf");
9+
.file(file)
10+
.compile(output);
11+
}
1912

20-
cc::Build::new()
21-
.define("_GNU_SOURCE", None)
22-
.opt_level(3)
23-
.flag("-Werror")
24-
.flag("-fno-stack-protector")
25-
.flag("-ffunction-sections")
26-
.include("cc/include/")
27-
.file("cc/src/log.c")
28-
.compile("log");
13+
fn main() {
14+
println!("cargo:rerun-if-changed=cc");
2915

30-
cc::Build::new()
31-
.define("_GNU_SOURCE", None)
32-
.opt_level(3)
33-
.flag("-Werror")
34-
.flag("-fno-stack-protector")
35-
.flag("-ffunction-sections")
36-
.include("cc/include/")
37-
.file("cc/src/printf.c")
38-
.compile("printf");
16+
compile("cc/src/asprintf.c", "asprintf");
17+
compile("cc/src/log.c", "log");
18+
compile("cc/src/printf.c", "printf");
19+
compile("cc/src/vasprintf.c", "vasprintf");
3920

40-
cc::Build::new()
41-
.define("_GNU_SOURCE", None)
42-
.opt_level(3)
43-
.flag("-Werror")
44-
.flag("-fno-stack-protector")
45-
.flag("-ffunction-sections")
46-
.include("cc/include/")
47-
.file("cc/src/vasprintf.c")
48-
.compile("vasprintf");
21+
compile("cc/src/memcmp.c", "memcmp");
22+
compile("cc/src/memcpy.c", "memcpy");
23+
compile("cc/src/memmove.c", "memmove");
24+
compile("cc/src/memset.c", "memset");
25+
compile("cc/src/strlen.c", "strlen");
4926
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# These files are ignored since they are copied verbatim from musl
2+
memcmp.c
3+
memcpy.c
4+
memmove.c
5+
memset.c
6+
strlen.c
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* https://git.musl-libc.org/cgit/musl/tree/src/string/memcmp.c?h=v1.2.5
3+
*
4+
* This file has been copied from musl v1.2.5, which is licensed under the
5+
* following license:
6+
*
7+
* Copyright © 2005-2020 Rich Felker, et al.
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining
10+
* a copy of this software and associated documentation files (the
11+
* "Software"), to deal in the Software without restriction, including
12+
* without limitation the rights to use, copy, modify, merge, publish,
13+
* distribute, sublicense, and/or sell copies of the Software, and to
14+
* permit persons to whom the Software is furnished to do so, subject to
15+
* the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be
18+
* included in all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27+
*
28+
* The bcmp function has been added since it is identical to memcmp.
29+
*/
30+
31+
#include <string.h>
32+
33+
int memcmp(const void *vl, const void *vr, size_t n)
34+
{
35+
const unsigned char *l=vl, *r=vr;
36+
for (; n && *l == *r; n--, l++, r++);
37+
return n ? *l-*r : 0;
38+
}
39+
40+
int bcmp(const void *s1, const void *s2, size_t n)
41+
{
42+
return memcmp(s1, s2, n);
43+
}
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* https://git.musl-libc.org/cgit/musl/tree/src/string/memcpy.c?h=v1.2.5
3+
*
4+
* This file has been copied from musl v1.2.5, which is licensed under the
5+
* following license:
6+
*
7+
* Copyright © 2005-2020 Rich Felker, et al.
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining
10+
* a copy of this software and associated documentation files (the
11+
* "Software"), to deal in the Software without restriction, including
12+
* without limitation the rights to use, copy, modify, merge, publish,
13+
* distribute, sublicense, and/or sell copies of the Software, and to
14+
* permit persons to whom the Software is furnished to do so, subject to
15+
* the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be
18+
* included in all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#include <string.h>
30+
#include <stdint.h>
31+
#include <endian.h>
32+
33+
void *memcpy(void *restrict dest, const void *restrict src, size_t n)
34+
{
35+
unsigned char *d = dest;
36+
const unsigned char *s = src;
37+
38+
#ifdef __GNUC__
39+
40+
#if __BYTE_ORDER == __LITTLE_ENDIAN
41+
#define LS >>
42+
#define RS <<
43+
#else
44+
#define LS <<
45+
#define RS >>
46+
#endif
47+
48+
typedef uint32_t __attribute__((__may_alias__)) u32;
49+
uint32_t w, x;
50+
51+
for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++;
52+
53+
if ((uintptr_t)d % 4 == 0) {
54+
for (; n>=16; s+=16, d+=16, n-=16) {
55+
*(u32 *)(d+0) = *(u32 *)(s+0);
56+
*(u32 *)(d+4) = *(u32 *)(s+4);
57+
*(u32 *)(d+8) = *(u32 *)(s+8);
58+
*(u32 *)(d+12) = *(u32 *)(s+12);
59+
}
60+
if (n&8) {
61+
*(u32 *)(d+0) = *(u32 *)(s+0);
62+
*(u32 *)(d+4) = *(u32 *)(s+4);
63+
d += 8; s += 8;
64+
}
65+
if (n&4) {
66+
*(u32 *)(d+0) = *(u32 *)(s+0);
67+
d += 4; s += 4;
68+
}
69+
if (n&2) {
70+
*d++ = *s++; *d++ = *s++;
71+
}
72+
if (n&1) {
73+
*d = *s;
74+
}
75+
return dest;
76+
}
77+
78+
if (n >= 32) switch ((uintptr_t)d % 4) {
79+
case 1:
80+
w = *(u32 *)s;
81+
*d++ = *s++;
82+
*d++ = *s++;
83+
*d++ = *s++;
84+
n -= 3;
85+
for (; n>=17; s+=16, d+=16, n-=16) {
86+
x = *(u32 *)(s+1);
87+
*(u32 *)(d+0) = (w LS 24) | (x RS 8);
88+
w = *(u32 *)(s+5);
89+
*(u32 *)(d+4) = (x LS 24) | (w RS 8);
90+
x = *(u32 *)(s+9);
91+
*(u32 *)(d+8) = (w LS 24) | (x RS 8);
92+
w = *(u32 *)(s+13);
93+
*(u32 *)(d+12) = (x LS 24) | (w RS 8);
94+
}
95+
break;
96+
case 2:
97+
w = *(u32 *)s;
98+
*d++ = *s++;
99+
*d++ = *s++;
100+
n -= 2;
101+
for (; n>=18; s+=16, d+=16, n-=16) {
102+
x = *(u32 *)(s+2);
103+
*(u32 *)(d+0) = (w LS 16) | (x RS 16);
104+
w = *(u32 *)(s+6);
105+
*(u32 *)(d+4) = (x LS 16) | (w RS 16);
106+
x = *(u32 *)(s+10);
107+
*(u32 *)(d+8) = (w LS 16) | (x RS 16);
108+
w = *(u32 *)(s+14);
109+
*(u32 *)(d+12) = (x LS 16) | (w RS 16);
110+
}
111+
break;
112+
case 3:
113+
w = *(u32 *)s;
114+
*d++ = *s++;
115+
n -= 1;
116+
for (; n>=19; s+=16, d+=16, n-=16) {
117+
x = *(u32 *)(s+3);
118+
*(u32 *)(d+0) = (w LS 8) | (x RS 24);
119+
w = *(u32 *)(s+7);
120+
*(u32 *)(d+4) = (x LS 8) | (w RS 24);
121+
x = *(u32 *)(s+11);
122+
*(u32 *)(d+8) = (w LS 8) | (x RS 24);
123+
w = *(u32 *)(s+15);
124+
*(u32 *)(d+12) = (x LS 8) | (w RS 24);
125+
}
126+
break;
127+
}
128+
if (n&16) {
129+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
130+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
131+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
132+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
133+
}
134+
if (n&8) {
135+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
136+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
137+
}
138+
if (n&4) {
139+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
140+
}
141+
if (n&2) {
142+
*d++ = *s++; *d++ = *s++;
143+
}
144+
if (n&1) {
145+
*d = *s;
146+
}
147+
return dest;
148+
#endif
149+
150+
for (; n; n--) *d++ = *s++;
151+
return dest;
152+
}
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* https://git.musl-libc.org/cgit/musl/tree/src/string/memmove.c?h=v1.2.5
3+
*
4+
* This file has been copied from musl v1.2.5, which is licensed under the
5+
* following license:
6+
*
7+
* Copyright © 2005-2020 Rich Felker, et al.
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining
10+
* a copy of this software and associated documentation files (the
11+
* "Software"), to deal in the Software without restriction, including
12+
* without limitation the rights to use, copy, modify, merge, publish,
13+
* distribute, sublicense, and/or sell copies of the Software, and to
14+
* permit persons to whom the Software is furnished to do so, subject to
15+
* the following conditions:
16+
*
17+
* The above copyright notice and this permission notice shall be
18+
* included in all copies or substantial portions of the Software.
19+
*
20+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#include <string.h>
30+
#include <stdint.h>
31+
32+
#ifdef __GNUC__
33+
typedef __attribute__((__may_alias__)) size_t WT;
34+
#define WS (sizeof(WT))
35+
#endif
36+
37+
void *memmove(void *dest, const void *src, size_t n)
38+
{
39+
char *d = dest;
40+
const char *s = src;
41+
42+
if (d==s) return d;
43+
if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n);
44+
45+
if (d<s) {
46+
#ifdef __GNUC__
47+
if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
48+
while ((uintptr_t)d % WS) {
49+
if (!n--) return dest;
50+
*d++ = *s++;
51+
}
52+
for (; n>=WS; n-=WS, d+=WS, s+=WS) *(WT *)d = *(WT *)s;
53+
}
54+
#endif
55+
for (; n; n--) *d++ = *s++;
56+
} else {
57+
#ifdef __GNUC__
58+
if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
59+
while ((uintptr_t)(d+n) % WS) {
60+
if (!n--) return dest;
61+
d[n] = s[n];
62+
}
63+
while (n>=WS) n-=WS, *(WT *)(d+n) = *(WT *)(s+n);
64+
}
65+
#endif
66+
while (n) n--, d[n] = s[n];
67+
}
68+
69+
return dest;
70+
}

0 commit comments

Comments
 (0)