1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "vm/Xdr.h"
#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_ASSERT_IF
#include "mozilla/EndianUtils.h" // mozilla::NativeEndian, MOZ_LITTLE_ENDIAN
#include "mozilla/Result.h" // mozilla::{Result, Ok, Err}, MOZ_TRY
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include <algorithm> // std::transform
#include <stddef.h> // size_t
#include <stdint.h> // uint8_t, uint32_t, uintptr_t
#include <string> // std::char_traits
#include <type_traits> // std::is_same_v
#include <utility> // std::move
#include "frontend/FrontendContext.h" // FrontendContext
#include "js/Transcoding.h" // JS::TranscodeResult, JS::TranscodeBuffer, JS::TranscodeRange
#include "js/UniquePtr.h" // UniquePtr
#include "js/Utility.h" // JS::FreePolicy, js_delete
#include "vm/JSContext.h" // JSContext, ReportAllocationOverflow
#include "vm/StringType.h" // JSString
using namespace js;
using mozilla::Utf8Unit;
#ifdef DEBUG
bool XDRCoderBase::validateResultCode(JSContext* cx, FrontendContext* fc,
JS::TranscodeResult code) const {
// NOTE: This function is called to verify that we do not have a pending
// exception on the JSContext at the same time as a TranscodeResult failure.
if (cx->isHelperThreadContext()) {
return true;
}
// NOTE: Errors during XDR encode/decode are supposed to be reported to
// FrontendContext, instead of JSContext.
// This branch is for covering remaining consumer of JSContext for
// error reporting (e.g. memory allocation).
// TODO: Remove this once JSContext is removed from frontend and all errors
// are reported to FrontendContext.
if (cx->isExceptionPending()) {
return bool(code == JS::TranscodeResult::Throw);
}
return fc->hadErrors() == bool(code == JS::TranscodeResult::Throw);
}
#endif
template <XDRMode mode>
XDRResult XDRState<mode>::codeChars(char* chars, size_t nchars) {
return codeBytes(chars, nchars);
}
template <XDRMode mode>
XDRResult XDRState<mode>::codeChars(Latin1Char* chars, size_t nchars) {
static_assert(sizeof(Latin1Char) == 1,
"Latin1Char must be 1 byte for nchars below to be the "
"proper count of bytes");
static_assert(std::is_same_v<Latin1Char, unsigned char>,
"Latin1Char must be unsigned char to C++-safely reinterpret "
"the bytes generically copied below as Latin1Char");
return codeBytes(chars, nchars);
}
template <XDRMode mode>
XDRResult XDRState<mode>::codeChars(Utf8Unit* units, size_t count) {
if (count == 0) {
return Ok();
}
if (mode == XDR_ENCODE) {
uint8_t* ptr = buf->write(count);
if (!ptr) {
return fail(JS::TranscodeResult::Throw);
}
std::transform(units, units + count, ptr,
[](const Utf8Unit& unit) { return unit.toUint8(); });
} else {
const uint8_t* ptr = buf->read(count);
if (!ptr) {
return fail(JS::TranscodeResult::Failure_BadDecode);
}
std::transform(ptr, ptr + count, units,
[](const uint8_t& value) { return Utf8Unit(value); });
}
return Ok();
}
template <XDRMode mode>
XDRResult XDRState<mode>::codeChars(char16_t* chars, size_t nchars) {
if (nchars == 0) {
return Ok();
}
size_t nbytes = nchars * sizeof(char16_t);
if (mode == XDR_ENCODE) {
uint8_t* ptr = buf->write(nbytes);
if (!ptr) {
return fail(JS::TranscodeResult::Throw);
}
// |mozilla::NativeEndian| correctly handles writing into unaligned |ptr|.
mozilla::NativeEndian::copyAndSwapToLittleEndian(ptr, chars, nchars);
} else {
const uint8_t* ptr = buf->read(nbytes);
if (!ptr) {
return fail(JS::TranscodeResult::Failure_BadDecode);
}
// |mozilla::NativeEndian| correctly handles reading from unaligned |ptr|.
mozilla::NativeEndian::copyAndSwapFromLittleEndian(chars, ptr, nchars);
}
return Ok();
}
template <XDRMode mode, typename CharT>
static XDRResult XDRCodeCharsZ(XDRState<mode>* xdr,
XDRTranscodeString<CharT>& buffer) {
MOZ_ASSERT_IF(mode == XDR_ENCODE, !buffer.empty());
MOZ_ASSERT_IF(mode == XDR_DECODE, buffer.empty());
using OwnedString = js::UniquePtr<CharT[], JS::FreePolicy>;
OwnedString owned;
static_assert(JSString::MAX_LENGTH <= INT32_MAX,
"String length must fit in int32_t");
uint32_t length = 0;
CharT* chars = nullptr;
if (mode == XDR_ENCODE) {
chars = const_cast<CharT*>(buffer.template ref<const CharT*>());
// Set a reasonable limit on string length.
size_t lengthSizeT = std::char_traits<CharT>::length(chars);
if (lengthSizeT > JSString::MAX_LENGTH) {
ReportAllocationOverflow(xdr->cx());
return xdr->fail(JS::TranscodeResult::Throw);
}
length = static_cast<uint32_t>(lengthSizeT);
}
MOZ_TRY(xdr->codeUint32(&length));
if (mode == XDR_DECODE) {
owned = xdr->cx()->template make_pod_array<CharT>(length + 1);
if (!owned) {
return xdr->fail(JS::TranscodeResult::Throw);
}
chars = owned.get();
}
MOZ_TRY(xdr->codeChars(chars, length));
if (mode == XDR_DECODE) {
// Null-terminate and transfer ownership to caller.
owned[length] = '\0';
buffer.template construct<OwnedString>(std::move(owned));
}
return Ok();
}
template <XDRMode mode>
XDRResult XDRState<mode>::codeCharsZ(XDRTranscodeString<char>& buffer) {
return XDRCodeCharsZ(this, buffer);
}
template <XDRMode mode>
XDRResult XDRState<mode>::codeCharsZ(XDRTranscodeString<char16_t>& buffer) {
return XDRCodeCharsZ(this, buffer);
}
template class js::XDRState<XDR_ENCODE>;
template class js::XDRState<XDR_DECODE>;
|