From 58ab8006821d6c95e90ac6a4f9531628fa469a38 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sat, 21 Nov 2026 15:32:41 -0300 Subject: [PATCH 1/2] feat(runtime): guard native instance methods against instance shadowing in debug builds In debug builds under the "rooted" and "canary" wrapper lifetime modes, assigning a native instance method's name on a native object throws a TypeError: native code never calls a JS own property of a native object, so the assignment only changes what JS callers see. Native instance methods (protocol members included, and those of protocol bindings), toString and NSArray's Symbol.iterator become accessor pairs on their prototype once it is instantiated. The getter is a JS closure that returns the original function, so reads, identity and super loads stay on V8's fast paths. The setter throws for a native, released or proxied native receiver and otherwise creates the own data property an assignment would, so prototype-level writes still swizzle (once) and subclass prototypes and objects inheriting from native ones are unaffected. Object.defineProperty remains the route to a JS-only own member. "wrapperLifetime": { "allowShadowingNativeMembers": true } turns the guard off; ns:runtime exposes it read-only as wrapperLifetimeAllowShadowingNativeMembers. Release builds and the resurrecting mode keep data properties. --- NativeScript/runtime/MetadataBuilder.h | 10 +- NativeScript/runtime/MetadataBuilder.mm | 171 ++++++++++- NativeScript/runtime/NsBuiltinModules.cpp | 9 +- NativeScript/runtime/WrapperLifetime.h | 11 + NativeScript/runtime/WrapperLifetime.mm | 11 + TestFixtures/Api/TNSApi.h | 10 + TestFixtures/Api/TNSApi.m | 18 ++ .../app/tests/NativeMemberGuardTests.js | 278 ++++++++++++++++++ .../app/tests/WrapperLifetimeModesTests.js | 21 +- TestRunner/app/tests/index.js | 1 + docs/ns-builtin-modules.md | 54 +++- types/ns-runtime.d.ts | 15 +- 12 files changed, 589 insertions(+), 20 deletions(-) create mode 100644 TestRunner/app/tests/NativeMemberGuardTests.js diff --git a/NativeScript/runtime/MetadataBuilder.h b/NativeScript/runtime/MetadataBuilder.h index 0b525fd54..806867d1d 100644 --- a/NativeScript/runtime/MetadataBuilder.h +++ b/NativeScript/runtime/MetadataBuilder.h @@ -111,11 +111,13 @@ class MetadataBuilder { static void RegisterAllocMethod( v8::Isolate* isolate, v8::Local ctorFuncTemplate, const InterfaceMeta* interfaceMeta); + // `guarded`, when given, records the JS name of each method installed. static void RegisterInstanceMethods( v8::Local context, v8::Local ctorFuncTemplate, const BaseClassMeta* meta, KnownUnknownClassPair pair, - robin_hood::unordered_map& names); + robin_hood::unordered_map& names, + std::vector* guarded = nullptr); // `setters`, when given, records each accessor pair installed. static void RegisterInstanceProperties( v8::Local context, @@ -130,13 +132,15 @@ class MetadataBuilder { const BaseClassMeta* meta, std::string className, KnownUnknownClassPair pair, robin_hood::unordered_map& names, - NativeSetters* setters = nullptr); + NativeSetters* setters = nullptr, + std::vector* guarded = nullptr); static void RegisterAdditionalProtocols( v8::Local context, v8::Local ctorFuncTemplate, KnownUnknownClassPair pair, const std::vector& additionalProtocols, - robin_hood::unordered_map& names); + robin_hood::unordered_map& names, + std::vector* guarded = nullptr); static void RegisterStaticMethods( v8::Local context, v8::Local ctorFunc, const BaseClassMeta* meta, KnownUnknownClassPair pair, diff --git a/NativeScript/runtime/MetadataBuilder.mm b/NativeScript/runtime/MetadataBuilder.mm index fbcd6f46e..3eeb3b430 100644 --- a/NativeScript/runtime/MetadataBuilder.mm +++ b/NativeScript/runtime/MetadataBuilder.mm @@ -205,6 +205,126 @@ void ForgetNativeSetter(Isolate* isolate, Local prototype, Local getterFactory; +}; + +std::string DescribeMemberName(Isolate* isolate, Local name) { + if (name->IsSymbol()) { + Local description = name.As()->Description(isolate); + return "[" + (description->IsString() ? tns::ToString(isolate, description) : "") + "]"; + } + return tns::ToString(isolate, name); +} + +// The guard's setter: `info.Data()` is the member's name. An own property on a +// native object never reaches native code, which calls the class's +// implementation, so assigning a native method's name there throws. Any other +// receiver, such as a class prototype (whose handlers have swizzled already) +// or an object inheriting from a native one, gets the own data property an +// ordinary assignment would create. +void NativeMemberGuardSetter(const FunctionCallbackInfo& info) { + Isolate* isolate = info.GetIsolate(); + Local context = isolate->GetCurrentContext(); + Local name = info.Data().As(); + Local receiver = info.This(); + Local value = info.Length() > 0 ? info[0] : v8::Undefined(isolate).As(); + + BaseDataWrapper* wrapper = tns::GetValue(isolate, receiver); + bool nativeObject = wrapper != nullptr + ? wrapper->Type() == WrapperType::ObjCObject || + wrapper->Type() == WrapperType::ObjCAllocObject + : tns::GetReleasedReason(receiver) != ReleasedReason::kNone; + if (!nativeObject) { + DefiningGuardedMember defining; + (void)receiver->CreateDataProperty(context, name, value); + return; + } + + // The JS class name: the native one can be a private class cluster member. + std::string className; + Local target = tns::UnwrapProxy(receiver); + if (!target.IsEmpty() && target->IsObject()) { + className = tns::ToString(isolate, target.As()->GetConstructorName()); + } + std::string member = DescribeMemberName(isolate, name); + std::string message = + "Cannot assign '" + member + "' on an instance of " + className + + ": it would shadow the native method for JS callers only, since native code never calls " + "JS own properties of a native object (wrapperLifetime mode \"" + + WrapperLifetimeModeName(GetWrapperLifetimeMode()) + + "\", debug builds). Override the method on a class instead (extend() or a subclass), " + "define a JS-only property with Object.defineProperty, or set \"wrapperLifetime\": { " + "\"allowShadowingNativeMembers\": true } in the app config."; + isolate->ThrowException(Exception::TypeError(tns::ToV8String(isolate, message))); +} + +// Turns each named own method of a native class prototype, and the symbol +// `iterator` when given, into an accessor pair whose getter returns that same +// function. Configurable, so an assignment the setter allows can replace it. +void GuardNativeMembers(Isolate* isolate, Local context, Local prototype, + const std::vector& names, Local iterator) { + NativeMemberGuardState* state = Caches::StateFor(isolate); + if (state == nullptr) { + return; + } + if (state->getterFactory.IsEmpty()) { + Local