Skip to content

Commit 8c9085d

Browse files
committed
ubsan: Add some missing handlers
One missing handler for GCC and three for clang. Signed-off-by: Keith Packard <[email protected]>
1 parent 91888f7 commit 8c9085d

File tree

7 files changed

+252
-5
lines changed

7 files changed

+252
-5
lines changed

newlib/libc/ubsan/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@
3333
# OF THE POSSIBILITY OF SUCH DAMAGE.
3434
#
3535
picolibc_sources(
36+
ubsan_cfi_type_check_to_string.c
3637
ubsan_error.c
3738
ubsan_handle_add_overflow.c
3839
ubsan_handle_alignment_assumption.c
3940
ubsan_handle_builtin_unreachable.c
41+
ubsan_handle_cfi_bad_type.c
42+
ubsan_handle_cfi_check_fail.c
4043
ubsan_handle_divrem_overflow.c
44+
ubsan_handle_dynamic_type_cache_miss.c
4145
ubsan_handle_float_cast_overflow.c
4246
ubsan_handle_function_type_mismatch.c
4347
ubsan_handle_implicit_conversion.c

newlib/libc/ubsan/meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@
3333
# OF THE POSSIBILITY OF SUCH DAMAGE.
3434
#
3535
srcs_ubsan = [
36+
'ubsan_cfi_type_check_to_string.c',
3637
'ubsan_error.c',
3738
'ubsan_handle_add_overflow.c',
3839
'ubsan_handle_alignment_assumption.c',
3940
'ubsan_handle_builtin_unreachable.c',
41+
'ubsan_handle_cfi_bad_type.c',
42+
'ubsan_handle_cfi_check_fail.c',
4043
'ubsan_handle_divrem_overflow.c',
44+
'ubsan_handle_dynamic_type_cache_miss.c',
4145
'ubsan_handle_float_cast_overflow.c',
4246
'ubsan_handle_function_type_mismatch.c',
4347
'ubsan_handle_implicit_conversion.c',

newlib/libc/ubsan/ubsan.h

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ enum {
7676
implicit_conversion_signed_integer_truncation_or_sign_change = 4,
7777
};
7878

79+
enum {
80+
cfi_type_check_v_call,
81+
cfi_type_check_nv_call,
82+
cfi_type_check_derived_cast,
83+
cfi_type_check_unrelated_cast,
84+
cfi_type_check_i_call,
85+
cfi_type_check_nvmf_call,
86+
cfi_type_check_vmf_call,
87+
};
88+
7989
struct type_descriptor {
8090
uint16_t type_kind;
8191
uint16_t type_info;
@@ -121,6 +131,13 @@ struct float_cast_overflow_data {
121131
struct type_descriptor *to_type;
122132
};
123133

134+
struct dynamic_type_cache_miss_data {
135+
struct source_location location;
136+
struct type_descriptor *type;
137+
void *type_info;
138+
unsigned char type_check_kind;
139+
};
140+
124141
struct type_mismatch_data {
125142
struct source_location location;
126143
struct type_descriptor *type;
@@ -201,6 +218,12 @@ struct alignment_assumption_data {
201218
struct type_descriptor *type;
202219
};
203220

221+
struct cfi_check_fail_data {
222+
unsigned char cfi_type_check_kind;
223+
struct source_location location;
224+
struct type_descriptor *type;
225+
};
226+
204227
struct vla_bound_not_positive_data {
205228
struct source_location location;
206229
struct type_descriptor *type;
@@ -221,19 +244,27 @@ void
221244
__ubsan_handle_builtin_unreachable(void *data);
222245

223246
void
224-
__ubsan_cfi_bad_icall(void *data,
225-
void *function);
247+
__ubsan_handle_cfi_bad_type(void *data,
248+
void *vtable,
249+
void *valid_vtable,
250+
void *opts);
251+
226252

227253
void
228-
__ubsan_cfi_check_fail(void *data,
229-
void *function,
230-
uintptr_t vtable_is_valid);
254+
__ubsan_handle_cfi_check_fail(void *data,
255+
void *function,
256+
void *vtable_is_valid);
231257

232258
void
233259
__ubsan_handle_divrem_overflow(void *data,
234260
void *lhs,
235261
void *rhs);
236262

263+
void
264+
__ubsan_handle_dynamic_type_cache_miss(void *data,
265+
void *pointer,
266+
void *hash);
267+
237268
void
238269
__ubsan_handle_float_cast_overflow(void *data,
239270
void *from);
@@ -258,6 +289,10 @@ void
258289
__ubsan_handle_load_invalid_value(void *data,
259290
void *val);
260291

292+
void
293+
__ubsan_handle_local_out_of_bounds(
294+
);
295+
261296
void
262297
__ubsan_handle_missing_return(void *data);
263298

@@ -357,4 +392,7 @@ __ubsan_val_to_umax(struct type_descriptor *type,
357392
const char*
358393
__ubsan_type_check_to_string(unsigned char type_check_kind);
359394

395+
const char*
396+
__ubsan_cfi_type_check_to_string(unsigned char cfi_type_check_kind);
397+
360398
#endif /* _UBSAN_H_ */
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2025 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#include "ubsan.h"
37+
38+
static const char *const cfi_type_check_kinds[] = {
39+
[cfi_type_check_v_call] = "cfi_type_check_v_call",
40+
[cfi_type_check_nv_call] = "cfi_type_check_nv_call",
41+
[cfi_type_check_derived_cast] = "cfi_type_check_derived_cast",
42+
[cfi_type_check_unrelated_cast] = "cfi_type_check_unrelated_cast",
43+
[cfi_type_check_i_call] = "cfi_type_check_i_call",
44+
[cfi_type_check_nvmf_call] = "cfi_type_check_nvmf_call",
45+
[cfi_type_check_vmf_call] = "cfi_type_check_vmf_call",
46+
};
47+
48+
const char*
49+
__ubsan_cfi_type_check_to_string(unsigned char cfi_type_check_kind)
50+
{
51+
if (cfi_type_check_kind < sizeof(cfi_type_check_kinds)/sizeof(cfi_type_check_kinds[0]))
52+
return cfi_type_check_kinds[cfi_type_check_kind];
53+
return "unknown";
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2025 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#include "ubsan.h"
37+
38+
void
39+
__ubsan_handle_cfi_bad_type(void *_data,
40+
void *vtable,
41+
void *valid_vtable,
42+
void *opts)
43+
{
44+
struct cfi_check_fail_data *data = _data;
45+
__ubsan_error(&data->location, "cfi_bad_type", "(%s) %p valid %p opts %p %s\n",
46+
data->type->type_name,
47+
vtable,
48+
valid_vtable,
49+
opts,
50+
__ubsan_cfi_type_check_to_string(data->cfi_type_check_kind));
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2025 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#include "ubsan.h"
37+
38+
void
39+
__ubsan_handle_cfi_check_fail(void *_data,
40+
void *function,
41+
void *vtable_is_valid)
42+
{
43+
struct cfi_check_fail_data *data = _data;
44+
__ubsan_error(&data->location, "cfi_check_fail", "(%s) %p valid %p %s\n",
45+
data->type->type_name,
46+
function,
47+
vtable_is_valid,
48+
__ubsan_cfi_type_check_to_string(data->cfi_type_check_kind));
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* SPDX-License-Identifier: BSD-3-Clause
3+
*
4+
* Copyright © 2025 Keith Packard
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
*
13+
* 2. Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* 3. Neither the name of the copyright holder nor the names of its
19+
* contributors may be used to endorse or promote products derived
20+
* from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33+
* OF THE POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#include "ubsan.h"
37+
38+
void
39+
__ubsan_handle_dynamic_type_cache_miss(void *_data,
40+
void *pointer,
41+
void *hash)
42+
{
43+
struct dynamic_type_cache_miss_data *data = _data;
44+
__ubsan_error(&data->location, "dynamic_type_cache_miss", "(%s) %p (hash %p)\n",
45+
data->type->type_name, pointer, hash);
46+
}
47+

0 commit comments

Comments
 (0)