Exemplo n.º 1
0
void check_storeload(){
    create_segment(1, 0x1);
    create_segment(5, 0x2);
    create_segment(10, 0x3);
    store_word(0x1, 0, 0x1);
    store_word(0x2, 4, 0x10);
    store_word(0x3, 4, 0x100);
    uint64_t a = load_word(1, 0);
    uint64_t b = load_word(2, 4);
    uint64_t c = load_word(3, 4);
    assert(a == 0x1 && b == 0x10 && c == 0x100);
}
Exemplo n.º 2
0
static void run_test(void) {
	size_t num_bytes = sysconf(_SC_PAGESIZE);
	int fd = create_segment(num_bytes);
	int* wpage = mmap(NULL, num_bytes, PROT_WRITE, MAP_SHARED, fd, 0);
	int i;
	int* rpage;

	close(128);
	munmap(NULL, 0);

	struct mmap_arg_struct args;
	args.addr = 0;
	args.len = num_bytes;
	args.prot = PROT_READ;
	args.flags = MAP_SHARED;
	args.fd = fd;
	args.offset = 0;
	rpage = (int*)syscall(SYS_mmap, &args);

	test_assert(wpage != (void*)-1 && rpage != (void*)-1
		    && rpage != wpage);

	close(128);

	for (i = 0; i < num_bytes / sizeof(int); ++i) {
		wpage[i] = i;
		test_assert(rpage[i] == i);
	}
}
Exemplo n.º 3
0
void check_length(){
    create_segment(5, 0x1);
    int length = *(int*)UArray_at(Seg_Length_Array, 1);
    printf("%d\n", length);
    assert(length == 5);
}
Exemplo n.º 4
0
void check_newfree(){
    create_segment(1, 0x1);
    uint64_t * segment = UArray_at(seg_array, 1);
    assert(segment != NULL);
    free_segment(0x1);
}
Exemplo n.º 5
0
int main(int ac, char **av)
{
	int lc;
	const char *msg;

	void *ptr;
	int retval, func;

	int flag;
	int seg[4];

	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}

	setup();		/* global setup */

	/* The following loop checks looping state if -i option given */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		/* reset tst_count in case we are looping */
		tst_count = 0;

//block1:
		/*
		 * Check for ENOSYS.
		 */
		tst_resm(TINFO, "Enter block 1");
		flag = 0;
		ptr = (void *)malloc(10);
		func = 100;
		retval = modify_ldt(func, ptr, sizeof(ptr));
		if (retval < 0) {
			if (errno != ENOSYS) {
				tst_resm(TFAIL, "modify_ldt() set invalid "
					 "errno, expected ENOSYS, got: %d",
					 errno);
				flag = FAILED;
			}
		} else {
			tst_resm(TFAIL, "modify_ldt error: "
				 "unexpected return value %d", retval);
			flag = FAILED;
		}

		if (flag) {
			tst_resm(TINFO, "block 1 FAILED");
		} else {
			tst_resm(TINFO, "block 1 PASSED");
		}
		tst_resm(TINFO, "Exit block 1");
		free(ptr);

//block2:
		/*
		 * Check for EINVAL
		 */
		tst_resm(TINFO, "Enter block 2");
		flag = 0;

		ptr = 0;

		retval = modify_ldt(1, ptr, sizeof(ptr));
		if (retval < 0) {
			if (errno != EINVAL) {
				tst_resm(TFAIL, "modify_ldt() set invalid "
					 "errno, expected EINVAL, got: %d",
					 errno);
				flag = FAILED;
			}
		} else {
			tst_resm(TFAIL, "modify_ldt error: "
				 "unexpected return value %d", retval);
			flag = FAILED;
		}

		if (flag) {
			tst_resm(TINFO, "block 2 FAILED");
		} else {
			tst_resm(TINFO, "block 2 PASSED");
		}
		tst_resm(TINFO, "Exit block 2");

