0

I am working on Assignment 2 of ops-class.

The following function bootstraps a file handler array for the process that is being created (eg. user processes for test programs provided here).

int _fh_bootstrap(struct fharray *fhs){

/* Initialize the file handle array of this process */
fharray_init(fhs);

/* String variables initialized for passage to vfs_open */
char* console_inp = kstrdup(CONSOLE); // CONSOLE = "con:"
char* console_out = kstrdup(console_inp);
char* console_err = kstrdup(console_inp);

/* Return variable */
int ret = 0;

/* Initialize the console files STDIN, STDOUT and STDERR */
struct vnode *stdin;
ret = vfs_open(console_inp,O_RDONLY,0,&stdin);
if(ret != 0){
    return ret;
}
kfree(console_inp);

struct fh *stdinfh = kmalloc(sizeof(struct fh));
ret =  _fh_create(O_RDONLY,stdin,stdinfh);
if(ret != 0){
    return ret;
}

stdinfh->fd = STDIN_FILENO;
fharray_add(fhs,stdinfh,NULL);

struct vnode *stdout;
ret = vfs_open(console_out,O_WRONLY,0,&stdout);
if(ret != 0){
    return ret;
}
kfree(console_out);

struct fh *stdoutfh = kmalloc(sizeof(struct fh));
ret =  _fh_create(O_WRONLY,stdout,stdoutfh);
if(ret != 0){
    return ret;
}

stdoutfh->fd = STDOUT_FILENO;
fharray_add(fhs,stdoutfh,NULL);

struct vnode *stderr;
ret = vfs_open(console_err,O_WRONLY,0,&stderr);
if(ret != 0){
    return ret;
}
kfree(console_err);

struct fh *stderrfh = kmalloc(sizeof(struct fh));
ret =  _fh_create(O_WRONLY,stderr,stderrfh);
if(ret != 0){
    return ret;
}

stderrfh->fd = STDERR_FILENO;
fharray_add(fhs,stderrfh,NULL);

fharray_setsize(fhs,MAX_FD);    

return 0;

/* Initialization of stdin, out and err filehandlers complete */
}

If I use os161-gdb to step through this function, I notice the following:

//*stdinfh after the call to _fh_create 
{fd = 0, flag = 0, fh_seek = 0, fh_vnode = 0x80044ddc}

//**stdinfh->fh_vnode
{vn_refcount = 2, vn_countlock = {splk_lock = 0, splk_holder = 0x0}, vn_fs = 0x0,
vn_data = 0x8004ab60, vn_ops = 0x8003e690 <dev_vnode_ops>}

This is the strange part. After stepping through the second call to kmalloc (to init stdoutfh), the stdinfh->fh_vnode pointer changes value!

//**stdinfh->fh_vnode
(struct vnode *) 0x1

And even stranger, after proceeding to the following line

fharray_add(fhs,stdoutfh,NULL);

The value of *stdoutfh->fh_vnode and *stdinfh->fh_vnode IS THE SAME

1 possible explanation: Does the OS not have enough heap memory. I find it unlikely and even after assuming this, I can't exactly explain what is happening here.

Some extra code

  1. _fh_create
  2. struct fh definition

    static int _fh_create(int flag, struct vnode *file, struct fh *handle){
    
    KASSERT(file != NULL);
    
    /* W , R , RW */
    if ( 
        ((flag & O_RDONLY) && (flag & O_WRONLY)) ||
        ((flag & O_RDWR) && ((flag & O_RDONLY) || (flag & O_WRONLY)))
    ) {
        handle = NULL;
        return 1;
    }
    
    handle->flag = flag;
    handle->fh_seek = 0;
    handle->fh_vnode = &file;
    
    return 0;
    }
    

    struct fh { uint32_t fd; // file descriptor int flag; // File handler mode off_t fh_seek; // seek position in file struct vnode **fh_vnode; // File object of the file }

Definition of struct vnode can be found here.

Please let me know if you need more info and thanks for the help!

4

1 回答 1

1

代码handle->fh_vnode设置了一个指向自动变量的指针(“在堆栈上”),函数参数是自动变量。函数返回后,这将是一个悬空指针。

要解决这个问题,您将需要重新设计您的代码,例如,也许struct fh应该只存储file而不是指向file.

于 2017-01-12T04:45:03.303 回答