summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKhem Raj <raj.khem@gmail.com>2019-08-10 02:41:22 -0700
committerKhem Raj <raj.khem@gmail.com>2019-08-10 02:42:46 -0700
commit396e486a3bb379d34d89e1f868b1ebccdc4f4a31 (patch)
treecf3e090d104c6f88daf5df70a32156266799a3e5
parent7b687f10809be97350c605e5a96dcf61b2558302 (diff)
downloadmeta-clang-kraj/clang-busybox.tar.gz
busybox: Update the clang compatibility patch to work with clang 9.xkraj/clang-busybox
Signed-off-by: Khem Raj <raj.khem@gmail.com>
-rw-r--r--recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch137
-rw-r--r--recipes-core/busybox/busybox/0001-writing-to-a-const-variable-is-undefined-behavior-C9.patch27
-rw-r--r--recipes-core/busybox/busybox_%.bbappend5
3 files changed, 141 insertions, 28 deletions
diff --git a/recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch b/recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch
new file mode 100644
index 0000000..481b125
--- /dev/null
+++ b/recipes-core/busybox/busybox/0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch
@@ -0,0 +1,137 @@
1From c7580fdb4be69a872291ccf22334091cace749fc Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Wed, 16 Jan 2019 22:39:24 -0800
4Subject: [PATCH] Turn ptr_to_globals and bb_errno to be non const
5
6writing to a const variable is undefined behavior
7
8This is undefined as per (C99 6.7.3 paragraph 5) see [1]
9
10errno and ptr_to_globals is written to in code, this fails with
11segfaults when compiled with clang
12
13unsigned FAST_FUNC bb_strtou(const char *arg, char **endp, int base)
14{
15 unsigned long v;
16 char *endptr;
17
18 if (!endp) endp = &endptr;
19 *endp = (char*) arg;
20
21 if (!isalnum(arg[0])) return ret_ERANGE();
22 errno = 0;
23 v = strtoul(arg, endp, base);
24 if (v > UINT_MAX) return ret_ERANGE();
25 return handle_errors(v, endp);
26 }
27
28without 'const' ( working code )
29
30Dump of assembler code for function bb_strtou:
31 0x0000555555568298 <+0>: push %rbx
32 0x0000555555568299 <+1>: sub $0x10,%rsp
33 0x000055555556829d <+5>: test %rsi,%rsi
34 0x00005555555682a0 <+8>: lea 0x8(%rsp),%rbx
35 0x00005555555682a5 <+13>: cmovne %rsi,%rbx
36 0x00005555555682a9 <+17>: mov %rdi,(%rbx)
37 0x00005555555682ac <+20>: mov (%rdi),%al
38 0x00005555555682ae <+22>: lea -0x30(%rax),%ecx
39 0x00005555555682b1 <+25>: cmp $0xa,%cl
40 0x00005555555682b4 <+28>: jb 0x5555555682be <bb_strtou+38>
41 0x00005555555682b6 <+30>: or $0x20,%al
42 0x00005555555682b8 <+32>: add $0x9f,%al
43 0x00005555555682ba <+34>: cmp $0x1a,%al
44 0x00005555555682bc <+36>: jae 0x5555555682dc <bb_strtou+68>
45 0x00005555555682be <+38>: mov 0x107da3(%rip),%rax # 0x555555670068 <bb_errno>
46=> 0x00005555555682c5 <+45>: movl $0x0,(%rax)
47 0x00005555555682cb <+51>: mov %rbx,%rsi
48 0x00005555555682ce <+54>: callq 0x555555564310 <strtoul@plt>
49 0x00005555555682d3 <+59>: mov %rax,%rcx
50 0x00005555555682d6 <+62>: shr $0x20,%rcx
51 0x00005555555682da <+66>: je 0x5555555682f0 <bb_strtou+88>
52 0x00005555555682dc <+68>: mov 0x107d85(%rip),%rax # 0x555555670068 <bb_errno>
53 0x00005555555682e3 <+75>: movl $0x22,(%rax)
54 0x00005555555682e9 <+81>: mov $0xffffffff,%eax
55 0x00005555555682ee <+86>: jmp 0x5555555682fb <bb_strtou+99>
56 0x00005555555682f0 <+88>: mov %rax,%rdi
57 0x00005555555682f3 <+91>: mov %rbx,%rsi
58 0x00005555555682f6 <+94>: callq 0x5555555681e8 <handle_errors>
59 0x00005555555682fb <+99>: add $0x10,%rsp
60 0x00005555555682ff <+103>: pop %rbx
61 0x0000555555568300 <+104>: retq
62
63here address of bb_errno is valid rax = 0x7ffff7cac6c0
64
65with 'const' ( non-working code )
66
67Dump of assembler code for function bb_strtou:
68 0x00005555555682a4 <+0>: push %r14
69 0x00005555555682a6 <+2>: push %rbx
70 0x00005555555682a7 <+3>: push %rax
71 0x00005555555682a8 <+4>: test %rsi,%rsi
72 0x00005555555682ab <+7>: mov %rsp,%rbx
73 0x00005555555682ae <+10>: cmovne %rsi,%rbx
74 0x00005555555682b2 <+14>: mov %rdi,(%rbx)
75 0x00005555555682b5 <+17>: mov (%rdi),%al
76 0x00005555555682b7 <+19>: lea -0x30(%rax),%ecx
77 0x00005555555682ba <+22>: cmp $0xa,%cl
78 0x00005555555682bd <+25>: jb 0x5555555682d6 <bb_strtou+50>
79 0x00005555555682bf <+27>: or $0x20,%al
80 0x00005555555682c1 <+29>: add $0x9f,%al
81 0x00005555555682c3 <+31>: cmp $0x1a,%al
82 0x00005555555682c5 <+33>: jb 0x5555555682d6 <bb_strtou+50>
83 0x00005555555682c7 <+35>: mov 0x107d9a(%rip),%rax # 0x555555670068 <bb_errno>
84 0x00005555555682ce <+42>: movl $0x22,(%rax)
85 0x00005555555682d4 <+48>: jmp 0x5555555682fc <bb_strtou+88>
86 0x00005555555682d6 <+50>: mov 0x107d8b(%rip),%r14 # 0x555555670068 <bb_errno>
87=> 0x00005555555682dd <+57>: movl $0x0,(%r14)
88 0x00005555555682e4 <+64>: mov %rbx,%rsi
89 0x00005555555682e7 <+67>: callq 0x555555564300 <strtoul@plt>
90 0x00005555555682ec <+72>: mov %rax,%rcx
91 0x00005555555682ef <+75>: shr $0x20,%rcx
92 0x00005555555682f3 <+79>: je 0x555555568303 <bb_strtou+95>
93 0x00005555555682f5 <+81>: movl $0x22,(%r14)
94 0x00005555555682fc <+88>: mov $0xffffffff,%eax
95 0x0000555555568301 <+93>: jmp 0x55555556830e <bb_strtou+106>
96 0x0000555555568303 <+95>: mov %rax,%rdi
97 0x0000555555568306 <+98>: mov %rbx,%rsi
98 0x0000555555568309 <+101>: callq 0x5555555681f4 <handle_errors>
99 0x000055555556830e <+106>: add $0x8,%rsp
100 0x0000555555568312 <+110>: pop %rbx
101 0x0000555555568313 <+111>: pop %r14
102 0x0000555555568315 <+113>: retq
103
104r14 is 0x0 and writing to this ofcourse ends up in segfault
105
106[1] https://bugs.llvm.org/show_bug.cgi?id=39919
107
108Signed-off-by: Khem Raj <raj.khem@gmail.com>
109---
110 include/libbb.h | 4 ++--
111 1 file changed, 2 insertions(+), 2 deletions(-)
112
113diff --git a/include/libbb.h b/include/libbb.h
114index 111d1b790..a52265e77 100644
115--- a/include/libbb.h
116+++ b/include/libbb.h
117@@ -341,7 +341,7 @@ struct BUG_off_t_size_is_misdetected {
118 #if defined(__GLIBC__)
119 /* glibc uses __errno_location() to get a ptr to errno */
120 /* We can just memorize it once - no multithreading in busybox :) */
121-extern int *const bb_errno;
122+extern int *bb_errno;
123 #undef errno
124 #define errno (*bb_errno)
125 #endif
126@@ -2152,7 +2152,7 @@ struct globals;
127 /* '*const' ptr makes gcc optimize code much better.
128 * Magic prevents ptr_to_globals from going into rodata.
129 * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
130-extern struct globals *const ptr_to_globals;
131+extern struct globals *ptr_to_globals;
132 /* At least gcc 3.4.6 on mipsel system needs optimization barrier */
133 #define barrier() __asm__ __volatile__("":::"memory")
134 #define SET_PTR_TO_GLOBALS(x) do { \
135--
1362.22.0
137
diff --git a/recipes-core/busybox/busybox/0001-writing-to-a-const-variable-is-undefined-behavior-C9.patch b/recipes-core/busybox/busybox/0001-writing-to-a-const-variable-is-undefined-behavior-C9.patch
deleted file mode 100644
index 6b933da..0000000
--- a/recipes-core/busybox/busybox/0001-writing-to-a-const-variable-is-undefined-behavior-C9.patch
+++ /dev/null
@@ -1,27 +0,0 @@
1From 64c27d12dab4d5a31103f48773733f17c84b0a77 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Wed, 16 Jan 2019 22:39:24 -0800
4Subject: [PATCH] writing to a const variable is undefined behavior (C99 6.7.3
5 paragraph 5).
6
7Signed-off-by: Khem Raj <raj.khem@gmail.com>
8---
9 include/libbb.h | 2 +-
10 1 file changed, 1 insertion(+), 1 deletion(-)
11
12diff --git a/include/libbb.h b/include/libbb.h
13index 3366df30f..6b925eb2d 100644
14--- a/include/libbb.h
15+++ b/include/libbb.h
16@@ -2092,7 +2092,7 @@ struct globals;
17 /* '*const' ptr makes gcc optimize code much better.
18 * Magic prevents ptr_to_globals from going into rodata.
19 * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
20-extern struct globals *const ptr_to_globals;
21+extern struct globals *ptr_to_globals;
22 /* At least gcc 3.4.6 on mipsel system needs optimization barrier */
23 #define barrier() __asm__ __volatile__("":::"memory")
24 #define SET_PTR_TO_GLOBALS(x) do { \
25--
262.20.1
27
diff --git a/recipes-core/busybox/busybox_%.bbappend b/recipes-core/busybox/busybox_%.bbappend
index a4c90f4..2187660 100644
--- a/recipes-core/busybox/busybox_%.bbappend
+++ b/recipes-core/busybox/busybox_%.bbappend
@@ -1,3 +1,6 @@
1FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:" 1FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
2SRC_URI_append_toolchain-clang = " file://0001-writing-to-a-const-variable-is-undefined-behavior-C9.patch" 2SRC_URI_append_toolchain-clang = "\
3 file://0001-Turn-ptr_to_globals-and-bb_errno-to-be-non-const.patch \
4 "
5
3TOOLCHAIN_x86 = "gcc" 6TOOLCHAIN_x86 = "gcc"