Skip to content

Commit bd8dde5

Browse files
committed
[JSC] Remove UB for truncate-double-to-int32 by injecting conversion inline asm
https://bugs.webkit.org/show_bug.cgi?id=310490 rdar://173114293 Reviewed by Keith Miller and Justin Michaud. Recent clang does optimization based on this UB, breaking JSC when these UB is used. But this is **really hot** code in JSC, thus we should not use something slow-but-correct implementation. We should keep what we are getting as a codegen as is while removing UB to prevent compilers from breaking the meaning. This patch introduces helper functions which convers fp to integers, and just use these helper functions. They are just one inline asm in many cases. The purpose is just making sure that we should have solid semantics (not UB) for this one conversion. And the rest of code using this is just fine if this is not UB. We move tryConvertToStrictInt32 to WTF too, and make sure they are not having UB in slow path too. We implemented optimized inline asm for x64 / ARM64 so that these architectures do not see the difference from the currently generated code. We also add WTF_PROVEN_TRUE. This leverages __builtin_constant_p to do range analysis onto the input, so we do not lose the opportunities of constant folding. Tests: Tools/TestWebKitAPI/CMakeLists.txt Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj Tools/TestWebKitAPI/Tests/WTF/TruncateFloat.cpp * Source/JavaScriptCore/dfg/DFGOperations.cpp: (JSC::DFG::JSC_DEFINE_JIT_OPERATION): * Source/JavaScriptCore/runtime/CommonSlowPaths.cpp: (JSC::updateArithProfileForUnaryArithOp): (JSC::updateArithProfileForBinaryArithOp): (JSC::JSC_DEFINE_COMMON_SLOW_PATH): * Source/JavaScriptCore/runtime/JSCJSValueInlines.h: (JSC::JSValue::tryGetAsUint32Index): (JSC::JSValue::tryGetAsInt32): * Source/JavaScriptCore/runtime/MathCommon.cpp: (JSC::Math::JSC_DEFINE_NOEXCEPT_JIT_OPERATION): * Source/JavaScriptCore/runtime/MathCommon.h: (JSC::tryConvertToStrictInt32): Deleted. * Source/JavaScriptCore/runtime/NumberPrototype.cpp: (JSC::toStringWithRadix): (JSC::numberToStringInternal): * Source/JavaScriptCore/wasm/WasmFormat.h: (JSC::Wasm::internalizeExternref): * Source/WTF/wtf/MathExtras.h: (WTF::truncateDoubleToInt32): (WTF::truncateDoubleToInt64): (WTF::truncateDoubleToUint32): (WTF::truncateDoubleToUint64): (WTF::truncateFloatToInt32): (WTF::truncateFloatToInt64): (WTF::truncateFloatToUint32): (WTF::truncateFloatToUint64): (WTF::tryConvertToStrictInt32): * Tools/TestWebKitAPI/CMakeLists.txt: * Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * Tools/TestWebKitAPI/Tests/WTF/TruncateFloat.cpp: Added. (TestWebKitAPI::opaque): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToInt32_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToInt32_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToInt32_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToUint32_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToUint32_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToUint32_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToInt64_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToInt64_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToInt64_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToUint64_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToUint64_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateDoubleToUint64_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToInt32_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToInt32_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToInt32_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToUint32_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToUint32_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToUint32_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToInt64_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToInt64_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToInt64_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToUint64_InRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToUint64_InRange_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TruncateFloatToUint64_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_ExactIntegers)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_ExactIntegers_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_NonIntegers)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_NonIntegers_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_SpecialValues)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_SpecialValues_Opaque)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_OutOfRange)): (TestWebKitAPI::TEST(WTF_MathExtras, TryConvertToStrictInt32_OutOfRange_Opaque)): Canonical link: https://commits.webkit.org/309786@main
1 parent c959242 commit bd8dde5

11 files changed

Lines changed: 807 additions & 39 deletions

File tree

