Changeset 235071 in webkit
- Timestamp:
- Aug 20, 2018 7:07:52 AM (6 years ago)
- Location:
- releases/WebKitGTK/webkit-2.22/Source
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
releases/WebKitGTK/webkit-2.22/Source/JavaScriptCore/ChangeLog
r235067 r235071 1 2018-08-19 Yusuke Suzuki <yusukesuzuki@slowstart.org> 2 3 [WTF] Add WTF::unalignedLoad and WTF::unalignedStore 4 https://bugs.webkit.org/show_bug.cgi?id=188716 5 6 Reviewed by Darin Adler. 7 8 Use WTF::unalignedLoad and WTF::unalignedStore to avoid undefined behavior. 9 The compiler can emit appropriate mov operations in x86 even if we use these 10 helper functions. 11 12 * assembler/AssemblerBuffer.h: 13 (JSC::AssemblerBuffer::LocalWriter::putIntegralUnchecked): 14 (JSC::AssemblerBuffer::putIntegral): 15 (JSC::AssemblerBuffer::putIntegralUnchecked): 16 * assembler/MacroAssemblerX86.h: 17 (JSC::MacroAssemblerX86::readCallTarget): 18 * assembler/X86Assembler.h: 19 (JSC::X86Assembler::linkJump): 20 (JSC::X86Assembler::readPointer): 21 (JSC::X86Assembler::replaceWithHlt): 22 (JSC::X86Assembler::replaceWithJump): 23 (JSC::X86Assembler::setPointer): 24 (JSC::X86Assembler::setInt32): 25 (JSC::X86Assembler::setInt8): 26 * interpreter/InterpreterInlines.h: 27 (JSC::Interpreter::getOpcodeID): Embedded opcode may be misaligned. Actually UBSan detects misaligned accesses here. 28 1 29 2018-08-17 Saam barati <sbarati@apple.com> 2 30 -
releases/WebKitGTK/webkit-2.22/Source/JavaScriptCore/assembler/AssemblerBuffer.h
r228582 r235071 35 35 #include <wtf/FastMalloc.h> 36 36 #include <wtf/StdLibExtras.h> 37 #include <wtf/UnalignedAccess.h> 37 38 38 39 namespace JSC { … … 240 241 { 241 242 ASSERT(m_index + sizeof(IntegralType) <= m_buffer.m_storage.capacity()); 242 *reinterpret_cast_ptr<IntegralType*>(m_storageBuffer + m_index) = value;243 WTF::unalignedStore<IntegralType>(m_storageBuffer + m_index, value); 243 244 m_index += sizeof(IntegralType); 244 245 } … … 259 260 if (UNLIKELY(nextIndex > m_storage.capacity())) 260 261 outOfLineGrow(); 261 ASSERT(isAvailable(sizeof(IntegralType))); 262 *reinterpret_cast_ptr<IntegralType*>(m_storage.buffer() + m_index) = value; 263 m_index = nextIndex; 262 putIntegralUnchecked<IntegralType>(value); 264 263 } 265 264 … … 268 267 { 269 268 ASSERT(isAvailable(sizeof(IntegralType))); 270 *reinterpret_cast_ptr<IntegralType*>(m_storage.buffer() + m_index) = value;269 WTF::unalignedStore<IntegralType>(m_storage.buffer() + m_index, value); 271 270 m_index += sizeof(IntegralType); 272 271 } -
releases/WebKitGTK/webkit-2.22/Source/JavaScriptCore/assembler/MacroAssemblerX86.h
r230748 r235071 303 303 static FunctionPtr<resultTag> readCallTarget(CodeLocationCall<locationTag> call) 304 304 { 305 intptr_t offset = reinterpret_cast<int32_t*>(call.dataLocation())[-1];305 intptr_t offset = WTF::unalignedLoad<int32_t>(bitwise_cast<int32_t*>(call.dataLocation()) - 1); 306 306 return FunctionPtr<resultTag>(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(call.dataLocation()) + offset)); 307 307 } -
releases/WebKitGTK/webkit-2.22/Source/JavaScriptCore/assembler/X86Assembler.h
r235057 r235071 3681 3681 3682 3682 char* code = reinterpret_cast<char*>(m_formatter.data()); 3683 ASSERT(! reinterpret_cast<int32_t*>(code + from.m_offset)[-1]);3683 ASSERT(!WTF::unalignedLoad<int32_t>(bitwise_cast<int32_t*>(code + from.m_offset) - 1)); 3684 3684 setRel32(code + from.m_offset, code + to.m_offset); 3685 3685 } … … 3740 3740 static void* readPointer(void* where) 3741 3741 { 3742 return reinterpret_cast<void**>(where)[-1];3742 return WTF::unalignedLoad<void*>(bitwise_cast<void**>(where) - 1); 3743 3743 } 3744 3744 3745 3745 static void replaceWithHlt(void* instructionStart) 3746 3746 { 3747 uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart); 3748 ptr[0] = static_cast<uint8_t>(OP_HLT); 3747 WTF::unalignedStore<uint8_t>(instructionStart, static_cast<uint8_t>(OP_HLT)); 3749 3748 } 3750 3749 3751 3750 static void replaceWithJump(void* instructionStart, void* to) 3752 3751 { 3753 uint8_t* ptr = reinterpret_cast<uint8_t*>(instructionStart);3754 uint8_t* dstPtr = reinterpret_cast<uint8_t*>(to);3752 uint8_t* ptr = bitwise_cast<uint8_t*>(instructionStart); 3753 uint8_t* dstPtr = bitwise_cast<uint8_t*>(to); 3755 3754 intptr_t distance = (intptr_t)(dstPtr - (ptr + 5)); 3756 ptr[0] = static_cast<uint8_t>(OP_JMP_rel32);3757 *reinterpret_cast<int32_t*>(ptr + 1) = static_cast<int32_t>(distance);3755 WTF::unalignedStore<uint8_t>(ptr, static_cast<uint8_t>(OP_JMP_rel32)); 3756 WTF::unalignedStore<int32_t>(ptr + 1, static_cast<int32_t>(distance)); 3758 3757 } 3759 3758 … … 3957 3956 static void setPointer(void* where, void* value) 3958 3957 { 3959 reinterpret_cast<void**>(where)[-1] = value;3958 WTF::unalignedStore<void*>(bitwise_cast<void**>(where) - 1, value); 3960 3959 } 3961 3960 3962 3961 static void setInt32(void* where, int32_t value) 3963 3962 { 3964 reinterpret_cast<int32_t*>(where)[-1] = value;3963 WTF::unalignedStore<int32_t>(bitwise_cast<int32_t*>(where) - 1, value); 3965 3964 } 3966 3965 3967 3966 static void setInt8(void* where, int8_t value) 3968 3967 { 3969 reinterpret_cast<int8_t*>(where)[-1] = value;3968 WTF::unalignedStore<int8_t>(bitwise_cast<int8_t*>(where) - 1, value); 3970 3969 } 3971 3970 -
releases/WebKitGTK/webkit-2.22/Source/JavaScriptCore/interpreter/InterpreterInlines.h
r231741 r235071 34 34 #include "LLIntData.h" 35 35 #include "UnlinkedCodeBlock.h" 36 #include <wtf/UnalignedAccess.h> 36 37 37 38 namespace JSC { … … 52 53 auto codePtr = MacroAssemblerCodePtr<BytecodePtrTag>::createFromExecutableAddress(opcode); 53 54 int32_t* opcodeIDAddress = codePtr.dataLocation<int32_t*>() - 1; 54 OpcodeID opcodeID = static_cast<OpcodeID>( *opcodeIDAddress);55 OpcodeID opcodeID = static_cast<OpcodeID>(WTF::unalignedLoad<int32_t>(opcodeIDAddress)); 55 56 ASSERT(opcodeID < NUMBER_OF_BYTECODE_IDS); 56 57 return opcodeID; -
releases/WebKitGTK/webkit-2.22/Source/WTF/ChangeLog
r235054 r235071 1 2018-08-19 Yusuke Suzuki <yusukesuzuki@slowstart.org> 2 3 [WTF] Add WTF::unalignedLoad and WTF::unalignedStore 4 https://bugs.webkit.org/show_bug.cgi?id=188716 5 6 Reviewed by Darin Adler. 7 8 While some CPUs allow unaligned accesses to memory, doing it in C++ with `reinterpret_cast<>` is 9 undefined behavior. This patch adds WTF::{unalignedLoad,unalignedStore} helper functions, which 10 can load from and store to the pointer in an unaligned manner. 11 Actual implementation uses `memcpy`. This can be optimized to direct unaligned access operations 12 in supported CPUs like x86. Even though a CPU does not support unaligned accesses, memcpy is still 13 safe and the compiler emits appropriate code. 14 15 We name these functions `unalignedLoad` and `unalignedStore` instead of `loadUnaligned` and `storeUnaligned` 16 in order to align them to `atomicLoad` and `atomicStore`. 17 18 * WTF.xcodeproj/project.pbxproj: 19 * wtf/CMakeLists.txt: 20 * wtf/UnalignedAccess.h: Added. 21 (WTF::unalignedLoad): 22 (WTF::unalignedStore): 23 * wtf/text/StringCommon.h: 24 (WTF::equal): 25 (WTF::loadUnaligned): Deleted. 26 1 27 2018-08-17 David Kilzer <ddkilzer@apple.com> 2 28 -
releases/WebKitGTK/webkit-2.22/Source/WTF/WTF.xcodeproj/project.pbxproj
r234685 r235071 624 624 E3200AB41E9A536D003B59D2 /* PlatformRegisters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlatformRegisters.h; sourceTree = "<group>"; }; 625 625 E33D5F871FBED66700BF625E /* RecursableLambda.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecursableLambda.h; sourceTree = "<group>"; }; 626 E360C7642127B85B00C90F0E /* UnalignedAccess.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UnalignedAccess.h; sourceTree = "<group>"; }; 627 E360C7652127B85C00C90F0E /* Unexpected.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Unexpected.h; sourceTree = "<group>"; }; 626 628 E388886D20C9095100E632BC /* WorkerPool.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkerPool.cpp; sourceTree = "<group>"; }; 627 629 E388886E20C9095100E632BC /* WorkerPool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerPool.h; sourceTree = "<group>"; }; … … 1118 1120 149EF16216BBFE0D000A4331 /* TriState.h */, 1119 1121 83FBA93119DF459700F30ADB /* TypeCasts.h */, 1122 E360C7642127B85B00C90F0E /* UnalignedAccess.h */, 1123 E360C7652127B85C00C90F0E /* Unexpected.h */, 1120 1124 A8A4735C151A825B004123FF /* UnionFind.h */, 1121 1125 E300E521203D645F00DA79BE /* UniqueArray.h */, -
releases/WebKitGTK/webkit-2.22/Source/WTF/wtf/CMakeLists.txt
r234677 r235071 241 241 TypeCasts.h 242 242 UUID.h 243 UnalignedAccess.h 243 244 Unexpected.h 244 245 UniStdExtras.h -
releases/WebKitGTK/webkit-2.22/Source/WTF/wtf/text/StringCommon.h
r225618 r235071 31 31 #include <wtf/ASCIICType.h> 32 32 #include <wtf/NotFound.h> 33 #include <wtf/UnalignedAccess.h> 33 34 34 35 namespace WTF { … … 48 49 bool equalIgnoringASCIICase(const char*, const char*); 49 50 template<unsigned lowercaseLettersLength> bool equalLettersIgnoringASCIICase(const char*, const char (&lowercaseLetters)[lowercaseLettersLength]); 50 51 template<typename T>52 inline T loadUnaligned(const char* s)53 {54 #if COMPILER(CLANG)55 T tmp;56 memcpy(&tmp, s, sizeof(T));57 return tmp;58 #else59 // This may result in undefined behavior due to unaligned access.60 return *reinterpret_cast<const T*>(s);61 #endif62 }63 51 64 52 // Do comparisons 8 or 4 bytes-at-a-time on architectures where it's safe. … … 73 61 if (dwordLength) { 74 62 for (unsigned i = 0; i != dwordLength; ++i) { 75 if ( loadUnaligned<uint64_t>(a) != loadUnaligned<uint64_t>(b))63 if (unalignedLoad<uint64_t>(a) != unalignedLoad<uint64_t>(b)) 76 64 return false; 77 65 … … 82 70 83 71 if (length & 4) { 84 if ( loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))72 if (unalignedLoad<uint32_t>(a) != unalignedLoad<uint32_t>(b)) 85 73 return false; 86 74 … … 90 78 91 79 if (length & 2) { 92 if ( loadUnaligned<uint16_t>(a) != loadUnaligned<uint16_t>(b))80 if (unalignedLoad<uint16_t>(a) != unalignedLoad<uint16_t>(b)) 93 81 return false; 94 82 … … 112 100 if (dwordLength) { 113 101 for (unsigned i = 0; i != dwordLength; ++i) { 114 if ( loadUnaligned<uint64_t>(a) != loadUnaligned<uint64_t>(b))102 if (unalignedLoad<uint64_t>(a) != unalignedLoad<uint64_t>(b)) 115 103 return false; 116 104 … … 121 109 122 110 if (length & 2) { 123 if ( loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))111 if (unalignedLoad<uint32_t>(a) != unalignedLoad<uint32_t>(b)) 124 112 return false; 125 113 … … 141 129 unsigned wordLength = length >> 2; 142 130 for (unsigned i = 0; i != wordLength; ++i) { 143 if ( loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))131 if (unalignedLoad<uint32_t>(a) != unalignedLoad<uint32_t>(b)) 144 132 return false; 145 133 a += sizeof(uint32_t); … … 169 157 unsigned wordLength = length >> 1; 170 158 for (unsigned i = 0; i != wordLength; ++i) { 171 if ( loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))159 if (unalignedLoad<uint32_t>(a) != unalignedLoad<uint32_t>(b)) 172 160 return false; 173 161 a += sizeof(uint32_t);
Note: See TracChangeset
for help on using the changeset viewer.