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 61518 : void VPrintFToString(std::string& str, size_t str_offset, const char* format,
22 : va_list args) {
23 : DCHECK_LE(str_offset, str.size());
24 61518 : size_t len = str_offset + strlen(format);
25 : // Allocate increasingly large buffers until the message fits.
26 144885 : for (;; len = base::bits::RoundUpToPowerOfTwo64(len + 1)) {
27 : DCHECK_GE(kMaxInt, len);
28 : str.resize(len);
29 : va_list args_copy;
30 206403 : va_copy(args_copy, args);
31 412806 : int written = VSNPrintF(Vector<char>(&str.front() + str_offset,
32 : static_cast<int>(len - str_offset)),
33 206403 : format, args_copy);
34 206403 : va_end(args_copy);
35 351288 : if (written < 0) continue; // not enough space.
36 61518 : str.resize(str_offset + written);
37 61518 : return;
38 : }
39 : }
40 :
41 : PRINTF_FORMAT(3, 4)
42 30471 : void PrintFToString(std::string& str, size_t str_offset, const char* format,
43 : ...) {
44 : va_list args;
45 30471 : va_start(args, format);
46 30471 : VPrintFToString(str, str_offset, format, args);
47 30471 : va_end(args);
48 30471 : }
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 31345 : void ErrorThrower::Format(ErrorType type, const char* format, va_list args) {
60 : DCHECK_NE(kNone, type);
61 : // Only report the first error.
62 31345 : if (error()) return;
63 :
64 : size_t context_len = 0;
65 30471 : if (context_) {
66 30471 : PrintFToString(error_msg_, 0, "%s: ", context_);
67 : context_len = error_msg_.size();
68 : }
69 30471 : VPrintFToString(error_msg_, context_len, format, args);
70 30471 : error_type_ = type;
71 : }
72 :
73 9888 : void ErrorThrower::TypeError(const char* format, ...) {
74 : va_list arguments;
75 9888 : va_start(arguments, format);
76 9888 : Format(kTypeError, format, arguments);
77 9888 : va_end(arguments);
78 9888 : }
79 :
80 1056 : void ErrorThrower::RangeError(const char* format, ...) {
81 : va_list arguments;
82 1056 : va_start(arguments, format);
83 1056 : Format(kRangeError, format, arguments);
84 1056 : va_end(arguments);
85 1056 : }
86 :
87 17704 : void ErrorThrower::CompileError(const char* format, ...) {
88 : va_list arguments;
89 17704 : va_start(arguments, format);
90 17704 : Format(kCompileError, format, arguments);
91 17704 : va_end(arguments);
92 17704 : }
93 :
94 2657 : void ErrorThrower::LinkError(const char* format, ...) {
95 : va_list arguments;
96 2657 : va_start(arguments, format);
97 2657 : Format(kLinkError, format, arguments);
98 2657 : va_end(arguments);
99 2657 : }
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 30259 : Handle<Object> ErrorThrower::Reify() {
109 : Handle<JSFunction> constructor;
110 30259 : switch (error_type_) {
111 : case kNone:
112 0 : UNREACHABLE();
113 : case kTypeError:
114 9795 : constructor = isolate_->type_error_function();
115 9795 : break;
116 : case kRangeError:
117 1056 : constructor = isolate_->range_error_function();
118 1056 : break;
119 : case kCompileError:
120 17136 : constructor = isolate_->wasm_compile_error_function();
121 17136 : break;
122 : case kLinkError:
123 2232 : constructor = isolate_->wasm_link_error_function();
124 2232 : break;
125 : case kRuntimeError:
126 40 : constructor = isolate_->wasm_runtime_error_function();
127 40 : break;
128 : }
129 30259 : Handle<String> message = isolate_->factory()
130 60518 : ->NewStringFromUtf8(VectorOf(error_msg_))
131 30259 : .ToHandleChecked();
132 : Reset();
133 30259 : return isolate_->factory()->NewError(constructor, message);
134 : }
135 :
136 2032 : void ErrorThrower::Reset() {
137 32291 : error_type_ = kNone;
138 : error_msg_.clear();
139 2032 : }
140 :
141 0 : ErrorThrower::ErrorThrower(ErrorThrower&& other) V8_NOEXCEPT
142 0 : : isolate_(other.isolate_),
143 0 : context_(other.context_),
144 0 : error_type_(other.error_type_),
145 0 : error_msg_(std::move(other.error_msg_)) {
146 0 : other.error_type_ = kNone;
147 0 : }
148 :
149 107766162 : ErrorThrower::~ErrorThrower() {
150 53883140 : 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 118 : isolate_->Throw(*Reify());
155 : }
156 53883081 : }
157 :
158 : } // namespace wasm
159 : } // namespace internal
160 120216 : } // namespace v8
|