js/src/vm/Xdr.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "vm/Xdr.h"
michael@0 8
michael@0 9 #include <string.h>
michael@0 10
michael@0 11 #include "jsapi.h"
michael@0 12 #include "jsscript.h"
michael@0 13
michael@0 14 #include "vm/Debugger.h"
michael@0 15
michael@0 16 using namespace js;
michael@0 17
michael@0 18 void
michael@0 19 XDRBuffer::freeBuffer()
michael@0 20 {
michael@0 21 js_free(base);
michael@0 22 #ifdef DEBUG
michael@0 23 memset(this, 0xe2, sizeof *this);
michael@0 24 #endif
michael@0 25 }
michael@0 26
michael@0 27 bool
michael@0 28 XDRBuffer::grow(size_t n)
michael@0 29 {
michael@0 30 JS_ASSERT(n > size_t(limit - cursor));
michael@0 31
michael@0 32 const size_t MEM_BLOCK = 8192;
michael@0 33 size_t offset = cursor - base;
michael@0 34 size_t newCapacity = JS_ROUNDUP(offset + n, MEM_BLOCK);
michael@0 35 if (isUint32Overflow(newCapacity)) {
michael@0 36 JS_ReportErrorNumber(cx(), js_GetErrorMessage, nullptr, JSMSG_TOO_BIG_TO_ENCODE);
michael@0 37 return false;
michael@0 38 }
michael@0 39
michael@0 40 void *data = js_realloc(base, newCapacity);
michael@0 41 if (!data) {
michael@0 42 js_ReportOutOfMemory(cx());
michael@0 43 return false;
michael@0 44 }
michael@0 45 base = static_cast<uint8_t *>(data);
michael@0 46 cursor = base + offset;
michael@0 47 limit = base + newCapacity;
michael@0 48 return true;
michael@0 49 }
michael@0 50
michael@0 51 template<XDRMode mode>
michael@0 52 bool
michael@0 53 XDRState<mode>::codeChars(jschar *chars, size_t nchars)
michael@0 54 {
michael@0 55 size_t nbytes = nchars * sizeof(jschar);
michael@0 56 if (mode == XDR_ENCODE) {
michael@0 57 uint8_t *ptr = buf.write(nbytes);
michael@0 58 if (!ptr)
michael@0 59 return false;
michael@0 60 mozilla::NativeEndian::copyAndSwapToLittleEndian(ptr, chars, nchars);
michael@0 61 } else {
michael@0 62 const uint8_t *ptr = buf.read(nbytes);
michael@0 63 mozilla::NativeEndian::copyAndSwapFromLittleEndian(chars, ptr, nchars);
michael@0 64 }
michael@0 65 return true;
michael@0 66 }
michael@0 67
michael@0 68 template<XDRMode mode>
michael@0 69 static bool
michael@0 70 VersionCheck(XDRState<mode> *xdr)
michael@0 71 {
michael@0 72 uint32_t bytecodeVer;
michael@0 73 if (mode == XDR_ENCODE)
michael@0 74 bytecodeVer = XDR_BYTECODE_VERSION;
michael@0 75
michael@0 76 if (!xdr->codeUint32(&bytecodeVer))
michael@0 77 return false;
michael@0 78
michael@0 79 if (mode == XDR_DECODE && bytecodeVer != XDR_BYTECODE_VERSION) {
michael@0 80 /* We do not provide binary compatibility with older scripts. */
michael@0 81 JS_ReportErrorNumber(xdr->cx(), js_GetErrorMessage, nullptr, JSMSG_BAD_SCRIPT_MAGIC);
michael@0 82 return false;
michael@0 83 }
michael@0 84
michael@0 85 return true;
michael@0 86 }
michael@0 87
michael@0 88 template<XDRMode mode>
michael@0 89 bool
michael@0 90 XDRState<mode>::codeFunction(MutableHandleObject objp)
michael@0 91 {
michael@0 92 if (mode == XDR_DECODE)
michael@0 93 objp.set(nullptr);
michael@0 94
michael@0 95 if (!VersionCheck(this))
michael@0 96 return false;
michael@0 97
michael@0 98 return XDRInterpretedFunction(this, NullPtr(), NullPtr(), objp);
michael@0 99 }
michael@0 100
michael@0 101 template<XDRMode mode>
michael@0 102 bool
michael@0 103 XDRState<mode>::codeScript(MutableHandleScript scriptp)
michael@0 104 {
michael@0 105 if (mode == XDR_DECODE)
michael@0 106 scriptp.set(nullptr);
michael@0 107
michael@0 108 if (!VersionCheck(this))
michael@0 109 return false;
michael@0 110
michael@0 111 if (!XDRScript(this, NullPtr(), NullPtr(), NullPtr(), scriptp))
michael@0 112 return false;
michael@0 113
michael@0 114 return true;
michael@0 115 }
michael@0 116
michael@0 117 template<XDRMode mode>
michael@0 118 bool
michael@0 119 XDRState<mode>::codeConstValue(MutableHandleValue vp)
michael@0 120 {
michael@0 121 return XDRScriptConst(this, vp);
michael@0 122 }
michael@0 123
michael@0 124 XDRDecoder::XDRDecoder(JSContext *cx, const void *data, uint32_t length,
michael@0 125 JSPrincipals *originPrincipals)
michael@0 126 : XDRState<XDR_DECODE>(cx)
michael@0 127 {
michael@0 128 buf.setData(data, length);
michael@0 129 this->originPrincipals_ = originPrincipals;
michael@0 130 }
michael@0 131
michael@0 132 template class js::XDRState<XDR_ENCODE>;
michael@0 133 template class js::XDRState<XDR_DECODE>;

mercurial