dart-sdk/runtime/bin/namespace_patch.dart
Zachary Anderson d0295c873c [dart:io] Namespaces for file IO
Fuchsia requires the ability to sandbox Isolates w.r.t. file IO.
When a new Isolate starts, Fuchsia will pass the Isolate an object
called a namespace. We can translate the namespace object into a
file descriptor suitable for passing to the *at() family of
POSIX file system calls. The file system calls will then
have visibility only into the specified namespace.

We also plumb Namespaces through on all the other platforms as well to
make the change easier to test and so that in the future we can
implement e.g. per-isolate cwds.

This change adds a new internal class to dart:io called _Namespace,
which is implemented in a patch file. See:

sdk/lib/io/namespace_impl.dart
runtime/bin/namespace_patch.dart

The embedder can set up a non-default namespace by calling
_Namespace._setupNamespace during Isolate setup.

Instances of _Namespace have a native field that holds a pointer
to a native Namespace object. See:

runtime/bin/namespace.h

Calls from e.g. file_impl.dart are now also passed a
_Namespace object. The implementations in e.g. file.cc and
file_linux.cc then extract the namespace, and use it to compute a
file descriptor and path suitable for passing to e.g. openat().

related US-313

R=asiva@google.com, rmacnak@google.com

Review-Url: https://codereview.chromium.org/3007703002 .
2017-08-30 09:34:36 -07:00

45 lines
1.5 KiB
Dart

// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
class _NamespaceImpl extends NativeFieldWrapperClass1 implements _Namespace {
_NamespaceImpl._();
static _NamespaceImpl _create(_NamespaceImpl namespace, var n)
native "Namespace_Create";
static int _getPointer(_NamespaceImpl namespace)
native "Namespace_GetPointer";
static int _getDefault() native "Namespace_GetDefault";
// If the platform supports "namespaces", this method is called by the
// embedder with the platform-specific namespace information.
static _NamespaceImpl _cachedNamespace = null;
static void _setupNamespace(var namespace) {
_cachedNamespace = _create(new _NamespaceImpl._(), namespace);
}
static _NamespaceImpl get _namespace {
if (_cachedNamespace == null) {
// The embedder has not supplied a namespace before one is needed, so
// instead use a safe-ish default value.
_cachedNamespace = _create(new _NamespaceImpl._(), _getDefault());
}
return _cachedNamespace;
}
static int get _namespacePointer => _getPointer(_namespace);
}
@patch
class _Namespace {
@patch
static void _setupNamespace(var namespace) {
_NamespaceImpl._setupNamespace(namespace);
}
@patch
static _Namespace get _namespace => _NamespaceImpl._namespace;
@patch
static int get _namespacePointer => _NamespaceImpl._namespacePointer;
}