‎Source/JavaScriptCore/dfg/DFGOperations.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4888,7 +4888,7 @@ JSC_DEFINE_JIT_OPERATION(operationNewArrayWithSpecies, JSObject*, (JSGlobalObjec
48884888
JITOperationPrologueCallFrameTracer tracer(vm, callFrame);
48894889
auto scope = DECLARE_THROW_SCOPE(vm);
48904890

4891-
uint64_t length = static_cast<uint64_t>(JSValue::decode(encodedLength).asNumber());
4891+
uint64_t length = truncateDoubleToUint64(JSValue::decode(encodedLength).asNumber());
48924892
OPERATION_RETURN(scope, newArrayWithSpeciesImpl(globalObject, length, array, indexingType));
48934893
}
48944894

‎Source/JavaScriptCore/runtime/CommonSlowPaths.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ static void updateArithProfileForUnaryArithOp(UnaryArithProfile& profile, JSValu
415415
// Therefore, we will get a false positive if the result is that value. This is intentionally
416416
// done to simplify the checking algorithm.
417417
static const int64_t int52OverflowPoint = (1ll << 51);
418-
int64_t int64Val = static_cast<int64_t>(std::abs(doubleVal));
418+
int64_t int64Val = truncateDoubleToInt64(std::abs(doubleVal));
419419
if (int64Val >= int52OverflowPoint)
420420
profile.setObservedInt52Overflow();
421421
}
@@ -481,7 +481,7 @@ static void updateArithProfileForBinaryArithOp(JSGlobalObject*, CodeBlock* codeB
481481
// Therefore, we will get a false positive if the result is that value. This is intentionally
482482
// done to simplify the checking algorithm.
483483
static const int64_t int52OverflowPoint = (1ll << 51);
484-
int64_t int64Val = static_cast<int64_t>(std::abs(doubleVal));
484+
int64_t int64Val = truncateDoubleToInt64(std::abs(doubleVal));
485485
if (int64Val >= int52OverflowPoint)
486486
profile.setObservedInt52Overflow();
487487
}
@@ -1354,7 +1354,7 @@ JSC_DEFINE_COMMON_SLOW_PATH(slow_path_new_array_with_species)
13541354
BEGIN();
13551355
auto bytecode = pc->as<OpNewArrayWithSpecies>();
13561356
JSObject* array = asObject(GET_C(bytecode.m_array).jsValue());
1357-
uint64_t length = static_cast<uint64_t>(GET_C(bytecode.m_length).jsValue().asNumber());
1357+
uint64_t length = truncateDoubleToUint64(GET_C(bytecode.m_length).jsValue().asNumber());
13581358
auto& metadata = bytecode.metadata(codeBlock);
13591359
auto& arrayAllocationProfile = metadata.m_arrayAllocationProfile;
13601360
auto& arrayProfile = metadata.m_arrayProfile;

‎Source/JavaScriptCore/runtime/JSCJSValueInlines.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ inline std::optional<uint32_t> JSValue::tryGetAsUint32Index()
125125
}
126126
if (isNumber()) {
127127
double number = asNumber();
128-
uint32_t asUint = static_cast<uint32_t>(number);
128+
uint32_t asUint = static_cast<uint32_t>(truncateDoubleToUint64(number));
129129
if (static_cast<double>(asUint) == number && isIndex(asUint))
130130
return asUint;
131131
}
@@ -138,7 +138,7 @@ inline std::optional<int32_t> JSValue::tryGetAsInt32()
138138
return asInt32();
139139
if (isNumber()) {
140140
double number = asNumber();
141-
int32_t asInt = static_cast<int32_t>(number);
141+
int32_t asInt = truncateDoubleToInt32(number);
142142
if (static_cast<double>(asInt) == number)
143143
return asInt;
144144
}

‎Source/JavaScriptCore/runtime/MathCommon.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,10 @@ JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_div_u, uint64_t, (uint64_t a, uint64_t b))
640640
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_rem_s, int64_t, (int64_t a, int64_t b)) { return a % b; }
641641
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_rem_u, uint64_t, (uint64_t a, uint64_t b)) { return a % b; }
642642

