Files
sdk/runtime/bin/namespace.cc
T
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

139 lines
4.4 KiB
C++

// 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.
#if !defined(DART_IO_DISABLED)
#include "bin/namespace.h"
#include "bin/builtin.h"
#include "bin/dartutils.h"
#include "include/dart_api.h"
#include "platform/globals.h"
namespace dart {
namespace bin {
static const int kNamespaceNativeFieldIndex = 0;
static void ReleaseNamespace(void* isolate_callback_data,
Dart_WeakPersistentHandle handle,
void* peer) {
Namespace* namespc = reinterpret_cast<Namespace*>(peer);
ASSERT(namespc != NULL);
namespc->Release();
}
#if defined(DEBUG)
static bool IsNamespace(Dart_Handle namespc_obj) {
Dart_Handle namespc_type =
DartUtils::GetDartType("dart:io", "_NamespaceImpl");
ASSERT(!Dart_IsError(namespc_type));
bool isinstance = false;
Dart_Handle result =
Dart_ObjectIsType(namespc_obj, namespc_type, &isinstance);
ASSERT(!Dart_IsError(result));
return isinstance;
}
#endif
void FUNCTION_NAME(Namespace_Create)(Dart_NativeArguments args) {
Dart_Handle namespc_obj = Dart_GetNativeArgument(args, 0);
if (Dart_IsError(namespc_obj)) {
Dart_PropagateError(namespc_obj);
}
DEBUG_ASSERT(IsNamespace(namespc_obj));
// Allocate a native wrapper for the platform namespc bits.
Namespace* namespc = NULL;
Dart_Handle result;
Dart_Handle native_namespc = Dart_GetNativeArgument(args, 1);
if (Dart_IsInteger(native_namespc)) {
int64_t namespc_val;
result = Dart_IntegerToInt64(native_namespc, &namespc_val);
if (Dart_IsError(result)) {
Dart_PropagateError(result);
}
namespc = Namespace::Create(namespc_val);
} else if (Dart_IsString(native_namespc)) {
const char* namespc_path;
result = Dart_StringToCString(native_namespc, &namespc_path);
if (Dart_IsError(result)) {
Dart_PropagateError(result);
}
namespc = Namespace::Create(namespc_path);
} else {
// Propagate a type error.
Dart_ThrowException(
DartUtils::NewDartArgumentError("Argument must be an int or a String"));
}
// We were unable to create a native Namespace wrapper object due to some
// OS-level error.
if (namespc == NULL) {
Dart_SetReturnValue(args, DartUtils::NewDartOSError());
}
// Set the Dart objects native field to the native wrapper.
result = Dart_SetNativeInstanceField(namespc_obj, kNamespaceNativeFieldIndex,
reinterpret_cast<intptr_t>(namespc));
if (Dart_IsError(result)) {
namespc->Release();
Dart_PropagateError(result);
}
// Set up a finalizer for the Dart object so that we can do any necessary
// platform-specific cleanup for the namespc.
Dart_NewWeakPersistentHandle(namespc_obj, reinterpret_cast<void*>(namespc),
sizeof(*namespc), ReleaseNamespace);
Dart_SetReturnValue(args, namespc_obj);
}
void FUNCTION_NAME(Namespace_GetDefault)(Dart_NativeArguments args) {
Dart_SetIntegerReturnValue(args, Namespace::Default());
}
void FUNCTION_NAME(Namespace_GetPointer)(Dart_NativeArguments args) {
Namespace* namespc = Namespace::GetNamespace(args, 0);
ASSERT(namespc != NULL);
namespc->Retain();
Dart_SetIntegerReturnValue(args, reinterpret_cast<intptr_t>(namespc));
}
Namespace* Namespace::GetNamespace(Dart_NativeArguments args, intptr_t index) {
Namespace* namespc;
Dart_Handle status =
Namespace::GetNativeNamespaceArgument(args, index, &namespc);
if (Dart_IsError(status)) {
Dart_PropagateError(status);
}
return namespc;
}
bool Namespace::IsDefault(Namespace* namespc) {
return (namespc == NULL) || (namespc->namespc() == Namespace::Default());
}
Dart_Handle Namespace::GetNativeNamespaceArgument(Dart_NativeArguments args,
intptr_t index,
Namespace** namespc) {
Dart_Handle namespc_obj = Dart_GetNativeArgument(args, index);
if (Dart_IsError(namespc_obj)) {
Dart_PropagateError(namespc_obj);
}
DEBUG_ASSERT(IsNamespace(namespc_obj));
Dart_Handle result =
Dart_GetNativeInstanceField(namespc_obj, kNamespaceNativeFieldIndex,
reinterpret_cast<intptr_t*>(namespc));
if (Dart_IsError(result)) {
return result;
}
return Dart_Null();
}
} // namespace bin
} // namespace dart
#endif // !defined(DART_IO_DISABLED)