Skip to content

Commit 818c2fb

Browse files
bnoordhuisMylesBorins
authored andcommitted
deps: cherry-pick 46c4979e86 from upstream v8
Original commit message: Use wider types for max_old_space_size and co. Make --max_old_space_size and friends work with values >= 2**31. Such values did not work reliably (or sometimes not all) due to signed integer overflow in size computations, which is UB. Fixes #18786. Bug: chromium:814138 Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng Change-Id: Ibe23cef2417fd5b4a727022b8b0d4b50f1417182 Reviewed-on: https://chromium-review.googlesource.com/927063 Commit-Queue: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Cr-Commit-Position: refs/heads/master@{#51433} PR-URL: #18920 Fixes: #18786 Reviewed-By: Yang Guo <yangguo@chromium.org> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 8a8c792 commit 818c2fb

File tree

6 files changed

+82
-42
lines changed

6 files changed

+82
-42
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# Reset this number to 0 on major V8 upgrades.
2929
# Increment by one for each non-official patch applied to deps/v8.
30-
'v8_embedder_string': '-node.20',
30+
'v8_embedder_string': '-node.21',
3131

3232
# Enable disassembler for `--print-code` v8 options
3333
'v8_enable_disassembler': 1,

deps/v8/src/api.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,7 @@ void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
889889
uint64_t virtual_memory_limit) {
890890
set_max_semi_space_size_in_kb(
891891
i::Heap::ComputeMaxSemiSpaceSize(physical_memory));
892-
set_max_old_space_size(
893-
static_cast<int>(i::Heap::ComputeMaxOldGenerationSize(physical_memory)));
892+
set_max_old_space_size(i::Heap::ComputeMaxOldGenerationSize(physical_memory));
894893
set_max_zone_pool_size(i::AccountingAllocator::kMaxPoolSize);
895894

896895
if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
@@ -905,7 +904,9 @@ void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
905904
void SetResourceConstraints(i::Isolate* isolate,
906905
const ResourceConstraints& constraints) {
907906
size_t semi_space_size = constraints.max_semi_space_size_in_kb();
908-
int old_space_size = constraints.max_old_space_size();
907+
size_t old_space_size =
908+
static_cast<size_t>(
909+
static_cast<unsigned int>(constraints.max_old_space_size()));
909910
size_t code_range_size = constraints.code_range_size();
910911
size_t max_pool_size = constraints.max_zone_pool_size();
911912
if (semi_space_size != 0 || old_space_size != 0 || code_range_size != 0) {

deps/v8/src/flag-definitions.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ struct MaybeBoolFlag {
161161
#define DEFINE_INT(nam, def, cmt) FLAG(INT, int, nam, def, cmt)
162162
#define DEFINE_UINT(nam, def, cmt) FLAG(UINT, unsigned int, nam, def, cmt)
163163
#define DEFINE_FLOAT(nam, def, cmt) FLAG(FLOAT, double, nam, def, cmt)
164+
#define DEFINE_SIZE_T(nam, def, cmt) FLAG(SIZE_T, size_t, nam, def, cmt)
164165
#define DEFINE_STRING(nam, def, cmt) FLAG(STRING, const char*, nam, def, cmt)
165166
#define DEFINE_ARGS(nam, cmt) FLAG(ARGS, JSArguments, nam, {0 COMMA NULL}, cmt)
166167

167168
#define DEFINE_ALIAS_BOOL(alias, nam) FLAG_ALIAS(BOOL, bool, alias, nam)
168169
#define DEFINE_ALIAS_INT(alias, nam) FLAG_ALIAS(INT, int, alias, nam)
169170
#define DEFINE_ALIAS_FLOAT(alias, nam) FLAG_ALIAS(FLOAT, double, alias, nam)
171+
#define DEFINE_ALIAS_SIZE_T(alias, nam) FLAG_ALIAS(SIZE_T, size_t, alias, nam)
170172
#define DEFINE_ALIAS_STRING(alias, nam) \
171173
FLAG_ALIAS(STRING, const char*, alias, nam)
172174
#define DEFINE_ALIAS_ARGS(alias, nam) FLAG_ALIAS(ARGS, JSArguments, alias, nam)
@@ -553,18 +555,18 @@ DEFINE_BOOL(trace_opt_verbose, false, "extra verbose compilation tracing")
553555
DEFINE_IMPLICATION(trace_opt_verbose, trace_opt)
554556

555557
// Garbage collections flags.
556-
DEFINE_INT(min_semi_space_size, 0,
557-
"min size of a semi-space (in MBytes), the new space consists of two"
558-
"semi-spaces")
559-
DEFINE_INT(max_semi_space_size, 0,
560-
"max size of a semi-space (in MBytes), the new space consists of two"
561-
"semi-spaces")
558+
DEFINE_SIZE_T(min_semi_space_size, 0,
559+
"min size of a semi-space (in MBytes), the new space consists of "
560+
"two semi-spaces")
561+
DEFINE_SIZE_T(max_semi_space_size, 0,
562+
"max size of a semi-space (in MBytes), the new space consists of "
563+
"two semi-spaces")
562564
DEFINE_INT(semi_space_growth_factor, 2, "factor by which to grow the new space")
563565
DEFINE_BOOL(experimental_new_space_growth_heuristic, false,
564566
"Grow the new space based on the percentage of survivors instead "
565567
"of their absolute value.")
566-
DEFINE_INT(max_old_space_size, 0, "max size of the old space (in Mbytes)")
567-
DEFINE_INT(initial_old_space_size, 0, "initial old space size (in Mbytes)")
568+
DEFINE_SIZE_T(max_old_space_size, 0, "max size of the old space (in Mbytes)")
569+
DEFINE_SIZE_T(initial_old_space_size, 0, "initial old space size (in Mbytes)")
568570
DEFINE_BOOL(gc_global, false, "always perform global GCs")
569571
DEFINE_INT(gc_interval, -1, "garbage collect after <n> allocations")
570572
DEFINE_INT(retain_maps_for_n_gc, 2,

deps/v8/src/flags.cc

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "src/flags.h"
66

77
#include <cctype>
8+
#include <cerrno>
89
#include <cstdlib>
910
#include <sstream>
1011

@@ -40,6 +41,7 @@ struct Flag {
4041
TYPE_INT,
4142
TYPE_UINT,
4243
TYPE_FLOAT,
44+
TYPE_SIZE_T,
4345
TYPE_STRING,
4446
TYPE_ARGS
4547
};
@@ -82,6 +84,11 @@ struct Flag {
8284
return reinterpret_cast<double*>(valptr_);
8385
}
8486

87+
size_t* size_t_variable() const {
88+
DCHECK(type_ == TYPE_SIZE_T);
89+
return reinterpret_cast<size_t*>(valptr_);
90+
}
91+
8592
const char* string_value() const {
8693
DCHECK(type_ == TYPE_STRING);
8794
return *reinterpret_cast<const char**>(valptr_);
@@ -120,6 +127,11 @@ struct Flag {
120127
return *reinterpret_cast<const double*>(defptr_);
121128
}
122129

130+
size_t size_t_default() const {
131+
DCHECK(type_ == TYPE_SIZE_T);
132+
return *reinterpret_cast<const size_t*>(defptr_);
133+
}
134+
123135
const char* string_default() const {
124136
DCHECK(type_ == TYPE_STRING);
125137
return *reinterpret_cast<const char* const *>(defptr_);
@@ -143,6 +155,8 @@ struct Flag {
143155
return *uint_variable() == uint_default();
144156
case TYPE_FLOAT:
145157
return *float_variable() == float_default();
158+
case TYPE_SIZE_T:
159+
return *size_t_variable() == size_t_default();
146160
case TYPE_STRING: {
147161
const char* str1 = string_value();
148162
const char* str2 = string_default();
@@ -174,6 +188,9 @@ struct Flag {
174188
case TYPE_FLOAT:
175189
*float_variable() = float_default();
176190
break;
191+
case TYPE_SIZE_T:
192+
*size_t_variable() = size_t_default();
193+
break;
177194
case TYPE_STRING:
178195
set_string_value(string_default(), false);
179196
break;
@@ -202,6 +219,8 @@ static const char* Type2String(Flag::FlagType type) {
202219
case Flag::TYPE_UINT:
203220
return "uint";
204221
case Flag::TYPE_FLOAT: return "float";
222+
case Flag::TYPE_SIZE_T:
223+
return "size_t";
205224
case Flag::TYPE_STRING: return "string";
206225
case Flag::TYPE_ARGS: return "arguments";
207226
}
@@ -228,6 +247,9 @@ std::ostream& operator<<(std::ostream& os, const Flag& flag) { // NOLINT
228247
case Flag::TYPE_FLOAT:
229248
os << *flag.float_variable();
230249
break;
250+
case Flag::TYPE_SIZE_T:
251+
os << *flag.size_t_variable();
252+
break;
231253
case Flag::TYPE_STRING: {
232254
const char* str = flag.string_value();
233255
os << (str ? str : "NULL");
@@ -360,6 +382,27 @@ static Flag* FindFlag(const char* name) {
360382
return NULL;
361383
}
362384

385+
template <typename T>
386+
bool TryParseUnsigned(Flag* flag, const char* arg, const char* value,
387+
char** endp, T* out_val) {
388+
// We do not use strtoul because it accepts negative numbers.
389+
// Rejects values >= 2**63 when T is 64 bits wide but that
390+
// seems like an acceptable trade-off.
391+
uint64_t max = static_cast<uint64_t>(std::numeric_limits<T>::max());
392+
errno = 0;
393+
int64_t val = static_cast<int64_t>(strtoll(value, endp, 10));
394+
if (val < 0 || static_cast<uint64_t>(val) > max || errno != 0) {
395+
PrintF(stderr,
396+
"Error: Value for flag %s of type %s is out of bounds "
397+
"[0-%" PRIu64
398+
"]\n"
399+
"Try --help for options\n",
400+
arg, Type2String(flag->type()), max);
401+
return false;
402+
}
403+
*out_val = static_cast<T>(val);
404+
return true;
405+
}
363406

364407
// static
365408
int FlagList::SetFlagsFromCommandLine(int* argc,
@@ -425,27 +468,21 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
425468
case Flag::TYPE_INT:
426469
*flag->int_variable() = static_cast<int>(strtol(value, &endp, 10));
427470
break;
428-
case Flag::TYPE_UINT: {
429-
// We do not use strtoul because it accepts negative numbers.
430-
int64_t val = static_cast<int64_t>(strtoll(value, &endp, 10));
431-
if (val < 0 || val > std::numeric_limits<unsigned int>::max()) {
432-
PrintF(stderr,
433-
"Error: Value for flag %s of type %s is out of bounds "
434-
"[0-%" PRIu64
435-
"]\n"
436-
"Try --help for options\n",
437-
arg, Type2String(flag->type()),
438-
static_cast<uint64_t>(
439-
std::numeric_limits<unsigned int>::max()));
471+
case Flag::TYPE_UINT:
472+
if (!TryParseUnsigned(flag, arg, value, &endp,
473+
flag->uint_variable())) {
440474
return_code = j;
441-
break;
442475
}
443-
*flag->uint_variable() = static_cast<unsigned int>(val);
444476
break;
445-
}
446477
case Flag::TYPE_FLOAT:
447478
*flag->float_variable() = strtod(value, &endp);
448479
break;
480+
case Flag::TYPE_SIZE_T:
481+
if (!TryParseUnsigned(flag, arg, value, &endp,
482+
flag->size_t_variable())) {
483+
return_code = j;
484+
}
485+
break;
449486
case Flag::TYPE_STRING:
450487
flag->set_string_value(value ? StrDup(value) : NULL, true);
451488
break;

deps/v8/src/heap/heap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5463,8 +5463,8 @@ bool Heap::ConfigureHeap(size_t max_semi_space_size_in_kb,
54635463

54645464
// The new space size must be a power of two to support single-bit testing
54655465
// for containment.
5466-
max_semi_space_size_ = base::bits::RoundUpToPowerOfTwo32(
5467-
static_cast<uint32_t>(max_semi_space_size_));
5466+
max_semi_space_size_ = static_cast<size_t>(base::bits::RoundUpToPowerOfTwo64(
5467+
static_cast<uint64_t>(max_semi_space_size_)));
54685468

54695469
if (max_semi_space_size_ == kMaxSemiSpaceSizeInKB * KB) {
54705470
// Start with at least 1*MB semi-space on machines with a lot of memory.

deps/v8/src/heap/heap.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,15 @@ class Heap {
584584
#endif
585585

586586
// Semi-space size needs to be a multiple of page size.
587-
static const int kMinSemiSpaceSizeInKB =
587+
static const size_t kMinSemiSpaceSizeInKB =
588588
1 * kPointerMultiplier * ((1 << kPageSizeBits) / KB);
589-
static const int kMaxSemiSpaceSizeInKB =
589+
static const size_t kMaxSemiSpaceSizeInKB =
590590
16 * kPointerMultiplier * ((1 << kPageSizeBits) / KB);
591591

592592
// The old space size has to be a multiple of Page::kPageSize.
593593
// Sizes are in MB.
594-
static const int kMinOldGenerationSize = 128 * kPointerMultiplier;
595-
static const int kMaxOldGenerationSize = 1024 * kPointerMultiplier;
594+
static const size_t kMinOldGenerationSize = 128 * kPointerMultiplier;
595+
static const size_t kMaxOldGenerationSize = 1024 * kPointerMultiplier;
596596

597597
static const int kTraceRingBufferSize = 512;
598598
static const int kStacktraceBufferSize = 512;
@@ -1293,10 +1293,10 @@ class Heap {
12931293
size_t MaxOldGenerationSize() { return max_old_generation_size_; }
12941294

12951295
static size_t ComputeMaxOldGenerationSize(uint64_t physical_memory) {
1296-
const int old_space_physical_memory_factor = 4;
1297-
int computed_size =
1298-
static_cast<int>(physical_memory / i::MB /
1299-
old_space_physical_memory_factor * kPointerMultiplier);
1296+
const size_t old_space_physical_memory_factor = 4;
1297+
size_t computed_size = static_cast<size_t>(
1298+
physical_memory / i::MB / old_space_physical_memory_factor *
1299+
kPointerMultiplier);
13001300
return Max(Min(computed_size, kMaxOldGenerationSize),
13011301
kMinOldGenerationSize);
13021302
}
@@ -1308,11 +1308,11 @@ class Heap {
13081308
uint64_t capped_physical_memory =
13091309
Max(Min(physical_memory, max_physical_memory), min_physical_memory);
13101310
// linearly scale max semi-space size: (X-A)/(B-A)*(D-C)+C
1311-
int semi_space_size_in_kb =
1312-
static_cast<int>(((capped_physical_memory - min_physical_memory) *
1313-
(kMaxSemiSpaceSizeInKB - kMinSemiSpaceSizeInKB)) /
1314-
(max_physical_memory - min_physical_memory) +
1315-
kMinSemiSpaceSizeInKB);
1311+
size_t semi_space_size_in_kb =
1312+
static_cast<size_t>(((capped_physical_memory - min_physical_memory) *
1313+
(kMaxSemiSpaceSizeInKB - kMinSemiSpaceSizeInKB)) /
1314+
(max_physical_memory - min_physical_memory) +
1315+
kMinSemiSpaceSizeInKB);
13161316
return RoundUp(semi_space_size_in_kb, (1 << kPageSizeBits) / KB);
13171317
}
13181318

0 commit comments

Comments
 (0)