Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/wasm/wasm-result.h"
6 :
7 : #include "src/heap/factory.h"
8 : #include "src/heap/heap.h"
9 : #include "src/isolate-inl.h"
10 : #include "src/objects.h"
11 :
12 : #include "src/base/platform/platform.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 : namespace wasm {
17 :
18 : namespace {
19 :
20 : PRINTF_FORMAT(3, 0)
21 57620 : void VPrintFToString(std::string& str, size_t str_offset, const char* format,
22 : va_list args) {
23 : DCHECK_LE(str_offset, str.size());
24 57620 : size_t len = str_offset + strlen(format);
25 : // Allocate increasingly large buffers until the message fits.
26 146810 : for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
27 : DCHECK_GE(kMaxInt, len);
28 : str.resize(len);
29 : va_list args_copy;
30 204430 : va_copy(args_copy, args);
31 : int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
32 : static_cast<int>(len - str_offset)),
33 613290 : format, args_copy);
34 204430 : va_end(args_copy);
35 351240 : if (written < 0) continue; // not enough space.
36 57620 : str.resize(str_offset + written);
37 57620 : return;
38 146810 : }
39 : }
40 :
41 : PRINTF_FORMAT(3, 4)
42 28522 : void PrintFToString(std::string& str, size_t str_offset, const char* format,
43 : ...) {
44 : va_list args;
45 28522 : va_start(args, format);
46 28522 : VPrintFToString(str, str_offset, format, args);
47 28522 : va_end(args);
48 28522 : }
49 :
50 : } // namespace
51 :
52 : // static
53 576 : std::string WasmError::FormatError(const char* format, va_list args) {
54 : std::string result;
55 576 : VPrintFToString(result, 0, format, args);
56 576 : return result;
57 : }
58 :
59 35773 : void ErrorThrower::Format(ErrorType type, const char* format, va_list args) {
60 : DCHECK_NE(kNone, type);
61 : // Only report the first error.
62 71546 : if (error()) return;
63 :
64 : size_t context_len = 0;
65 28522 : if (context_) {
66 28522 : PrintFToString(error_msg_, 0, "%s: ", context_);
67 : context_len = error_msg_.size();
68 : }
69 28522 : VPrintFToString(error_msg_, context_len, format, args);
70 28522 : error_type_ = type;
71 : }
72 :
73 8988 : void ErrorThrower::TypeError(const char* format, ...) {
74 : va_list arguments;
75 8988 : va_start(arguments, format);
76 8988 : Format(kTypeError, format, arguments);
77 8988 : va_end(arguments);
78 8988 : }
79 :
80 1000 : void ErrorThrower::RangeError(const char* format, ...) {
81 : va_list arguments;
82 1000 : va_start(arguments, format);
83 1000 : Format(kRangeError, format, arguments);
84 1000 : va_end(arguments);
85 1000 : }
86 :
87 23096 : void ErrorThrower::CompileError(const char* format, ...) {
88 : va_list arguments;
89 23096 : va_start(arguments, format);
90 23096 : Format(kCompileError, format, arguments);
91 23096 : va_end(arguments);
92 23096 : }
93 :
94 2649 : void ErrorThrower::LinkError(const char* format, ...) {
95 : va_list arguments;
96 2649 : va_start(arguments, format);
97 2649 : Format(kLinkError, format, arguments);
98 2649 : va_end(arguments);
99 2649 : }
100 :
101 40 : void ErrorThrower::RuntimeError(const char* format, ...) {
102 : va_list arguments;
103 40 : va_start(arguments, format);
104 40 : Format(kRuntimeError, format, arguments);
105 40 : va_end(arguments);
106 40 : }
107 :
108 28310 : Handle<Object> ErrorThrower::Reify() {
109 : Handle<JSFunction> constructor;
110 28310 : switch (error_type_) {
111 : case kNone:
112 0 : UNREACHABLE();
113 : case kTypeError:
114 8895 : constructor = isolate_->type_error_function();
115 8895 : break;
116 : case kRangeError:
117 1000 : constructor = isolate_->range_error_function();
118 1000 : break;
119 : case kCompileError:
120 16151 : constructor = isolate_->wasm_compile_error_function();
121 16151 : break;
122 : case kLinkError:
123 2224 : constructor = isolate_->wasm_link_error_function();
124 2224 : break;
125 : case kRuntimeError:
126 40 : constructor = isolate_->wasm_runtime_error_function();
127 40 : break;
128 : }
129 : Handle<String> message = isolate_->factory()
130 28310 : ->NewStringFromUtf8(VectorOf(error_msg_))
131 56620 : .ToHandleChecked();
132 : Reset();
133 28310 : return isolate_->factory()->NewError(constructor, message);
134 : }
135 :
136 2024 : void ErrorThrower::Reset() {
137 30334 : error_type_ = kNone;
138 30334 : error_msg_.clear();
139 2024 : }
140 :
141 0 : ErrorThrower::ErrorThrower(ErrorThrower&& other) V8_NOEXCEPT
142 : : isolate_(other.isolate_),
143 : context_(other.context_),
144 : error_type_(other.error_type_),
145 0 : error_msg_(std::move(other.error_msg_)) {
146 0 : other.error_type_ = kNone;
147 0 : }
148 :
149 21676186 : ErrorThrower::~ErrorThrower() {
150 21676186 : if (error() && !isolate_->has_pending_exception()) {
151 : // We don't want to mix pending exceptions and scheduled exceptions, hence
152 : // an existing exception should be pending, never scheduled.
153 : DCHECK(!isolate_->has_scheduled_exception());
154 108 : isolate_->Throw(*Reify());
155 : }
156 21676185 : }
157 :
158 : } // namespace wasm
159 : } // namespace internal
160 178779 : } // namespace v8
|