<commit_before>// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "platform/assert.h"
#include "vm/unit_test.h"
#include "vm/verified_memory.h"

namespace dart {

void Init() {
#if defined(DEBUG)
  FLAG_verified_mem = true;
#endif
}


UNIT_TEST_CASE(VerifiedMemoryBasic) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  vm->Commit(false);
  double* addr = reinterpret_cast<double*>(vm->address());
  VerifiedMemory::Write(&addr[0], 0.5);
  EXPECT_EQ(0.5, addr[0]);
  VerifiedMemory::Write(&addr[1], 1.5);
  VerifiedMemory::Write(&addr[2], 2.5);
  VerifiedMemory::Write(&addr[0], 0.25);
  static const double kNaN = NAN;
  VerifiedMemory::Write(&addr[0], kNaN);  // Bitwise comparison should be used.
  VerifiedMemory::Write(&addr[0], 0.5);
  int64_t* unverified = reinterpret_cast<int64_t*>(&addr[3]);
  *unverified = 123;
  VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double));
}


UNIT_TEST_CASE(VerifiedMemoryAccept) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  vm->Commit(false);
  double* addr = reinterpret_cast<double*>(vm->address());
  VerifiedMemory::Write(&addr[0], 0.5);
  VerifiedMemory::Write(&addr[1], 1.5);
  VerifiedMemory::Write(&addr[2], 2.5);
  VerifiedMemory::Write(&addr[0], 0.25);
  // Unverified write followed by Accept ("I know what I'm doing").
  memset(addr, 0xf3, 2 * sizeof(double));
  VerifiedMemory::Accept(reinterpret_cast<uword>(addr), 2 * sizeof(double));
  VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double));
}


// Negative tests below.

UNIT_TEST_CASE(VerifyImplicit_Crash) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  vm->Commit(false);
  double* addr = reinterpret_cast<double*>(vm->address());
  addr[0] = 0.5;  // Forget to use Write.
  VerifiedMemory::Write(&addr[0], 1.5);
}


UNIT_TEST_CASE(VerifyExplicit_Crash) {<commit_msg>Add simpler unit tests for VerifiedMemory.<commit_after>// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include "platform/assert.h"
#include "vm/unit_test.h"
#include "vm/verified_memory.h"

namespace dart {

void Init() {
#if defined(DEBUG)
  FLAG_verified_mem = true;
#endif
}


UNIT_TEST_CASE(VerifiedMemoryReserve) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  delete vm;
}


UNIT_TEST_CASE(VerifiedMemoryCommit) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  vm->Commit(false);
  delete vm;
}


UNIT_TEST_CASE(VerifiedMemoryBasic) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  vm->Commit(false);
  double* addr = reinterpret_cast<double*>(vm->address());
  VerifiedMemory::Write(&addr[0], 0.5);
  EXPECT_EQ(0.5, addr[0]);
  VerifiedMemory::Write(&addr[1], 1.5);
  VerifiedMemory::Write(&addr[2], 2.5);
  VerifiedMemory::Write(&addr[0], 0.25);
  static const double kNaN = NAN;
  VerifiedMemory::Write(&addr[0], kNaN);  // Bitwise comparison should be used.
  VerifiedMemory::Write(&addr[0], 0.5);
  int64_t* unverified = reinterpret_cast<int64_t*>(&addr[3]);
  *unverified = 123;
  VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double));
  delete vm;
}


UNIT_TEST_CASE(VerifiedMemoryAccept) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  vm->Commit(false);
  double* addr = reinterpret_cast<double*>(vm->address());
  VerifiedMemory::Write(&addr[0], 0.5);
  VerifiedMemory::Write(&addr[1], 1.5);
  VerifiedMemory::Write(&addr[2], 2.5);
  VerifiedMemory::Write(&addr[0], 0.25);
  // Unverified write followed by Accept ("I know what I'm doing").
  memset(addr, 0xf3, 2 * sizeof(double));
  VerifiedMemory::Accept(reinterpret_cast<uword>(addr), 2 * sizeof(double));
  VerifiedMemory::Verify(reinterpret_cast<uword>(addr), 3 * sizeof(double));
  delete vm;
}


// Negative tests below.

UNIT_TEST_CASE(VerifyImplicit_Crash) {
  Init();
  const intptr_t kReservationSize = 64 * KB;
  VirtualMemory* vm = VerifiedMemory::Reserve(kReservationSize);
  EXPECT_EQ(kReservationSize, vm->size());
  vm->Commit(false);
  double* addr = reinterpret_cast<double*>(vm->address());
  addr[0] = 0.5;  // Forget to use Write.
  VerifiedMemory::Write(&addr[0], 1.5);
}


UNIT_TEST_CASE(VerifyExplicit_Crash) {
{'fraction_non_alphanumeric': 0.15443571565433992, 'fraction_numerical': 0.024908986395861275, 'mean_word_length': 5.61596958174905, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3933230', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1822, 'n_words': 382}