643-
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_u_f32, uint64_t, (float operand)) { return static_cast<uint64_t>(operand); }
644-
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_s_f32, int64_t, (float operand)) { return static_cast<int64_t>(operand); }
645-
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_u_f64, uint64_t, (double operand)) { return static_cast<uint64_t>(operand); }
646-
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_s_f64, int64_t, (double operand)) { return static_cast<int64_t>(operand); }
643+
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_u_f32, uint64_t, (float operand)) { return truncateFloatToUint64(operand); }
644+
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_s_f32, int64_t, (float operand)) { return truncateFloatToInt64(operand); }
645+
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_u_f64, uint64_t, (double operand)) { return truncateDoubleToUint64(operand); }
646+
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(i64_trunc_s_f64, int64_t, (double operand)) { return truncateDoubleToInt64(operand); }
647647

648648
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(f32_convert_u_i64, float, (uint64_t operand)) { return static_cast<float>(operand); }
649649
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(f32_convert_s_i64, float, (int64_t operand)) { return static_cast<float>(operand); }

‎Source/JavaScriptCore/runtime/MathCommon.h‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <climits>
3232
#include <cmath>
3333
#include <optional>
34+
#include <wtf/MathExtras.h>
3435

3536
namespace JSC {
3637

@@ -240,32 +241,6 @@ inline std::optional<double> safeReciprocalForDivByConst(double constant)
240241
return reciprocal;
241242
}
242243

243-
ALWAYS_INLINE std::optional<int32_t> tryConvertToStrictInt32(double value)
244-
{
245-
#if HAVE(FJCVTZS_INSTRUCTION)
246-
int32_t result;
247-
bool isExact;
248-
__asm__(
249-
"fjcvtzs %w0, %d2"
250-
: "=r" (result), "=@cceq" (isExact)
251-
: "w" (value)
252-
: "cc");
253-
if (isExact)
254-
return result;
255-
return std::nullopt;
256-
#else
257-
if (std::isinf(value) || std::isnan(value))
258-
return std::nullopt;
259-
260-
// Note that -0.0 is not StrictInt32.
261-
const int32_t asInt32 = static_cast<int32_t>(value);
262-
if (!(asInt32 != value || (!asInt32 && std::signbit(value))))
263-
return asInt32;
264-
265-
return std::nullopt;
266-
#endif
267-
}
268-
269244
extern "C" {
270245
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(jsRound, double, (double));
271246
}
@@ -377,6 +352,7 @@ JSC_DECLARE_NOEXCEPT_JIT_OPERATION(roundDouble, double, (double));
377352
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(jsRoundDouble, double, (double));
378353
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(roundFloat, float, (float));
379354

355+
// FIXME: Remote them. These functions were only used in 32bit wasm.
380356
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(f32_nearest, float, (float));
381357
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(f64_nearest, double, (double));
382358
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(f32_roundeven, float, (float));

‎Source/JavaScriptCore/runtime/NumberPrototype.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ String toStringWithRadix(double doubleValue, int32_t radix)
375375
{
376376
ASSERT(2 <= radix && radix <= 36);
377377

378-
int32_t integerValue = static_cast<int32_t>(doubleValue);
378+
int32_t integerValue = truncateDoubleToInt32(doubleValue);
379379
if (integerValue == doubleValue)
380380
return toStringWithRadixInternal(integerValue, radix);
381381

@@ -560,7 +560,7 @@ static ALWAYS_INLINE JSString* numberToStringInternal(VM& vm, double doubleValue
560560
{
561561
ASSERT(!(radix < 2 || radix > 36));
562562

563-
int32_t integerValue = static_cast<int32_t>(doubleValue);
563+
int32_t integerValue = truncateDoubleToInt32(doubleValue);
564564
if (integerValue == doubleValue)
565565
return int32ToStringInternal(vm, integerValue, radix);
566566

‎Source/JavaScriptCore/wasm/WasmFormat.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ inline size_t sizeOfType(TypeKind kind)
555555
inline JSValue internalizeExternref(JSValue value)
556556
{
557557
if (value.isDouble()) {
558-
if (auto int32Value = JSC::tryConvertToStrictInt32(value.asDouble())) {
558+
if (auto int32Value = tryConvertToStrictInt32(value.asDouble())) {
559559
if (int32Value.value() <= Wasm::maxI31ref && int32Value.value() >= Wasm::minI31ref)
560560
return jsNumber(int32Value.value());
561561
}

0 commit comments

Comments
 (0)