// Copyright 2016 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include #include #include #include #include "include/v8config.h" #include "src/base/bits.h" #include "src/base/ieee754.h" #include "src/utils/memcopy.h" #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \ defined(UNDEFINED_SANITIZER) #define V8_WITH_SANITIZER #endif #if defined(V8_OS_WIN) && defined(V8_WITH_SANITIZER) // With ASAN on Windows we have to reset the thread-in-wasm flag. Exceptions // caused by ASAN let the thread-in-wasm flag get out of sync. Even marking // functions with DISABLE_ASAN is not sufficient when the compiler produces // calls to memset. Therefore we add test-specific code for ASAN on // Windows. #define RESET_THREAD_IN_WASM_FLAG_FOR_ASAN_ON_WINDOWS #include "src/trap-handler/trap-handler.h" #endif #include "src/base/memory.h" #include "src/utils/utils.h" #include "src/wasm/wasm-external-refs.h" namespace v8 { namespace internal { namespace wasm { using base::ReadUnalignedValue; using base::WriteUnalignedValue; void f32_trunc_wrapper(Address data) { WriteUnalignedValue(data, truncf(ReadUnalignedValue(data))); } void f32_floor_wrapper(Address data) { WriteUnalignedValue(data, floorf(ReadUnalignedValue(data))); } void f32_ceil_wrapper(Address data) { WriteUnalignedValue(data, ceilf(ReadUnalignedValue(data))); } void f32_nearest_int_wrapper(Address data) { WriteUnalignedValue(data, nearbyintf(ReadUnalignedValue(data))); } void f64_trunc_wrapper(Address data) { WriteUnalignedValue(data, trunc(ReadUnalignedValue(data))); } void f64_floor_wrapper(Address data) { WriteUnalignedValue(data, floor(ReadUnalignedValue(data))); } void f64_ceil_wrapper(Address data) { WriteUnalignedValue(data, ceil(ReadUnalignedValue(data))); } void f64_nearest_int_wrapper(Address data) { WriteUnalignedValue(data, nearbyint(ReadUnalignedValue(data))); } void int64_to_float32_wrapper(Address data) { int64_t input = ReadUnalignedValue(data); WriteUnalignedValue(data, static_cast(input)); } void uint64_to_float32_wrapper(Address data) { uint64_t input = ReadUnalignedValue(data); float result = static_cast(input); #if V8_CC_MSVC // With MSVC we use static_cast(uint32_t) instead of // static_cast(uint64_t) to achieve round-to-nearest-ties-even // semantics. The idea is to calculate // static_cast(high_word) * 2^32 + static_cast(low_word). To // achieve proper rounding in all cases we have to adjust the high_word // with a "rounding bit" sometimes. The rounding bit is stored in the LSB of // the high_word if the low_word may affect the rounding of the high_word. uint32_t low_word = static_cast(input & 0xFFFFFFFF); uint32_t high_word = static_cast(input >> 32); float shift = static_cast(1ull << 32); // If the MSB of the high_word is set, then we make space for a rounding bit. if (high_word < 0x80000000) { high_word <<= 1; shift = static_cast(1ull << 31); } if ((high_word & 0xFE000000) && low_word) { // Set the rounding bit. high_word |= 1; } result = static_cast(high_word); result *= shift; result += static_cast(low_word); #endif WriteUnalignedValue(data, result); } void int64_to_float64_wrapper(Address data) { int64_t input = ReadUnalignedValue(data); WriteUnalignedValue(data, static_cast(input)); } void uint64_to_float64_wrapper(Address data) { uint64_t input = ReadUnalignedValue(data); double result = static_cast(input); #if V8_CC_MSVC // With MSVC we use static_cast(uint32_t) instead of // static_cast(uint64_t) to achieve round-to-nearest-ties-even // semantics. The idea is to calculate // static_cast(high_word) * 2^32 + static_cast(low_word). uint32_t low_word = static_cast(input & 0xFFFFFFFF); uint32_t high_word = static_cast(input >> 32); double shift = static_cast(1ull << 32); result = static_cast(high_word); result *= shift; result += static_cast(low_word); #endif WriteUnalignedValue(data, result); } int32_t float32_to_int64_wrapper(Address data) { // We use "<" here to check the upper bound because of rounding problems: With // "<=" some inputs would be considered within int64 range which are actually // not within int64 range. float input = ReadUnalignedValue(data); if (input >= static_cast(std::numeric_limits::min()) && input < static_cast(std::numeric_limits::max())) { WriteUnalignedValue(data, static_cast(input)); return 1; } return 0; } int32_t float32_to_uint64_wrapper(Address data) { float input = ReadUnalignedValue(data); // We use "<" here to check the upper bound because of rounding problems: With // "<=" some inputs would be considered within uint64 range which are actually // not within uint64 range. if (input > -1.0 && input < static_cast(std::numeric_limits::max())) { WriteUnalignedValue(data, static_cast(input)); return 1; } return 0; } int32_t float64_to_int64_wrapper(Address data) { // We use "<" here to check the upper bound because of rounding problems: With // "<=" some inputs would be considered within int64 range which are actually // not within int64 range. double input = ReadUnalignedValue(data); if (input >= static_cast(std::numeric_limits::min()) && input < static_cast(std::numeric_limits::max())) { WriteUnalignedValue(data, static_cast(input)); return 1; } return 0; } int32_t float64_to_uint64_wrapper(Address data) { // We use "<" here to check the upper bound because of rounding problems: With // "<=" some inputs would be considered within uint64 range which are actually // not within uint64 range. double input = ReadUnalignedValue(data); if (input > -1.0 && input < static_cast(std::numeric_limits::max())) { WriteUnalignedValue(data, static_cast(input)); return 1; } return 0; } int32_t int64_div_wrapper(Address data) { int64_t dividend = ReadUnalignedValue(data); int64_t divisor = ReadUnalignedValue(data + sizeof(dividend)); if (divisor == 0) { return 0; } if (divisor == -1 && dividend == std::numeric_limits::min()) { return -1; } WriteUnalignedValue(data, dividend / divisor); return 1; } int32_t int64_mod_wrapper(Address data) { int64_t dividend = ReadUnalignedValue(data); int64_t divisor = ReadUnalignedValue(data + sizeof(dividend)); if (divisor == 0) { return 0; } WriteUnalignedValue(data, dividend % divisor); return 1; } int32_t uint64_div_wrapper(Address data) { uint64_t dividend = ReadUnalignedValue(data); uint64_t divisor = ReadUnalignedValue(data + sizeof(dividend)); if (divisor == 0) { return 0; } WriteUnalignedValue(data, dividend / divisor); return 1; } int32_t uint64_mod_wrapper(Address data) { uint64_t dividend = ReadUnalignedValue(data); uint64_t divisor = ReadUnalignedValue(data + sizeof(dividend)); if (divisor == 0) { return 0; } WriteUnalignedValue(data, dividend % divisor); return 1; } uint32_t word32_ctz_wrapper(Address data) { return base::bits::CountTrailingZeros(ReadUnalignedValue(data)); } uint32_t word64_ctz_wrapper(Address data) { return base::bits::CountTrailingZeros(ReadUnalignedValue(data)); } uint32_t word32_popcnt_wrapper(Address data) { return base::bits::CountPopulation(ReadUnalignedValue(data)); } uint32_t word64_popcnt_wrapper(Address data) { return base::bits::CountPopulation(ReadUnalignedValue(data)); } uint32_t word32_rol_wrapper(Address data) { uint32_t input = ReadUnalignedValue(data); uint32_t shift = ReadUnalignedValue(data + sizeof(input)) & 31; return (input << shift) | (input >> ((32 - shift) & 31)); } uint32_t word32_ror_wrapper(Address data) { uint32_t input = ReadUnalignedValue(data); uint32_t shift = ReadUnalignedValue(data + sizeof(input)) & 31; return (input >> shift) | (input << ((32 - shift) & 31)); } void float64_pow_wrapper(Address data) { double x = ReadUnalignedValue(data); double y = ReadUnalignedValue(data + sizeof(x)); WriteUnalignedValue(data, base::ieee754::pow(x, y)); } // Asan on Windows triggers exceptions in this function to allocate // shadow memory lazily. When this function is called from WebAssembly, // these exceptions would be handled by the trap handler before they get // handled by Asan, and thereby confuse the thread-in-wasm flag. // Therefore we disable ASAN for this function. Alternatively we could // reset the thread-in-wasm flag before calling this function. However, // as this is only a problem with Asan on Windows, we did not consider // it worth the overhead. DISABLE_ASAN void memory_copy_wrapper(Address dst, Address src, uint32_t size) { // Use explicit forward and backward copy to match the required semantics for // the memory.copy instruction. It is assumed that the caller of this // function has already performed bounds checks, so {src + size} and // {dst + size} should not overflow. DCHECK(src + size >= src && dst + size >= dst); uint8_t* dst8 = reinterpret_cast(dst); uint8_t* src8 = reinterpret_cast(src); if (src < dst && src + size > dst && dst + size > src) { dst8 += size - 1; src8 += size - 1; for (; size > 0; size--) { *dst8-- = *src8--; } } else { for (; size > 0; size--) { *dst8++ = *src8++; } } } // Asan on Windows triggers exceptions in this function that confuse the // WebAssembly trap handler, so Asan is disabled. See the comment on // memory_copy_wrapper above for more info. void memory_fill_wrapper(Address dst, uint32_t value, uint32_t size) { #if defined(RESET_THREAD_IN_WASM_FLAG_FOR_ASAN_ON_WINDOWS) bool thread_was_in_wasm = trap_handler::IsThreadInWasm(); if (thread_was_in_wasm) { trap_handler::ClearThreadInWasm(); } #endif // Use an explicit forward copy to match the required semantics for the // memory.fill instruction. It is assumed that the caller of this function // has already performed bounds checks, so {dst + size} should not overflow. DCHECK(dst + size >= dst); uint8_t* dst8 = reinterpret_cast(dst); uint8_t value8 = static_cast(value); for (; size > 0; size--) { *dst8++ = value8; } #if defined(RESET_THREAD_IN_WASM_FLAG_FOR_ASAN_ON_WINDOWS) if (thread_was_in_wasm) { trap_handler::SetThreadInWasm(); } #endif } static WasmTrapCallbackForTesting wasm_trap_callback_for_testing = nullptr; void set_trap_callback_for_testing(WasmTrapCallbackForTesting callback) { wasm_trap_callback_for_testing = callback; } void call_trap_callback_for_testing() { if (wasm_trap_callback_for_testing) { wasm_trap_callback_for_testing(); } } } // namespace wasm } // namespace internal } // namespace v8 #undef V8_WITH_SANITIZER #undef RESET_THREAD_IN_WASM_FLAG_FOR_ASAN_ON_WINDOWS