From 4486c47d9b207e95f15598135262bb43f0c5dc00 Mon Sep 17 00:00:00 2001 From: bmeurer Date: Mon, 27 Apr 2015 23:54:08 -0700 Subject: [PATCH] [clang] Use -Wshorten-64-to-32 to enable warnings about 64bit to 32bit truncations. Currently only the Win64 bots report this warnings, which adds quite some overhead to the development process. With this flag we also get compiler warnings about implicit 64bit to 32bit truncations when building with clang on Linux/x64 and Mac/x64. R=svenpanne@chromium.org Review URL: https://codereview.chromium.org/1111733002 Cr-Commit-Position: refs/heads/master@{#28093} --- build/standalone.gypi | 6 +++ samples/process.cc | 15 ++++--- samples/shell.cc | 15 ++++--- src/base/platform/platform-aix.cc | 49 -------------------- src/base/platform/platform-cygwin.cc | 48 -------------------- src/base/platform/platform-freebsd.cc | 48 -------------------- src/base/platform/platform-linux.cc | 62 +------------------------ src/base/platform/platform-macos.cc | 58 ------------------------ src/base/platform/platform-openbsd.cc | 48 -------------------- src/base/platform/platform-posix.cc | 65 +++++++++++++++++++++++++-- src/base/platform/platform-qnx.cc | 58 ------------------------ src/base/platform/platform-solaris.cc | 48 -------------------- src/base/platform/platform-win32.cc | 47 +++++++++---------- src/base/platform/platform.h | 10 +++-- src/base/platform/time.cc | 6 +-- src/d8-posix.cc | 18 ++++---- src/d8.cc | 14 +++--- src/flags.cc | 2 +- src/gdb-jit.cc | 15 ++++--- src/snapshot/mksnapshot.cc | 8 ++-- src/utils.cc | 2 +- test/cctest/compiler/call-tester.h | 6 +++ test/cctest/test-mark-compact.cc | 2 +- tools/shell-utils.h | 4 +- 24 files changed, 163 insertions(+), 491 deletions(-) diff --git a/build/standalone.gypi b/build/standalone.gypi index cd7847b349..7c96720673 100644 --- a/build/standalone.gypi +++ b/build/standalone.gypi @@ -243,6 +243,7 @@ '-Wall', '-Werror', '-Wextra', + '-Wshorten-64-to-32', ], 'cflags+': [ # Clang considers the `register` keyword as deprecated, but @@ -389,6 +390,11 @@ 'cflags_cc': [ '-Wnon-virtual-dtor', '-fno-rtti', '-std=gnu++0x' ], 'ldflags': [ '-pthread', ], 'conditions': [ + # TODO(arm64): It'd be nice to enable this for arm64 as well, + # but the Assembler requires some serious fixing first. + [ 'clang==1 and v8_target_arch=="x64"', { + 'cflags': [ '-Wshorten-64-to-32' ], + }], [ 'host_arch=="ppc64" and OS!="aix"', { 'cflags': [ '-mminimal-toc' ], }], diff --git a/samples/process.cc b/samples/process.cc index f44797066b..e0c273e08a 100644 --- a/samples/process.cc +++ b/samples/process.cc @@ -595,18 +595,21 @@ Handle ReadFile(Isolate* isolate, const string& name) { if (file == NULL) return Handle(); fseek(file, 0, SEEK_END); - int size = ftell(file); + size_t size = ftell(file); rewind(file); char* chars = new char[size + 1]; chars[size] = '\0'; - for (int i = 0; i < size;) { - int read = static_cast(fread(&chars[i], 1, size - i, file)); - i += read; + for (size_t i = 0; i < size;) { + i += fread(&chars[i], 1, size - i, file); + if (ferror(file)) { + fclose(file); + return Handle(); + } } fclose(file); - Handle result = - String::NewFromUtf8(isolate, chars, String::kNormalString, size); + Handle result = String::NewFromUtf8( + isolate, chars, String::kNormalString, static_cast(size)); delete[] chars; return result; } diff --git a/samples/shell.cc b/samples/shell.cc index 0ed742d3e3..a061aa469a 100644 --- a/samples/shell.cc +++ b/samples/shell.cc @@ -238,18 +238,21 @@ v8::Handle ReadFile(v8::Isolate* isolate, const char* name) { if (file == NULL) return v8::Handle(); fseek(file, 0, SEEK_END); - int size = ftell(file); + size_t size = ftell(file); rewind(file); char* chars = new char[size + 1]; chars[size] = '\0'; - for (int i = 0; i < size;) { - int read = static_cast(fread(&chars[i], 1, size - i, file)); - i += read; + for (size_t i = 0; i < size;) { + i += fread(&chars[i], 1, size - i, file); + if (ferror(file)) { + fclose(file); + return v8::Handle(); + } } fclose(file); - v8::Handle result = - v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size); + v8::Handle result = v8::String::NewFromUtf8( + isolate, chars, v8::String::kNormalString, static_cast(size)); delete[] chars; return result; } diff --git a/src/base/platform/platform-aix.cc b/src/base/platform/platform-aix.cc index 3083f752da..513f5c8eca 100644 --- a/src/base/platform/platform-aix.cc +++ b/src/base/platform/platform-aix.cc @@ -73,55 +73,6 @@ void* OS::Allocate(const size_t requested, size_t* allocated, bool executable) { } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) {} - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmapHelper(size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) munmap(memory_, size_); - fclose(file_); -} - - static unsigned StringToLong(char* buffer) { return static_cast(strtol(buffer, NULL, 16)); // NOLINT } diff --git a/src/base/platform/platform-cygwin.cc b/src/base/platform/platform-cygwin.cc index 8a767cf296..e09fc68395 100644 --- a/src/base/platform/platform-cygwin.cc +++ b/src/base/platform/platform-cygwin.cc @@ -59,54 +59,6 @@ void* OS::Allocate(const size_t requested, } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) { } - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) munmap(memory_, size_); - fclose(file_); -} - - std::vector OS::GetSharedLibraryAddresses() { std::vector result; // This function assumes that the layout of the file is as follows: diff --git a/src/base/platform/platform-freebsd.cc b/src/base/platform/platform-freebsd.cc index 68ed70af93..8fe908c2df 100644 --- a/src/base/platform/platform-freebsd.cc +++ b/src/base/platform/platform-freebsd.cc @@ -68,54 +68,6 @@ void* OS::Allocate(const size_t requested, } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) { } - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) munmap(memory_, size_); - fclose(file_); -} - - static unsigned StringToLong(char* buffer) { return static_cast(strtol(buffer, NULL, 16)); // NOLINT } diff --git a/src/base/platform/platform-linux.cc b/src/base/platform/platform-linux.cc index 36857e6219..874c6dbc31 100644 --- a/src/base/platform/platform-linux.cc +++ b/src/base/platform/platform-linux.cc @@ -142,64 +142,6 @@ void* OS::Allocate(const size_t requested, } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) { } - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmap(OS::GetRandomMmapAddr(), - size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fileno(file), - 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmap(OS::GetRandomMmapAddr(), - size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fileno(file), - 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) OS::Free(memory_, size_); - fclose(file_); -} - - std::vector OS::GetSharedLibraryAddresses() { std::vector result; // This function assumes that the layout of the file is as follows: @@ -271,7 +213,7 @@ void OS::SignalCodeMovingGC() { // it. This injects a GC marker into the stream of events generated // by the kernel and allows us to synchronize V8 code log and the // kernel log. - int size = sysconf(_SC_PAGESIZE); + long size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int) FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+"); if (f == NULL) { OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile()); @@ -286,7 +228,7 @@ void OS::SignalCodeMovingGC() { PROT_READ | PROT_EXEC, #endif MAP_PRIVATE, fileno(f), 0); - DCHECK(addr != MAP_FAILED); + DCHECK_NE(MAP_FAILED, addr); OS::Free(addr, size); fclose(f); } diff --git a/src/base/platform/platform-macos.cc b/src/base/platform/platform-macos.cc index 77893ee1b9..4b760c3b86 100644 --- a/src/base/platform/platform-macos.cc +++ b/src/base/platform/platform-macos.cc @@ -68,64 +68,6 @@ void* OS::Allocate(const size_t requested, } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) { } - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmap(OS::GetRandomMmapAddr(), - size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fileno(file), - 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmap(OS::GetRandomMmapAddr(), - size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fileno(file), - 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) OS::Free(memory_, size_); - fclose(file_); -} - - std::vector OS::GetSharedLibraryAddresses() { std::vector result; unsigned int images_count = _dyld_image_count(); diff --git a/src/base/platform/platform-openbsd.cc b/src/base/platform/platform-openbsd.cc index 4e706cb7c1..32b1af1ca8 100644 --- a/src/base/platform/platform-openbsd.cc +++ b/src/base/platform/platform-openbsd.cc @@ -66,54 +66,6 @@ void* OS::Allocate(const size_t requested, } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) { } - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) OS::Free(memory_, size_); - fclose(file_); -} - - std::vector OS::GetSharedLibraryAddresses() { std::vector result; // This function assumes that the layout of the file is as follows: diff --git a/src/base/platform/platform-posix.cc b/src/base/platform/platform-posix.cc index e43493f0ae..8fb40b0bc0 100644 --- a/src/base/platform/platform-posix.cc +++ b/src/base/platform/platform-posix.cc @@ -259,6 +259,65 @@ void OS::DebugBreak() { } +class PosixMemoryMappedFile final : public OS::MemoryMappedFile { + public: + PosixMemoryMappedFile(FILE* file, void* memory, size_t size) + : file_(file), memory_(memory), size_(size) {} + ~PosixMemoryMappedFile() final; + void* memory() const final { return memory_; } + size_t size() const final { return size_; } + + private: + FILE* const file_; + void* const memory_; + size_t const size_; +}; + + +// static +OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { + if (FILE* file = fopen(name, "r+")) { + if (fseek(file, 0, SEEK_END) == 0) { + long size = ftell(file); // NOLINT(runtime/int) + if (size >= 0) { + void* const memory = + mmap(OS::GetRandomMmapAddr(), size, PROT_READ | PROT_WRITE, + MAP_SHARED, fileno(file), 0); + if (memory != MAP_FAILED) { + return new PosixMemoryMappedFile(file, memory, size); + } + } + } + fclose(file); + } + return nullptr; +} + + +// static +OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, + size_t size, void* initial) { + if (FILE* file = fopen(name, "w+")) { + size_t result = fwrite(initial, 1, size, file); + if (result == size && !ferror(file)) { + void* memory = mmap(OS::GetRandomMmapAddr(), result, + PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); + if (memory != MAP_FAILED) { + return new PosixMemoryMappedFile(file, memory, result); + } + } + fclose(file); + } + return nullptr; +} + + +PosixMemoryMappedFile::~PosixMemoryMappedFile() { + if (memory_) OS::Free(memory_, size_); + fclose(file_); +} + + int OS::GetCurrentProcessId() { return static_cast(getpid()); } @@ -285,7 +344,7 @@ int OS::GetCurrentThreadId() { // POSIX date/time support. // -int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { +int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { #if V8_OS_NACL // Optionally used in Logger::ResourceEvent. return -1; @@ -293,8 +352,8 @@ int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { struct rusage usage; if (getrusage(RUSAGE_SELF, &usage) < 0) return -1; - *secs = usage.ru_utime.tv_sec; - *usecs = usage.ru_utime.tv_usec; + *secs = static_cast(usage.ru_utime.tv_sec); + *usecs = static_cast(usage.ru_utime.tv_usec); return 0; #endif } diff --git a/src/base/platform/platform-qnx.cc b/src/base/platform/platform-qnx.cc index 2cb3228400..9f22db5d1e 100644 --- a/src/base/platform/platform-qnx.cc +++ b/src/base/platform/platform-qnx.cc @@ -117,64 +117,6 @@ void* OS::Allocate(const size_t requested, } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) { } - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmap(OS::GetRandomMmapAddr(), - size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fileno(file), - 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmap(OS::GetRandomMmapAddr(), - size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - fileno(file), - 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) OS::Free(memory_, size_); - fclose(file_); -} - - std::vector OS::GetSharedLibraryAddresses() { std::vector result; procfs_mapinfo *mapinfos = NULL, *mapinfo; diff --git a/src/base/platform/platform-solaris.cc b/src/base/platform/platform-solaris.cc index b9a2ec8e2e..e844aa12b4 100644 --- a/src/base/platform/platform-solaris.cc +++ b/src/base/platform/platform-solaris.cc @@ -63,54 +63,6 @@ void* OS::Allocate(const size_t requested, } -class PosixMemoryMappedFile : public OS::MemoryMappedFile { - public: - PosixMemoryMappedFile(FILE* file, void* memory, int size) - : file_(file), memory_(memory), size_(size) { } - virtual ~PosixMemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } - private: - FILE* file_; - void* memory_; - int size_; -}; - - -OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { - FILE* file = fopen(name, "r+"); - if (file == NULL) return NULL; - - fseek(file, 0, SEEK_END); - int size = ftell(file); - - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { - FILE* file = fopen(name, "w+"); - if (file == NULL) return NULL; - int result = fwrite(initial, size, 1, file); - if (result < 1) { - fclose(file); - return NULL; - } - void* memory = - mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); - return new PosixMemoryMappedFile(file, memory, size); -} - - -PosixMemoryMappedFile::~PosixMemoryMappedFile() { - if (memory_) munmap(memory_, size_); - fclose(file_); -} - - std::vector OS::GetSharedLibraryAddresses() { return std::vector(); } diff --git a/src/base/platform/platform-win32.cc b/src/base/platform/platform-win32.cc index 4328039c22..b0677afad5 100644 --- a/src/base/platform/platform-win32.cc +++ b/src/base/platform/platform-win32.cc @@ -838,38 +838,38 @@ void OS::DebugBreak() { } -class Win32MemoryMappedFile : public OS::MemoryMappedFile { +class Win32MemoryMappedFile final : public OS::MemoryMappedFile { public: - Win32MemoryMappedFile(HANDLE file, - HANDLE file_mapping, - void* memory, - int size) + Win32MemoryMappedFile(HANDLE file, HANDLE file_mapping, void* memory, + size_t size) : file_(file), file_mapping_(file_mapping), memory_(memory), - size_(size) { } - virtual ~Win32MemoryMappedFile(); - virtual void* memory() { return memory_; } - virtual int size() { return size_; } + size_(size) {} + ~Win32MemoryMappedFile() final; + void* memory() const final { return memory_; } + size_t size() const final { return size_; } + private: - HANDLE file_; - HANDLE file_mapping_; - void* memory_; - int size_; + HANDLE const file_; + HANDLE const file_mapping_; + void* const memory_; + size_t const size_; }; +// static OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { // Open a physical file HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (file == INVALID_HANDLE_VALUE) return NULL; - int size = static_cast(GetFileSize(file, NULL)); + DWORD size = GetFileSize(file, NULL); // Create a file mapping for the physical file - HANDLE file_mapping = CreateFileMapping(file, NULL, - PAGE_READWRITE, 0, static_cast(size), NULL); + HANDLE file_mapping = + CreateFileMapping(file, NULL, PAGE_READWRITE, 0, size, NULL); if (file_mapping == NULL) return NULL; // Map a view of the file into memory @@ -878,15 +878,17 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) { } -OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, - void* initial) { +// static +OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, + size_t size, void* initial) { // Open a physical file HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL); + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + OPEN_ALWAYS, 0, NULL); if (file == NULL) return NULL; // Create a file mapping for the physical file - HANDLE file_mapping = CreateFileMapping(file, NULL, - PAGE_READWRITE, 0, static_cast(size), NULL); + HANDLE file_mapping = CreateFileMapping(file, NULL, PAGE_READWRITE, 0, + static_cast(size), NULL); if (file_mapping == NULL) return NULL; // Map a view of the file into memory void* memory = MapViewOfFile(file_mapping, FILE_MAP_ALL_ACCESS, 0, 0, size); @@ -896,8 +898,7 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, Win32MemoryMappedFile::~Win32MemoryMappedFile() { - if (memory_ != NULL) - UnmapViewOfFile(memory_); + if (memory_) UnmapViewOfFile(memory_); CloseHandle(file_mapping_); CloseHandle(file_); } diff --git a/src/base/platform/platform.h b/src/base/platform/platform.h index 1873bbe57d..2d837baff6 100644 --- a/src/base/platform/platform.h +++ b/src/base/platform/platform.h @@ -210,11 +210,13 @@ class OS { class MemoryMappedFile { public: + virtual ~MemoryMappedFile() {} + virtual void* memory() const = 0; + virtual size_t size() const = 0; + static MemoryMappedFile* open(const char* name); - static MemoryMappedFile* create(const char* name, int size, void* initial); - virtual ~MemoryMappedFile() { } - virtual void* memory() = 0; - virtual int size() = 0; + static MemoryMappedFile* create(const char* name, size_t size, + void* initial); }; // Safe formatting print. Ensures that str is always null-terminated. diff --git a/src/base/platform/time.cc b/src/base/platform/time.cc index a2594f471d..b6a11cff34 100644 --- a/src/base/platform/time.cc +++ b/src/base/platform/time.cc @@ -133,7 +133,7 @@ TimeDelta TimeDelta::FromTimespec(struct timespec ts) { struct timespec TimeDelta::ToTimespec() const { struct timespec ts; - ts.tv_sec = delta_ / Time::kMicrosecondsPerSecond; + ts.tv_sec = static_cast(delta_ / Time::kMicrosecondsPerSecond); ts.tv_nsec = (delta_ % Time::kMicrosecondsPerSecond) * Time::kNanosecondsPerMicrosecond; return ts; @@ -292,7 +292,7 @@ struct timespec Time::ToTimespec() const { ts.tv_nsec = static_cast(kNanosecondsPerSecond - 1); // NOLINT return ts; } - ts.tv_sec = us_ / kMicrosecondsPerSecond; + ts.tv_sec = static_cast(us_ / kMicrosecondsPerSecond); ts.tv_nsec = (us_ % kMicrosecondsPerSecond) * kNanosecondsPerMicrosecond; return ts; } @@ -324,7 +324,7 @@ struct timeval Time::ToTimeval() const { tv.tv_usec = static_cast(kMicrosecondsPerSecond - 1); return tv; } - tv.tv_sec = us_ / kMicrosecondsPerSecond; + tv.tv_sec = static_cast(us_ / kMicrosecondsPerSecond); tv.tv_usec = us_ % kMicrosecondsPerSecond; return tv; } diff --git a/src/d8-posix.cc b/src/d8-posix.cc index 9a20b06643..3bca14f530 100644 --- a/src/d8-posix.cc +++ b/src/d8-posix.cc @@ -89,8 +89,9 @@ static bool WaitOnFD(int fd, if (total_timeout != -1) { struct timeval time_now; gettimeofday(&time_now, NULL); - int seconds = time_now.tv_sec - start_time.tv_sec; - gone = seconds * 1000 + (time_now.tv_usec - start_time.tv_usec) / 1000; + time_t seconds = time_now.tv_sec - start_time.tv_sec; + gone = static_cast(seconds * 1000 + + (time_now.tv_usec - start_time.tv_usec) / 1000); if (gone >= total_timeout) return false; } FD_ZERO(&readfds); @@ -125,12 +126,12 @@ static bool TimeIsOut(const struct timeval& start_time, const int& total_time) { struct timeval time_now; gettimeofday(&time_now, NULL); // Careful about overflow. - int seconds = time_now.tv_sec - start_time.tv_sec; + int seconds = static_cast(time_now.tv_sec - start_time.tv_sec); if (seconds > 100) { if (seconds * 1000 > total_time) return true; return false; } - int useconds = time_now.tv_usec - start_time.tv_usec; + int useconds = static_cast(time_now.tv_usec - start_time.tv_usec); if (seconds * 1000000 + useconds > total_time * 1000) { return true; } @@ -264,7 +265,7 @@ static void ExecSubprocess(int* exec_error_fds, // Only get here if the exec failed. Write errno to the parent to tell // them it went wrong. If it went well the pipe is closed. int err = errno; - int bytes_written; + ssize_t bytes_written; do { bytes_written = write(exec_error_fds[kWriteFD], &err, sizeof(err)); } while (bytes_written == -1 && errno == EINTR); @@ -275,7 +276,7 @@ static void ExecSubprocess(int* exec_error_fds, // Runs in the parent process. Checks that the child was able to exec (closing // the file desriptor), or reports an error if it failed. static bool ChildLaunchedOK(Isolate* isolate, int* exec_error_fds) { - int bytes_read; + ssize_t bytes_read; int err; do { bytes_read = read(exec_error_fds[kReadFD], &err, sizeof(err)); @@ -308,9 +309,8 @@ static Handle GetStdout(Isolate* isolate, int bytes_read; do { - bytes_read = read(child_fd, - buffer + fullness, - kStdoutReadBufferSize - fullness); + bytes_read = static_cast( + read(child_fd, buffer + fullness, kStdoutReadBufferSize - fullness)); if (bytes_read == -1) { if (errno == EAGAIN) { if (!WaitOnFD(child_fd, diff --git a/src/d8.cc b/src/d8.cc index cf448172f7..8c6d6bacac 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -1113,17 +1113,21 @@ static char* ReadChars(Isolate* isolate, const char* name, int* size_out) { if (file == NULL) return NULL; fseek(file, 0, SEEK_END); - int size = ftell(file); + size_t size = ftell(file); rewind(file); char* chars = new char[size + 1]; chars[size] = '\0'; - for (int i = 0; i < size;) { - int read = static_cast(fread(&chars[i], 1, size - i, file)); - i += read; + for (size_t i = 0; i < size;) { + i += fread(&chars[i], 1, size - i, file); + if (ferror(file)) { + fclose(file); + delete[] chars; + return nullptr; + } } fclose(file); - *size_out = size; + *size_out = static_cast(size); return chars; } diff --git a/src/flags.cc b/src/flags.cc index 7386238090..88f06a301c 100644 --- a/src/flags.cc +++ b/src/flags.cc @@ -394,7 +394,7 @@ int FlagList::SetFlagsFromCommandLine(int* argc, *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !is_bool); break; case Flag::TYPE_INT: - *flag->int_variable() = strtol(value, &endp, 10); // NOLINT + *flag->int_variable() = static_cast(strtol(value, &endp, 10)); break; case Flag::TYPE_FLOAT: *flag->float_variable() = strtod(value, &endp); diff --git a/src/gdb-jit.cc b/src/gdb-jit.cc index 0664c6091a..2de4e66b49 100644 --- a/src/gdb-jit.cc +++ b/src/gdb-jit.cc @@ -179,7 +179,7 @@ class DebugSectionBase : public ZoneObject { uintptr_t start = writer->position(); if (WriteBodyInternal(writer)) { uintptr_t end = writer->position(); - header->offset = start; + header->offset = static_cast(start); #if defined(__MACH_O) header->addr = 0; #endif @@ -465,7 +465,7 @@ class ELFStringTable : public ELFSection { void ELFSection::PopulateHeader(Writer::Slot header, ELFStringTable* strtab) { - header->name = strtab->Add(name_); + header->name = static_cast(strtab->Add(name_)); header->type = type_; header->alignment = align_; PopulateHeader(header); @@ -813,7 +813,7 @@ class ELFSymbol BASE_EMBEDDED { void Write(Writer::Slot s, ELFStringTable* t) { // Convert symbol names from strings to indexes in the string table. - s->name = t->Add(name); + s->name = static_cast(t->Add(name)); s->value = value; s->size = size; s->info = info; @@ -1638,7 +1638,7 @@ void UnwindInfoSection::WriteLength(Writer* w, } DCHECK((w->position() - initial_position) % kPointerSize == 0); - length_slot->set(w->position() - initial_position); + length_slot->set(static_cast(w->position() - initial_position)); } @@ -1653,7 +1653,7 @@ UnwindInfoSection::UnwindInfoSection(CodeDescription* desc) int UnwindInfoSection::WriteCIE(Writer* w) { Writer::Slot cie_length_slot = w->CreateSlotHere(); - uint32_t cie_position = w->position(); + uint32_t cie_position = static_cast(w->position()); // Write out the CIE header. Currently no 'common instructions' are // emitted onto the CIE; every FDE has its own set of instructions. @@ -1674,7 +1674,7 @@ int UnwindInfoSection::WriteCIE(Writer* w) { void UnwindInfoSection::WriteFDE(Writer* w, int cie_position) { // The only FDE for this function. The CFA is the current RBP. Writer::Slot fde_length_slot = w->CreateSlotHere(); - int fde_position = w->position(); + int fde_position = static_cast(w->position()); w->Write(fde_position - cie_position + 4); w->Write(desc_->CodeStart()); @@ -2067,7 +2067,8 @@ static void AddJITCodeEntry(CodeMap* map, const AddressRange& range, SNPrintF(Vector(file_name, kMaxFileNameSize), "/tmp/elfdump%s%d.o", (name_hint != NULL) ? name_hint : "", file_num++); - WriteBytes(file_name, entry->symfile_addr_, entry->symfile_size_); + WriteBytes(file_name, entry->symfile_addr_, + static_cast(entry->symfile_size_)); } #endif diff --git a/src/snapshot/mksnapshot.cc b/src/snapshot/mksnapshot.cc index 79c1643fb8..f44eca523a 100644 --- a/src/snapshot/mksnapshot.cc +++ b/src/snapshot/mksnapshot.cc @@ -118,13 +118,13 @@ char* GetExtraCode(char* filename) { exit(1); } fseek(file, 0, SEEK_END); - int size = ftell(file); + size_t size = ftell(file); rewind(file); char* chars = new char[size + 1]; chars[size] = '\0'; - for (int i = 0; i < size;) { - int read = static_cast(fread(&chars[i], 1, size - i, file)); - if (read < 0) { + for (size_t i = 0; i < size;) { + size_t read = fread(&chars[i], 1, size - i, file); + if (ferror(file)) { fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno); exit(1); } diff --git a/src/utils.cc b/src/utils.cc index c6fe55b5c6..b33e4c2de3 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -203,7 +203,7 @@ char* ReadCharsFromFile(FILE* file, } // Get the size of the file and rewind it. - *size = ftell(file); + *size = static_cast(ftell(file)); rewind(file); char* result = NewArray(*size + extra_space); diff --git a/test/cctest/compiler/call-tester.h b/test/cctest/compiler/call-tester.h index 30bbe1e8aa..6d8c761452 100644 --- a/test/cctest/compiler/call-tester.h +++ b/test/cctest/compiler/call-tester.h @@ -128,6 +128,9 @@ struct ParameterTraits { static uintptr_t Cast(void* r) { return reinterpret_cast(r); } }; + +#if !V8_TARGET_ARCH_32_BIT + // Additional template specialization required for mips64 to sign-extend // parameters defined by calling convention. template <> @@ -142,6 +145,9 @@ struct ParameterTraits { } }; +#endif // !V8_TARGET_ARCH_64_BIT + + class CallHelper { public: explicit CallHelper(Isolate* isolate, MachineSignature* machine_sig) diff --git a/test/cctest/test-mark-compact.cc b/test/cctest/test-mark-compact.cc index 5b7fe56133..ec70a4ca7b 100644 --- a/test/cctest/test-mark-compact.cc +++ b/test/cctest/test-mark-compact.cc @@ -423,7 +423,7 @@ static intptr_t MemoryInUse() { const int kBufSize = 10000; char buffer[kBufSize]; - int length = read(fd, buffer, kBufSize); + ssize_t length = read(fd, buffer, kBufSize); intptr_t line_start = 0; CHECK_LT(length, kBufSize); // Make the buffer bigger. CHECK_GT(length, 0); // We have to find some data in the file. diff --git a/tools/shell-utils.h b/tools/shell-utils.h index 7b51d2f5b0..31bd8ea8f6 100644 --- a/tools/shell-utils.h +++ b/tools/shell-utils.h @@ -27,6 +27,8 @@ // Utility functions used by parser-shell. +#include "src/globals.h" + #include namespace v8 { @@ -44,7 +46,7 @@ const byte* ReadFileAndRepeat(const char* name, int* size, int repeat) { if (file == NULL) return NULL; fseek(file, 0, SEEK_END); - int file_size = ftell(file); + int file_size = static_cast(ftell(file)); rewind(file); *size = file_size * repeat;