* Content of LICENSE file mentioned above:
Copyright 2019 The Fuchsia Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FBL_CONFINE_ARRAY_INDEX_H_
#define FBL_CONFINE_ARRAY_INDEX_H_
#include <stddef.h>
#ifdef __aarch64__
static inline size_t confine_array_index(size_t index, size_t size) {
size_t safe_index;
asm(
"cmp %1, %2\n"
"csel %0, %1, xzr, lo\n"
"hint #20\n"
: "=r"(safe_index)
: "r"(index), "r"(size)
: "cc");
return safe_index;
}
#endif
#ifdef __arm__
static inline size_t confine_array_index(size_t index, size_t size)
{
size_t ret_val = index;
* For the ARMv7/AArch32 case we're basing the select and barrier
* code on __load_no_speculate1() in <speculation_barrier.h> as we
* lack the csel instruction.
*/
#ifdef __thumb2__
asm volatile (
".syntax unified\n"
"cmp %0, %1\n"
"it cs\n"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winline-asm"
#endif
"movcs %0, #0\n"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
".inst.n 0xf3af\t@ CSDB\n"
".inst.n 0x8014\t@ CSDB"
: "+r" (ret_val) : "r" (size) : "cc");
#else
asm volatile (
".syntax unified\n"
"cmp %0, %1\n"
"movcs %0, #0\n"
".inst 0xe320f014\t@ CSDB"
: "+r" (ret_val) : "r" (size) : "cc");
#endif
return ret_val;
}
#endif
#ifdef __x86_64__
static inline size_t confine_array_index(size_t index, size_t size) {
size_t safe_index = 0;
__asm__(
"cmp %1, %2\n"
"cmova %1, %0\n"
: "+r"(safe_index)
: "r"(index), "r"(size)
: "cc");
return safe_index;
}
#endif
#ifdef __riscv
static inline size_t confine_array_index(size_t index, size_t size) {
* The naive C implementation without protection
* against side-channel attacks.
*/
if (index < size)
return index;
return 0;
}
#endif
#endif