Skip to content

Commit 515cc07

Browse files
schuayCommit Bot
authored andcommitted
[csa] Ensure the requested allocation size fits in a Smi
In CSA::AllocateRaw, ensure that the given allocation size fits into a Smi. Bug: chromium:848672 Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng Change-Id: I4e74791296163188b1ca77cae8226a9833fba8ef Reviewed-on: https://chromium-review.googlesource.com/1084930 Reviewed-by: Yang Guo <[email protected]> Reviewed-by: Igor Sheludko <[email protected]> Commit-Queue: Jakob Gruber <[email protected]> Cr-Commit-Position: refs/heads/master@{#53495}
1 parent 3348ed0 commit 515cc07

4 files changed

Lines changed: 72 additions & 1 deletion

File tree

src/code-stub-assembler.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,18 @@ TNode<Smi> CodeStubAssembler::SmiFromInt32(SloppyTNode<Int32T> value) {
534534
WordShl(value_intptr, SmiShiftBitsConstant()));
535535
}
536536

537+
TNode<BoolT> CodeStubAssembler::IsValidPositiveSmi(TNode<IntPtrT> value) {
538+
intptr_t constant_value;
539+
if (ToIntPtrConstant(value, constant_value)) {
540+
return (static_cast<uintptr_t>(constant_value) <=
541+
static_cast<uintptr_t>(Smi::kMaxValue))
542+
? Int32TrueConstant()
543+
: Int32FalseConstant();
544+
}
545+
546+
return UintPtrLessThanOrEqual(value, IntPtrConstant(Smi::kMaxValue));
547+
}
548+
537549
TNode<Smi> CodeStubAssembler::SmiTag(SloppyTNode<IntPtrT> value) {
538550
int32_t constant_value;
539551
if (ToInt32Constant(value, constant_value) && Smi::IsValid(constant_value)) {
@@ -1024,6 +1036,19 @@ void CodeStubAssembler::GotoIfForceSlowPath(Label* if_true) {
10241036

10251037
Node* CodeStubAssembler::AllocateRaw(Node* size_in_bytes, AllocationFlags flags,
10261038
Node* top_address, Node* limit_address) {
1039+
// TODO(jgruber, chromium:848672): TNodeify AllocateRaw.
1040+
// TODO(jgruber, chromium:848672): Call FatalProcessOutOfMemory if this fails.
1041+
{
1042+
intptr_t constant_value;
1043+
if (ToIntPtrConstant(size_in_bytes, constant_value)) {
1044+
CHECK(Internals::IsValidSmi(constant_value));
1045+
CHECK_GT(constant_value, 0);
1046+
} else {
1047+
CSA_CHECK(this,
1048+
IsValidPositiveSmi(UncheckedCast<IntPtrT>(size_in_bytes)));
1049+
}
1050+
}
1051+
10271052
Node* top = Load(MachineType::Pointer(), top_address);
10281053
Node* limit = Load(MachineType::Pointer(), limit_address);
10291054

src/code-stub-assembler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ class V8_EXPORT_PRIVATE CodeStubAssembler : public compiler::CodeAssembler {
274274
TNode<Object> index,
275275
TNode<IntPtrT> length);
276276

277+
// Returns true iff the given value fits into smi range and is >= 0.
278+
TNode<BoolT> IsValidPositiveSmi(TNode<IntPtrT> value);
279+
277280
// Tag an IntPtr as a Smi value.
278281
TNode<Smi> SmiTag(SloppyTNode<IntPtrT> value);
279282
// Untag a Smi value as an IntPtr.

test/cctest/test-code-stub-assembler.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,49 @@ TEST(ToUint32) {
209209
ft.CheckThrows(factory->match_symbol());
210210
}
211211

212+
namespace {
213+
void IsValidPositiveSmiCase(Isolate* isolate, intptr_t value, bool expected) {
214+
const int kNumParams = 0;
215+
CodeAssemblerTester asm_tester(isolate, kNumParams);
216+
217+
CodeStubAssembler m(asm_tester.state());
218+
m.Return(
219+
m.SelectBooleanConstant(m.IsValidPositiveSmi(m.IntPtrConstant(value))));
220+
221+
FunctionTester ft(asm_tester.GenerateCode(), kNumParams);
222+
MaybeHandle<Object> maybe_handle = ft.Call();
223+
224+
if (expected) {
225+
CHECK(maybe_handle.ToHandleChecked()->IsTrue(isolate));
226+
} else {
227+
CHECK(maybe_handle.ToHandleChecked()->IsFalse(isolate));
228+
}
229+
}
230+
} // namespace
231+
232+
TEST(IsValidPositiveSmi) {
233+
Isolate* isolate(CcTest::InitIsolateOnce());
234+
235+
IsValidPositiveSmiCase(isolate, -1, false);
236+
IsValidPositiveSmiCase(isolate, 0, true);
237+
IsValidPositiveSmiCase(isolate, 1, true);
238+
239+
#ifdef V8_TARGET_ARCH_32_BIT
240+
IsValidPositiveSmiCase(isolate, 0x3FFFFFFFU, true);
241+
IsValidPositiveSmiCase(isolate, 0xC0000000U, false);
242+
IsValidPositiveSmiCase(isolate, 0x40000000U, false);
243+
IsValidPositiveSmiCase(isolate, 0xBFFFFFFFU, false);
244+
#else
245+
typedef std::numeric_limits<int32_t> int32_limits;
246+
IsValidPositiveSmiCase(isolate, int32_limits::max(), true);
247+
IsValidPositiveSmiCase(isolate, int32_limits::min(), false);
248+
IsValidPositiveSmiCase(isolate,
249+
static_cast<intptr_t>(int32_limits::max()) + 1, false);
250+
IsValidPositiveSmiCase(isolate,
251+
static_cast<intptr_t>(int32_limits::min()) - 1, false);
252+
#endif
253+
}
254+
212255
TEST(FixedArrayAccessSmiIndex) {
213256
Isolate* isolate(CcTest::InitIsolateOnce());
214257
CodeAssemblerTester asm_tester(isolate);

test/mkgrokdump/mkgrokdump.status

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

55
[
66
# Only test for default mode x64.
7-
['variant != default or arch != x64', {
7+
['variant != default or arch != x64 or asan == True', {
88
'*': [SKIP],
99
}], # variant != default or arch != x64
1010
]

0 commit comments

Comments
 (0)