1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/vm/Xdr.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "vm/Xdr.h" 1.11 + 1.12 +#include <string.h> 1.13 + 1.14 +#include "jsapi.h" 1.15 +#include "jsscript.h" 1.16 + 1.17 +#include "vm/Debugger.h" 1.18 + 1.19 +using namespace js; 1.20 + 1.21 +void 1.22 +XDRBuffer::freeBuffer() 1.23 +{ 1.24 + js_free(base); 1.25 +#ifdef DEBUG 1.26 + memset(this, 0xe2, sizeof *this); 1.27 +#endif 1.28 +} 1.29 + 1.30 +bool 1.31 +XDRBuffer::grow(size_t n) 1.32 +{ 1.33 + JS_ASSERT(n > size_t(limit - cursor)); 1.34 + 1.35 + const size_t MEM_BLOCK = 8192; 1.36 + size_t offset = cursor - base; 1.37 + size_t newCapacity = JS_ROUNDUP(offset + n, MEM_BLOCK); 1.38 + if (isUint32Overflow(newCapacity)) { 1.39 + JS_ReportErrorNumber(cx(), js_GetErrorMessage, nullptr, JSMSG_TOO_BIG_TO_ENCODE); 1.40 + return false; 1.41 + } 1.42 + 1.43 + void *data = js_realloc(base, newCapacity); 1.44 + if (!data) { 1.45 + js_ReportOutOfMemory(cx()); 1.46 + return false; 1.47 + } 1.48 + base = static_cast<uint8_t *>(data); 1.49 + cursor = base + offset; 1.50 + limit = base + newCapacity; 1.51 + return true; 1.52 +} 1.53 + 1.54 +template<XDRMode mode> 1.55 +bool 1.56 +XDRState<mode>::codeChars(jschar *chars, size_t nchars) 1.57 +{ 1.58 + size_t nbytes = nchars * sizeof(jschar); 1.59 + if (mode == XDR_ENCODE) { 1.60 + uint8_t *ptr = buf.write(nbytes); 1.61 + if (!ptr) 1.62 + return false; 1.63 + mozilla::NativeEndian::copyAndSwapToLittleEndian(ptr, chars, nchars); 1.64 + } else { 1.65 + const uint8_t *ptr = buf.read(nbytes); 1.66 + mozilla::NativeEndian::copyAndSwapFromLittleEndian(chars, ptr, nchars); 1.67 + } 1.68 + return true; 1.69 +} 1.70 + 1.71 +template<XDRMode mode> 1.72 +static bool 1.73 +VersionCheck(XDRState<mode> *xdr) 1.74 +{ 1.75 + uint32_t bytecodeVer; 1.76 + if (mode == XDR_ENCODE) 1.77 + bytecodeVer = XDR_BYTECODE_VERSION; 1.78 + 1.79 + if (!xdr->codeUint32(&bytecodeVer)) 1.80 + return false; 1.81 + 1.82 + if (mode == XDR_DECODE && bytecodeVer != XDR_BYTECODE_VERSION) { 1.83 + /* We do not provide binary compatibility with older scripts. */ 1.84 + JS_ReportErrorNumber(xdr->cx(), js_GetErrorMessage, nullptr, JSMSG_BAD_SCRIPT_MAGIC); 1.85 + return false; 1.86 + } 1.87 + 1.88 + return true; 1.89 +} 1.90 + 1.91 +template<XDRMode mode> 1.92 +bool 1.93 +XDRState<mode>::codeFunction(MutableHandleObject objp) 1.94 +{ 1.95 + if (mode == XDR_DECODE) 1.96 + objp.set(nullptr); 1.97 + 1.98 + if (!VersionCheck(this)) 1.99 + return false; 1.100 + 1.101 + return XDRInterpretedFunction(this, NullPtr(), NullPtr(), objp); 1.102 +} 1.103 + 1.104 +template<XDRMode mode> 1.105 +bool 1.106 +XDRState<mode>::codeScript(MutableHandleScript scriptp) 1.107 +{ 1.108 + if (mode == XDR_DECODE) 1.109 + scriptp.set(nullptr); 1.110 + 1.111 + if (!VersionCheck(this)) 1.112 + return false; 1.113 + 1.114 + if (!XDRScript(this, NullPtr(), NullPtr(), NullPtr(), scriptp)) 1.115 + return false; 1.116 + 1.117 + return true; 1.118 +} 1.119 + 1.120 +template<XDRMode mode> 1.121 +bool 1.122 +XDRState<mode>::codeConstValue(MutableHandleValue vp) 1.123 +{ 1.124 + return XDRScriptConst(this, vp); 1.125 +} 1.126 + 1.127 +XDRDecoder::XDRDecoder(JSContext *cx, const void *data, uint32_t length, 1.128 + JSPrincipals *originPrincipals) 1.129 + : XDRState<XDR_DECODE>(cx) 1.130 +{ 1.131 + buf.setData(data, length); 1.132 + this->originPrincipals_ = originPrincipals; 1.133 +} 1.134 + 1.135 +template class js::XDRState<XDR_ENCODE>; 1.136 +template class js::XDRState<XDR_DECODE>;