Page 1 of 1

Code Improvement?

Posted: Mon Jun 07, 2004 12:02 pm
by amirsadig
I think in my code there is alot of bugs ;D. that is normal - linux has also bugs -.

see this function

Code: Select all

/* shell process begin here */
int main()
{    
      
    char *promt = "sh:#";
    int i;
    write("\x1B[2J", strlen("\x1B[2J"));
    while(1)
    {      
      /*strcpy(text,"hallo ^1");*/
      write(promt, strlen(promt));      
      readline(command);      
      if(execve(command, 0) < 0)
     write(command, strlen(command));
      for(i=0; i< 100; i++)
     command[i] = '\0';
    }    
    
  return 0;
}
this a part from small shell whish OS execute it (loaded from floppy). with this shell I can run programs. until now I have test small program and executed the shell it self.
now see the problem. when I have change the Message of promt to (Enter Your Name), it crash with page fault.
look only the message has been change my OS crash.

Have you some suggestion about how to improve my OS Code?

Re:Code Improvement?

Posted: Mon Jun 07, 2004 1:40 pm
by Pype.Clicker
how exactly do you 'change' the prompt ? you know that as you programmed it, prompt is pointing towards a supposed-constant string within the .text (or .data) section ...

Considering this,

Code: Select all

  prompt="some other string:>";
shouldn't do any harm, but if you ever try

Code: Select all

  strcpy(prompt, "some other string:>");
there are chances you'll overwrite code with the new content while trying to write it within the small char array of "sh:#"

Re:Code Improvement?

Posted: Mon Jun 07, 2004 3:07 pm
by amirsadig
the code as above except :

Code: Select all

char *promt = "Enter Your Name: ";
that is the only change has been made.

Re:Code Improvement?

Posted: Mon Jun 07, 2004 3:24 pm
by Pype.Clicker
can you find the message in the final binary image (e.g. does the linker include *all* what it should) ? did you made sure the loader loaded the *whole* file into memory ?

If both answer are positive, i fear you'll have to search for uninitialized pointers or things like that somewhere in your code. If possible, use BOCHS to locate the faulty instruction ...

Before touching anything in the source, take a snapshot of your working directory to make sure you can understand how the bug disappeared if you make it disappear "magically".

Re:Code Improvement?

Posted: Mon Jun 07, 2004 4:36 pm
by amirsadig
can you find the message in the final binary image (e.g. does the linker include *all* what it should) ? did you made sure the loader loaded the *whole* file into memory ?
your are correct about that. my linker did not copy all things, the code which indicate page fault, is not a part of the real code of the program. it's address is inside the code segment but the instruction is not like which one in the program code. I have notice that with the bochsdbg. Code Segment did not copyed correctly.

but the question why only this prgramm case problem?. I could load the first prgramm 10 times without problem.

Re:Code Improvement?

Posted: Tue Jun 08, 2004 3:18 am
by Pype.Clicker
there may be various reasons, all coming from a hackish-way to write the linker script. Under linux/ELF for instance, small strings and long strings do not go to the same .rodata section. If you didn't include .rodata*, it may play tricks on you...

Re:Code Improvement?

Posted: Tue Jun 08, 2004 4:42 am
by amirsadig
the program has 2 program header, the first PH include text and rodata. the rodata size is exactly the size of string.
the second PH is the BSS. and my linker see those 2 PH and give the correct size, address equal with which I have readed from command "readelf".

as I said before, the Code segment are not equal with the real code of my program ( when diassembled it). as I use bochsdbg and I run the program step by step. As I reached the point of calling strlen and jump to the address of strlen, there the instruction is not correct. it seems that code for strlen does not exist!!!!

note that I do not use any library, the strlen is build in the program as function.

Re:Code Improvement?

Posted: Tue Jun 08, 2004 9:51 am
by Pype.Clicker
could it be that the loader missed parts of your code ? that's unlikely if you use something like GRUB, but damn common with MyOwnBootLoader, unfortunately ... make sure everything is in place (using a checksum could be nice)