//block3:

		/*
		 * Create a new LDT segment.
		 */
		if (create_segment(seg, sizeof(seg)) == -1) {
			tst_brkm(TINFO, cleanup, "Creation of segment failed");
		}

		/*
		 * Check for EFAULT
		 */
		ptr = sbrk(0);

		retval = modify_ldt(0, ptr + 0xFFF, sizeof(ptr));
		if (retval < 0) {
			if (errno != EFAULT) {
				tst_resm(TFAIL, "modify_ldt() set invalid "
					 "errno, expected EFAULT, got: %d",
					 errno);
				flag = FAILED;
			}
		} else {
			tst_resm(TFAIL, "modify_ldt error: "
				 "unexpected return value %d", retval);
			flag = FAILED;
		}

		if (flag) {
			tst_resm(TINFO, "block 3 FAILED");
		} else {
			tst_resm(TINFO, "block 3 PASSED");
		}
		tst_resm(TINFO, "Exit block 3");

	}
	cleanup();
	tst_exit();

}
Exemplo n.º 6
0
void execute_program(UM_state um)
{
        int ra =0;
        int rb =0;
        int rc =0;         
        //uint32_t val= 0;
        bool proceed = true;
        int num_instructions = get_words_in_seg(um->memory, 0);
        int i = um->instr_ctr;

        while (i < num_instructions && proceed) {
                uint32_t word = get_word(um->memory, 0, i);
                uint32_t op_code = get_op_code(word);

                /*if (op_code == 13) {
                        int ra = get_reg_num(word, LOAD_RA_LSB);
                        proceed &= valid_reg(ra);

                        uint32_t val = get_val(word);
                        (void) val;
                }

                else {*/
                        ra = get_reg_num(word, RA_LSB);
                        rb = get_reg_num(word, RB_LSB);
                        rc = get_reg_num(word, RC_LSB);

                        proceed &= valid_reg(ra);
                        proceed &= valid_reg(rb);
                        proceed &= valid_reg(rc);
                //}

                if (!proceed) {
                        break;
                }
                        
                if (op_code == 0) {
                        /* Conditional move - op code 0 */

                        if (val_in_reg(um, rc) != 0) {
                                uint32_t val = val_in_reg(um, rb);
                                set_reg_val(um, ra, val);
                        }
                                        
                } else if (op_code == 1) {
                        /* Segmented load - op code 1 */
                        uint32_t val = get_word(um->memory, val_in_reg(um, rb),
                                       val_in_reg(um, rc));
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 2) {

                        /* Segmented store - op code 2 */

                        uint32_t ID = val_in_reg(um, ra);
                        uint32_t offset = val_in_reg(um, rb);
                        uint32_t val = val_in_reg(um, rc);

                        put_word(um->memory, ID, offset, val);
                                        
                } else if (op_code == 3) {

                        /* Add - op code 3 */
                        uint32_t val = (val_in_reg(um, rb) + val_in_reg(um, rc)) % UINT_MAX;
                        set_reg_val(um, ra, val);

                                        
                } else if (op_code == 4) {
                        /* Multiply - op code 4 */
                        uint32_t val = (val_in_reg(um, rb) * val_in_reg(um, rc)) % UINT_MAX;
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 5) {
                        /* Divide - op code 5 */

                        uint32_t val = val_in_reg(um, rb) / val_in_reg(um, rc);
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 6) {
                        /* Bitwise NAND - op code 6 */

                        uint32_t val = ~(val_in_reg(um, rb) & val_in_reg(um, rc));
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 7) {
                        /* Halt */
                        proceed = false;
                                        
                } else if (op_code == 8) {
                        /* Map segment - op code 8 */

                        if (Stack_empty(um->unmapped_IDs)) {
                              
                                uint32_t num_words = val_in_reg(um, rc);

                                proceed &= create_segment(um->memory, num_words);             
                                set_reg_val(um, rb, (get_num_segs(um->memory) - 1));
                        }

                        else {
                                uint32_t ID = get_unmapped_ID(um);
                                uint32_t num_words = val_in_reg(um, rc);

                                proceed &= resize(um->memory, ID, num_words);

                                set_reg_val(um, rb, ID);
                        }

                } else if (op_code == 9) {
                        /* Unmap segment - op code 9 */
                        
                        uint32_t ID = val_in_reg(um, rc);
                        proceed &= clear_seg(um->memory, ID);
                        proceed &= add_unmapped_ID(um, ID);
                                        
                } else if (op_code == 10) {
                        /* Output - op code 10 */
                        uint32_t val = val_in_reg(um, rc);

                        if (val < 256) {
                                fprintf(stdout, "%c", (char) val);
                        } else {
                                proceed &= false;

                        }
                                        
                } else if (op_code == 11) {
                        /* Input - op code 11 */

                        uint32_t val = getc(stdin);

                        if ((int) val == EOF) {
                                val = ~0;

                        } else if (val > 255) {
                                proceed &= false;
                        }

                        set_reg_val(um, rc, val);
                                        
                } else if (op_code == 12) {
                        /* Load program - op code 12 */

                        uint32_t ID = val_in_reg(um, rb);

                        if (ID != 0) {
                                proceed &= clear_seg(um->memory, 0);

                                int num_words = get_words_in_seg(um->memory, ID);
                                
                                resize(um->memory, 0, num_words);

                                for (int i = 0; i < num_words; i++) {
                                        proceed &= put_word(um->memory, 0, i,
                                                               get_word(um->memory, ID, i));
                                }
                        }

                        um->instr_ctr = val_in_reg(um, rc);

                        num_instructions = 
                                get_words_in_seg(um->memory, 0);
                        i = um->instr_ctr;
                                        
                } else if (op_code == 13) {

                       /* Load value - op code 13 */
                        ra = get_reg_num(word, LOAD_RA_LSB);
                        proceed &= valid_reg(ra);

                        uint32_t val = get_val(word);
                        set_reg_val(um, ra, val);
                        
                } else {

                        fprintf(stderr, "op code doesn't exist\n");
                        proceed = false;
                }
                
                if (op_code != 12) {
                        i++;
                }
        }

        return;
}