Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
speculation.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_MISC_SPECULATION_H
8#define ZEPHYR_MISC_SPECULATION_H
9
10#include <zephyr/types.h>
11
34{
35#ifdef CONFIG_BOUNDS_CHECK_BYPASS_MITIGATION
36 int32_t signed_index = index, signed_array_size = array_size;
37
38 /* Take the difference between index and max.
39 * A proper value will result in a negative result. We also AND in
40 * the complement of index, so that we automatically reject any large
41 * indexes which would wrap around the difference calculation.
42 *
43 * Sign-extend just the sign bit to produce a mask of all 1s (accept)
44 * or all 0s (truncate).
45 */
46 uint32_t mask = ((signed_index - signed_array_size) & ~signed_index) >> 31;
47
48 return index & mask;
49#else
50 ARG_UNUSED(array_size);
51
52 return index;
53#endif /* CONFIG_BOUNDS_CHECK_BYPASS_MITIGATION */
54}
55#endif /* ZEPHYR_MISC_SPECULATION_H */
ZTEST_BMEM int index[(3)]
Definition main.c:32
static uint32_t k_array_index_sanitize(uint32_t index, uint32_t array_size)
Sanitize an array index against bounds check bypass attacks aka the Spectre V1 vulnerability.
Definition speculation.h:33
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74