[LLVMdev] Another memory fun

Bill Wendling isanbard at gmail.com
Sun Jan 6 16:37:36 PST 2008


On Jan 6, 2008, at 1:20 PM, Zalunin Pavel wrote:

> but why this code don't work:
>
It does work, but you wrote code that violates the C standard and,  
therefore, it has undefined behavior--code that compiles and code  
that actually works are two separate things. For instance, on my  
machine, it produces code that looks like this in the DATA section:

         .data
_.str1:                         ; '.str1'
         .asciz  "world"

_.str2:                         ; '.str2'
         .asciz  "hello "

_.str7:                         ; '.str7'
         .asciz  "father"

_.str8:                         ; '.str8'
         .asciz  "mother "

With the first strcat, you overwrote the "father" string ("_.str7" in  
this example) with the "world" string ("_.str1" here). Boom! instant  
undefined behavior. You're lucky; it could have resulted in  
reformatting your hard drive. :-)

-bw

> ; ModuleID = 'sample.lz'
> @.str1 = internal global [6 x i8] c"world\00"           ; <[6 x i8] 
> *> [#uses=1]
> @.str2 = internal global [7 x i8] c"hello \00"          ; <[7 x i8] 
> *> [#uses=1]
> @.str7 = internal global [7 x i8] c"father\00"          ; <[7 x i8] 
> *> [#uses=1]
> @.str8 = internal global [8 x i8] c"mother \00"         ; <[8 x i8] 
> *> [#uses=1]
>
> declare i32 @puts(i8*)
>
> declare i8* @strcat(i8*, i8*)
>
> declare i32 @strlen(i8*)
>
> declare void @llvm.memcpy.i32(i8*, i8*, i32, i32)
>
> define i32 @main() {
> mainBlock:
>         %str3 = getelementptr [7 x i8]* @.str2, i64 0, i64  
> 0            ; <i8*> [#uses=2]
>         %str4 = getelementptr [6 x i8]* @.str1, i64 0, i64  
> 0            ; <i8*> [#uses=1]
>         call i8* @strcat( i8* %str3, i8* %str4 )                ;  
> <i8*>:0 [#uses=0]
>         %tmp6 = call i32 @puts( i8* %str3 )             ; <i32>  
> [#uses=0]
>         %str9 = getelementptr [8 x i8]* @.str8, i64 0, i64  
> 0            ; <i8*> [#uses=2]
>         %str10 = getelementptr [7 x i8]* @.str7, i64 0, i64  
> 0           ; <i8*> [#uses=1]
>         call i8* @strcat( i8* %str9, i8* %str10 )               ;  
> <i8*>:1 [#uses=0]
>         %tmp12 = call i32 @puts( i8* %str9 )            ; <i32>  
> [#uses=0]
>         ret i32 0
> }
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list