Home | History | Annotate | Download | only in cctest
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include <stdlib.h>
     29 
     30 #include "src/v8.h"
     31 #include "src/macro-assembler.h"
     32 #include "src/mips/macro-assembler-mips.h"
     33 #include "src/mips/simulator-mips.h"
     34 #include "test/cctest/cctest.h"
     35 
     36 
     37 using namespace v8::internal;
     38 
     39 typedef void* (*F)(int x, int y, int p2, int p3, int p4);
     40 
     41 #define __ masm->
     42 
     43 
     44 static byte to_non_zero(int n) {
     45   return static_cast<unsigned>(n) % 255 + 1;
     46 }
     47 
     48 
     49 static bool all_zeroes(const byte* beg, const byte* end) {
     50   CHECK(beg);
     51   CHECK(beg <= end);
     52   while (beg < end) {
     53     if (*beg++ != 0)
     54       return false;
     55   }
     56   return true;
     57 }
     58 
     59 
     60 TEST(CopyBytes) {
     61   CcTest::InitializeVM();
     62   Isolate* isolate = Isolate::Current();
     63   HandleScope handles(isolate);
     64 
     65   const int data_size = 1 * KB;
     66   size_t act_size;
     67 
     68   // Allocate two blocks to copy data between.
     69   byte* src_buffer = static_cast<byte*>(OS::Allocate(data_size, &act_size, 0));
     70   CHECK(src_buffer);
     71   CHECK(act_size >= static_cast<size_t>(data_size));
     72   byte* dest_buffer = static_cast<byte*>(OS::Allocate(data_size, &act_size, 0));
     73   CHECK(dest_buffer);
     74   CHECK(act_size >= static_cast<size_t>(data_size));
     75 
     76   // Storage for a0 and a1.
     77   byte* a0_;
     78   byte* a1_;
     79 
     80   MacroAssembler assembler(isolate, NULL, 0);
     81   MacroAssembler* masm = &assembler;
     82 
     83   // Code to be generated: The stuff in CopyBytes followed by a store of a0 and
     84   // a1, respectively.
     85   __ CopyBytes(a0, a1, a2, a3);
     86   __ li(a2, Operand(reinterpret_cast<int>(&a0_)));
     87   __ li(a3, Operand(reinterpret_cast<int>(&a1_)));
     88   __ sw(a0, MemOperand(a2));
     89   __ jr(ra);
     90   __ sw(a1, MemOperand(a3));
     91 
     92   CodeDesc desc;
     93   masm->GetCode(&desc);
     94   Handle<Code> code = isolate->factory()->NewCode(
     95       desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
     96 
     97   ::F f = FUNCTION_CAST< ::F>(code->entry());
     98 
     99   // Initialise source data with non-zero bytes.
    100   for (int i = 0; i < data_size; i++) {
    101     src_buffer[i] = to_non_zero(i);
    102   }
    103 
    104   const int fuzz = 11;
    105 
    106   for (int size = 0; size < 600; size++) {
    107     for (const byte* src = src_buffer; src < src_buffer + fuzz; src++) {
    108       for (byte* dest = dest_buffer; dest < dest_buffer + fuzz; dest++) {
    109         memset(dest_buffer, 0, data_size);
    110         CHECK(dest + size < dest_buffer + data_size);
    111         (void) CALL_GENERATED_CODE(f, reinterpret_cast<int>(src),
    112                                       reinterpret_cast<int>(dest), size, 0, 0);
    113         // a0 and a1 should point at the first byte after the copied data.
    114         CHECK_EQ(src + size, a0_);
    115         CHECK_EQ(dest + size, a1_);
    116         // Check that we haven't written outside the target area.
    117         CHECK(all_zeroes(dest_buffer, dest));
    118         CHECK(all_zeroes(dest + size, dest_buffer + data_size));
    119         // Check the target area.
    120         CHECK_EQ(0, memcmp(src, dest, size));
    121       }
    122     }
    123   }
    124 
    125   // Check that the source data hasn't been clobbered.
    126   for (int i = 0; i < data_size; i++) {
    127     CHECK(src_buffer[i] == to_non_zero(i));
    128   }
    129 }
    130 
    131 
    132 static void TestNaN(const char *code) {
    133   // NaN value is different on MIPS and x86 architectures, and TEST(NaNx)
    134   // tests checks the case where a x86 NaN value is serialized into the
    135   // snapshot on the simulator during cross compilation.
    136   v8::HandleScope scope(CcTest::isolate());
    137   v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
    138   v8::Context::Scope context_scope(context);
    139 
    140   v8::Local<v8::Script> script = v8::Script::Compile(v8_str(code));
    141   v8::Local<v8::Object> result = v8::Local<v8::Object>::Cast(script->Run());
    142   // Have to populate the handle manually, as it's not Cast-able.
    143   i::Handle<i::JSObject> o =
    144       v8::Utils::OpenHandle<v8::Object, i::JSObject>(result);
    145   i::Handle<i::JSArray> array1(reinterpret_cast<i::JSArray*>(*o));
    146   i::FixedDoubleArray* a = i::FixedDoubleArray::cast(array1->elements());
    147   double value = a->get_scalar(0);
    148   CHECK(std::isnan(value) &&
    149         i::BitCast<uint64_t>(value) ==
    150         i::BitCast<uint64_t>(
    151             i::FixedDoubleArray::canonical_not_the_hole_nan_as_double()));
    152 }
    153 
    154 
    155 TEST(NaN0) {
    156   TestNaN(
    157           "var result;"
    158           "for (var i = 0; i < 2; i++) {"
    159           "  result = new Array(Number.NaN, Number.POSITIVE_INFINITY);"
    160           "}"
    161           "result;");
    162 }
    163 
    164 
    165 TEST(NaN1) {
    166   TestNaN(
    167           "var result;"
    168           "for (var i = 0; i < 2; i++) {"
    169           "  result = [NaN];"
    170           "}"
    171           "result;");
    172 }
    173 
    174 
    175 #undef __
    176