Skip to content

Commit 13cb056

Browse files
bnoordhuisBridgeAR
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 2623154 commit 13cb056

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.3',
30+
'v8_embedder_string': '-node.4',
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
@@ -911,8 +911,7 @@ void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
911911
uint64_t virtual_memory_limit) {
912912
set_max_semi_space_size_in_kb(
913913
i::Heap::ComputeMaxSemiSpaceSize(physical_memory));
914-
set_max_old_space_size(
915-
static_cast<int>(i::Heap::ComputeMaxOldGenerationSize(physical_memory)));
914+
set_max_old_space_size(i::Heap::ComputeMaxOldGenerationSize(physical_memory));
916915
set_max_zone_pool_size(i::AccountingAllocator::kMaxPoolSize);
917916

918917
if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
@@ -927,7 +926,9 @@ void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
927926
void SetResourceConstraints(i::Isolate* isolate,
928927
const ResourceConstraints& constraints) {
929928
size_t semi_space_size = constraints.max_semi_space_size_in_kb();
930-
int old_space_size = constraints.max_old_space_size();
929+
size_t old_space_size =
930+
static_cast<size_t>(
931+
static_cast<unsigned int>(constraints.max_old_space_size()));
931932
size_t code_range_size = constraints.code_range_size();
932933
size_t max_pool_size = constraints.max_zone_pool_size();
933934
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,13 +161,15 @@ 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) \
166167
FLAG(ARGS, JSArguments, nam, {0 COMMA nullptr}, cmt)
167168

168169
#define DEFINE_ALIAS_BOOL(alias, nam) FLAG_ALIAS(BOOL, bool, alias, nam)
169170
#define DEFINE_ALIAS_INT(alias, nam) FLAG_ALIAS(INT, int, alias, nam)
170171
#define DEFINE_ALIAS_FLOAT(alias, nam) FLAG_ALIAS(FLOAT, double, alias, nam)
172+
#define DEFINE_ALIAS_SIZE_T(alias, nam) FLAG_ALIAS(SIZE_T, size_t, alias, nam)
171173
#define DEFINE_ALIAS_STRING(alias, nam) \
172174
FLAG_ALIAS(STRING, const char*, alias, nam)
173175
#define DEFINE_ALIAS_ARGS(alias, nam) FLAG_ALIAS(ARGS, JSArguments, alias, nam)
@@ -580,18 +582,18 @@ DEFINE_INT(generic_ic_threshold, 30,
580582
DEFINE_INT(self_opt_count, 130, "call count before self-optimization")
581583

582584
// Garbage collections flags.
583-
DEFINE_INT(min_semi_space_size, 0,
584-
"min size of a semi-space (in MBytes), the new space consists of two"
585-
"semi-spaces")
586-
DEFINE_INT(max_semi_space_size, 0,
587-
"max size of a semi-space (in MBytes), the new space consists of two"
588-
"semi-spaces")
585+
DEFINE_SIZE_T(min_semi_space_size, 0,
586+
"min size of a semi-space (in MBytes), the new space consists of "
587+
"two semi-spaces")
588+
DEFINE_SIZE_T(max_semi_space_size, 0,
589+
"max size of a semi-space (in MBytes), the new space consists of "
590+
"two semi-spaces")
589591
DEFINE_INT(semi_space_growth_factor, 2, "factor by which to grow the new space")
590592
DEFINE_BOOL(experimental_new_space_growth_heuristic, false,
591593
"Grow the new space based on the percentage of survivors instead "
592594
"of their absolute value.")
593-
DEFINE_INT(max_old_space_size, 0, "max size of the old space (in Mbytes)")
594-
DEFINE_INT(initial_old_space_size, 0, "initial old space size (in Mbytes)")
595+
DEFINE_SIZE_T(max_old_space_size, 0, "max size of the old space (in Mbytes)")
596+
DEFINE_SIZE_T(initial_old_space_size, 0, "initial old space size (in Mbytes)")
595597
DEFINE_BOOL(gc_global, false, "always perform global GCs")
596598
DEFINE_INT(gc_interval, -1, "garbage collect after <n> allocations")
597599
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

@@ -39,6 +40,7 @@ struct Flag {
3940
TYPE_INT,
4041
TYPE_UINT,
4142
TYPE_FLOAT,
43+
TYPE_SIZE_T,
4244
TYPE_STRING,
4345
TYPE_ARGS
4446
};
@@ -81,6 +83,11 @@ struct Flag {
8183
return reinterpret_cast<double*>(valptr_);
8284
}
8385

86+
size_t* size_t_variable() const {
87+
DCHECK(type_ == TYPE_SIZE_T);
88+
return reinterpret_cast<size_t*>(valptr_);
89+
}
90+
8491
const char* string_value() const {
8592
DCHECK(type_ == TYPE_STRING);
8693
return *reinterpret_cast<const char**>(valptr_);
@@ -119,6 +126,11 @@ struct Flag {
119126
return *reinterpret_cast<const double*>(defptr_);
120127
}
121128

129+
size_t size_t_default() const {
130+
DCHECK(type_ == TYPE_SIZE_T);
131+
return *reinterpret_cast<const size_t*>(defptr_);
132+
}
133+
122134
const char* string_default() const {
123135
DCHECK(type_ == TYPE_STRING);
124136
return *reinterpret_cast<const char* const *>(defptr_);
@@ -142,6 +154,8 @@ struct Flag {
142154
return *uint_variable() == uint_default();
143155
case TYPE_FLOAT:
144156
return *float_variable() == float_default();
157+
case TYPE_SIZE_T:
158+
return *size_t_variable() == size_t_default();
145159
case TYPE_STRING: {
146160
const char* str1 = string_value();
147161
const char* str2 = string_default();
@@ -173,6 +187,9 @@ struct Flag {
173187
case TYPE_FLOAT:
174188
*float_variable() = float_default();
175189
break;
190+
case TYPE_SIZE_T:
191+
*size_t_variable() = size_t_default();
192+
break;
176193
case TYPE_STRING:
177194
set_string_value(string_default(), false);
178195
break;
@@ -201,6 +218,8 @@ static const char* Type2String(Flag::FlagType type) {
201218
case Flag::TYPE_UINT:
202219
return "uint";
203220
case Flag::TYPE_FLOAT: return "float";
221+
case Flag::TYPE_SIZE_T:
222+
return "size_t";
204223
case Flag::TYPE_STRING: return "string";
205224
case Flag::TYPE_ARGS: return "arguments";
206225
}
@@ -227,6 +246,9 @@ std::ostream& operator<<(std::ostream& os, const Flag& flag) { // NOLINT
227246
case Flag::TYPE_FLOAT:
228247
os << *flag.float_variable();
229248
break;
249+
case Flag::TYPE_SIZE_T:
250+
os << *flag.size_t_variable();
251+
break;
230252
case Flag::TYPE_STRING: {
231253
const char* str = flag.string_value();
232254
os << (str ? str : "nullptr");
@@ -358,6 +380,27 @@ static Flag* FindFlag(const char* name) {
358380
return nullptr;
359381
}
360382

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

362405
// static
363406
int FlagList::SetFlagsFromCommandLine(int* argc,
@@ -422,27 +465,21 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
422465
case Flag::TYPE_INT:
423466
*flag->int_variable() = static_cast<int>(strtol(value, &endp, 10));
424467
break;
425-
case Flag::TYPE_UINT: {
426-
// We do not use strtoul because it accepts negative numbers.
427-
int64_t val = static_cast<int64_t>(strtoll(value, &endp, 10));
428-
if (val < 0 || val > std::numeric_limits<unsigned int>::max()) {
429-
PrintF(stderr,
430-
"Error: Value for flag %s of type %s is out of bounds "
431-
"[0-%" PRIu64
432-
"]\n"
433-
"Try --help for options\n",
434-
arg, Type2String(flag->type()),
435-
static_cast<uint64_t>(
436-
std::numeric_limits<unsigned int>::max()));
468+
case Flag::TYPE_UINT:
469+
if (!TryParseUnsigned(flag, arg, value, &endp,
470+
flag->uint_variable())) {
437471
return_code = j;
438-
break;
439472
}
440-
*flag->uint_variable() = static_cast<unsigned int>(val);
441473
break;
442-
}
443474
case Flag::TYPE_FLOAT:
444475
*flag->float_variable() = strtod(value, &endp);
445476
break;
477+
case Flag::TYPE_SIZE_T:
478+
if (!TryParseUnsigned(flag, arg, value, &endp,
479+
flag->size_t_variable())) {
480+
return_code = j;
481+
}
482+
break;
446483
case Flag::TYPE_STRING:
447484
flag->set_string_value(value ? StrDup(value) : nullptr, true);
448485
break;

deps/v8/src/heap/heap.cc

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

50965096
// The new space size must be a power of two to support single-bit testing
50975097
// for containment.
5098-
max_semi_space_size_ = base::bits::RoundUpToPowerOfTwo32(
5099-
static_cast<uint32_t>(max_semi_space_size_));
5098+
max_semi_space_size_ = static_cast<size_t>(base::bits::RoundUpToPowerOfTwo64(
5099+
static_cast<uint64_t>(max_semi_space_size_)));
51005100

51015101
if (max_semi_space_size_ == kMaxSemiSpaceSizeInKB * KB) {
51025102
// 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
@@ -626,15 +626,15 @@ class Heap {
626626
#endif
627627

628628
// Semi-space size needs to be a multiple of page size.
629-
static const int kMinSemiSpaceSizeInKB =
629+
static const size_t kMinSemiSpaceSizeInKB =
630630
1 * kPointerMultiplier * ((1 << kPageSizeBits) / KB);
631-
static const int kMaxSemiSpaceSizeInKB =
631+
static const size_t kMaxSemiSpaceSizeInKB =
632632
16 * kPointerMultiplier * ((1 << kPageSizeBits) / KB);
633633

634634
// The old space size has to be a multiple of Page::kPageSize.
635635
// Sizes are in MB.
636-
static const int kMinOldGenerationSize = 128 * kPointerMultiplier;
637-
static const int kMaxOldGenerationSize = 1024 * kPointerMultiplier;
636+
static const size_t kMinOldGenerationSize = 128 * kPointerMultiplier;
637+
static const size_t kMaxOldGenerationSize = 1024 * kPointerMultiplier;
638638

639639
static const int kTraceRingBufferSize = 512;
640640
static const int kStacktraceBufferSize = 512;
@@ -1372,10 +1372,10 @@ class Heap {
13721372
size_t MaxOldGenerationSize() { return max_old_generation_size_; }
13731373

13741374
static size_t ComputeMaxOldGenerationSize(uint64_t physical_memory) {
1375-
const int old_space_physical_memory_factor = 4;
1376-
int computed_size =
1377-
static_cast<int>(physical_memory / i::MB /
1378-
old_space_physical_memory_factor * kPointerMultiplier);
1375+
const size_t old_space_physical_memory_factor = 4;
1376+
size_t computed_size = static_cast<size_t>(
1377+
physical_memory / i::MB / old_space_physical_memory_factor *
1378+
kPointerMultiplier);
13791379
return Max(Min(computed_size, kMaxOldGenerationSize),
13801380
kMinOldGenerationSize);
13811381
}
@@ -1387,11 +1387,11 @@ class Heap {
13871387
uint64_t capped_physical_memory =
13881388
Max(Min(physical_memory, max_physical_memory), min_physical_memory);
13891389
// linearly scale max semi-space size: (X-A)/(B-A)*(D-C)+C
1390-
int semi_space_size_in_kb =
1391-
static_cast<int>(((capped_physical_memory - min_physical_memory) *
1392-
(kMaxSemiSpaceSizeInKB - kMinSemiSpaceSizeInKB)) /
1393-
(max_physical_memory - min_physical_memory) +
1394-
kMinSemiSpaceSizeInKB);
1390+
size_t semi_space_size_in_kb =
1391+
static_cast<size_t>(((capped_physical_memory - min_physical_memory) *
1392+
(kMaxSemiSpaceSizeInKB - kMinSemiSpaceSizeInKB)) /
1393+
(max_physical_memory - min_physical_memory) +
1394+
kMinSemiSpaceSizeInKB);
13951395
return RoundUp(semi_space_size_in_kb, (1 << kPageSizeBits) / KB);
13961396
}
13971397

0 commit comments

Comments
 (0)