Ejemplo n.º 1
0
static void 
do_test(char *buf, size_t len)
{
    int i, j;
    char *code;
    protect_mem(buf, len, ALLOW_READ|ALLOW_WRITE);
    for (i = 0; i < 7; i++) {
        for (j = 0; j < 7; j++) {
            code = copy_to_buf(buf, len, NULL, CODE_INC, COPY_NORMAL); 
            protect_mem_check(buf, len, prot_codes[i], ALLOW_READ|ALLOW_WRITE);
            protect_mem_check(buf, len, prot_codes[j], prot_codes[i]);
            test_print(code, 5);
            test_print(code, 2);
            if (j > 1 && j < 6) {
                code = copy_to_buf(buf, len, NULL, CODE_DEC, COPY_NORMAL);
                test_print(code, 3);
                test_print(code, 1);
                code = copy_to_buf(buf, len, NULL, CODE_SELF_MOD, COPY_NORMAL);
                test_print(code, 43981);
                test_print(code, 4660);
            }
            buf++;
            len--;
            if (j > 1 && j < 6) {
                protect_mem_check(buf, len, ALLOW_READ|ALLOW_WRITE, prot_codes[j]);
                code = copy_to_buf(buf, len, NULL, CODE_SELF_MOD, COPY_NORMAL);
                protect_mem_check(buf, len, prot_codes[i], ALLOW_READ|ALLOW_WRITE);
                protect_mem_check(buf, len, prot_codes[j], prot_codes[i]);
                test_print(code, 4660);
                test_print(code, 43981);
            }
            protect_mem_check(buf, len, ALLOW_READ|ALLOW_WRITE, prot_codes[j]);
        }
    }
}
Ejemplo n.º 2
0
void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, const char *filepath, bool update_on_change, const char *format) {
        const char *walk;
        char *outwalk = buffer;

        INSTANCE(title);

        if (initialRead) {
            if (!inotifyFd && update_on_change) {
                inotifyFd = inotify_init();
                fcntl(inotifyFd, F_SETFL, fcntl(inotifyFd, F_GETFL) | O_NONBLOCK);
                wd = inotify_add_watch(inotifyFd, filepath, IN_MODIFY);
                if (wd == -1)
                    strcpy(pbuffer, "Failed to add event listener");
            }

            copy_to_buf(filepath);

            initialRead = false;
        } else {
            if (update_on_change) {
                int numRead = read(inotifyFd, buf, BUF_LEN);
                char *p;
                for (p = buf; p < buf + numRead; ) {
                    struct inotify_event *event = (struct inotify_event *) p;
                    if (event->mask & IN_MODIFY) {
                        copy_to_buf(filepath);
                    }
                    p += sizeof(struct inotify_event) + event->len;
                }
            }
        }
            
        for (walk = format; *walk != '\0'; walk++) {
                if (*walk != '%') {
                        *(outwalk++) = *walk;
                        continue;
                }

                if (strncmp(walk+1, "title", strlen("title")) == 0) {
                        outwalk += sprintf(outwalk, "%s", title);
                        walk += strlen("title");
                } else if (strncmp(walk+1, "status", strlen("status")) == 0) {
                        outwalk += sprintf(outwalk, "%s", pbuffer);
                        walk += strlen("status");
                }
        }

        END_COLOR;
        OUTPUT_FULL_TEXT(buffer);
}
Ejemplo n.º 3
0
static void
test_alloc_overlap(void)
{
    /* Test i#1175: create some +rx DGC.  Then change it to +rw via mmap
     * instead of mprotect and ensure DR catches a subsequent code modification.
     * We allocate two pages and put the code one page in so that our
     * modifying mmap can have its prot match the region base's prot to
     * make it harder for DR to detect.
     */
    char *buf = allocate_mem(PAGE_SIZE*2, ALLOW_READ|ALLOW_WRITE|ALLOW_EXEC);
    char *code = copy_to_buf(buf + PAGE_SIZE, PAGE_SIZE, NULL, CODE_INC, COPY_NORMAL);
    protect_mem(code, PAGE_SIZE, ALLOW_READ|ALLOW_EXEC);
    test_print(code, 42);
#ifdef UNIX
    code = mmap(buf, PAGE_SIZE*2, PROT_READ|PROT_WRITE|PROT_EXEC,
                MAP_PRIVATE|MAP_ANON|MAP_FIXED, 0, 0);
#else
    code = VirtualAlloc(buf, PAGE_SIZE*2, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
#endif
    code = copy_to_buf(code, PAGE_SIZE, NULL, CODE_DEC, COPY_NORMAL);
    test_print(code, 42);
    /* Hmm, there is no free_mem()... */
}
Ejemplo n.º 4
0
int
main()
{
    HMODULE lib;
    char *code;
    INIT();

    print("starting good xdata test\n");
    lib = LoadLibrary(dll_name);
    if (lib == NULL) {
        print("error loading library %s\n", dll_name);
    }
    FreeLibrary(lib);

    print("starting bad xdata test\n");
    code = copy_to_buf(bad_xdata_buf, sizeof(bad_xdata_buf), NULL, CODE_INC, COPY_NORMAL);
    test_print(code, 0);
    print("done\n");
    return 0;
}
Ejemplo n.º 5
0
DWORD WINAPI
ThreadProcBusyBuild(LPVOID param)
{
    char *buf = VirtualAlloc(NULL, BUF_LEN, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    /* use selfmod to keep building */
    copy_to_buf(buf, BUF_LEN, NULL, CODE_SELF_MOD, COPY_NORMAL);
    print("Starting busy build\n");
    in_busy_build = TRUE;
    while (!exit_busy_build) {
        /* don't do more than sandbox2ro_threshold iters (20) to avoid case 9908
         * triggered resets (xref 10036 too) leading to hangs from detach at same time
         * (case 8492). We're only trying to test code creation here anyways.
         * Using 2 different values so there's a real change with the write. */
        test(buf, 4);
        test(buf, 5);
    }
    VirtualFree(buf, 0, MEM_RELEASE);
    in_busy_build = FALSE;
    exit_busy_build = FALSE;
    print("Done busy building\n");
    return 0;
}
Ejemplo n.º 6
0
/*
  Function to print a .ppm file for a given time interval.
  The RGB values in a pixel of the cell is set proportional
  to the respective densities in the cell.
*/
int
write_ppm_file(int map[NX][NY], REAL hare[NX][NY], REAL puma[NX][NY],
               const int nx, const int ny, const int write_interval)
{
    int i = 0, j = 0;
    char filename[FILE_NAME_SIZE] = {'\0'};
    int pixel[3] = {0};
    int red_val = 0, green_val = 0;
    int ret = PUMA_NOERR;

    int pixel_counter = 0;
    int *pixel_buffer = NULL;
    int PIXBUFSIZE = 0;

    FILE *file = NULL;
    int scale_factor = 0;
    scale_factor = MAX_SIZE/ny;

    //writeMatrix(hare, "hare", write_interval, nx, ny);
    //writeMatrix(puma, "puma", write_interval, nx, ny);
    //writeMatrix(puma,"map",write_interval);

    if(scale_factor <= 0) scale_factor = 1; /* In case the array is bigger than MAX_SIZE */

    PIXBUFSIZE = YPIXELS * scale_factor * ny * 3; /* 3 since each pixel has RGB */

    debug_msg("[%s:%d]: scale_factor: %d\n",__FILE__,__LINE__, scale_factor);
    debug_msg("[%s:%d]: PIXBUFSIZE: %d\n",__FILE__,__LINE__, PIXBUFSIZE);
    debug_msg("[%s:%d]: write_interval: %d\n",__FILE__,__LINE__, write_interval);

    snprintf(filename, FILE_NAME_SIZE, "pumaHare_%03d.ppm", write_interval);

    file = fopen(filename, "w");

    ret = PUMA_OSERROR;
    if (!file)
        goto errexit;

    if (fprintf(file, "P3\n") < 0)
        goto errexit;

    if (fprintf(file, "%d %d\n", YPIXELS * scale_factor * ny, XPIXELS * scale_factor * nx) < 0)
        goto errexit;

    if (fprintf(file, "%d\n", MAX_COLOR_VAL) < 0)
        goto errexit;

    ret = PUMA_NOERR;

    pixel_buffer = (int*) malloc(sizeof(int) * PIXBUFSIZE);

    for (i = 1; i <= nx; i++)
    {
        for (j = 1; j <= ny; j++)
        {
            if (map[i][j] == 0)
            {
                /* color water grids with blue */
                pixel[BLUE] = MAX_COLOR_VAL / 2;
                copy_to_buf(pixel_buffer, pixel, &pixel_counter, scale_factor);
                memset(pixel, 0, sizeof(int) * 3);
                continue;
            }

            /* set puma color value propotional to puma density in the cell */
            red_val = (puma[i][j] * MAX_COLOR_VAL) / MAX_DENSITY;

            /* set hare color value propotional to hare density in the cell */
            green_val = hare[i][j] * MAX_COLOR_VAL / MAX_DENSITY;

            pixel[RED] = red_val;
            pixel[GREEN] = green_val;
            pixel[BLUE] = 0;

            copy_to_buf(pixel_buffer, pixel, &pixel_counter, scale_factor);
            memset(pixel, 0, sizeof(int) * 3);
        }
        pixel_counter = 0;

        /* Write the RGB values for all cells in the row */
        ret = write_pixel_row(file, pixel_buffer, PIXBUFSIZE, scale_factor);
        if (ret != PUMA_NOERR) goto errexit;

        memset(pixel_buffer, 0, sizeof(int) * PIXBUFSIZE);
    }

errexit:
    if (file) fclose(file);

    free(pixel_buffer);
    return ret;
}
Ejemplo n.º 7
0
Archivo: pmsh.c Proyecto: pdsouza/pmsh
/** SIGCHLD handler **/
void handler(int signum, siginfo_t *info, void *context) {
    int cpid, status;

    cpid = info->si_pid; // sending pid
    //printf("Got a SIGCHLD from %d!\n", cpid);

    // loop to catch simultaneous SIGCHLDs
    while( (cpid = waitpid(-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) > 0 ) {

        //printf("HANDLER Got %d from waitpid\n", cpid);
        job *j = get_job(job_list, cpid);
        if(j == NULL) {
            printf("Error: get_job returned null\n");
            _exit(EXIT_FAILURE);
        }

        if(WIFEXITED(status)) { // completed
            if( set_complete(j, cpid) == -1 ) {
                printf("HANDLER set_complete went awry!\n");
                _exit(EXIT_FAILURE);
            }
        }
        else if(WIFSIGNALED(status)) { // terminated
            set_complete(j, cpid);
            printf("\n");
            //printf("TERMINATED: %d\n", WTERMSIG(status));
        }
        else if(WIFSTOPPED(status)) { // stopped
            j->running = 0;

            if(j->fg) {
                send_back(job_list, j->pgid);
                printf("\nStopped: %s\n", j->rawcmd);
            } else {

                // queue stopped message
                message *m = malloc(sizeof(message));
                copy_to_buf(j->rawcmd, m->msg);
                m->status = 0;
                push(msg_q, PROCESS, (void *)m);
            }
        }
        else if(WIFCONTINUED(status)) { // continued
            j->running = 1;
            //printf("CONTINUED\n");
        }

        if(j->complete) {
            j->running = 0;
            if(!j->fg) {
                // queue finished message
                message *m = malloc(sizeof(message));
                copy_to_buf(j->rawcmd, m->msg);
                m->status = 1;
                push(msg_q, PROCESS, (void *)m);

                free_job(del_job(job_list, j->pgid));
            }
        }
    }

    //printf("HANDLER Done with waitpid loop\n");
}
Ejemplo n.º 8
0
int
crystal_router( CrRouter *crr )
{
    L4C(__log4c_category_trace(GlobLogCat, "crystal_router(%x) -- begin", crr));
    L4C(log_buffer_content("crystal_router() -- begin: output buffer",
                       &crr->out_buf, (CURRENT|OLD)));
    L4C(log_buffer_content("crystal_router() -- begin: input buffer",
                       &crr->in_buf, (CURRENT|OLD)));

    int rec_bytes, d, resp, chan, read_bytes;
    CrBuffer combuf;

    resp = 0;
    compress(&crr->in_buf);
    resp += copy_to_buf(&crr->in_buf, &crr->out_buf, crr->procnum,
                        crr->write_msg_func, crr->userdata);

    for (d = 0; d < crr->doc; d++)
    {
        L4C(__log4c_category_trace(GlobLogCat,
                               "  crystal_router():\tdimension %d", d));

        chan = 1<<d;
        if ( d > 0 )
            resp += compress(&crr->out_buf);

        crb_clear(&crr->send_buf);
        if ( crr->read_msg_func != NULL )
        (*crr->read_msg_func)(&crr->send_buf, d, crr->userdata);
        resp += check_buffer(&crr->send_buf, &crr->out_buf, d, crr->procnum);

        sub_buffer(&combuf, &crr->out_buf);

        L4C(log_buffer_content("crystal_router() - send buffer",
                               &crr->send_buf, (CURRENT|OLD)));

        read_bytes = cishift(combuf.start_ptr, combuf.buf_size, chan,
                            crr->send_buf.start_ptr, crr->send_buf.bytes, chan,
                            crr->comm);

        if (read_bytes < 0)
        {
            // TODO: This seems to be wrong ??!!??!!
            rec_bytes = rescue_buf(combuf.start_ptr, combuf.buf_size);
            resp = -1;
        }
        else
            rec_bytes = read_bytes;
        combuf.bytes = rec_bytes;
        combuf.buf_ptr = combuf.start_ptr + rec_bytes;

        resp += copy_to_buf(&crr->in_buf, &combuf, crr->procnum,
                            crr->write_msg_func, crr->userdata);

        // absorb combuf
        crr->out_buf.bytes  += rec_bytes;
        crr->out_buf.buf_ptr = crr->out_buf.start_ptr + crr->out_buf.bytes;
    }
    crr->in_buf.cur_pos = crr->in_buf.bytes;
    compress(&crr->out_buf);

    L4C(log_buffer_content("crystal_router() -- end: output buffer",
                       &crr->out_buf, (CURRENT|OLD)));
    L4C(log_buffer_content("crystal_router() -- end: input buffer",
                       &crr->in_buf, (CURRENT|OLD)));
    L4C(__log4c_category_trace(GlobLogCat, "crystal_router() -- end"));

    return (resp < 0) ? -1 : 0;
}
Ejemplo n.º 9
0
int sfs_readdir(int fd, char *mem_pointer)
{
	inode directory = get_null_inode();
	locations index_block = NULL;
	uint32_t inode_location = 0;
	uint32_t i = 0;
	uint32_t num_locations = 0;
	int count = 0;
	byte* buf = NULL;

	if(fd >= 0 && fd < NUMOFL)
	{

		/* Validate the file descriptor */
		if(validate_fd(fd) < 0)
		{
			/*
			 * file descriptor not found in swoft
			 */
			print_error(INVALID_FILE_DESCRIPTOR);
			return -1;
		}

		/*
		 * Retrieve the contents of the directory's index block. Use the Inode
		 * to retrieve the names of the contents. Store the values into
		 * mem_pointer.
		 */
		directory = get_swoft_inode(fd);

		/* If the inode is not a directory return an error */
		if(directory.type != 1)
		{
			/*
			 * Invalid file type error
			 */
			print_error(INVALID_FILE_TYPE);
			return -1;
		}

		/*
		 * Iterate through the index block
		 * when the directory is empty
		 */
		index_block = iterate_index(directory.location, NULL);

		if(index_block == NULL)
		{
			/*
			 * Invalid index block
			 */
			print_error(INDEX_ALLOCATION_ERROR);
			return -1;
		}

		num_locations = count_files_in_dir(directory.location);

		if(num_locations < 0)
		{
			/*
			 * Invalid index block
			 */
			print_error(INDEX_ALLOCATION_ERROR);
			return -1;
		}

		if(num_locations == 0)
		{
			/*
			 * Empty Directory Read
			 */
			print_error(DIRECTORY_EMPTY);
			return 0;
		}

		int cur_index = get_index_entry(*get_inode(directory.location));

		if(num_locations == cur_index)
		{
			reset_index_entry();
			return 0;
		}

		char* name = get_name(index_block[cur_index]);
		strcpy(mem_pointer, name);

		/* Update the directory inode date last accessed and write the inode back to disk */
		directory.date_last_accessed = time(NULL);

		/* Get the inode location */
		inode_location = get_inode_loc(fd);

		buf = allocate_buf(buf, BLKSIZE);
		buf = (byte *) copy_to_buf((byte *) &directory, (byte *) buf, sizeof(inode), BLKSIZE);

		if(write_block(inode_location, buf) < 0)
		{
			free(buf);
			print_error(DISK_WRITE_ERROR);
			return -1;
		}
		free(buf);

		/*
		 * return value > 0 for a successful read dir
		 * return value = 0 if there is no contents in dir
		 * return value < 0 for a unsuccessful read dir
		 */
		print_error(SUCCESS);
		return 1;
	}

	print_error(UNKNOWN);
	return -1;
}