Answer the question
In order to leave comments, you need to log in
Hell (C, LKM, unusual bug)?
Faced with an inexplicable bug. Well, the fact that there may be an error in the code - maybe (most likely it is), but the symptoms are just not at all what you might expect. Just in case, the program is a kernel module for Linux, although it probably doesn't matter.
Bug:
1) the value of the variable (dir) suddenly changes
2) checks (if) somehow do not correspond to the new value.
Now the code below will show what it does.
Code (monitor the dir variable :-) ):
int canary1=0xdeadbeef;<br/>
int dir=666, dir2=666;<br/>
int canary2=0xdeadbeef;<br/>
<br/>
/* .... */<br/>
<br/>
if(!strcmp(dev->name,outif)){<br/>
dir=1;<br/>
}else{<br/>
dir=0;<br/>
}<br/>
<br/>
dir2=dir;<br/>
printk("dir %d dir2 %d\n",dir,dir2);<br/>
/* ... */<br/>
skb = dr_release(dir, &qempty);<br/>
<br/>
if(dir>1){<br/>
printk("bad dir %d after dr_release\n",dir);<br/>
}else{<br/>
printk("right after dr_release dir is %d (%d)\n",dir,dir2);<br/>
<br/>
if(dir>1){<br/>
printk("condition dir>1 matches\n");<br/>
}else{<br/>
printk("condition dir>1 NOT matches\n");<br/>
}<br/>
}<br/>
printk("before if dir: %d (0x%x 0x%x)\n",dir,canary1,canary2);<br/>
Answer the question
In order to leave comments, you need to log in
Somewhere spoil the memory.
> How can a bug in a function change the value of a local variable?
Elementary, the stack is one for all.
Definitely memory is being corrupted somewhere and/or the compiler is incorrectly rearranging the statements to be executed. I had a similar situation in C ++, it was solved by transferring variables from one memory area to another. Another stupid suggestion - maybe rename the dir variable?
Just the fact that dir>1 is not executed - this is not surprising - is the usual code optimization. The compiler saw that either 0 or 1 is assigned and none of these values is greater than 1, which means that the check can simply be thrown out. You can mark a variable as volatile to override optimizations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question