<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//#define DEBUG // Enable debugging
//#define DEBUG2

#include <kernel/elf.hpp>
#include <util/fixedvec.hpp>
#include <cassert>
#include <cstdlib>
#include <cstdint>
#include <map>
#include <stdexcept>

extern void print_backtrace();
extern void* heap_begin;
extern void* heap_end;
static void safe_print_symbol(int N, void* addr);

static char dbg_write_buffer[1024];
#include <unistd.h>
#define DPRINTF(X,...)  { \
        int L = snprintf(dbg_write_buffer, sizeof(dbg_write_buffer), \
        X, ##__VA_ARGS__); write(1, dbg_write_buffer, L); }


struct allocation
{
  allocation() : addr(0) {}
  allocation(char* A, size_t S, void* L1, void* L2, void* L3)
    :  addr(A), len(S), level1(L1), level2(L2), level3(L3) {}

  char*  addr;
  size_t len;
  void*  level1;
  void*  level2;
  void*  level3;
};

static int enable_debugging = 1;
static int enable_debugging_verbose = 0;
static int enable_buffer_protection = 1;
static fixedvector<allocation,  65536> allocs;
static fixedvector<allocation*, 65536> free_allocs;

// There is a chance of a buffer overrun where this exact value
// is written, but the chance of that happening is minimal
static const uint64_t buffer_protection_checksum = 0xdeadbeef1badcafe;

extern "C"
void _enable_heap_debugging(int enabled)
{
  enable_debugging = enabled;
}
extern "C"
void _enable_heap_debugging_verbose(int enabled)
{
  enable_debugging_verbose = enabled;
}
extern "C"
int _get_heap_debugging_buffers_usage()
{
  return allocs.size();
}
extern "C"
int _get_heap_debugging_buffers_total()
{<commit_msg>heapdebug: Enable toggling buffer overflow checks<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//#define DEBUG // Enable debugging
//#define DEBUG2

#include <kernel/elf.hpp>
#include <util/fixedvec.hpp>
#include <cassert>
#include <cstdlib>
#include <cstdint>
#include <map>
#include <stdexcept>

extern void print_backtrace();
extern void* heap_begin;
extern void* heap_end;
static void safe_print_symbol(int N, void* addr);

static char dbg_write_buffer[1024];
#include <unistd.h>
#define DPRINTF(X,...)  { \
        int L = snprintf(dbg_write_buffer, sizeof(dbg_write_buffer), \
        X, ##__VA_ARGS__); write(1, dbg_write_buffer, L); }


struct allocation
{
  allocation() : addr(0) {}
  allocation(char* A, size_t S, void* L1, void* L2, void* L3)
    :  addr(A), len(S), level1(L1), level2(L2), level3(L3) {}

  char*  addr;
  size_t len;
  void*  level1;
  void*  level2;
  void*  level3;
};

static int enable_debugging = 1;
static int enable_debugging_verbose = 0;
static int enable_buffer_protection = 1;
static fixedvector<allocation,  65536> allocs;
static fixedvector<allocation*, 65536> free_allocs;

// There is a chance of a buffer overrun where this exact value
// is written, but the chance of that happening is minimal
static const uint64_t buffer_protection_checksum = 0xdeadbeef1badcafe;

extern "C"
void _enable_heap_debugging(int enabled)
{
  enable_debugging = enabled;
}
extern "C"
void _enable_heap_debugging_verbose(int enabled)
{
  enable_debugging_verbose = enabled;
}
extern "C"
void _enable_heap_debugging_buffer_protection(int enabled)
{
  enable_buffer_protection = enabled;
}
extern "C"
int _get_heap_debugging_buffers_usage()
{
  return allocs.size();
}
extern "C"
int _get_heap_debugging_buffers_total()
{
{'fraction_non_alphanumeric': 0.10453774385072095, 'fraction_numerical': 0.018659881255301103, 'mean_word_length': 4.4594907407407405, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1054121', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1515, 'n_words': 516}