GithubHelp home page GithubHelp logo

dangerous_buffer's Introduction

Dangerous buffer for testing purposes

About

Allocates a buffer with a "dangerous zone" immediately afterwards for testing purposes. Any access beyond the useable area by even one byte triggers a SIGSEGV.

It is intended to be small so that it can be easily copied into C++ unit test programs.

License: Public Domain

Dependencies: mmap

Contributions: If you have an idea to improve it, you are welcome!

Just copy this into your test program:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

/**
    Allocates a buffer with a "dangerous zone" immediately afterwards for testing purposes.
    Dispose with: munmap(bufmm, buflen);
     * len: Length of the useable buffer in bytes. The buffer is not aligned.
     * protect_last: PROT_NONE to disallow all acesses, PROT_READ to allow only read
     * mmptr: Pointer to the mmap mapping
     * mmlen: Length of the mmap mapping
     Returns: Pointer to the usable buffer (identical to mmptr when len % PAGE_SIZE == 0)
*/
static char* dangerous_buffer(size_t len, int protect_last, void*& mmptr, size_t& mmlen)
{
    #ifndef PAGE_SIZE
        #define PAGE_SIZE 4096
    #endif
    mmlen = (len &~ (PAGE_SIZE - 1)) + ((len % PAGE_SIZE) ? 2 : 1) * PAGE_SIZE; // One extra page
    mmptr = mmap(NULL, mmlen, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    if (mmptr == MAP_FAILED) {
        perror("mmap in dangerous_buffer");
        exit(-1);
    }
    if (mprotect((char*)mmptr + mmlen - PAGE_SIZE, PAGE_SIZE, protect_last)) { // Protect last page
        perror("mprotect in dangerous_buffer");
        exit(-1);
    }
    // Directly after the user pointer is the protected page
    return (char*)mmptr + ((len % PAGE_SIZE) ? (PAGE_SIZE - (len % PAGE_SIZE)) : 0);
}

dangerous_buffer's People

Contributors

evelance avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.