Thank you. (I just used another email account and forgot to add nickname in 
previous email.)


After inspecting the code more deeply, I found another way to fix this bug.


Most functions in tccgen.c ,which modify the vstack , call vcheck_cmp() at 
the beginning (like "vsetc" ,"vswap", "vrotb" etc.). but "vpushv" seem to omit 
it. So the below patch should also work.


diff --git a/tccgen.c b/tccgen.c
index c36032a..dc6dd9d 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -1473,6 +1473,7 @@ ST_FUNC void vpushv(SValue *v)
 {
     if (vtop >= vstack + (VSTACK_SIZE - 1))
         tcc_error("memory full (vstack)");
+   vcheck_cmp();
     vtop++;
     *vtop = *v;
 }



I prefer latter patch, add vcheck_cmp() just like other vstack-related 
functions did.


------------------ Original ------------------
From:                                                                           
                                             "jullien"                          
                                                          <[email protected]&gt;;
Date:&nbsp;Sat, Jun 19, 2021 10:56 PM
To:&nbsp;"jullien"<[email protected]&gt;;"tinycc-devel"<[email protected]&gt;;

Subject:&nbsp;Re: [Tinycc-devel] Segfault on arm64 when making a function call 
with many arguments



 
All parameters go to the right arguments. So patch looks good

&nbsp;

#include <stdio.h&gt;

&nbsp;

void map_add(int x0, int x1, int x2, int x3, int x4, int x5, int x6, int x7, in\

t x8) {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x0);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x1);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x2);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x3);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x4);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x5);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x6);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x7);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; printf("%d ", x8);

}

&nbsp; void main() {

&nbsp; &nbsp; int A = 1;

&nbsp; &nbsp; int B = 2;

&nbsp; &nbsp; map_add(10, 20, 30, 40, 50, 60, 70, 80, A &amp;&amp; B);

&nbsp; }

&nbsp;

$ &nbsp;tcc foo.c -o foo &amp;&amp; ./foo;echo

10 20 30 40 50 60 70 80 1

&nbsp;

&nbsp;

From: Tinycc-devel [mailto:[email protected]] On 
Behalf Of Christian Jullien
Sent: Saturday, June 19, 2021 08:57
To: [email protected]
Subject: Re: [Tinycc-devel] Segfault on arm64 when making a function call with 
many arguments



&nbsp;

Hi just quickly tested on my RPi arm64.

I don??t know if it works, i.e. all arguments go to the right parameter with 
the right value but, at least it no longer segfault.

&nbsp;

I??ll make more tests today.

&nbsp;

C.

&nbsp;

From: Tinycc-devel [mailto:[email protected]] On 
Behalf Of pursuer2 via Tinycc-devel
Sent: Saturday, June 19, 2021 08:28
To: jullien; tinycc-devel
Cc: pursuer2
Subject: Re: [Tinycc-devel] Segfault on arm64 when making a function call with 
many arguments

&nbsp;

This bug may caused by "vpushv" SValue with VT_CMP flag. There should be only 
one VT_CMP SValue on vstack. 


I make below patch to fix it, then the compilation&nbsp;exit normally. But I 
have no arm64 device with GNU/Linux to verify the test. 


&nbsp;


diff --git a/arm64-gen.c b/arm64-gen.c


index 6389409..a9cbfa2 100644


--- a/arm64-gen.c


+++ b/arm64-gen.c


@@ -1017,6 +1017,9 @@ ST_FUNC void gfunc_call(int nb_args)


&nbsp; &nbsp; &nbsp;if (stack &gt;&gt; 12)


&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;o(0xd14003ff | (stack &gt;&gt; 
12) << 10);


&nbsp;


+&nbsp; &nbsp; if((vtop-&gt;r&amp;VT_VALMASK)==VT_CMP){


+&nbsp; &nbsp; &nbsp; &nbsp; gv(RC_INT);


+&nbsp; &nbsp; }


&nbsp; &nbsp; &nbsp;// First pass: set all values on stack


&nbsp; &nbsp; &nbsp;for (i = nb_args; i; i--) {


&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vpushv(vtop - nb_args + i);



&nbsp;


&nbsp;


------------------ Original ------------------


From: "jullien" <[email protected]&gt;;


Date:&nbsp;Fri, Jun 18, 2021 04:04 PM


To:&nbsp;"tinycc-devel"<[email protected]&gt;;


Subject:&nbsp;Re: [Tinycc-devel] Segfault on arm64 when making a function call 
with many arguments



&nbsp;


I confirm it fails on arm64 (but works on arm 32bits).

It also fails with complete prototype:

&nbsp;

void map_add(int a, int b, int c, int d, int e, int f, int g, int h, int i)

{}

&nbsp;

C.

&nbsp;

From: Tinycc-devel [mailto:[email protected]] On 
Behalf Of Arthur Williams
Sent: Wednesday, June 16, 2021 20:18
To: [email protected]
Subject: [Tinycc-devel] Segfault on arm64 when making a function call with many 
arguments

&nbsp;

Was trying to compile vim with tcc on arm64 but got a segfault. I can simplify 
it to the following case:

&nbsp; void map_add(){}
&nbsp; void main() {
&nbsp; &nbsp; int A;
&nbsp; &nbsp; int B;
&nbsp; &nbsp; map_add(0, 0, 0, 0, 0, 0, 0, 0, A &amp;&amp; B); // segfaults 
when compiling

&nbsp; }


&nbsp;


The bad pointer was generated in arm64-gen.c::gsym_addr and the actual segfault 
occurred in tcc.h::read16le.


Removing one of the 0s or removing A/B or replacing A &amp;&amp; B with a 
constant avoids the problem. Cannot repro on x86. I'm running musl on Linux and 
using the latest tcc from mob.


&nbsp;
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to