| Index: third_party/tcmalloc/chromium/src/metadata_encode_posix.cc
|
| diff --git a/third_party/tcmalloc/chromium/src/metadata_encode_posix.cc b/third_party/tcmalloc/chromium/src/metadata_encode_posix.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..43b12509273ee1b158b21d6702c1fd9756c16f3f
|
| --- /dev/null
|
| +++ b/third_party/tcmalloc/chromium/src/metadata_encode_posix.cc
|
| @@ -0,0 +1,96 @@
|
| +// Copyright (c) 2011, Google Inc.
|
| +// All rights reserved.
|
| +//
|
| +// Redistribution and use in source and binary forms, with or without
|
| +// modification, are permitted provided that the following conditions are
|
| +// met:
|
| +//
|
| +// * Redistributions of source code must retain the above copyright
|
| +// notice, this list of conditions and the following disclaimer.
|
| +// * Redistributions in binary form must reproduce the above
|
| +// copyright notice, this list of conditions and the following disclaimer
|
| +// in the documentation and/or other materials provided with the
|
| +// distribution.
|
| +// * Neither the name of Google Inc. nor the names of its
|
| +// contributors may be used to endorse or promote products derived from
|
| +// this software without specific prior written permission.
|
| +//
|
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| +
|
| +// ---
|
| +// Author: Rebecca Shapiro
|
| +//
|
| +// Posix metadata encryption/encoding implementation.
|
| +
|
| +#include "config.h"
|
| +#include <errno.h>
|
| +#include <fcntl.h>
|
| +#ifdef HAVE_STDINT_H
|
| +#include <stdint.h> // for uintptr_t
|
| +#endif
|
| +#include "base/logging.h"
|
| +
|
| +
|
| +namespace {
|
| +static uintptr_t key_ = 0;
|
| +static uintptr_t ReadFromFD(int fd, size_t size) {
|
| + uintptr_t ret;
|
| + size_t num_read_bytes = 0;
|
| + while (num_read_bytes < size) {
|
| + ssize_t num_bytes = read(fd, reinterpret_cast<void *>(&ret)+num_read_bytes,
|
| + size-num_read_bytes);
|
| + if (num_bytes < 0) {
|
| + CHECK(errno == EINTR);
|
| + } else if (num_bytes == 0) {
|
| + break;
|
| + }
|
| + num_read_bytes += num_bytes;
|
| + }
|
| + CHECK(num_read_bytes == size);
|
| + return ret;
|
| +}
|
| +
|
| +// Initialization code is not thread safe.
|
| +void InitMetadataEncoderPosix() { // TODO: doesn't link when not inline
|
| + // Ensure this function only executes once.
|
| + CHECK(key_ == false);
|
| +
|
| + // Read random bytes from urandom to initialize key.
|
| + int fd = open("/dev/urandom", O_RDONLY);
|
| + if (fd > 0 && errno == ENOENT) { // If urandom doesn't exist.
|
| + fd = open("/dev/random", O_RDONLY);
|
| + }
|
| + CHECK(fd > 0);
|
| + key_ = ReadFromFD(fd, sizeof(uintptr_t));
|
| + if (key_ == 0 ) { // Try reading from (u)random again.
|
| + key_ = ReadFromFD(fd, sizeof(uintptr_t));
|
| + }
|
| + CHECK(key_ != 0);
|
| + close(fd);
|
| +}
|
| +
|
| +uintptr_t EncodeUintptrPosix(uintptr_t ptr) { return ptr ^ key_; }
|
| +
|
| +uintptr_t DecodeUintptrPosix(uintptr_t ptr) { return EncodeUintptrPosix(ptr); }
|
| +
|
| +}
|
| +
|
| +namespace tcmalloc {
|
| +
|
| +void InitMetadataEncoder() { InitMetadataEncoderPosix(); }
|
| +
|
| +uintptr_t EncodeUintptr(uintptr_t ptr){ return EncodeUintptrPosix(ptr); }
|
| +
|
| +uintptr_t DecodeUintptr(uintptr_t ptr){ return DecodeUintptrPosix(ptr); }
|
| +
|
| +}
|
|
|