All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fix implicit size of unsized arrays
@ 2017-12-28 15:07 Luc Van Oostenryck
  2017-12-28 21:02 ` Dibyendu Majumdar
  0 siblings, 1 reply; 7+ messages in thread
From: Luc Van Oostenryck @ 2017-12-28 15:07 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

When an array is declared without an explicit size. In this case,
an implicit size is given by the number of elements in its initializer
if one is present.

Currently, in sparse, this implicit size is only associated with
the node corresponding to the initializer while the base type is
left unsized. This is a problem because the node is only used for
the modifiers & address-space and the bitsize of nodes are expected
to match the size of the basetype. So this implicit size can be used
for when directly using the bit_size of the node but the array is
still left, essentially unsized.

It's not enough to simply copy the bitsize of the node to the base
type because:
1) sym->array_size need to be set in the node & the base type.
2) the base type can be shared between several declarators.
It's thus needed to copy the the base type to unshare it before
setting the sym->array_size.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---

This patch is available in the Git repository at:
  git://github.com/lucvoo/sparse-dev.git size-unsized-arrays

 symbol.c                         | 19 ++++++++++++++++++-
 validation/array-implicit-size.c | 26 ++++++++++++++++++++++++++
 validation/constexpr-preop.c     |  2 ++
 3 files changed, 46 insertions(+), 1 deletion(-)
 create mode 100644 validation/array-implicit-size.c

diff --git a/symbol.c b/symbol.c
index 2f4afd515..61d1acbea 100644
--- a/symbol.c
+++ b/symbol.c
@@ -364,6 +364,23 @@ static struct expression *get_symbol_initializer(struct symbol *sym)
 	return NULL;
 }
 
+static unsigned int implicit_array_size(struct symbol *node, unsigned int count)
+{
+	struct symbol *arr_ori = node->ctype.base_type;
+	struct symbol *arr_new = alloc_symbol(node->pos, SYM_ARRAY);
+	struct symbol *elem_type = arr_ori->ctype.base_type;
+	struct expression *size = alloc_const_expression(node->pos, count);
+	unsigned int bit_size = array_element_offset(elem_type->bit_size, count);
+
+	*arr_new = *arr_ori;
+	arr_new->bit_size = bit_size;
+	arr_new->array_size = size;
+	node->array_size = size;
+	node->ctype.base_type = arr_new;
+
+	return bit_size;
+}
+
 static struct symbol * examine_node_type(struct symbol *sym)
 {
 	struct symbol *base_type = examine_base_type(sym);
@@ -393,7 +410,7 @@ static struct symbol * examine_node_type(struct symbol *sym)
 			int count = count_array_initializer(node_type, initializer);
 
 			if (node_type && node_type->bit_size >= 0)
-				bit_size = array_element_offset(node_type->bit_size, count);
+				bit_size = implicit_array_size(sym, count);
 		}
 	}
 	
diff --git a/validation/array-implicit-size.c b/validation/array-implicit-size.c
new file mode 100644
index 000000000..7011008b6
--- /dev/null
+++ b/validation/array-implicit-size.c
@@ -0,0 +1,26 @@
+static int array[] = { 0, 1, 2, 3, };
+_Static_assert(sizeof(array) == 4 * sizeof(int), "size of array");
+
+
+typedef int table_t[];
+static table_t tbl2 = {
+	0,
+	1,
+};
+_Static_assert(sizeof(tbl2) == 2 * sizeof(int), "size of tbl2");
+
+static table_t tbl1 = {
+	0,
+};
+_Static_assert(sizeof(tbl1) == 1 * sizeof(int), "size of tbl1");
+
+static table_t tbl3 = {
+	0,
+	1,
+	2,
+};
+_Static_assert(sizeof(tbl3) == 3 * sizeof(int), "size of tbl3");
+
+/*
+ * check-name: array-implicit-size
+ */
diff --git a/validation/constexpr-preop.c b/validation/constexpr-preop.c
index 4b54defd9..3fd577459 100644
--- a/validation/constexpr-preop.c
+++ b/validation/constexpr-preop.c
@@ -25,5 +25,7 @@ constexpr-preop.c:8:4: error: bad constant expression
 constexpr-preop.c:9:4: error: bad constant expression
 constexpr-preop.c:14:4: error: bad integer constant expression
 constexpr-preop.c:15:4: error: bad integer constant expression
+constexpr-preop.c:10:4: error: index out of bounds in initializer
+constexpr-preop.c:11:4: error: index out of bounds in initializer
  * check-error-end
  */
-- 
2.15.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix implicit size of unsized arrays
  2017-12-28 15:07 [PATCH] fix implicit size of unsized arrays Luc Van Oostenryck
@ 2017-12-28 21:02 ` Dibyendu Majumdar
  2017-12-28 21:19   ` Luc Van Oostenryck
  0 siblings, 1 reply; 7+ messages in thread
From: Dibyendu Majumdar @ 2017-12-28 21:02 UTC (permalink / raw)
  To: Luc Van Oostenryck; +Cc: Linux-Sparse

Hi Luc,

On 28 December 2017 at 15:07, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> When an array is declared without an explicit size. In this case,
> an implicit size is given by the number of elements in its initializer
> if one is present.
>
> Currently, in sparse, this implicit size is only associated with
> the node corresponding to the initializer while the base type is
> left unsized. This is a problem because the node is only used for
> the modifiers & address-space and the bitsize of nodes are expected
> to match the size of the basetype. So this implicit size can be used
> for when directly using the bit_size of the node but the array is
> still left, essentially unsized.
>

Previous thread on this issue:

https://www.spinics.net/lists/linux-sparse/msg05427.html

Regards
Dibyendu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix implicit size of unsized arrays
  2017-12-28 21:02 ` Dibyendu Majumdar
