#include "src/base/macros.h"
#include "src/base/platform/yield-processor.h"
#include "src/builtins/builtins-utils-inl.h"
#include "src/common/globals.h"
#include "src/execution/futex-emulation.h"
#include "src/heap/factory.h"
#include "src/logging/counters.h"
#include "src/numbers/conversions-inl.h"
#include "src/objects/js-array-buffer-inl.h"
#include "src/objects/objects-inl.h"
namespace v8 {
namespace internal {
inline bool AtomicIsLockFree(double size) {
return size == 1 || size == 2 || size == 4 || size == 8;
}
BUILTIN(AtomicsIsLockFree) {
HandleScope scope(isolate);
Handle<Object> size = args.atOrUndefined(isolate, 1);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, size,
Object::ToNumber(isolate, size));
return *isolate->factory()->ToBoolean(
AtomicIsLockFree(Object::NumberValue(*size)));
}
V8_WARN_UNUSED_RESULT MaybeDirectHandle<JSTypedArray> ValidateIntegerTypedArray(
Isolate* isolate, Handle<Object> object, const char* method_name,
bool only_int32_and_big_int64 = false) {
if (IsJSTypedArray(*object)) {
DirectHandle<JSTypedArray> typed_array = Cast<JSTypedArray>(object);
if (typed_array->IsDetachedOrOutOfBounds()) {
THROW_NEW_ERROR(
isolate, NewTypeError(MessageTemplate::kDetachedOperation,
isolate->factory()->NewStringFromAsciiChecked(
method_name)));
}
if (only_int32_and_big_int64) {
if (typed_array->type() == kExternalInt32Array ||
typed_array->type() == kExternalBigInt64Array) {
return typed_array;
}
} else {
if (typed_array->type() != kExternalFloat32Array &&
typed_array->type() != kExternalFloat64Array &&
typed_array->type() != kExternalUint8ClampedArray)
return typed_array;
}
}
THROW_NEW_ERROR(
isolate, NewTypeError(only_int32_and_big_int64
? MessageTemplate::kNotInt32OrBigInt64TypedArray
: MessageTemplate::kNotIntegerTypedArray,
object));
}
V8_WARN_UNUSED_RESULT Maybe<size_t> ValidateAtomicAccess(
Isolate* isolate, DirectHandle<JSTypedArray> typed_array,
Handle<Object> request_index) {
DirectHandle<Object> access_index_obj;
ASSIGN_RETURN_ON_EXCEPTION(
isolate, access_index_obj,
Object::ToIndex(isolate, request_index,
MessageTemplate::kInvalidAtomicAccessIndex));
size_t access_index;
size_t typed_array_length = typed_array->GetLength();
if (!TryNumberToSize(*access_index_obj, &access_index) ||
access_index >= typed_array_length) {
isolate->Throw(*isolate->factory()->NewRangeError(
MessageTemplate::kInvalidAtomicAccessIndex));
return Nothing<size_t>();
}
return Just<size_t>(access_index);
}
namespace {
inline size_t GetAddress64(size_t index, size_t byte_offset) {
return (index << 3) + byte_offset;
}
inline size_t GetAddress32(size_t index, size_t byte_offset) {
return (index << 2) + byte_offset;
}
}
BUILTIN(AtomicsNotify) {
HandleScope scope(isolate);
Handle<Object> array = args.atOrUndefined(isolate, 1);
Handle<Object> index = args.atOrUndefined(isolate, 2);
Handle<Object> count = args.atOrUndefined(isolate, 3);
DirectHandle<JSTypedArray> sta;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, sta,
ValidateIntegerTypedArray(isolate, array, "Atomics.notify", true));
Maybe<size_t> maybe_index = ValidateAtomicAccess(isolate, sta, index);
if (maybe_index.IsNothing()) return ReadOnlyRoots(isolate).exception();
size_t i = maybe_index.FromJust();
uint32_t c;
if (IsUndefined(*count, isolate)) {
c = kMaxUInt32;
} else {
double count_double;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, count_double,
Object::IntegerValue(isolate, count));
if (count_double < 0) {
count_double = 0;
} else if (count_double > kMaxUInt32) {
count_double = kMaxUInt32;
}
c = static_cast<uint32_t>(count_double);
}
DirectHandle<JSArrayBuffer> array_buffer = sta->GetBuffer(isolate);
if (V8_UNLIKELY(!array_buffer->is_shared())) {
return Smi::zero();
}
size_t wake_addr;
if (sta->type() == kExternalBigInt64Array) {
wake_addr = GetAddress64(i, sta->byte_offset());
} else {
DCHECK(sta->type() == kExternalInt32Array);
wake_addr = GetAddress32(i, sta->byte_offset());
}
int num_waiters_woken = FutexEmulation::Wake(*array_buffer, wake_addr, c);
return Smi::FromInt(num_waiters_woken);
}
Tagged<Object> DoWait(Isolate* isolate, FutexEmulation::WaitMode mode,
Handle<Object> array, Handle<Object> index,
Handle<Object> value, Handle<Object> timeout) {
DirectHandle<JSTypedArray> sta;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, sta,
ValidateIntegerTypedArray(isolate, array, "Atomics.wait", true));
if (V8_UNLIKELY(!sta->GetBuffer(isolate)->is_shared())) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kNotSharedTypedArray, array));
}
Maybe<size_t> maybe_index = ValidateAtomicAccess(isolate, sta, index);
if (maybe_index.IsNothing()) return ReadOnlyRoots(isolate).exception();
size_t i = maybe_index.FromJust();
if (sta->type() == kExternalBigInt64Array) {
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value,
BigInt::FromObject(isolate, value));
} else {
DCHECK(sta->type() == kExternalInt32Array);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, value,
Object::ToInt32(isolate, value));
}
double timeout_number;
if (IsUndefined(*timeout, isolate)) {
timeout_number =
Object::NumberValue(ReadOnlyRoots(isolate).infinity_value());
} else {
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, timeout,
Object::ToNumber(isolate, timeout));
timeout_number = Object::NumberValue(*timeout);
if (std::isnan(timeout_number))
timeout_number =
Object::NumberValue(ReadOnlyRoots(isolate).infinity_value());
else if (timeout_number < 0)
timeout_number = 0;
}
if (mode == FutexEmulation::WaitMode::kSync &&
!isolate->allow_atomics_wait()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kAtomicsOperationNotAllowed,
isolate->factory()->NewStringFromAsciiChecked(
"Atomics.wait")));
}
DirectHandle<JSArrayBuffer> array_buffer = sta->GetBuffer(isolate);
if (sta->type() == kExternalBigInt64Array) {
return FutexEmulation::WaitJs64(
isolate, mode, array_buffer, GetAddress64(i, sta->byte_offset()),
Cast<BigInt>(value)->AsInt64(), timeout_number);
} else {
DCHECK(sta->type() == kExternalInt32Array);
return FutexEmulation::WaitJs32(isolate, mode, array_buffer,
GetAddress32(i, sta->byte_offset()),
NumberToInt32(*value), timeout_number);
}
}
BUILTIN(AtomicsWait) {
HandleScope scope(isolate);
Handle<Object> array = args.atOrUndefined(isolate, 1);
Handle<Object> index = args.atOrUndefined(isolate, 2);
Handle<Object> value = args.atOrUndefined(isolate, 3);
Handle<Object> timeout = args.atOrUndefined(isolate, 4);
return DoWait(isolate, FutexEmulation::WaitMode::kSync, array, index, value,
timeout);
}
BUILTIN(AtomicsWaitAsync) {
HandleScope scope(isolate);
Handle<Object> array = args.atOrUndefined(isolate, 1);
Handle<Object> index = args.atOrUndefined(isolate, 2);
Handle<Object> value = args.atOrUndefined(isolate, 3);
Handle<Object> timeout = args.atOrUndefined(isolate, 4);
isolate->CountUsage(v8::Isolate::kAtomicsWaitAsync);
return DoWait(isolate, FutexEmulation::WaitMode::kAsync, array, index, value,
timeout);
}
namespace {
V8_NOINLINE Maybe<bool> CheckAtomicsPauseIterationNumber(
Isolate* isolate, DirectHandle<Object> iteration_number) {
constexpr char method_name[] = "Atomics.pause";
if (IsNumber(*iteration_number)) {
double iter = Object::NumberValue(*iteration_number);
if (std::isfinite(iter) && nearbyint(iter) == iter) {
return Just(true);
}
}
THROW_NEW_ERROR(
isolate,
NewError(isolate->type_error_function(),
MessageTemplate::kArgumentIsNotUndefinedOrInteger,
isolate->factory()->NewStringFromAsciiChecked(method_name)));
}
}
BUILTIN(AtomicsPause) {
HandleScope scope(isolate);
DirectHandle<Object> iteration_number = args.atOrUndefined(isolate, 1);
isolate->CountUsage(v8::Isolate::kAtomicsPause);
if (V8_UNLIKELY(!IsUndefined(*iteration_number, isolate) &&
!IsSmi(*iteration_number))) {
RETURN_ON_EXCEPTION_VALUE(
isolate, CheckAtomicsPauseIterationNumber(isolate, iteration_number),
ReadOnlyRoots(isolate).exception());
}
YIELD_PROCESSOR;
return ReadOnlyRoots(isolate).undefined_value();
}
}
}