Closed Bug 2040286 Opened 5 months ago Closed 2 months ago

`using` bindings can be silently mutated, violating spec const-like semantics

Categories

(Core :: JavaScript Engine, defect, P2)

defect

Tracking

()

RESOLVED FIXED
156 Branch
Tracking Status
firefox156 --- fixed

People

(Reporter: jandem, Assigned: debadree333)

References

(Blocks 1 open bug)

Details

(Keywords: ai-involved)

Attachments

(1 external file)

Summary

using declarations must behave like const: assignments must throw TypeError. SpiderMonkey enforces this for statically-resolved assignments (Bug 2000184 added BindingKind::Using to isConst() in NameAnalysisTypes.h), but two runtime paths bypass the static check:

  1. Sloppy-mode direct eval() — resolves dynamically through the environment chain. AddToEnvironmentMap() in js/src/vm/Scope.cpp only marks Const and NamedLambdaCallee non-writable; Using falls through to the writable default.
  2. Debugger API — DebugEnvironmentProxy::handleUnaliasedAccess() in js/src/vm/EnvironmentObject.cpp only checks BindingKind::Const, so Debugger.Environment.setVariable() silently mutates using bindings.

V8 throws TypeError: Assignment to using variable. for the eval case.

There is also a dead-code gap in LexicalScopeHasClosedOverBindings() (js/src/frontend/Parser.cpp) which omits BindingKind::Using — currently unreachable (only called for named-lambda scopes) but worth fixing for defense-in-depth.

Test case

// Reproducer 1: sloppy-mode direct eval
function testEval() {
  {
    using x = { [Symbol.dispose]() {} };
    eval("x = 2");
    print("using via eval: typeof=" + typeof x + " value=" + x);
  }
}
testEval();
// Actual:   using via eval: typeof=number value=2
// Expected: TypeError: invalid assignment to const 'x'

// Reproducer 2: Debugger.Environment.setVariable
var g = newGlobal({ newCompartment: true });
g.eval(`
  function f() {
    { using x = { [Symbol.dispose]() {} }; debugger; print(typeof x + " " + x); }
  }
`);
var dbg = Debugger(g);
dbg.onDebuggerStatement = function (frame) {
  frame.environment.setVariable("x", 99);
};
g.f();
// Actual:   number 99
// Expected: TypeError from setVariable

Suggested fix

--- a/js/src/vm/Scope.cpp
+++ b/js/src/vm/Scope.cpp
@@ -111,6 +111,7 @@ static bool AddToEnvironmentMap(JSContext* cx, const JSClass* clasp,
   PropertyFlags propFlags = {PropertyFlag::Enumerable};
   switch (bindKind) {
     case BindingKind::Const:
+    case BindingKind::Using:
     case BindingKind::NamedLambdaCallee:
       // Non-writable.
       break;
--- a/js/src/vm/EnvironmentObject.cpp
+++ b/js/src/vm/EnvironmentObject.cpp
@@ -1745,7 +1745,8 @@
       if (!bi) {
         return true;
       }
-      if (action == SET && bi.kind() == BindingKind::Const) {
+      if (action == SET && (bi.kind() == BindingKind::Const ||
+                            bi.kind() == BindingKind::Using)) {
         ReportRuntimeLexicalError(cx, JSMSG_BAD_CONST_ASSIGN, id);
         return false;
       }
@@ -1870,7 +1871,8 @@
       if (!bi) {
         return true;
       }
-      if (action == SET && bi.kind() == BindingKind::Const) {
+      if (action == SET && (bi.kind() == BindingKind::Const ||
+                            bi.kind() == BindingKind::Using)) {
         ReportRuntimeLexicalError(cx, JSMSG_BAD_CONST_ASSIGN, id);
         return false;
       }
--- a/js/src/frontend/Parser.cpp
+++ b/js/src/frontend/Parser.cpp
@@ -1555,6 +1555,7 @@ bool LexicalScopeHasClosedOverBindings(ParseContext* pc,
     switch (bi.kind()) {
       case BindingKind::Let:
       case BindingKind::Const:
+      case BindingKind::Using:
         if (allBindingsClosedOver || bi.closedOver()) {
           return true;
         }

Debadree, I'm adding a needinfo in case you want to take a look at this :)

Flags: needinfo?(debadree333)

On it! Keeping NI on until taking a full look! :)

Severity: -- → S3
Priority: -- → P2

Working on it!

Flags: needinfo?(debadree333)
Keywords: ai-involved

Explicit Resource Management declarations create immutable lexical bindings, but the environment and Debugger mutation paths only handled const bindings as non-writable. This let the different binding representations disagree about assignment semantics.

Classify BindingKind::Using with const when determining closed-over lexical scopes, defining environment properties, and servicing Debugger environment writes. Cover direct eval assignment plus aliased and unaliased Debugger environments, and make the Debugger assertions non-vacuous by verifying that both frame paths were observed.

Assignee: nobody → debadree333
Attachment #9620755 - Attachment description: WIP: Bug 2040286 - Treat using bindings as non-writable like const. → Bug 2040286 - Treat using bindings as non-writable like const. r=arai
Status: NEW → ASSIGNED
Status: ASSIGNED → RESOLVED
Closed: 2 months ago
Resolution: --- → FIXED
Target Milestone: --- → 156 Branch
QA Whiteboard: [qa-triage-done-c157/b156]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: