Repository navigation
Commit bd8dde5
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@main1 parent c959242 commit bd8dde5
11 files changed
Lines changed: 807 additions & 39 deletions
File tree
- Source
- JavaScriptCore
- dfg
- runtime
- wasm
- WTF/wtf
- Tools/TestWebKitAPI
- TestWebKitAPI.xcodeproj
- Tests/WTF
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4888 | 4888 | | |
4889 | 4889 | | |
4890 | 4890 | | |
4891 | | - | |
| 4891 | + | |
4892 | 4892 | | |
4893 | 4893 | | |
4894 | 4894 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
415 | 415 | | |
416 | 416 | | |
417 | 417 | | |
418 | | - | |
| 418 | + | |
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
| |||
481 | 481 | | |
482 | 482 | | |
483 | 483 | | |
484 | | - | |
| 484 | + | |
485 | 485 | | |
486 | 486 | | |
487 | 487 | | |
| |||
1354 | 1354 | | |
1355 | 1355 | | |
1356 | 1356 | | |
1357 | | - | |
| 1357 | + | |
1358 | 1358 | | |
1359 | 1359 | | |
1360 | 1360 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
141 | | - | |
| 141 | + | |
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
640 | 640 | | |
641 | 641 | | |
642 | 642 | | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
240 | 241 | | |
241 | 242 | | |
242 | 243 | | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | 244 | | |
270 | 245 | | |
271 | 246 | | |
| |||
377 | 352 | | |
378 | 353 | | |
379 | 354 | | |
| 355 | + | |
380 | 356 | | |
381 | 357 | | |
382 | 358 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
375 | 375 | | |
376 | 376 | | |
377 | 377 | | |
378 | | - | |
| 378 | + | |
379 | 379 | | |
380 | 380 | | |
381 | 381 | | |
| |||
560 | 560 | | |
561 | 561 | | |
562 | 562 | | |
563 | | - | |
| 563 | + | |
564 | 564 | | |
565 | 565 | | |
566 | 566 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
555 | 555 | | |
556 | 556 | | |
557 | 557 | | |
558 | | - | |
| 558 | + | |
559 | 559 | | |
560 | 560 | | |
561 | 561 | | |
| |||
0 commit comments