Skip to content
Permalink
Browse files
Introducing RawHex, a counterpart to RawPointer
https://bugs.webkit.org/show_bug.cgi?id=241743

Reviewed by Saam Barati.

RawHex is for dumping integral values in hex just like RawPointer is for dumping
pointers.  And similarly, RawHex is meant to be used with PrintStream.

For example:
    dataLog(RawHex(42));        // prints 0x2a
    dataLog(RawHex(0x42);       // prints 0x42
    dataLog(RawHex(256));       // prints 0x100
    dataLog(RawHex(65536);      // prints 0x10000
    dataLog(RawHex(4294967295); // prints 0xffffffff
    dataLog(RawHex(4294967296); // prints 0x100000000
    dataLog(RawHex(INT8_MIN);   // prints 0x80
    dataLog(RawHex(INT16_MIN);  // prints 0x8000
    dataLog(RawHex(INT32_MIN);  // prints 0x80000000
    dataLog(RawHex(-1);         // prints 0xffffffff
    dataLog(RawHex(INT64_MIN);  // prints 0x8000000000000000
    dataLog(RawHex(INT64_MAX);  // prints 0x7fffffffffffffff
    dataLog(RawHex(UINT64_MAX); // prints 0xffffffffffffffff

Also fixed up places where we were casting integral values into pointers, and
then using RawPointer to dump them in hex.

* Source/JavaScriptCore/bytecode/BytecodeDumper.cpp:
(JSC::Wasm::BytecodeDumper::formatConstant const):
* Source/JavaScriptCore/interpreter/StackVisitor.cpp:
(JSC::StackVisitor::Frame::dump const):
* Source/JavaScriptCore/runtime/ExecutableBase.cpp:
(JSC::ExecutableBase::dump const):
* Source/JavaScriptCore/runtime/LazyPropertyInlines.h:
(JSC::ElementType>::dump const):
* Source/JavaScriptCore/runtime/SamplingProfiler.cpp:
(JSC::SamplingProfiler::reportTopBytecodes):
* Source/JavaScriptCore/tools/Integrity.cpp:
(JSC::Integrity::Random::reloadAndCheckShouldAuditSlow):
* Source/JavaScriptCore/wasm/WasmFunctionParser.h:
(JSC::Wasm::FunctionParser<Context>::parseBody):
* Source/JavaScriptCore/wasm/WasmOpcodeOrigin.cpp:
(JSC::Wasm::OpcodeOrigin::dump const):
* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/CMakeLists.txt:
* Source/WTF/wtf/PrintStream.cpp:
(WTF::printInternal):
* Source/WTF/wtf/PrintStream.h:
* Source/WTF/wtf/RawHex.h: Added.
(WTF::RawHex::RawHex):
(WTF::RawHex::ptr const):
(WTF::RawHex::is64Bit const):
(WTF::RawHex::u64 const):

Canonical link: https://commits.webkit.org/251665@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@295660 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Mark Lam committed Jun 19, 2022
1 parent 627fc1e commit 1726a5ef82bb7a05b9e201f1849caa5aae4cc366
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 19 deletions.
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>
* Copyright (C) 2017-2019 Apple Inc. All rights reserved.
* Copyright (C) 2017-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -413,7 +413,7 @@ CString BytecodeDumper::formatConstant(Type type, uint64_t constant) const
if (isFuncref(type) || isExternref(type)) {
if (JSValue::decode(constant) == jsNull())
return "null";
return toCString(RawPointer(bitwise_cast<void*>(static_cast<uintptr_t>(constant))));
return toCString(RawHex(constant));
}

RELEASE_ASSERT_NOT_REACHED();
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2013-2019 Apple Inc. All rights reserved.
* Copyright (C) 2013-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -462,7 +462,7 @@ void StackVisitor::Frame::dump(PrintStream& out, Indenter indent, WTF::Function<
out.print(indent, "callerFrame: ", RawPointer(callerFrame), "\n");
uintptr_t locationRawBits = callFrame->callSiteAsRawBits();
out.print(indent, "rawLocationBits: ", locationRawBits,
" ", RawPointer(reinterpret_cast<void*>(locationRawBits)), "\n");
" ", RawHex(locationRawBits), "\n");
out.print(indent, "codeBlock: ", RawPointer(codeBlock));
if (codeBlock)
out.print(" ", *codeBlock);
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2018 Apple Inc. All rights reserved.
* Copyright (C) 2009-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -49,7 +49,7 @@ void ExecutableBase::dump(PrintStream& out) const
switch (type()) {
case NativeExecutableType: {
NativeExecutable* native = jsCast<NativeExecutable*>(realThis);
out.print("NativeExecutable:", RawPointer(bitwise_cast<void*>(native->function())), "/", RawPointer(bitwise_cast<void*>(native->constructor())));
out.print("NativeExecutable:", RawPointer(native->function().rawPointer()), "/", RawPointer(native->constructor().rawPointer()));
return;
}
case EvalExecutableType: {
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2021 Apple Inc. All rights reserved.
* Copyright (C) 2016-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -83,12 +83,12 @@ void LazyProperty<OwnerType, ElementType>::dump(PrintStream& out) const
return;
}
if (m_pointer & lazyTag) {
out.print("Lazy:", RawPointer(bitwise_cast<void*>(m_pointer & ~lazyTag)));
out.print("Lazy:", RawHex(m_pointer & ~lazyTag));
if (m_pointer & initializingTag)
out.print("(Initializing)");
return;
}
out.print(RawPointer(bitwise_cast<void*>(m_pointer)));
out.print(RawHex(m_pointer));
}

template<typename OwnerType, typename ElementType>
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2021 Apple Inc. All rights reserved.
* Copyright (C) 2016-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -1196,7 +1196,7 @@ void SamplingProfiler::reportTopBytecodes(PrintStream& out)
description.print(":");
if (wasmOffset) {
uintptr_t offset = wasmOffset.offset();
description.print(RawPointer(bitwise_cast<void*>(offset)));
description.print(RawHex(offset));
} else
description.print("nil");
return description.toString();
@@ -54,7 +54,7 @@ bool Random::reloadAndCheckShouldAuditSlow(VM& vm)
if (!Options::randomIntegrityAuditRate()) {
m_triggerBits = 0; // Never trigger, and don't bother reloading.
if (IntegrityInternal::verbose)
dataLogLn("disabled Integrity audits: trigger bits ", RawPointer(reinterpret_cast<void*>(m_triggerBits)));
dataLogLn("disabled Integrity audits: trigger bits ", RawHex(m_triggerBits));
return false;
}

@@ -67,7 +67,7 @@ bool Random::reloadAndCheckShouldAuditSlow(VM& vm)
m_triggerBits = m_triggerBits | (static_cast<uint64_t>(trigger) << i);
}
if (IntegrityInternal::verbose)
dataLogLn("reloaded Integrity trigger bits ", RawPointer(reinterpret_cast<void*>(m_triggerBits)));
dataLogLn("reloaded Integrity trigger bits ", RawHex(m_triggerBits));
ASSERT(m_triggerBits >= (1ull << 63));
return vm.random().getUint32() <= threshold;
}
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2019 Apple Inc. All rights reserved.
* Copyright (C) 2016-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -288,7 +288,7 @@ auto FunctionParser<Context>::parseBody() -> PartialResult
m_currentOpcode = static_cast<OpType>(op);

if (verbose) {
dataLogLn("processing op (", m_unreachableBlocks, "): ", RawPointer(reinterpret_cast<void*>(op)), ", ", makeString(static_cast<OpType>(op)), " at offset: ", RawPointer(reinterpret_cast<void*>(m_offset)));
dataLogLn("processing op (", m_unreachableBlocks, "): ", RawHex(op), ", ", makeString(static_cast<OpType>(op)), " at offset: ", RawHex(m_offset));
m_context.dump(m_controlStack, &m_expressionStack);
}

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
* Copyright (C) 2017-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -32,7 +32,7 @@ namespace JSC { namespace Wasm {

void OpcodeOrigin::dump(PrintStream& out) const
{
out.print("{opcode: ", makeString(opcode()), ", location: ", RawPointer(reinterpret_cast<void*>(location())), "}");
out.print("{opcode: ", makeString(opcode()), ", location: ", RawHex(location()), "}");
}

} } // namespace JSC::Wasm
@@ -810,6 +810,7 @@
FE1E2C3B2240C06600F6B729 /* PtrTag.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1E2C392240C05400F6B729 /* PtrTag.cpp */; };
FE1E2C42224187C600F6B729 /* PlatformRegisters.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1E2C41224187C600F6B729 /* PlatformRegisters.cpp */; };
FE35D09227DC5ECC009DFA5B /* StackCheck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE35D09127DC5ECC009DFA5B /* StackCheck.cpp */; };
FE7ACFEF285DC2F9007FC8E9 /* RawHex.h in Headers */ = {isa = PBXBuildFile; fileRef = FE7ACFEE285DC2F8007FC8E9 /* RawHex.h */; settings = {ATTRIBUTES = (Private, ); }; };
FEDACD3D1630F83F00C69634 /* StackStats.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEDACD3B1630F83F00C69634 /* StackStats.cpp */; };
FEEA4DF9216D7BE400AC0602 /* StackPointer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEEA4DF8216D7BE400AC0602 /* StackPointer.cpp */; };
FF895F11282AB96400E7BAE8 /* CompactRefPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = FF895F10282AB96400E7BAE8 /* CompactRefPtr.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -1706,6 +1707,7 @@
FE3842342325CC80009DD445 /* ResourceUsage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ResourceUsage.h; sourceTree = "<group>"; };
FE7497E4208FFCAA0003565B /* PtrTag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PtrTag.h; sourceTree = "<group>"; };
FE7497ED209163060003565B /* MetaAllocatorPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MetaAllocatorPtr.h; sourceTree = "<group>"; };
FE7ACFEE285DC2F8007FC8E9 /* RawHex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RawHex.h; sourceTree = "<group>"; };
FE8225301B2A1E5B00BA68FD /* NakedPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NakedPtr.h; sourceTree = "<group>"; };
FE86A8741E59440200111BBF /* ForbidHeapAllocation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ForbidHeapAllocation.h; sourceTree = "<group>"; };
FE8925AF1D00DAEC0046907E /* Indenter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Indenter.h; sourceTree = "<group>"; };
@@ -2175,6 +2177,7 @@
A8A472FD151A825B004123FF /* RandomNumberSeed.h */,
0F2AC5601E89F70C0001EE3F /* Range.h */,
0F725CAB1C50461600AD943A /* RangeSet.h */,
FE7ACFEE285DC2F8007FC8E9 /* RawHex.h */,
0F87105916643F190090B0AD /* RawPointer.h */,
FE05FAE61FDB214300093230 /* RawPtrTraits.h */,
AD653DA82006B6C200D820D7 /* RawValueTraits.h */,
@@ -3219,6 +3222,7 @@
DD4901E627B4748A00D7E50D /* UniStdExtras.h in Headers */,
DD3DC97027A4BF8E007E5B61 /* UnsafePointer.h in Headers */,
DD3DC90327A4BF8E007E5B61 /* URL.h in Headers */,
FE7ACFEF285DC2F9007FC8E9 /* RawHex.h in Headers */,
DD3DC8ED27A4BF8E007E5B61 /* URLHash.h in Headers */,
DD3DC99A27A4BF8E007E5B61 /* URLHelpers.h in Headers */,
DD3DC8BD27A4BF8E007E5B61 /* URLParser.h in Headers */,
@@ -218,6 +218,7 @@ set(WTF_PUBLIC_HEADERS
RandomNumberSeed.h
Range.h
RangeSet.h
RawHex.h
RawPointer.h
RawPtrTraits.h
RawValueTraits.h
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012 Apple Inc. All rights reserved.
* Copyright (C) 2012-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -184,6 +184,17 @@ void printInternal(PrintStream& out, double value)
out.printf("%lf", value);
}

void printInternal(PrintStream& out, RawHex value)
{
#if !CPU(ADDRESS64)
if (value.is64Bit()) {
out.printf("0x%" PRIx64, value.u64());
return;
}
#endif
out.printf("%p", value.ptr());
}

void printInternal(PrintStream& out, RawPointer value)
{
out.printf("%p", value.value());
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2018 Apple Inc. All rights reserved.
* Copyright (C) 2012-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -33,6 +33,7 @@
#include <wtf/FastMalloc.h>
#include <wtf/FixedWidthDouble.h>
#include <wtf/Noncopyable.h>
#include <wtf/RawHex.h>
#include <wtf/RawPointer.h>
#include <wtf/RefPtr.h>
#include <wtf/StdLibExtras.h>
@@ -123,6 +124,7 @@ WTF_EXPORT_PRIVATE void printInternal(PrintStream&, long long);
WTF_EXPORT_PRIVATE void printInternal(PrintStream&, unsigned long long);
WTF_EXPORT_PRIVATE void printInternal(PrintStream&, float);
WTF_EXPORT_PRIVATE void printInternal(PrintStream&, double);
WTF_EXPORT_PRIVATE void printInternal(PrintStream&, RawHex);
WTF_EXPORT_PRIVATE void printInternal(PrintStream&, RawPointer);
WTF_EXPORT_PRIVATE void printInternal(PrintStream&, FixedWidthDouble);

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

namespace WTF {

// For printing integral values in hex.
// See also the printInternal function for RaswHex in PrintStream.cpp.

class RawHex {
public:
RawHex() = default;

explicit RawHex(int8_t value)
: RawHex(static_cast<uint8_t>(value))
{ }
explicit RawHex(uint8_t value)
: RawHex(static_cast<uintptr_t>(value))
{ }

explicit RawHex(int16_t value)
: RawHex(static_cast<uint16_t>(value))
{ }
explicit RawHex(uint16_t value)
: RawHex(static_cast<uintptr_t>(value))
{ }

#if CPU(ADDRESS64) || OS(DARWIN)
// These causes build errors for CPU(ADDRESS32) on some ports because int32_t
// is already handled by intptr_t, and uint32_t is handled by uintptr_t.
explicit RawHex(int32_t value)
: RawHex(static_cast<uint32_t>(value))
{ }
explicit RawHex(uint32_t value)
: RawHex(static_cast<uintptr_t>(value))
{ }
#endif

#if CPU(ADDRESS32) || OS(DARWIN)
// These causes build errors for CPU(ADDRESS64) on some ports because int64_t
// is already handled by intptr_t, and uint64_t is handled by uintptr_t.
explicit RawHex(int64_t value)
: RawHex(static_cast<uint64_t>(value))
{ }
#if CPU(ADDRESS64) // on OS(DARWIN)
explicit RawHex(uint64_t value)
: RawHex(static_cast<uintptr_t>(value))
{ }
#else
explicit RawHex(uint64_t value)
: m_is64Bit(true)
, m_u64(value)
{ }
#endif
#endif // CPU(ADDRESS64)

explicit RawHex(intptr_t value)
: RawHex(static_cast<uintptr_t>(value))
{ }
explicit RawHex(uintptr_t value)
: m_ptr(reinterpret_cast<void*>(value))
{ }

const void* ptr() const { return m_ptr; }

#if !CPU(ADDRESS64)
bool is64Bit() const { return m_is64Bit; }
uint64_t u64() const { return m_u64; }
#endif

private:
#if !CPU(ADDRESS64)
bool m_is64Bit { false };
#endif
union {
const void* m_ptr { nullptr };
#if !CPU(ADDRESS64)
uint64_t m_u64;
#endif
};
};

} // namespace WTF

using WTF::RawHex;

0 comments on commit 1726a5e

Please sign in to comment.