@ 2017-12-28 21:19   ` Luc Van Oostenryck
  2017-12-28 21:20     ` Dibyendu Majumdar
  0 siblings, 1 reply; 7+ messages in thread
From: Luc Van Oostenryck @ 2017-12-28 21:19 UTC (permalink / raw)
  To: Dibyendu Majumdar; +Cc: Linux-Sparse

On Thu, Dec 28, 2017 at 09:02:27PM +0000, Dibyendu Majumdar wrote:
> Hi Luc,
> 
> Previous thread on this issue:
> 
> https://www.spinics.net/lists/linux-sparse/msg05427.html

Ah yes, sorry, I forgot you already specifically reported this one.
 
It should be fixed now.

-- Luc

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix implicit size of unsized arrays
  2017-12-28 21:19   ` Luc Van Oostenryck
@ 2017-12-28 21:20     ` Dibyendu Majumdar
  2017-12-28 21:40       ` Luc Van Oostenryck
  0 siblings, 1 reply; 7+ messages in thread
From: Dibyendu Majumdar @ 2017-12-28 21:20 UTC (permalink / raw)
  To: Luc Van Oostenryck; +Cc: Linux-Sparse

Hi Luc,

On 28 December 2017 at 21:19, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> On Thu, Dec 28, 2017 at 09:02:27PM +0000, Dibyendu Majumdar wrote:
>> Previous thread on this issue:
>>
>> https://www.spinics.net/lists/linux-sparse/msg05427.html
>
> Ah yes, sorry, I forgot you already specifically reported this one.
>
> It should be fixed now.
>

Well last time I thought the view was that a fix was not needed? What's changed?

Regards
Dibyendu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix implicit size of unsized arrays
  2017-12-28 21:20     ` Dibyendu Majumdar
@ 2017-12-28 21:40       ` Luc Van Oostenryck
  2017-12-28 21:44         ` Dibyendu Majumdar
  0 siblings, 1 reply; 7+ messages in thread
From: Luc Van Oostenryck @ 2017-12-28 21:40 UTC (permalink / raw)
  To: Dibyendu Majumdar; +Cc: Linux-Sparse

On Thu, Dec 28, 2017 at 09:20:41PM +0000, Dibyendu Majumdar wrote:
> Hi Luc,
> 
> On 28 December 2017 at 21:19, Luc Van Oostenryck
> <luc.vanoostenryck@gmail.com> wrote:
> > On Thu, Dec 28, 2017 at 09:02:27PM +0000, Dibyendu Majumdar wrote:
> >> Previous thread on this issue:
> >>
> >> https://www.spinics.net/lists/linux-sparse/msg05427.html
> >
> > Ah yes, sorry, I forgot you already specifically reported this one.
> >
> > It should be fixed now.
> >
> 
> Well last time I thought the view was that a fix was not needed? What's changed?

Another case which led to a better understanding of the situation.

Note: I'm not very happy with the current fix because I think that
      array_size (which is an expression) should not be used after
      examination but thing is that currently array_size is used
      here and there.

-- Luc

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix implicit size of unsized arrays
  2017-12-28 21:40       ` Luc Van Oostenryck
@ 2017-12-28 21:44         ` Dibyendu Majumdar
  2017-12-28 22:10           ` Luc Van Oostenryck
  0 siblings, 1 reply; 7+ messages in thread
From: Dibyendu Majumdar @ 2017-12-28 21:44 UTC (permalink / raw)
  To: Luc Van Oostenryck; +Cc: Linux-Sparse

Hi Luc,

On 28 December 2017 at 21:40, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> On Thu, Dec 28, 2017 at 09:20:41PM +0000, Dibyendu Majumdar wrote:
>> On 28 December 2017 at 21:19, Luc Van Oostenryck
>> <luc.vanoostenryck@gmail.com> wrote:
>> > On Thu, Dec 28, 2017 at 09:02:27PM +0000, Dibyendu Majumdar wrote:
>> >> Previous thread on this issue:
>> >>
>> >> https://www.spinics.net/lists/linux-sparse/msg05427.html
>> >
>> > Ah yes, sorry, I forgot you already specifically reported this one.
>> >
>> > It should be fixed now.
>> >
>>
>> Well last time I thought the view was that a fix was not needed? What's changed?
>
> Another case which led to a better understanding of the situation.
>

I understood that the correct solution was for the symbol to hold the size.

https://www.spinics.net/lists/linux-sparse/msg05442.html
https://www.spinics.net/lists/linux-sparse/msg05444.html


Regards
Dibyendu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] fix implicit size of unsized arrays
  2017-12-28 21:44         ` Dibyendu Majumdar
@ 2017-12-28 22:10           ` Luc Van Oostenryck
  0 siblings, 0 replies; 7+ messages in thread
From: Luc Van Oostenryck @ 2017-12-28 22:10 UTC (permalink / raw)
  To: Dibyendu Majumdar; +Cc: Linux-Sparse

On Thu, Dec 28, 2017 at 09:44:25PM +0000, Dibyendu Majumdar wrote:
> Hi Luc,
> 
> On 28 December 2017 at 21:40, Luc Van Oostenryck
> <luc.vanoostenryck@gmail.com> wrote:
> > On Thu, Dec 28, 2017 at 09:20:41PM +0000, Dibyendu Majumdar wrote:
> >> On 28 December 2017 at 21:19, Luc Van Oostenryck
> >> <luc.vanoostenryck@gmail.com> wrote:
> >> > On Thu, Dec 28, 2017 at 09:02:27PM +0000, Dibyendu Majumdar wrote:
> >> >> Previous thread on this issue:
> >> >>
> >> >> https://www.spinics.net/lists/linux-sparse/msg05427.html
> >> >
> >> > Ah yes, sorry, I forgot you already specifically reported this one.
> >> >
> >> > It should be fixed now.
> >> >
> >>
> >> Well last time I thought the view was that a fix was not needed? What's changed?
> >
> > Another case which led to a better understanding of the situation.
> >
> 
> I understood that the correct solution was for the symbol to hold the size.
> 
> https://www.spinics.net/lists/linux-sparse/msg05442.html
> https://www.spinics.net/lists/linux-sparse/msg05444.html

The situation is:
1) yes, all what's really needed is present in SYM_NODE::bit_size
2) but some part of the code use SYM_ARRAY::bit_size or
   SYM_ARRAY::array_size
3) the implicit size of unsized arrays is really a special case
   that's doesn't follow the normal handling of SYM_NODE and
   the corresponding base type
2) the SYM_ARRAY must not be updated because it can be shared
   between several nodes with different size (like in the testcase
   I joined with the fix)
So duplicating the SYM_ARRAY to unshare it then updating it
seems to be what is needed although I don't find it very clean.
Not all of this was understood in March.

Regards,
Luc

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-12-28 22:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-28 15:07 [PATCH] fix implicit size of unsized arrays Luc Van Oostenryck
2017-12-28 21:02 ` Dibyendu Majumdar
2017-12-28 21:19   ` Luc Van Oostenryck
2017-12-28 21:20     ` Dibyendu Majumdar
2017-12-28 21:40       ` Luc Van Oostenryck
2017-12-28 21:44         ` Dibyendu Majumdar
2017-12-28 22:10           ` Luc Van Oostenryck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.