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 
     32 #include "src/macro-assembler.h"
     33 #include "src/arm/macro-assembler-arm.h"
     34 #include "src/arm/simulator-arm.h"
     35 #include "test/cctest/cctest.h"
     36 
     37 
     38 using namespace v8::internal;
     39 
     40 typedef void* (*F)(int x, int y, int p2, int p3, int p4);
     41 
     42 #define __ masm->
     43 
     44 
     45 static byte to_non_zero(int n) {
     46   return static_cast<unsigned>(n) % 255 + 1;
     47 }
     48 
     49 
     50 static bool all_zeroes(const byte* beg, const byte* end) {
     51   CHECK(beg);
     52   CHECK(beg <= end);
     53   while (beg < end) {
     54     if (*beg++ != 0)
     55       return false;
     56   }
     57   return true;
     58 }
     59 
     60 
     61 TEST(CopyBytes) {
     62   CcTest::InitializeVM();
     63   Isolate* isolate = Isolate::Current();
     64   HandleScope handles(isolate);
     65 
     66   const int data_size = 1 * KB;
     67   size_t act_size;
     68 
     69   // Allocate two blocks to copy data between.
     70   byte* src_buffer = static_cast<byte*>(OS::Allocate(data_size, &act_size, 0));
     71   CHECK(src_buffer);
     72   CHECK(act_size >= static_cast<size_t>(data_size));
     73   byte* dest_buffer = static_cast<byte*>(OS::Allocate(data_size, &act_size, 0));
     74   CHECK(dest_buffer);
     75   CHECK(act_size >= static_cast<size_t>(data_size));
     76 
     77   // Storage for R0 and R1.
     78   byte* r0_;
     79   byte* r1_;
     80 
     81   MacroAssembler assembler(isolate, NULL, 0);
     82   MacroAssembler* masm = &assembler;
     83 
     84   // Code to be generated: The stuff in CopyBytes followed by a store of R0 and
     85   // R1, respectively.
     86   __ CopyBytes(r0, r1, r2, r3);
     87   __ mov(r2, Operand(reinterpret_cast<int>(&r0_)));
     88   __ mov(r3, Operand(reinterpret_cast<int>(&r1_)));
     89   __ str(r0, MemOperand(r2));
     90   __ str(r1, MemOperand(r3));
     91   __ bx(lr);
     92 
     93   CodeDesc desc;
     94   masm->GetCode(&desc);
     95   Handle<Code> code = isolate->factory()->NewCode(
     96       desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
     97 
     98   F f = FUNCTION_CAST<F>(code->entry());
     99 
    100   // Initialise source data with non-zero bytes.
    101   for (int i = 0; i < data_size; i++) {
    102     src_buffer[i] = to_non_zero(i);
    103   }
    104 
    105   const int fuzz = 11;
    106 
    107   for (int size = 0; size < 600; size++) {
    108     for (const byte* src = src_buffer; src < src_buffer + fuzz; src++) {
    109       for (byte* dest = dest_buffer; dest < dest_buffer + fuzz; dest++) {
    110         memset(dest_buffer, 0, data_size);
    111         CHECK(dest + size < dest_buffer + data_size);
    112         (void) CALL_GENERATED_CODE(f, reinterpret_cast<int>(src),
    113                                       reinterpret_cast<int>(dest), size, 0, 0);
    114         // R0 and R1 should point at the first byte after the copied data.
    115         CHECK_EQ(src + size, r0_);
    116         CHECK_EQ(dest + size, r1_);
    117         // Check that we haven't written outside the target area.
    118         CHECK(all_zeroes(dest_buffer, dest));
    119         CHECK(all_zeroes(dest + size, dest_buffer + data_size));
    120         // Check the target area.
    121         CHECK_EQ(0, memcmp(src, dest, size));
    122       }
    123     }
    124   }
    125 
    126   // Check that the source data hasn't been clobbered.
    127   for (int i = 0; i < data_size; i++) {
    128     CHECK(src_buffer[i] == to_non_zero(i));
    129   }
    130 }
    131 
    132 
    133 typedef int (*F5)(void*, void*, void*, void*, void*);
    134 
    135 
    136 TEST(LoadAndStoreWithRepresentation) {
    137   v8::internal::V8::Initialize(NULL);
    138 
    139   // Allocate an executable page of memory.
    140   size_t actual_size;
    141   byte* buffer = static_cast<byte*>(OS::Allocate(Assembler::kMinimalBufferSize,
    142                                                  &actual_size,
    143                                                  true));
    144   CHECK(buffer);
    145   Isolate* isolate = CcTest::i_isolate();
    146   HandleScope handles(isolate);
    147   MacroAssembler assembler(isolate, buffer, static_cast<int>(actual_size));
    148   MacroAssembler* masm = &assembler;  // Create a pointer for the __ macro.
    149   __ sub(sp, sp, Operand(1 * kPointerSize));
    150   Label exit;
    151 
    152   // Test 1.
    153   __ mov(r0, Operand(1));  // Test number.
    154   __ mov(r1, Operand(0));
    155   __ str(r1, MemOperand(sp, 0 * kPointerSize));
    156   __ mov(r2, Operand(-1));
    157   __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::UInteger8());
    158   __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
    159   __ mov(r2, Operand(255));
    160   __ cmp(r3, r2);
    161   __ b(ne, &exit);
    162   __ mov(r2, Operand(255));
    163   __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::UInteger8());
    164   __ cmp(r3, r2);
    165   __ b(ne, &exit);
    166 
    167   // Test 2.
    168   __ mov(r0, Operand(2));  // Test number.
    169   __ mov(r1, Operand(0));
    170   __ str(r1, MemOperand(sp, 0 * kPointerSize));
    171   __ mov(r2, Operand(-1));
    172   __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::Integer8());
    173   __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
    174   __ mov(r2, Operand(255));
    175   __ cmp(r3, r2);
    176   __ b(ne, &exit);
    177   __ mov(r2, Operand(-1));
    178   __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::Integer8());
    179   __ cmp(r3, r2);
    180   __ b(ne, &exit);
    181 
    182   // Test 3.
    183   __ mov(r0, Operand(3));  // Test number.
    184   __ mov(r1, Operand(0));
    185   __ str(r1, MemOperand(sp, 0 * kPointerSize));
    186   __ mov(r2, Operand(-1));
    187   __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::UInteger16());
    188   __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
    189   __ mov(r2, Operand(65535));
    190   __ cmp(r3, r2);
    191   __ b(ne, &exit);
    192   __ mov(r2, Operand(65535));
    193   __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::UInteger16());
    194   __ cmp(r3, r2);
    195   __ b(ne, &exit);
    196 
    197   // Test 4.
    198   __ mov(r0, Operand(4));  // Test number.
    199   __ mov(r1, Operand(0));
    200   __ str(r1, MemOperand(sp, 0 * kPointerSize));
    201   __ mov(r2, Operand(-1));
    202   __ Store(r2, MemOperand(sp, 0 * kPointerSize), Representation::Integer16());
    203   __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
    204   __ mov(r2, Operand(65535));
    205   __ cmp(r3, r2);
    206   __ b(ne, &exit);
    207   __ mov(r2, Operand(-1));
    208   __ Load(r3, MemOperand(sp, 0 * kPointerSize), Representation::Integer16());
    209   __ cmp(r3, r2);
    210   __ b(ne, &exit);
    211 
    212   __ mov(r0, Operand(0));  // Success.
    213   __ bind(&exit);
    214   __ add(sp, sp, Operand(1 * kPointerSize));
    215   __ bx(lr);
    216 
    217   CodeDesc desc;
    218   masm->GetCode(&desc);
    219   Handle<Code> code = isolate->factory()->NewCode(
    220       desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
    221 
    222   // Call the function from C++.
    223   F5 f = FUNCTION_CAST<F5>(code->entry());
    224   CHECK_EQ(0, CALL_GENERATED_CODE(f, 0, 0, 0, 0, 0));
    225 }
    226 
    227 #undef __
    228