wasm-module.cc 11.8 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include <functional>
6 7
#include <memory>

8
#include "src/api-inl.h"
9
#include "src/assembler-inl.h"
10
#include "src/compiler/wasm-compiler.h"
11
#include "src/debug/interface-types.h"
12
#include "src/frames-inl.h"
13
#include "src/objects.h"
14
#include "src/objects/js-array-inl.h"
15
#include "src/property-descriptor.h"
16
#include "src/simulator.h"
17 18
#include "src/snapshot/snapshot.h"
#include "src/v8.h"
19
#include "src/wasm/module-decoder.h"
20
#include "src/wasm/wasm-code-manager.h"
21
#include "src/wasm/wasm-js.h"
22
#include "src/wasm/wasm-module.h"
23
#include "src/wasm/wasm-objects-inl.h"
24 25
#include "src/wasm/wasm-result.h"

26 27 28
namespace v8 {
namespace internal {
namespace wasm {
29

30 31 32 33
WireBytesRef WasmModule::LookupFunctionName(const ModuleWireBytes& wire_bytes,
                                            uint32_t function_index) const {
  if (!function_names) {
    function_names.reset(new std::unordered_map<uint32_t, WireBytesRef>());
34 35
    DecodeFunctionNames(wire_bytes.start(), wire_bytes.end(),
                        function_names.get());
36
  }
37 38
  auto it = function_names->find(function_index);
  if (it == function_names->end()) return WireBytesRef();
39 40 41
  return it->second;
}

42 43 44 45
void WasmModule::AddFunctionNameForTesting(int function_index,
                                           WireBytesRef name) {
  if (!function_names) {
    function_names.reset(new std::unordered_map<uint32_t, WireBytesRef>());
46
  }
47
  function_names->insert(std::make_pair(function_index, name));
48 49 50 51 52 53
}

// Get a string stored in the module bytes representing a name.
WasmName ModuleWireBytes::GetNameOrNull(WireBytesRef ref) const {
  if (!ref.is_set()) return {nullptr, 0};  // no name.
  CHECK(BoundsCheck(ref.offset(), ref.length()));
54
  return WasmName::cast(
55 56 57 58 59 60
      module_bytes_.SubVector(ref.offset(), ref.end_offset()));
}

// Get a string stored in the module bytes representing a function name.
WasmName ModuleWireBytes::GetNameOrNull(const WasmFunction* function,
                                        const WasmModule* module) const {
61
  return GetNameOrNull(module->LookupFunctionName(*this, function->func_index));
62 63
}

64
std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name) {
65
  os << "#" << name.function_->func_index;
66
  if (!name.name_.is_empty()) {
67 68 69
    if (name.name_.start()) {
      os << ":";
      os.write(name.name_.start(), name.name_.length());
70 71 72 73 74 75 76
    }
  } else {
    os << "?";
  }
  return os;
}

77
WasmModule::WasmModule(std::unique_ptr<Zone> owned)
78
    : signature_zone(std::move(owned)) {}
79

80
bool IsWasmCodegenAllowed(Isolate* isolate, Handle<Context> context) {
81 82 83 84
  // TODO(wasm): Once wasm has its own CSP policy, we should introduce a
  // separate callback that includes information about the module about to be
  // compiled. For the time being, pass an empty string as placeholder for the
  // sources.
85 86 87 88 89 90 91 92
  if (auto wasm_codegen_callback = isolate->allow_wasm_code_gen_callback()) {
    return wasm_codegen_callback(
        v8::Utils::ToLocal(context),
        v8::Utils::ToLocal(isolate->factory()->empty_string()));
  }
  auto codegen_callback = isolate->allow_code_gen_callback();
  return codegen_callback == nullptr ||
         codegen_callback(
93 94
             v8::Utils::ToLocal(context),
             v8::Utils::ToLocal(isolate->factory()->empty_string()));
95 96
}

97 98
Handle<JSArray> GetImports(Isolate* isolate,
                           Handle<WasmModuleObject> module_object) {
99 100 101 102 103 104 105 106 107 108
  Factory* factory = isolate->factory();

  Handle<String> module_string = factory->InternalizeUtf8String("module");
  Handle<String> name_string = factory->InternalizeUtf8String("name");
  Handle<String> kind_string = factory->InternalizeUtf8String("kind");

  Handle<String> function_string = factory->InternalizeUtf8String("function");
  Handle<String> table_string = factory->InternalizeUtf8String("table");
  Handle<String> memory_string = factory->InternalizeUtf8String("memory");
  Handle<String> global_string = factory->InternalizeUtf8String("global");
109
  Handle<String> exception_string = factory->InternalizeUtf8String("exception");
110 111

  // Create the result array.
112
  const WasmModule* module = module_object->module();
113
  int num_imports = static_cast<int>(module->import_table.size());
114
  Handle<JSArray> array_object = factory->NewJSArray(PACKED_ELEMENTS, 0, 0);
115 116 117 118 119 120 121 122 123
  Handle<FixedArray> storage = factory->NewFixedArray(num_imports);
  JSArray::SetContent(array_object, storage);
  array_object->set_length(Smi::FromInt(num_imports));

  Handle<JSFunction> object_function =
      Handle<JSFunction>(isolate->native_context()->object_function(), isolate);

  // Populate the result array.
  for (int index = 0; index < num_imports; ++index) {
124
    const WasmImport& import = module->import_table[index];
125

126
    Handle<JSObject> entry = factory->NewJSObject(object_function);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    Handle<String> import_kind;
    switch (import.kind) {
      case kExternalFunction:
        import_kind = function_string;
        break;
      case kExternalTable:
        import_kind = table_string;
        break;
      case kExternalMemory:
        import_kind = memory_string;
        break;
      case kExternalGlobal:
        import_kind = global_string;
        break;
142 143 144
      case kExternalException:
        import_kind = exception_string;
        break;
145 146 147 148 149
      default:
        UNREACHABLE();
    }

    MaybeHandle<String> import_module =
150 151
        WasmModuleObject::ExtractUtf8StringFromModuleBytes(
            isolate, module_object, import.module_name);
152 153

    MaybeHandle<String> import_name =
154 155
        WasmModuleObject::ExtractUtf8StringFromModuleBytes(
            isolate, module_object, import.field_name);
156

157 158 159 160 161
    JSObject::AddProperty(isolate, entry, module_string,
                          import_module.ToHandleChecked(), NONE);
    JSObject::AddProperty(isolate, entry, name_string,
                          import_name.ToHandleChecked(), NONE);
    JSObject::AddProperty(isolate, entry, kind_string, import_kind, NONE);
162 163 164 165 166 167

    storage->set(index, *entry);
  }

  return array_object;
}
168

169 170
Handle<JSArray> GetExports(Isolate* isolate,
                           Handle<WasmModuleObject> module_object) {
171 172 173 174 175 176 177 178 179
  Factory* factory = isolate->factory();

  Handle<String> name_string = factory->InternalizeUtf8String("name");
  Handle<String> kind_string = factory->InternalizeUtf8String("kind");

  Handle<String> function_string = factory->InternalizeUtf8String("function");
  Handle<String> table_string = factory->InternalizeUtf8String("table");
  Handle<String> memory_string = factory->InternalizeUtf8String("memory");
  Handle<String> global_string = factory->InternalizeUtf8String("global");
180
  Handle<String> exception_string = factory->InternalizeUtf8String("exception");
181 182

  // Create the result array.
183
  const WasmModule* module = module_object->module();
184
  int num_exports = static_cast<int>(module->export_table.size());
185
  Handle<JSArray> array_object = factory->NewJSArray(PACKED_ELEMENTS, 0, 0);
186 187 188 189 190 191 192 193 194
  Handle<FixedArray> storage = factory->NewFixedArray(num_exports);
  JSArray::SetContent(array_object, storage);
  array_object->set_length(Smi::FromInt(num_exports));

  Handle<JSFunction> object_function =
      Handle<JSFunction>(isolate->native_context()->object_function(), isolate);

  // Populate the result array.
  for (int index = 0; index < num_exports; ++index) {
195
    const WasmExport& exp = module->export_table[index];
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    Handle<String> export_kind;
    switch (exp.kind) {
      case kExternalFunction:
        export_kind = function_string;
        break;
      case kExternalTable:
        export_kind = table_string;
        break;
      case kExternalMemory:
        export_kind = memory_string;
        break;
      case kExternalGlobal:
        export_kind = global_string;
        break;
211 212 213
      case kExternalException:
        export_kind = exception_string;
        break;
214 215 216 217
      default:
        UNREACHABLE();
    }

218 219
    Handle<JSObject> entry = factory->NewJSObject(object_function);

220
    MaybeHandle<String> export_name =
221 222
        WasmModuleObject::ExtractUtf8StringFromModuleBytes(
            isolate, module_object, exp.name);
223

224 225 226
    JSObject::AddProperty(isolate, entry, name_string,
                          export_name.ToHandleChecked(), NONE);
    JSObject::AddProperty(isolate, entry, kind_string, export_kind, NONE);
227 228 229 230 231 232

    storage->set(index, *entry);
  }

  return array_object;
}
233

234 235 236
Handle<JSArray> GetCustomSections(Isolate* isolate,
                                  Handle<WasmModuleObject> module_object,
                                  Handle<String> name, ErrorThrower* thrower) {
237 238
  Factory* factory = isolate->factory();

239 240 241 242
  Vector<const uint8_t> wire_bytes =
      module_object->native_module()->wire_bytes();
  std::vector<CustomSectionOffset> custom_sections =
      DecodeCustomSections(wire_bytes.start(), wire_bytes.end());
243 244 245 246

  std::vector<Handle<Object>> matching_sections;

  // Gather matching sections.
247
  for (auto& section : custom_sections) {
248
    MaybeHandle<String> section_name =
249 250
        WasmModuleObject::ExtractUtf8StringFromModuleBytes(
            isolate, module_object, section.name);
251 252 253 254

    if (!name->Equals(*section_name.ToHandleChecked())) continue;

    // Make a copy of the payload data in the section.
255 256 257 258 259
    size_t size = section.payload.length();
    void* memory =
        size == 0 ? nullptr : isolate->array_buffer_allocator()->Allocate(size);

    if (size && !memory) {
260 261 262
      thrower->RangeError("out of memory allocating custom section data");
      return Handle<JSArray>();
    }
263 264
    Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
    constexpr bool is_external = false;
265
    JSArrayBuffer::Setup(buffer, isolate, is_external, memory, size);
266 267
    memcpy(memory, wire_bytes.start() + section.payload.offset(),
           section.payload.length());
268

269
    matching_sections.push_back(buffer);
270 271 272
  }

  int num_custom_sections = static_cast<int>(matching_sections.size());
273
  Handle<JSArray> array_object = factory->NewJSArray(PACKED_ELEMENTS, 0, 0);
274 275 276 277 278 279 280 281 282 283
  Handle<FixedArray> storage = factory->NewFixedArray(num_custom_sections);
  JSArray::SetContent(array_object, storage);
  array_object->set_length(Smi::FromInt(num_custom_sections));

  for (int i = 0; i < num_custom_sections; i++) {
    storage->set(i, *matching_sections[i]);
  }

  return array_object;
}
284

285
Handle<FixedArray> DecodeLocalNames(Isolate* isolate,
286
                                    Handle<WasmModuleObject> module_object) {
287 288
  Vector<const uint8_t> wire_bytes =
      module_object->native_module()->wire_bytes();
289
  LocalNames decoded_locals;
290
  DecodeLocalNames(wire_bytes.start(), wire_bytes.end(), &decoded_locals);
291 292 293 294 295 296 297 298
  Handle<FixedArray> locals_names =
      isolate->factory()->NewFixedArray(decoded_locals.max_function_index + 1);
  for (LocalNamesPerFunction& func : decoded_locals.names) {
    Handle<FixedArray> func_locals_names =
        isolate->factory()->NewFixedArray(func.max_local_index + 1);
    locals_names->set(func.function_index, *func_locals_names);
    for (LocalName& name : func.names) {
      Handle<String> name_str =
299 300
          WasmModuleObject::ExtractUtf8StringFromModuleBytes(
              isolate, module_object, name.name)
301 302 303 304 305 306
              .ToHandleChecked();
      func_locals_names->set(name.local_index, *name_str);
    }
  }
  return locals_names;
}
307 308 309 310 311 312 313 314 315 316 317 318

namespace {
template <typename T>
inline size_t VectorSize(const std::vector<T>& vector) {
  return sizeof(T) * vector.size();
}
}  // namespace

size_t EstimateWasmModuleSize(const WasmModule* module) {
  size_t estimate =
      sizeof(WasmModule) + VectorSize(module->signatures) +
      VectorSize(module->signature_ids) + VectorSize(module->functions) +
319
      VectorSize(module->data_segments) + VectorSize(module->tables) +
320 321 322 323 324
      VectorSize(module->import_table) + VectorSize(module->export_table) +
      VectorSize(module->exceptions) + VectorSize(module->table_inits);
  // TODO(wasm): include names table and wire bytes in size estimate
  return estimate;
}
325 326 327
}  // namespace wasm
}  // namespace internal
}  // namespace v8