From fce8f48d6d1f42be02f4232400f511ec620507c2 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Sat, 14 Oct 2023 00:04:58 -0700 Subject: [PATCH 1/2] Bun JSC Debug Stuff --- .../builtins/BuiltinExecutables.cpp | 1 + Source/JavaScriptCore/builtins/BuiltinNames.h | 4 +++ Source/JavaScriptCore/parser/Lexer.cpp | 9 ++++--- .../runtime/AbstractModuleRecord.cpp | 6 +++-- .../runtime/InternalFieldTuple.h | 6 +++++ Source/JavaScriptCore/runtime/JSCellInlines.h | 6 ++++- .../runtime/JSFinalizationRegistry.cpp | 2 ++ Source/JavaScriptCore/runtime/JSFunction.cpp | 2 +- Source/JavaScriptCore/runtime/JSObject.h | 6 ++--- Source/JavaScriptCore/runtime/JSString.h | 26 +++++++++++++++++++ .../runtime/SyntheticModuleRecord.cpp | 5 ---- Source/JavaScriptCore/runtime/VM.h | 6 +++++ .../wasm/WasmStreamingCompiler.cpp | 2 ++ 13 files changed, 65 insertions(+), 16 deletions(-) diff --git a/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp b/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp index d2f85a70e784a..43d0182a4fb7d 100644 --- a/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp +++ b/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp @@ -253,6 +253,7 @@ UnlinkedFunctionExecutable* BuiltinExecutables::createExecutable(VM& vm, const S CRASH(); } } else { + printf("Error In %s\n", source.toUTF8().data()); RELEASE_ASSERT(error.isValid()); RELEASE_ASSERT(error.type() == ParserError::StackOverflow); } diff --git a/Source/JavaScriptCore/builtins/BuiltinNames.h b/Source/JavaScriptCore/builtins/BuiltinNames.h index affea51a630b1..d6dc9f4656306 100644 --- a/Source/JavaScriptCore/builtins/BuiltinNames.h +++ b/Source/JavaScriptCore/builtins/BuiltinNames.h @@ -288,6 +288,8 @@ inline void BuiltinNames::checkPublicToPrivateMapConsistency(UniquedStringImpl* ASSERT(String(privateName) != key); ASSERT(privateName->isSymbol()); ASSERT(static_cast(privateName)->isPrivate()); +#else + UNUSED_PARAM(privateName); #endif #else UNUSED_PARAM(privateName); @@ -298,6 +300,8 @@ inline void BuiltinNames::appendExternalName(const Identifier& publicName, const { #ifndef BUN_SKIP_FAILING_ASSERTIONS ASSERT_UNUSED(publicName, String(publicName.impl()) == String(privateName.impl())); + #else + UNUSED_PARAM(privateName); #endif checkPublicToPrivateMapConsistency(privateName.impl()); diff --git a/Source/JavaScriptCore/parser/Lexer.cpp b/Source/JavaScriptCore/parser/Lexer.cpp index 9699b212bea48..0bd741fa81b48 100644 --- a/Source/JavaScriptCore/parser/Lexer.cpp +++ b/Source/JavaScriptCore/parser/Lexer.cpp @@ -986,10 +986,11 @@ template ALWAYS_INLINE JSTokenType Lexer::p } else { ident = makeIdentifier(identifierStart, identifierLength); if (m_parsingBuiltinFunction) { - if (!isSafeBuiltinIdentifier(m_vm, ident)) { - m_lexErrorMessage = makeString("The use of '"_s, ident->string(), "' is disallowed in builtin functions."_s); - return ERRORTOK; - } + // todo: bun + // if (!isSafeBuiltinIdentifier(m_vm, ident)) { + // m_lexErrorMessage = makeString("The use of '"_s, ident->string(), "' is disallowed in builtin functions."_s); + // return ERRORTOK; + // } if (*ident == m_vm.propertyNames->undefinedKeyword) tokenData->ident = &m_vm.propertyNames->undefinedPrivateName; } diff --git a/Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp b/Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp index 2f9a707634799..7e8db614ca1f0 100644 --- a/Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp +++ b/Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp @@ -91,13 +91,15 @@ void AbstractModuleRecord::addStarExportEntry(const Identifier& moduleName) void AbstractModuleRecord::addImportEntry(const ImportEntry& entry) { bool isNewEntry = m_importEntries.add(entry.localName.impl(), entry).isNewEntry; - ASSERT_UNUSED(isNewEntry, isNewEntry); // This is guaranteed by the parser. + // This is guaranteed by the parser. + ASSERT_WITH_MESSAGE(isNewEntry, "Duplicate import entry name '%s'", entry.localName.impl()->utf8().data()); } void AbstractModuleRecord::addExportEntry(const ExportEntry& entry) { bool isNewEntry = m_exportEntries.add(entry.exportName.impl(), entry).isNewEntry; - ASSERT_UNUSED(isNewEntry, isNewEntry); // This is guaranteed by the parser. + // This is guaranteed by the parser. + ASSERT_WITH_MESSAGE(isNewEntry, "Duplicate export entry name '%s'", entry.exportName.impl()->utf8().data()); } auto AbstractModuleRecord::tryGetImportEntry(UniquedStringImpl* localName) -> std::optional diff --git a/Source/JavaScriptCore/runtime/InternalFieldTuple.h b/Source/JavaScriptCore/runtime/InternalFieldTuple.h index fdd48b39bfd13..948b96e901827 100644 --- a/Source/JavaScriptCore/runtime/InternalFieldTuple.h +++ b/Source/JavaScriptCore/runtime/InternalFieldTuple.h @@ -24,6 +24,12 @@ class InternalFieldTuple : public JSInternalFieldObjectImpl<2> { const WriteBarrier& internalField(Field field) const { return Base::internalField(static_cast(field)); } WriteBarrier& internalField(Field field) { return Base::internalField(static_cast(field)); } + static size_t allocationSize(Checked inlineCapacity) + { + ASSERT_UNUSED(inlineCapacity, inlineCapacity == 0U); + return sizeof(InternalFieldTuple); + } + template static GCClient::IsoSubspace* subspaceFor(VM& vm) { diff --git a/Source/JavaScriptCore/runtime/JSCellInlines.h b/Source/JavaScriptCore/runtime/JSCellInlines.h index 9b623da9c0b2a..7f123748c8f94 100644 --- a/Source/JavaScriptCore/runtime/JSCellInlines.h +++ b/Source/JavaScriptCore/runtime/JSCellInlines.h @@ -194,7 +194,11 @@ ALWAYS_INLINE void* tryAllocateCellHelper(VM& vm, size_t size, GCDeferralContext return nullptr; } #if ENABLE(GC_VALIDATION) - ASSERT(!vm.isInitializingObject()); + ASSERT_WITH_MESSAGE( + !vm.isInitializingObject(), + "Allocating JSCell while initializing an object is not allowed. Currently initializing '%s'\n" + "This means you either forgot `Base::finishCreation(...)` or you are actually allocating.", + vm.initializingObjectClass()->className.characters()); vm.setInitializingObjectClass(T::info()); #endif result->clearStructure(); diff --git a/Source/JavaScriptCore/runtime/JSFinalizationRegistry.cpp b/Source/JavaScriptCore/runtime/JSFinalizationRegistry.cpp index 95e2e121f5378..fd96d08063486 100644 --- a/Source/JavaScriptCore/runtime/JSFinalizationRegistry.cpp +++ b/Source/JavaScriptCore/runtime/JSFinalizationRegistry.cpp @@ -152,7 +152,9 @@ void JSFinalizationRegistry::finalizeUnconditionally(VM& vm, CollectionScope) if (!m_hasAlreadyScheduledWork && (readiedCell || deadCount(locker))) { auto ticket = vm.deferredWorkTimer->addPendingWork(vm, this, { }); + #ifndef BUN_SKIP_FAILING_ASSERTIONS ASSERT(vm.deferredWorkTimer->hasPendingWork(ticket)); + #endif vm.deferredWorkTimer->scheduleWorkSoon(ticket, [this](DeferredWorkTimer::Ticket) { JSGlobalObject* globalObject = this->globalObject(); this->m_hasAlreadyScheduledWork = false; diff --git a/Source/JavaScriptCore/runtime/JSFunction.cpp b/Source/JavaScriptCore/runtime/JSFunction.cpp index 2d01a2ebe3f5c..a7626130bac97 100644 --- a/Source/JavaScriptCore/runtime/JSFunction.cpp +++ b/Source/JavaScriptCore/runtime/JSFunction.cpp @@ -107,9 +107,9 @@ JSFunction::JSFunction(VM& vm, NativeExecutable* executable, JSGlobalObject* glo #if ASSERT_ENABLED void JSFunction::finishCreation(VM& vm) { + ASSERT(type() == JSFunctionType); Base::finishCreation(vm); ASSERT(jsDynamicCast(this)); - ASSERT(type() == JSFunctionType); // JSCell::{getCallData,getConstructData} relies on the following conditions. ASSERT(methodTable()->getConstructData == &JSFunction::getConstructData); ASSERT(methodTable()->getCallData == &JSFunction::getCallData); diff --git a/Source/JavaScriptCore/runtime/JSObject.h b/Source/JavaScriptCore/runtime/JSObject.h index 8e3653d2d3e90..b159d8ac1c882 100644 --- a/Source/JavaScriptCore/runtime/JSObject.h +++ b/Source/JavaScriptCore/runtime/JSObject.h @@ -1068,10 +1068,10 @@ class JSObject : public JSCell { void finishCreation(VM& vm) { Base::finishCreation(vm); - ASSERT(jsDynamicCast(this)); - ASSERT(structure()->hasPolyProto() || getPrototypeDirect().isNull() || Heap::heap(this) == Heap::heap(getPrototypeDirect())); - ASSERT(structure()->isObject()); ASSERT(classInfo()); + ASSERT(structure()->isObject()); + ASSERT(structure()->hasPolyProto() || getPrototypeDirect().isNull() || Heap::heap(this) == Heap::heap(getPrototypeDirect())); + ASSERT(jsDynamicCast(this)); } #endif diff --git a/Source/JavaScriptCore/runtime/JSString.h b/Source/JavaScriptCore/runtime/JSString.h index 82f767d49793d..50aa619221b5a 100644 --- a/Source/JavaScriptCore/runtime/JSString.h +++ b/Source/JavaScriptCore/runtime/JSString.h @@ -287,6 +287,8 @@ class JSString : public JSCell { friend JSString* jsString(VM&, const String&); friend JSString* jsString(VM&, String&&); friend JSString* jsString(VM&, StringView); + friend JSString* jsString(VM&, const String&, GCDeferralContext*); + friend JSString* jsString(VM&, String&&, GCDeferralContext*); friend JSString* jsString(JSGlobalObject*, JSString*, JSString*); friend JSString* jsString(JSGlobalObject*, const String&, JSString*); friend JSString* jsString(JSGlobalObject*, JSString*, const String&); @@ -951,6 +953,30 @@ inline JSString* jsString(VM& vm, String&& s) return JSString::create(vm, s.releaseImpl().releaseNonNull()); } +inline JSString* jsString(VM& vm, const String& s, GCDeferralContext* deferralContext) +{ + int size = s.length(); + if (!size) + return vm.smallStrings.emptyString(); + if (size == 1) { + if (auto c = s.characterAt(0); c <= maxSingleCharacterString) + return vm.smallStrings.singleCharacterString(c); + } + return JSString::create(vm, deferralContext, *s.impl()); +} + +inline JSString* jsString(VM& vm, String&& s, GCDeferralContext* deferralContext) +{ + int size = s.length(); + if (!size) + return vm.smallStrings.emptyString(); + if (size == 1) { + if (auto c = s.characterAt(0); c <= maxSingleCharacterString) + return vm.smallStrings.singleCharacterString(c); + } + return JSString::create(vm, deferralContext, s.releaseImpl().releaseNonNull()); +} + ALWAYS_INLINE JSString* jsString(VM& vm, const AtomString& s) { return jsString(vm, s.string()); diff --git a/Source/JavaScriptCore/runtime/SyntheticModuleRecord.cpp b/Source/JavaScriptCore/runtime/SyntheticModuleRecord.cpp index 46f977580036b..d5ee0969527cf 100644 --- a/Source/JavaScriptCore/runtime/SyntheticModuleRecord.cpp +++ b/Source/JavaScriptCore/runtime/SyntheticModuleRecord.cpp @@ -87,7 +87,6 @@ JSValue SyntheticModuleRecord::evaluate(JSGlobalObject*) return jsUndefined(); } - SyntheticModuleRecord* SyntheticModuleRecord::tryCreateWithExportNamesAndValues(JSGlobalObject* globalObject, const Identifier& moduleKey, const Vector& exportNames, const MarkedArgumentBuffer& exportValues) { VM& vm = globalObject->vm(); @@ -96,10 +95,6 @@ SyntheticModuleRecord* SyntheticModuleRecord::tryCreateWithExportNamesAndValues( ASSERT(exportNames.size() == exportValues.size()); auto* moduleRecord = create(globalObject, vm, globalObject->syntheticModuleRecordStructure(), moduleKey); - for (auto& exportName : exportNames) { - moduleRecord->addExportEntry(ExportEntry::createLocal(exportName, exportName)); - } - SymbolTable* exportSymbolTable = SymbolTable::create(vm); { auto offset = exportSymbolTable->takeNextScopeOffset(NoLockingNecessary); diff --git a/Source/JavaScriptCore/runtime/VM.h b/Source/JavaScriptCore/runtime/VM.h index ffdc1e369cd8c..8cc791a439cc0 100644 --- a/Source/JavaScriptCore/runtime/VM.h +++ b/Source/JavaScriptCore/runtime/VM.h @@ -870,6 +870,7 @@ class VM : public ThreadSafeRefCounted, public DoublyLinkedListNode { #if ENABLE(GC_VALIDATION) bool isInitializingObject() const; + const ClassInfo* initializingObjectClass() const; void setInitializingObjectClass(const ClassInfo*); #endif @@ -1168,6 +1169,11 @@ class VM : public ThreadSafeRefCounted, public DoublyLinkedListNode { }; #if ENABLE(GC_VALIDATION) +inline const ClassInfo* VM::initializingObjectClass() const +{ + return m_initializingObjectClass; +} + inline bool VM::isInitializingObject() const { return !!m_initializingObjectClass; diff --git a/Source/JavaScriptCore/wasm/WasmStreamingCompiler.cpp b/Source/JavaScriptCore/wasm/WasmStreamingCompiler.cpp index 6740ec80d4100..9a9f0a01cd97a 100644 --- a/Source/JavaScriptCore/wasm/WasmStreamingCompiler.cpp +++ b/Source/JavaScriptCore/wasm/WasmStreamingCompiler.cpp @@ -52,7 +52,9 @@ StreamingCompiler::StreamingCompiler(VM& vm, CompilerMode compilerMode, JSGlobal if (importObject) dependencies.append(Strong(vm, importObject)); m_ticket = vm.deferredWorkTimer->addPendingWork(vm, promise, WTFMove(dependencies), DeferredWorkTimer::WorkKind::WebAssembly); +#ifndef BUN_SKIP_FAILING_ASSERTIONS ASSERT(vm.deferredWorkTimer->hasPendingWork(m_ticket)); +#endif ASSERT(vm.deferredWorkTimer->hasDependancyInPendingWork(m_ticket, globalObject)); ASSERT(!importObject || vm.deferredWorkTimer->hasDependancyInPendingWork(m_ticket, importObject)); } From 561ef961e9054d56e1b0b63fc11c7b01b25db922 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Sat, 14 Oct 2023 00:15:14 -0700 Subject: [PATCH 2/2] changes --- Source/JavaScriptCore/builtins/BuiltinExecutables.cpp | 1 - Source/JavaScriptCore/parser/Lexer.cpp | 11 ++++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp b/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp index 43d0182a4fb7d..d2f85a70e784a 100644 --- a/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp +++ b/Source/JavaScriptCore/builtins/BuiltinExecutables.cpp @@ -253,7 +253,6 @@ UnlinkedFunctionExecutable* BuiltinExecutables::createExecutable(VM& vm, const S CRASH(); } } else { - printf("Error In %s\n", source.toUTF8().data()); RELEASE_ASSERT(error.isValid()); RELEASE_ASSERT(error.type() == ParserError::StackOverflow); } diff --git a/Source/JavaScriptCore/parser/Lexer.cpp b/Source/JavaScriptCore/parser/Lexer.cpp index 0bd741fa81b48..7b20cfac60904 100644 --- a/Source/JavaScriptCore/parser/Lexer.cpp +++ b/Source/JavaScriptCore/parser/Lexer.cpp @@ -986,11 +986,12 @@ template ALWAYS_INLINE JSTokenType Lexer::p } else { ident = makeIdentifier(identifierStart, identifierLength); if (m_parsingBuiltinFunction) { - // todo: bun - // if (!isSafeBuiltinIdentifier(m_vm, ident)) { - // m_lexErrorMessage = makeString("The use of '"_s, ident->string(), "' is disallowed in builtin functions."_s); - // return ERRORTOK; - // } +#if !USE(BUN_JSC_ADDITIONS) + if (!isSafeBuiltinIdentifier(m_vm, ident)) { + m_lexErrorMessage = makeString("The use of '"_s, ident->string(), "' is disallowed in builtin functions."_s); + return ERRORTOK; + } +#endif if (*ident == m_vm.propertyNames->undefinedKeyword) tokenData->ident = &m_vm.propertyNames->undefinedPrivateName; }