Skip to content

Instantly share code, notes, and snippets.

@atiq-cs
Created November 23, 2016 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atiq-cs/d3cbcaec1d13968fb464884a7f024607 to your computer and use it in GitHub Desktop.
Save atiq-cs/d3cbcaec1d13968fb464884a7f024607 to your computer and use it in GitHub Desktop.
Doing Assert Tests on x64 v8
// linked with http://blog.fftsys.com/tech/AssertNotSmi-test-x64
static void TestAssertNotSmi(MacroAssembler* masm, Label* exit, int id, int value) {
__ movl(rax, Immediate(id));
// __ Move(rcx, (Smi *) value); // be aware of this!
__ movl(rcx, Immediate(value));
__ AssertNotSmi(rcx);
__ j(equal, exit);
}
TEST(AssertNotSmi) {
// Allocate an executable page of memory.
size_t actual_size;
byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
Assembler::kMinimalBufferSize, &actual_size, true));
CHECK(buffer);
Isolate* isolate = CcTest::i_isolate();
HandleScope handles(isolate);
std::vector<int> smi_test_values = {-2147483647, -15, 1, 3, 5, 0x1001,
0x1235, 0x1234569, 0x30000001, 0x7ffffffd, 0x7fffedcd, 0x7FFFFFF1,
0x7FFFFFFF};
MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
v8::internal::CodeObjectRequired::kYes);
MacroAssembler* masm = &assembler;
EntryCode(masm);
Label exit;
for(int i=0; i < smi_test_values.size(); i++)
TestAssertNotSmi(masm, &exit, i+1, smi_test_values[i]);
__ xorq(rax, rax); // Success.
__ bind(&exit);
ExitCode(masm);
__ ret(0);
CodeDesc desc;
masm->GetCode(&desc);
// Call the function from C++.
int result = FUNCTION_CAST<F0>(buffer)();
CHECK_EQ(0, result);
}
TEST(AssertNotSmiFail) {
size_t actual_size;
byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
Assembler::kMinimalBufferSize, &actual_size, true));
CHECK(buffer);
Isolate* isolate = CcTest::i_isolate();
HandleScope handles(isolate);
MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
v8::internal::CodeObjectRequired::kYes);
MacroAssembler* masm = &assembler;
EntryCode(masm);
Label exit;
TestAssertNotSmi(masm, &exit, 1, 2); // pass an Smi
__ xorq(rax, rax); // Success.
__ bind(&exit);
ExitCode(masm);
__ ret(0);
CodeDesc desc;
masm->GetCode(&desc);
int result = FUNCTION_CAST<F0>(buffer)();
CHECK_EQ(0, result);
}
static void TestAssertSmi(MacroAssembler* masm, Label* exit, int id, int value) {
__ movl(rax, Immediate(id));
__ movl(rcx, Immediate(value));
__ AssertSmi(rcx);
__ j(not_equal, exit);
}
static void TestAssertSmi(MacroAssembler* masm, Label* exit, int id, Smi* value) {
__ movl(rax, Immediate(id));
__ Move(rcx, (Smi *) value);
__ AssertSmi(rcx);
__ j(not_equal, exit);
}
TEST(AssertSmi) {
// Allocate an executable page of memory.
size_t actual_size;
byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
Assembler::kMinimalBufferSize, &actual_size, true));
CHECK(buffer);
Isolate* isolate = CcTest::i_isolate();
HandleScope handles(isolate);
std::vector<int> int_values = {-2147483648, -16, 0, 2, 4, 6, 0x1000,
-1, 1, 3, 9, 11, 21, 0x1234567, 0x30000001, 0x7ffffffd, 0x7fffedcd,
0x1234, 0x1234568, 0x30000000, 0x7ffffffc, 0x7fffedcc, 0x7FFFFFF2,
0x7FFFFFFE};
MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
v8::internal::CodeObjectRequired::kYes);
MacroAssembler* masm = &assembler;
EntryCode(masm);
Label exit;
for(int i=0; i < int_values.size(); i++)
TestAssertSmi(masm, &exit, i+1, Smi::FromInt(int_values[i]));
__ xorq(rax, rax); // Success.
__ bind(&exit);
ExitCode(masm);
__ ret(0);
CodeDesc desc;
masm->GetCode(&desc);
// Call the function from C++.
int result = FUNCTION_CAST<F0>(buffer)();
CHECK_EQ(0, result);
}
TEST(AssertSmiFail) {
size_t actual_size;
byte* buffer = static_cast<byte*>(v8::base::OS::Allocate(
Assembler::kMinimalBufferSize, &actual_size, true));
CHECK(buffer);
Isolate* isolate = CcTest::i_isolate();
HandleScope handles(isolate);
MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size),
v8::internal::CodeObjectRequired::kYes);
MacroAssembler* masm = &assembler;
EntryCode(masm);
Label exit;
TestAssertSmi(masm, &exit, 1, 3);
__ xorq(rax, rax); // Success.
__ bind(&exit);
ExitCode(masm);
__ ret(0);
CodeDesc desc;
masm->GetCode(&desc);
int result = FUNCTION_CAST<F0>(buffer)();
CHECK_EQ(0, result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment