Skip to contentSkip to conversation

libfdt: Fix the undefined behavior in fdt_num_mem_rsv()

PATCH In review Bin Meng 2 participants 6 messages Mar 30, 2021 – Apr 20, 2021 5 messages not in the archive

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/1 libfdt: Fix the undefined behavior in fdt_num_mem_rsv()

With LLVM 10.0.0+, the following codes in fdt_num_mem_rsv() does not work any more for an fdt that is at address 0:

    for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
        if (fdt64_ld_(&re->size) == 0)
            return i;
    }

Due to LLVM's optimization engine utilizing a UB in C, the following code pattern:

    if ((pointer + offset) != NULL)

is transformed into:

    if (pointer != NULL)

because if pointer is NULL and offset is non-zero, the result of (pointer + offset) is UB. So LLVM is free to exploit such UB to perform some optimization.

In this case, fdt_mem_rsv() gets inlined and returns (pointer + offset). And LLVM in turns emits codes to check fdt against NULL, which won't work for fdt at address 0.

---

 libfdt/fdt_ro.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
libfdt/fdt_ro.c +13 -4
index 17584da..4db4013 100644
@@ -157,18 +157,26 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
 	return 0;
 }
 
-static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+static bool fdt_is_mem_rsv(const void *fdt, int n)
 {
 	unsigned int offset = n * sizeof(struct fdt_reserve_entry);
 	unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
 	if (!can_assume(VALID_INPUT)) {
 		if (absoffset < fdt_off_mem_rsvmap(fdt))
-			return NULL;
+			return false;
 		if (absoffset > fdt_totalsize(fdt) -
 		    sizeof(struct fdt_reserve_entry))
-			return NULL;
+			return false;
 	}
+
+	return true;
+}
+
+static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+{
+	if (!fdt_is_mem_rsv(fdt, n))
+		return NULL;
 	return fdt_mem_rsv_(fdt, n);
 }
 
@@ -191,7 +199,8 @@ int fdt_num_mem_rsv(const void *fdt)
 	int i;
 	const struct fdt_reserve_entry *re;
 
-	for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
+	for (i = 0; fdt_is_mem_rsv(fdt, i); i++) {
+		re = fdt_mem_rsv_(fdt, i);
 		if (fdt64_ld_(&re->size) == 0)
 			return i;
 	}
Signature · 1 line
2.25.1

A message that is not in the archive (sent off-list or not yet received): 20210330095645.515056-1-bmeng.cn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw
Quoted 17 lines · Bin Meng

With LLVM 10.0.0+, the following codes in fdt_num_mem_rsv() does not work any more for an fdt that is at address 0:

    for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
        if (fdt64_ld_(&re->size) == 0)
            return i;
    }

Due to LLVM's optimization engine utilizing a UB in C, the following code pattern:

    if ((pointer + offset) != NULL)

is transformed into:

    if (pointer != NULL)
Quoted 7 lines

because if pointer is NULL and offset is non-zero, the result of (pointer + offset) is UB. So LLVM is free to exploit such UB to perform some optimization.

In this case, fdt_mem_rsv() gets inlined and returns (pointer + offset). And LLVM in turns emits codes to check fdt against NULL, which won't work for fdt at address 0.

I don't think this really fixes anything. It might fool LLVM into doing what you need right now, but I don't see any reason to expect it will keep doing so.

IIUC, you're saying that the specific problem is that adding a non-zero offset to a NULL pointer is UB, which happens inside fdt_mem_rsv_() if n != 0. But with your patch, that UB still exists..

Quoted 42 lines

---

 libfdt/fdt_ro.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
libfdt/fdt_ro.c +11 -3
index 17584da..4db4013 100644
@@ -157,18 +157,26 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
 	return 0;
 }
 
-static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+static bool fdt_is_mem_rsv(const void *fdt, int n)
 {
 	unsigned int offset = n * sizeof(struct fdt_reserve_entry);
 	unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
 	if (!can_assume(VALID_INPUT)) {
 		if (absoffset < fdt_off_mem_rsvmap(fdt))
-			return NULL;
+			return false;
 		if (absoffset > fdt_totalsize(fdt) -
 		    sizeof(struct fdt_reserve_entry))
-			return NULL;
+			return false;
 	}
+
+	return true;
+}
+
+static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+{
+	if (!fdt_is_mem_rsv(fdt, n))
+		return NULL;
 	return fdt_mem_rsv_(fdt, n);
 }
 

libfdt/fdt_ro.c int fdt_num_mem_rsv(const void *fdt) · line 203

Quoted 6 lines
libfdt/fdt_ro.c +2 -1
@@ -191,7 +199,8 @@ int fdt_num_mem_rsv(const void *fdt)
 	int i;
 	const struct fdt_reserve_entry *re;
 
-	for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
+	for (i = 0; fdt_is_mem_rsv(fdt, i); i++) {
+		re = fdt_mem_rsv_(fdt, i);

.. here ^^.

Basically if your compiled is going to optimized based on (NULL + something) being UB, and the NULL pointer is address 0, that's fundamentally incompatible with storing a device tree at address 0.

Quoted 3 lines
libfdt/fdt_ro.c +0 -0
 		if (fdt64_ld_(&re->size) == 0)
 			return i;
 	}
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#4 lore raw

Hi David,

Quoted 23 lines · David Gibson
Quoted 21 lines · Bin Meng

With LLVM 10.0.0+, the following codes in fdt_num_mem_rsv() does not work any more for an fdt that is at address 0:

    for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
        if (fdt64_ld_(&re->size) == 0)
            return i;
    }

Due to LLVM's optimization engine utilizing a UB in C, the following code pattern:

    if ((pointer + offset) != NULL)

is transformed into:

    if (pointer != NULL)

because if pointer is NULL and offset is non-zero, the result of (pointer + offset) is UB. So LLVM is free to exploit such UB to perform some optimization.

Quoted 7 lines
Quoted 3 lines

In this case, fdt_mem_rsv() gets inlined and returns (pointer + offset). And LLVM in turns emits codes to check fdt against NULL, which won't work for fdt at address 0.

I don't think this really fixes anything. It might fool LLVM into doing what you need right now, but I don't see any reason to expect it will keep doing so.

This specific UB optimizer changes [1] have been merged in LLVM for at least 1.5 years, and it has been there since LLVM 10/11.

Quoted 5 lines

IIUC, you're saying that the specific problem is that adding a non-zero offset to a NULL pointer is UB, which happens inside fdt_mem_rsv_() if n != 0. But with your patch, that UB still exists..

The UB exists only when the fdt pointer is NULL.

Quoted 53 lines
Quoted 43 lines

---

 libfdt/fdt_ro.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
libfdt/fdt_ro.c +11 -3
index 17584da..4db4013 100644
@@ -157,18 +157,26 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
      return 0;
 }
 
-static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+static bool fdt_is_mem_rsv(const void *fdt, int n)
 {
      unsigned int offset = n * sizeof(struct fdt_reserve_entry);
      unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
      if (!can_assume(VALID_INPUT)) {
              if (absoffset < fdt_off_mem_rsvmap(fdt))
-                     return NULL;
+                     return false;
              if (absoffset > fdt_totalsize(fdt) -
                  sizeof(struct fdt_reserve_entry))
-                     return NULL;
+                     return false;
      }
+
+     return true;
+}
+
+static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+{
+     if (!fdt_is_mem_rsv(fdt, n))
+             return NULL;
      return fdt_mem_rsv_(fdt, n);
 }
 

libfdt/fdt_ro.c int fdt_num_mem_rsv(const void *fdt) · line 203

Quoted 6 lines
libfdt/fdt_ro.c +2 -1
@@ -191,7 +199,8 @@ int fdt_num_mem_rsv(const void *fdt)
      int i;
      const struct fdt_reserve_entry *re;
 
-     for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
+     for (i = 0; fdt_is_mem_rsv(fdt, i); i++) {
+             re = fdt_mem_rsv_(fdt, i);

.. here ^^.

Quoted 3 lines

Basically if your compiled is going to optimized based on (NULL + something) being UB, and the NULL pointer is address 0, that's fundamentally incompatible with storing a device tree at address 0.

As long as we don't put (pointer + offset) into a statement of flow control, it is fine. You can still get the correct value of (pointer + offset) when pointer is NULL and yes, it is still a UB but we can expect such usage is safe.

Quoted 5 lines

libfdt/fdt_ro.c int fdt_num_mem_rsv(const void *fdt) · line 206

Quoted 4 lines
libfdt/fdt_ro.c +0 -0
              if (fdt64_ld_(&re->size) == 0)
                      return i;
      }

PS: I added Fangrui who is an LLVM developer to this loop and he can provide a better explanation than me from the LLVM perspective.

[1] https://reviews.llvm.org/D66608

Regards,
Bin

A message that is not in the archive (sent off-list or not yet received): CAEUhbmVsszFcUC8tEYmJ4djPvyDmXspDg-gb9rYZpm8zWwxc1g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#6 lore raw
Quoted 31 lines · Bin Meng

Hi David,

Quoted 27 lines · David Gibson
Quoted 24 lines · Bin Meng

With LLVM 10.0.0+, the following codes in fdt_num_mem_rsv() does not work any more for an fdt that is at address 0:

    for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
        if (fdt64_ld_(&re->size) == 0)
            return i;
    }

Due to LLVM's optimization engine utilizing a UB in C, the following code pattern:

    if ((pointer + offset) != NULL)

is transformed into:

    if (pointer != NULL)

because if pointer is NULL and offset is non-zero, the result of (pointer + offset) is UB. So LLVM is free to exploit such UB to perform some optimization.

In this case, fdt_mem_rsv() gets inlined and returns (pointer + offset). And LLVM in turns emits codes to check fdt against NULL, which won't work for fdt at address 0.

Quoted 6 lines
Quoted 3 lines

I don't think this really fixes anything. It might fool LLVM into doing what you need right now, but I don't see any reason to expect it will keep doing so.

This specific UB optimizer changes [1] have been merged in LLVM for at least 1.5 years, and it has been there since LLVM 10/11.

Ok, but that doesn't change the basic fact that this is just fooling the compiler into behaving differently - it doesn't actually remove the UB.

Quoted 6 lines
Quoted 4 lines

IIUC, you're saying that the specific problem is that adding a non-zero offset to a NULL pointer is UB, which happens inside fdt_mem_rsv_() if n != 0. But with your patch, that UB still exists..

The UB exists only when the fdt pointer is NULL.

Yes...

Quoted 51 lines
Quoted 51 lines
Quoted 41 lines

---

 libfdt/fdt_ro.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
libfdt/fdt_ro.c +11 -3
index 17584da..4db4013 100644
@@ -157,18 +157,26 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
      return 0;
 }
 
-static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+static bool fdt_is_mem_rsv(const void *fdt, int n)
 {
      unsigned int offset = n * sizeof(struct fdt_reserve_entry);
      unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
      if (!can_assume(VALID_INPUT)) {
              if (absoffset < fdt_off_mem_rsvmap(fdt))
-                     return NULL;
+                     return false;
              if (absoffset > fdt_totalsize(fdt) -
                  sizeof(struct fdt_reserve_entry))
-                     return NULL;
+                     return false;
      }
+
+     return true;
+}
+
+static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+{
+     if (!fdt_is_mem_rsv(fdt, n))
+             return NULL;
      return fdt_mem_rsv_(fdt, n);
 }
 

libfdt/fdt_ro.c int fdt_num_mem_rsv(const void *fdt) · line 203

Quoted 6 lines
libfdt/fdt_ro.c +2 -1
@@ -191,7 +199,8 @@ int fdt_num_mem_rsv(const void *fdt)
      int i;
      const struct fdt_reserve_entry *re;
 
-     for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
+     for (i = 0; fdt_is_mem_rsv(fdt, i); i++) {
+             re = fdt_mem_rsv_(fdt, i);

.. here ^^.

Quoted 8 lines
Quoted 3 lines

Basically if your compiled is going to optimized based on (NULL + something) being UB, and the NULL pointer is address 0, that's fundamentally incompatible with storing a device tree at address 0.

As long as we don't put (pointer + offset) into a statement of flow control, it is fine. You can still get the correct value of (pointer + offset) when pointer is NULL and yes, it is still a UB but we can expect such usage is safe.

People always say that about UB until it isn't. This is just way to subtle a distinction for me to be comfortable relying on it.

Fundamentally the problem is that you want to store your fdt at address zero, but that is just not compatible with your compiler, since it considers that a NULL pointer.

Quoted 13 lines
Quoted 4 lines

libfdt/fdt_ro.c int fdt_num_mem_rsv(const void *fdt) · line 206

Quoted 3 lines
libfdt/fdt_ro.c +0 -0
              if (fdt64_ld_(&re->size) == 0)
                      return i;
      }

PS: I added Fangrui who is an LLVM developer to this loop and he can provide a better explanation than me from the LLVM perspective.

[1] https://reviews.llvm.org/D66608

Regards,
Bin

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

A message that is not in the archive (sent off-list or not yet received): YG5wZa/[email protected]

author
#8 lore raw

Hi David,

Quoted 37 lines · David Gibson
Quoted 35 lines · Bin Meng

Hi David,

Quoted 30 lines · David Gibson
Quoted 17 lines · Bin Meng

With LLVM 10.0.0+, the following codes in fdt_num_mem_rsv() does not work any more for an fdt that is at address 0:

    for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
        if (fdt64_ld_(&re->size) == 0)
            return i;
    }

Due to LLVM's optimization engine utilizing a UB in C, the following code pattern:

    if ((pointer + offset) != NULL)

is transformed into:

    if (pointer != NULL)
Quoted 7 lines

because if pointer is NULL and offset is non-zero, the result of (pointer + offset) is UB. So LLVM is free to exploit such UB to perform some optimization.

In this case, fdt_mem_rsv() gets inlined and returns (pointer + offset). And LLVM in turns emits codes to check fdt against NULL, which won't work for fdt at address 0.

I don't think this really fixes anything. It might fool LLVM into doing what you need right now, but I don't see any reason to expect it will keep doing so.

Quoted 6 lines
Quoted 2 lines

This specific UB optimizer changes [1] have been merged in LLVM for at least 1.5 years, and it has been there since LLVM 10/11.

Ok, but that doesn't change the basic fact that this is just fooling the compiler into behaving differently - it doesn't actually remove the UB.

As I explained, it removes the pre-condition of the UB optimizer effect. The UB of (pointer + offset) when pointer is NULL, is safe as long as it is not in the statement of flow control.

Quoted 70 lines
Quoted 7 lines
Quoted 5 lines

IIUC, you're saying that the specific problem is that adding a non-zero offset to a NULL pointer is UB, which happens inside fdt_mem_rsv_() if n != 0. But with your patch, that UB still exists..

The UB exists only when the fdt pointer is NULL.

Yes...

Quoted 51 lines
Quoted 51 lines
Quoted 41 lines

---

 libfdt/fdt_ro.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
libfdt/fdt_ro.c +11 -3
index 17584da..4db4013 100644
@@ -157,18 +157,26 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
      return 0;
 }
 
-static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+static bool fdt_is_mem_rsv(const void *fdt, int n)
 {
      unsigned int offset = n * sizeof(struct fdt_reserve_entry);
      unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
      if (!can_assume(VALID_INPUT)) {
              if (absoffset < fdt_off_mem_rsvmap(fdt))
-                     return NULL;
+                     return false;
              if (absoffset > fdt_totalsize(fdt) -
                  sizeof(struct fdt_reserve_entry))
-                     return NULL;
+                     return false;
      }
+
+     return true;
+}
+
+static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+{
+     if (!fdt_is_mem_rsv(fdt, n))
+             return NULL;
      return fdt_mem_rsv_(fdt, n);
 }
 

libfdt/fdt_ro.c int fdt_num_mem_rsv(const void *fdt) · line 203

Quoted 6 lines
libfdt/fdt_ro.c +2 -1
@@ -191,7 +199,8 @@ int fdt_num_mem_rsv(const void *fdt)
      int i;
      const struct fdt_reserve_entry *re;
 
-     for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
+     for (i = 0; fdt_is_mem_rsv(fdt, i); i++) {
+             re = fdt_mem_rsv_(fdt, i);

.. here ^^.

Quoted 8 lines
Quoted 3 lines

Basically if your compiled is going to optimized based on (NULL + something) being UB, and the NULL pointer is address 0, that's fundamentally incompatible with storing a device tree at address 0.

As long as we don't put (pointer + offset) into a statement of flow control, it is fine. You can still get the correct value of (pointer + offset) when pointer is NULL and yes, it is still a UB but we can expect such usage is safe.

Quoted 5 lines

People always say that about UB until it isn't. This is just way to subtle a distinction for me to be comfortable relying on it.

Fundamentally the problem is that you want to store your fdt at address zero, but that is just not compatible with your compiler,

I am not sure it is a compatibility issue when language layers were defining the C spec without considering the fact that RAM address zero is absolutely OK on some systems. In normal programming environment we don't store data at address zero so NULL can be used as an indication of invalid pointer, but this practice may not apply on low-level environment like for example, when bootloader passes the FDT at address zero for kernel to handle it, or a bootloader environment where FDT is placed at a ROM address 0 ...

Quoted 1 line

since it considers that a NULL pointer.

Regards,
Bin

A message that is not in the archive (sent off-list or not yet received): CAEUhbmWeTpRHPdqMtcNWdF-ORJ0OXcMi1YJeN+KUpbkqGKvFGg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#10 lore raw

On Thu, Apr 08, 2021 at 11:21:52AM +0800, Bin Meng wrote:
65;6203;1c> Hi David,

Quoted 43 lines
Quoted 40 lines · David Gibson
Quoted 37 lines · Bin Meng

Hi David,

Quoted 23 lines · David Gibson
Quoted 21 lines · Bin Meng

With LLVM 10.0.0+, the following codes in fdt_num_mem_rsv() does not work any more for an fdt that is at address 0:

    for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
        if (fdt64_ld_(&re->size) == 0)
            return i;
    }

Due to LLVM's optimization engine utilizing a UB in C, the following code pattern:

    if ((pointer + offset) != NULL)

is transformed into:

    if (pointer != NULL)

because if pointer is NULL and offset is non-zero, the result of (pointer + offset) is UB. So LLVM is free to exploit such UB to perform some optimization.

Quoted 7 lines
Quoted 3 lines

In this case, fdt_mem_rsv() gets inlined and returns (pointer + offset). And LLVM in turns emits codes to check fdt against NULL, which won't work for fdt at address 0.

I don't think this really fixes anything. It might fool LLVM into doing what you need right now, but I don't see any reason to expect it will keep doing so.

This specific UB optimizer changes [1] have been merged in LLVM for at least 1.5 years, and it has been there since LLVM 10/11.

Quoted 7 lines
Quoted 3 lines

Ok, but that doesn't change the basic fact that this is just fooling the compiler into behaving differently - it doesn't actually remove the UB.

As I explained, it removes the pre-condition of the UB optimizer effect. The UB of (pointer + offset) when pointer is NULL, is safe as long as it is not in the statement of flow control.

Yes, I understand what it's doing. But the fact remains you're not "fixing" any UB at all - the exact same UB is still there, you're just working around the specific behaviour of LLVM's optimizer.

Quoted 77 lines
Quoted 71 lines
Quoted 8 lines
Quoted 6 lines

IIUC, you're saying that the specific problem is that adding a non-zero offset to a NULL pointer is UB, which happens inside fdt_mem_rsv_() if n != 0. But with your patch, that UB still exists..

The UB exists only when the fdt pointer is NULL.

Yes...

Quoted 51 lines
Quoted 51 lines
Quoted 41 lines

---

 libfdt/fdt_ro.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
libfdt/fdt_ro.c +11 -3
index 17584da..4db4013 100644
@@ -157,18 +157,26 @@ int fdt_generate_phandle(const void *fdt, uint32_t *phandle)
      return 0;
 }
 
-static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+static bool fdt_is_mem_rsv(const void *fdt, int n)
 {
      unsigned int offset = n * sizeof(struct fdt_reserve_entry);
      unsigned int absoffset = fdt_off_mem_rsvmap(fdt) + offset;
 
      if (!can_assume(VALID_INPUT)) {
              if (absoffset < fdt_off_mem_rsvmap(fdt))
-                     return NULL;
+                     return false;
              if (absoffset > fdt_totalsize(fdt) -
                  sizeof(struct fdt_reserve_entry))
-                     return NULL;
+                     return false;
      }
+
+     return true;
+}
+
+static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n)
+{
+     if (!fdt_is_mem_rsv(fdt, n))
+             return NULL;
      return fdt_mem_rsv_(fdt, n);
 }
 

libfdt/fdt_ro.c int fdt_num_mem_rsv(const void *fdt) · line 203

Quoted 6 lines
libfdt/fdt_ro.c +2 -1
@@ -191,7 +199,8 @@ int fdt_num_mem_rsv(const void *fdt)
      int i;
      const struct fdt_reserve_entry *re;
 
-     for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
+     for (i = 0; fdt_is_mem_rsv(fdt, i); i++) {
+             re = fdt_mem_rsv_(fdt, i);

.. here ^^.

Quoted 8 lines
Quoted 3 lines

Basically if your compiled is going to optimized based on (NULL + something) being UB, and the NULL pointer is address 0, that's fundamentally incompatible with storing a device tree at address 0.

As long as we don't put (pointer + offset) into a statement of flow control, it is fine. You can still get the correct value of (pointer + offset) when pointer is NULL and yes, it is still a UB but we can expect such usage is safe.

Quoted 5 lines

People always say that about UB until it isn't. This is just way to subtle a distinction for me to be comfortable relying on it.

Fundamentally the problem is that you want to store your fdt at address zero, but that is just not compatible with your compiler,

Quoted 8 lines

I am not sure it is a compatibility issue when language layers were defining the C spec without considering the fact that RAM address zero is absolutely OK on some systems. In normal programming environment we don't store data at address zero so NULL can be used as an indication of invalid pointer, but this practice may not apply on low-level environment like for example, when bootloader passes the FDT at address zero for kernel to handle it, or a bootloader environment where FDT is placed at a ROM address 0 ...

Yes. A C implementation using address 0 as a NULL pointer is incompatible with manipulating real data at address 0; that's exactly what I'm saying. Historically people have gotten away with it because the compiler hasn't usually optimized this way, but as compilers get smarter it has broken.

This change is just a really non-obvious workaround for a specific compiler optimizer.

To fix this properly you need to either:
* Use asm code to copy the fdt to a different address, then use C

or

* Tell your compiler to be dumber and not apply this optimization.

Quoted 5 lines
Quoted 1 line

since it considers that a NULL pointer.

Regards,
Bin

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson