Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion NativeScript/runtime/NsBuiltinModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ constexpr const char* kWrapperLifetimeModeKey = "wrapperLifetimeMode";
constexpr const char* kWrapperLifetimeScopeKey = "wrapperLifetimeScope";
constexpr const char* kWrapperLifetimeAllowShadowingKey =
"wrapperLifetimeAllowShadowingNativeMembers";
constexpr const char* kWrapperLifetimeGuardsNativeMembersKey =
"wrapperLifetimeGuardsNativeMembers";

void ThrowTypeError(Isolate* isolate, const std::string& message) {
isolate->ThrowException(
Expand Down Expand Up @@ -149,7 +151,8 @@ void SetConfigCallback(const FunctionCallbackInfo<Value>& info) {
return;
}
if (key == kWrapperLifetimeModeKey || key == kWrapperLifetimeScopeKey ||
key == kWrapperLifetimeAllowShadowingKey) {
key == kWrapperLifetimeAllowShadowingKey ||
key == kWrapperLifetimeGuardsNativeMembersKey) {
ThrowTypeError(
isolate,
"'" + key +
Expand Down Expand Up @@ -234,6 +237,10 @@ void GetConfigCallback(const FunctionCallbackInfo<Value>& info) {
info.GetReturnValue().Set(tns::GetAllowShadowingNativeMembers());
return;
}
if (key == kWrapperLifetimeGuardsNativeMembersKey) {
info.GetReturnValue().Set(tns::GuardsNativeMembers());
return;
}
ThrowTypeError(isolate, "Unknown runtime config key: '" + key + "'");
}

Expand Down
25 changes: 21 additions & 4 deletions TestRunner/app/tests/NativeMemberGuardTests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Debug builds in the "rooted" and "canary" modes guard native instance
// methods, and the methods of JS-extended classes that native code calls:
// assigning one's name on a native object throws, since native code never
// calls that own property. The suite is a debug build; the resurrecting
// mode and "allowShadowingNativeMembers" keep plain data properties.
// calls that own property. Release builds, the resurrecting mode and
// "allowShadowingNativeMembers" keep plain data properties.
describe("Native member guard", function () {
var runtime = require("ns:runtime");
var mode = runtime.getConfig("wrapperLifetimeMode");
var guarded = (mode === "rooted" || mode === "canary") &&
!runtime.getConfig("wrapperLifetimeAllowShadowingNativeMembers");
var guarded = runtime.getConfig("wrapperLifetimeGuardsNativeMembers");

function replacement() {
return "js";
Expand Down Expand Up @@ -57,6 +56,24 @@ describe("Native member guard", function () {
}).toThrowError(TypeError, /read-only/);
});

it("reports the guard state read-only", function () {
var allowShadowing = runtime.getConfig("wrapperLifetimeAllowShadowingNativeMembers");
// canonicalizeHttpUrlKey is exported only when RuntimeConfig.IsDebug.
var debugBuild = typeof require("ns:module").canonicalizeHttpUrlKey === "function";
expect(typeof guarded).toBe("boolean");
if (guarded) {
expect(mode === "rooted" || mode === "canary").toBe(true);
expect(allowShadowing).toBe(false);
}
if (debugBuild && (mode === "rooted" || mode === "canary") && !allowShadowing) {
expect(guarded).toBe(true);
}
expect(function () {
runtime.setConfig("wrapperLifetimeGuardsNativeMembers", !guarded);
}).toThrowError(TypeError, /read-only/);
expect(runtime.getConfig("wrapperLifetimeGuardsNativeMembers")).toBe(guarded);
});

it("guards native instance methods against strict and sloppy instance writes", function () {
var object = NSObject.new();
var array = NSArray.arrayWithArray([1, 2]);
Expand Down
6 changes: 3 additions & 3 deletions TestRunner/app/tests/WrapperLifetimeModesTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ describe("Wrapper lifetime modes", function () {
var canary = mode === "canary";
// JS state on a natively owned object survives its wrapper's death.
var keepsState = rooted || canary;
// The suite is a debug build: assigning a native method's name on a native
// object throws in these modes (the native member guard).
var guardsNativeMembers = keepsState && !runtime.getConfig("wrapperLifetimeAllowShadowingNativeMembers");
// Assigning a native method's name on a native object throws (the native
// member guard; debug builds in these modes only).
var guardsNativeMembers = runtime.getConfig("wrapperLifetimeGuardsNativeMembers");

var Extended = (function (_super) {
__extends(Extended, _super);
Expand Down
5 changes: 4 additions & 1 deletion docs/ns-builtin-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Config keys:
| `wrapperLifetimeMode` | `"resurrecting"` \| `"rooted"` \| `"canary"` | process-wide, read-only | the app config `"wrapperLifetime": { "mode": … }`, or `"resurrecting"` |
| `wrapperLifetimeScope` | `"extended"` \| `"all"` | process-wide, read-only | the app config `"wrapperLifetime": { "scope": … }`, or `"extended"` |
| `wrapperLifetimeAllowShadowingNativeMembers` | `true` \| `false` | process-wide, read-only | the app config `"wrapperLifetime": { "allowShadowingNativeMembers": … }`, or `false` (see [Native member guard](#native-member-guard) and [Plain canaries](#plain-canaries)) |
| `wrapperLifetimeGuardsNativeMembers` | `true` \| `false` | process-wide, read-only | `true` in debug builds under the `"rooted"` and `"canary"` modes unless `wrapperLifetimeAllowShadowingNativeMembers` is `true`, else `false` (see [Native member guard](#native-member-guard)) |

```js
const { setConfig, getConfig } = require("ns:runtime");
Expand Down Expand Up @@ -149,7 +150,7 @@ The `TypeError` messages are part of the contract:
| invalid `releasedObjectPolicy` value | `'releasedObjectPolicy' must be 'report' or 'throw'` |
| non-string `debug` value | `'debug' must be a comma-separated category string (<categories>), or '' to disable tracing` |
| non-boolean `wrapperLifetimeWarnings` value | `'wrapperLifetimeWarnings' must be a boolean` |
| `setConfig` of `wrapperLifetimeMode`, `wrapperLifetimeScope` or `wrapperLifetimeAllowShadowingNativeMembers` | `'<key>' is read-only; set "wrapperLifetime" in the app config` |
| `setConfig` of `wrapperLifetimeMode`, `wrapperLifetimeScope`, `wrapperLifetimeAllowShadowingNativeMembers` or `wrapperLifetimeGuardsNativeMembers` | `'<key>' is read-only; set "wrapperLifetime" in the app config` |

### Wrapper lifetime counters

Expand Down Expand Up @@ -349,6 +350,8 @@ mirroring handler, so a shadowing property on one carries over to a new
wrapper as it does on a JS-extended instance, at the cost of a handler on every
write to a plain wrapper (see [Plain canaries](#plain-canaries));
`getConfig("wrapperLifetimeAllowShadowingNativeMembers")` reads it.
`getConfig("wrapperLifetimeGuardsNativeMembers")` reads whether the guard is
on, which also accounts for the build and the mode.

Release builds never throw and keep data properties, since the accessor costs
every native method load. An instance shadow there is silently ignored by
Expand Down
7 changes: 7 additions & 0 deletions types/ns-runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ declare module "ns:runtime" {
* handler on every write.
*/
wrapperLifetimeAllowShadowingNativeMembers: boolean;
/**
* Whether assigning a native member's name on a native object throws (see
* `wrapperLifetimeAllowShadowingNativeMembers`): `true` in debug builds
* under the `"rooted"` and `"canary"` modes unless shadowing is allowed,
* `false` otherwise. Fixed at startup. Read-only: `setConfig` throws.
*/
wrapperLifetimeGuardsNativeMembers: boolean;
}

/**
Expand Down
Loading