2016-05-10 17:58:03 +00:00
|
|
|
// 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 <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "src/base/platform/elapsed-timer.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/assembler-inl.h"
|
2016-05-10 17:58:03 +00:00
|
|
|
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/compiler/value-helper.h"
|
|
|
|
#include "test/cctest/wasm/wasm-run-utils.h"
|
2016-10-05 11:59:47 +00:00
|
|
|
#include "test/common/wasm/test-signatures.h"
|
2017-04-25 11:29:17 +00:00
|
|
|
#include "test/common/wasm/wasm-macro-gen.h"
|
2016-05-10 17:58:03 +00:00
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
2016-05-10 17:58:03 +00:00
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
// Liftoff does not support asm.js, and is never invoked with asm.js code in
|
|
|
|
// production. Hence test asm.js with TurboFan and Interpreter only.
|
2020-08-04 11:09:23 +00:00
|
|
|
#define ASMJS_EXEC_TEST(name) \
|
|
|
|
void RunWasm_##name(TestExecutionTier execution_tier); \
|
|
|
|
TEST(RunWasmTurbofan_##name) { \
|
|
|
|
RunWasm_##name(TestExecutionTier::kTurbofan); \
|
|
|
|
} \
|
|
|
|
TEST(RunWasmInterpreter_##name) { \
|
|
|
|
RunWasm_##name(TestExecutionTier::kInterpreter); \
|
|
|
|
} \
|
|
|
|
void RunWasm_##name(TestExecutionTier execution_tier)
|
2019-05-29 08:29:42 +00:00
|
|
|
|
|
|
|
ASMJS_EXEC_TEST(Int32AsmjsDivS) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_BINOP(kExprI32AsmjsDivS, WASM_LOCAL_GET(0), WASM_LOCAL_GET(1)));
|
2016-05-10 17:58:03 +00:00
|
|
|
const int32_t kMin = std::numeric_limits<int32_t>::min();
|
|
|
|
CHECK_EQ(0, r.Call(0, 100));
|
|
|
|
CHECK_EQ(0, r.Call(100, 0));
|
|
|
|
CHECK_EQ(0, r.Call(-1001, 0));
|
|
|
|
CHECK_EQ(kMin, r.Call(kMin, -1));
|
|
|
|
CHECK_EQ(0, r.Call(kMin, 0));
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(Int32AsmjsRemS) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_BINOP(kExprI32AsmjsRemS, WASM_LOCAL_GET(0), WASM_LOCAL_GET(1)));
|
2016-05-10 17:58:03 +00:00
|
|
|
const int32_t kMin = std::numeric_limits<int32_t>::min();
|
|
|
|
CHECK_EQ(33, r.Call(133, 100));
|
|
|
|
CHECK_EQ(0, r.Call(kMin, -1));
|
|
|
|
CHECK_EQ(0, r.Call(100, 0));
|
|
|
|
CHECK_EQ(0, r.Call(-1001, 0));
|
|
|
|
CHECK_EQ(0, r.Call(kMin, 0));
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(Int32AsmjsDivU) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_BINOP(kExprI32AsmjsDivU, WASM_LOCAL_GET(0), WASM_LOCAL_GET(1)));
|
2016-05-10 17:58:03 +00:00
|
|
|
const int32_t kMin = std::numeric_limits<int32_t>::min();
|
|
|
|
CHECK_EQ(0, r.Call(0, 100));
|
|
|
|
CHECK_EQ(0, r.Call(kMin, -1));
|
|
|
|
CHECK_EQ(0, r.Call(100, 0));
|
|
|
|
CHECK_EQ(0, r.Call(-1001, 0));
|
|
|
|
CHECK_EQ(0, r.Call(kMin, 0));
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(Int32AsmjsRemU) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, int32_t, int32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_BINOP(kExprI32AsmjsRemU, WASM_LOCAL_GET(0), WASM_LOCAL_GET(1)));
|
2016-05-10 17:58:03 +00:00
|
|
|
const int32_t kMin = std::numeric_limits<int32_t>::min();
|
|
|
|
CHECK_EQ(17, r.Call(217, 100));
|
|
|
|
CHECK_EQ(0, r.Call(100, 0));
|
|
|
|
CHECK_EQ(0, r.Call(-1001, 0));
|
|
|
|
CHECK_EQ(0, r.Call(kMin, 0));
|
|
|
|
CHECK_EQ(kMin, r.Call(kMin, -1));
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(I32AsmjsSConvertF32) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, float> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF32, WASM_LOCAL_GET(0)));
|
2016-05-11 09:31:32 +00:00
|
|
|
|
|
|
|
FOR_FLOAT32_INPUTS(i) {
|
2019-02-01 10:37:04 +00:00
|
|
|
int32_t expected = DoubleToInt32(i);
|
|
|
|
CHECK_EQ(expected, r.Call(i));
|
2016-05-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(I32AsmjsSConvertF64) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, double> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_UNOP(kExprI32AsmjsSConvertF64, WASM_LOCAL_GET(0)));
|
2016-05-11 09:31:32 +00:00
|
|
|
|
|
|
|
FOR_FLOAT64_INPUTS(i) {
|
2019-02-01 10:37:04 +00:00
|
|
|
int32_t expected = DoubleToInt32(i);
|
|
|
|
CHECK_EQ(expected, r.Call(i));
|
2016-05-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(I32AsmjsUConvertF32) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<uint32_t, float> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF32, WASM_LOCAL_GET(0)));
|
2016-05-11 09:31:32 +00:00
|
|
|
|
|
|
|
FOR_FLOAT32_INPUTS(i) {
|
2019-02-01 10:37:04 +00:00
|
|
|
uint32_t expected = DoubleToUint32(i);
|
|
|
|
CHECK_EQ(expected, r.Call(i));
|
2016-05-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(I32AsmjsUConvertF64) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<uint32_t, double> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_UNOP(kExprI32AsmjsUConvertF64, WASM_LOCAL_GET(0)));
|
2016-05-11 09:31:32 +00:00
|
|
|
|
|
|
|
FOR_FLOAT64_INPUTS(i) {
|
2019-02-01 10:37:04 +00:00
|
|
|
uint32_t expected = DoubleToUint32(i);
|
|
|
|
CHECK_EQ(expected, r.Call(i));
|
2016-05-11 09:31:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(LoadMemI32_oob_asm) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, uint32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
|
|
|
int32_t* memory = r.builder().AddMemoryElems<int32_t>(8);
|
|
|
|
r.builder().RandomizeMemory(1112);
|
2016-05-10 17:58:03 +00:00
|
|
|
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_UNOP(kExprI32AsmjsLoadMem, WASM_LOCAL_GET(0)));
|
2016-05-10 17:58:03 +00:00
|
|
|
|
|
|
|
memory[0] = 999999;
|
|
|
|
CHECK_EQ(999999, r.Call(0u));
|
|
|
|
// TODO(titzer): offset 29-31 should also be OOB.
|
|
|
|
for (uint32_t offset = 32; offset < 40; offset++) {
|
|
|
|
CHECK_EQ(0, r.Call(offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) {
|
|
|
|
CHECK_EQ(0, r.Call(offset));
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 11:50:30 +00:00
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(LoadMemF32_oob_asm) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<float, uint32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
|
|
|
float* memory = r.builder().AddMemoryElems<float>(8);
|
|
|
|
r.builder().RandomizeMemory(1112);
|
2016-05-11 11:50:30 +00:00
|
|
|
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_UNOP(kExprF32AsmjsLoadMem, WASM_LOCAL_GET(0)));
|
2016-05-11 11:50:30 +00:00
|
|
|
|
|
|
|
memory[0] = 9999.5f;
|
|
|
|
CHECK_EQ(9999.5f, r.Call(0u));
|
|
|
|
// TODO(titzer): offset 29-31 should also be OOB.
|
|
|
|
for (uint32_t offset = 32; offset < 40; offset++) {
|
|
|
|
CHECK(std::isnan(r.Call(offset)));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) {
|
|
|
|
CHECK(std::isnan(r.Call(offset)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(LoadMemF64_oob_asm) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<double, uint32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
|
|
|
double* memory = r.builder().AddMemoryElems<double>(8);
|
|
|
|
r.builder().RandomizeMemory(1112);
|
2016-05-11 11:50:30 +00:00
|
|
|
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_UNOP(kExprF64AsmjsLoadMem, WASM_LOCAL_GET(0)));
|
2016-05-11 11:50:30 +00:00
|
|
|
|
|
|
|
memory[0] = 9799.5;
|
|
|
|
CHECK_EQ(9799.5, r.Call(0u));
|
|
|
|
memory[1] = 11799.25;
|
|
|
|
CHECK_EQ(11799.25, r.Call(8u));
|
|
|
|
// TODO(titzer): offset 57-63 should also be OOB.
|
|
|
|
for (uint32_t offset = 64; offset < 80; offset++) {
|
|
|
|
CHECK(std::isnan(r.Call(offset)));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t offset = 0x80000000; offset < 0x80000010; offset++) {
|
|
|
|
CHECK(std::isnan(r.Call(offset)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(StoreMemI32_oob_asm) {
|
2018-08-21 15:01:31 +00:00
|
|
|
WasmRunner<int32_t, uint32_t, uint32_t> r(execution_tier);
|
2017-08-19 16:34:11 +00:00
|
|
|
r.builder().ChangeOriginToAsmjs();
|
|
|
|
int32_t* memory = r.builder().AddMemoryElems<int32_t>(8);
|
|
|
|
r.builder().RandomizeMemory(1112);
|
2016-05-11 11:50:30 +00:00
|
|
|
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_BINOP(kExprI32AsmjsStoreMem, WASM_LOCAL_GET(0),
|
|
|
|
WASM_LOCAL_GET(1)));
|
2016-05-11 11:50:30 +00:00
|
|
|
|
|
|
|
memory[0] = 7777;
|
|
|
|
CHECK_EQ(999999, r.Call(0u, 999999));
|
|
|
|
CHECK_EQ(999999, memory[0]);
|
|
|
|
// TODO(titzer): offset 29-31 should also be OOB.
|
|
|
|
for (uint32_t offset = 32; offset < 40; offset++) {
|
|
|
|
CHECK_EQ(8888, r.Call(offset, 8888));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t offset = 0x10000000; offset < 0xF0000000; offset += 0x1000000) {
|
|
|
|
CHECK_EQ(7777, r.Call(offset, 7777));
|
|
|
|
}
|
|
|
|
}
|
2016-06-13 04:20:59 +00:00
|
|
|
|
2019-05-29 08:29:42 +00:00
|
|
|
ASMJS_EXEC_TEST(Int32AsmjsDivS_byzero_const) {
|
|
|
|
for (int8_t denom = -2; denom < 8; ++denom) {
|
|
|
|
WasmRunner<int32_t, int32_t> r(execution_tier);
|
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_I32_ASMJS_DIVS(WASM_LOCAL_GET(0), WASM_I32V_1(denom)));
|
2019-05-29 08:29:42 +00:00
|
|
|
FOR_INT32_INPUTS(i) {
|
|
|
|
if (denom == 0) {
|
|
|
|
CHECK_EQ(0, r.Call(i));
|
|
|
|
} else if (denom == -1 && i == std::numeric_limits<int32_t>::min()) {
|
|
|
|
CHECK_EQ(std::numeric_limits<int32_t>::min(), r.Call(i));
|
|
|
|
} else {
|
|
|
|
CHECK_EQ(i / denom, r.Call(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASMJS_EXEC_TEST(Int32AsmjsRemS_byzero_const) {
|
|
|
|
for (int8_t denom = -2; denom < 8; ++denom) {
|
|
|
|
WasmRunner<int32_t, int32_t> r(execution_tier);
|
|
|
|
r.builder().ChangeOriginToAsmjs();
|
2020-12-17 16:55:33 +00:00
|
|
|
BUILD(r, WASM_I32_ASMJS_REMS(WASM_LOCAL_GET(0), WASM_I32V_1(denom)));
|
2019-05-29 08:29:42 +00:00
|
|
|
FOR_INT32_INPUTS(i) {
|
|
|
|
if (denom == 0) {
|
|
|
|
CHECK_EQ(0, r.Call(i));
|
|
|
|
} else if (denom == -1 && i == std::numeric_limits<int32_t>::min()) {
|
|
|
|
CHECK_EQ(0, r.Call(i));
|
|
|
|
} else {
|
|
|
|
CHECK_EQ(i % denom, r.Call(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ASMJS_EXEC_TEST
|
|
|
|
|
2017-09-01 12:57:34 +00:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|