Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit casts to mismatched pointer types in rsl_lite #2062

Merged
merged 4 commits into from
Sep 16, 2024

Conversation

islas
Copy link
Collaborator

@islas islas commented Jun 10, 2024

TYPE: bug fix

KEYWORDS: gcc14, compilation, c99

SOURCE: internal

DESCRIPTION OF CHANGES:
Problem:
From #2047 :
Compilation of WRF v4.6.0 fails with GCC 14 due to new standard changes

Type checking on pointer types (-Werror=incompatible-pointer-types)
GCC no longer allows implicitly casting all pointer types to all other pointer types. This behavior is now restricted to the void * type and its qualified variations.

To fix compilation errors resulting from that, you can add the appropriate casts, and maybe consider using void * in more places (particularly for old programs that predate the introduction of void * into the C language).

Programs that do not carefully track pointer types are likely to contain aliasing violations, so consider building with -fno-strict-aliasing. (Whether casts are written manually or performed by GCC automatically does not make a difference in terms of strict aliasing violations.)

A frequent source of incompatible function pointer types involves callback functions that have more specific argument types (or less specific return types) than the function pointer they are assigned to. For example, old code which attempts to sort an array of strings might look like this:

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

int
compare (char **a, char **b)
{
  return strcmp (*a, *b);
}

void
sort (char **array, size_t length)
{
  qsort (array, length, sizeof (*array), compare);
}

Example of failure :

/home/aislas/wrf-model/wrf/external/RSL_LITE/c_code.c: In function ‘rsl_lite_pack_’:
/home/aislas/wrf-model/wrf/external/RSL_LITE/c_code.c:487:27: error: passing argument 1 of ‘f_pack_lint_’ from incompatible pointer type [-Wincompatible-pointer-types]
  487 |             F_PACK_LINT ( buf, p+yp_curs, imemord, &js, &je, &ks, &ke, &is, &ie,
      |                           ^~~
      |                           |
      |                           char *
In file included from /home/aislas/wrf-model/wrf/external/RSL_LITE/c_code.c:31:
/home/aislas/wrf-model/wrf/external/RSL_LITE/rsl_lite.h:200:25: note: expected ‘long int *’ but argument is of type ‘char *’
  200 | void F_PACK_LINT (long *inbuf, long *outbuf, int* memorder, int* js, int* je, int* ks, int* ke, int* is, int* ie, int* jms, int* jme, int* kms, int* kme, int* ims, int* ime, int* curs);
      |                   ~~~~~~^~~~~
/home/aislas/wrf-model/wrf/external/RSL_LITE/c_code.c:487:33: error: passing argument 2 of ‘f_pack_lint_’ from incompatible pointer type [-Wincompatible-pointer-types]
  487 |             F_PACK_LINT ( buf, p+yp_curs, imemord, &js, &je, &ks, &ke, &is, &ie,
      |                                ~^~~~~~~~
      |                                 |
      |                                 char *
/home/aislas/wrf-model/wrf/external/RSL_LITE/rsl_lite.h:200:38: note: expected ‘long int *’ but argument is of type ‘char *’
  200 | void F_PACK_LINT (long *inbuf, long *outbuf, int* memorder, int* js, int* je, int* ks, int* ke, int* is, int* ie, int* jms, int* jme, int* kms, int* kme, int* ims, int* ime, int* curs);
      |                                ~~~~~~^~~~~~
/home/aislas/wrf-model/wrf/external/RSL_LITE/c_code.c:492:26: error: passing argument 1 of ‘f_pack_int_’ from incompatible pointer type [-Wincompatible-pointer-types]
  492 |             F_PACK_INT ( buf, p+yp_curs, imemord, &js, &je, &ks, &ke, &is, &ie,
      |                          ^~~
      |                          |
      |                          char *
/home/aislas/wrf-model/wrf/external/RSL_LITE/rsl_lite.h:201:23: note: expected ‘int *’ but argument is of type ‘char *’
  201 | void F_PACK_INT (int *inbuf, int *outbuf, int* memorder, int* js, int* je, int* ks, int* ke, int* is, int* ie, int* jms, int* jme, int* kms, int* kme, int* ims, int* ime, int* curs);
      |                  ~~~~~^~~~~

Error was reproduced AlmaLinux release 9.4 with GCC 14.1.0

Solution:
Use explicit type casting

ISSUE:
Fixes #2047

TESTS CONDUCTED:

  1. Tested on GCC 14.1.0

@islas islas requested a review from a team as a code owner June 10, 2024 21:45
@weiwangncar
Copy link
Collaborator

The regression test results:
Test Type | Expected | Received | Failed
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
Number of Tests : 23 24
Number of Builds : 60 57
Number of Simulations : 158 150 0
Number of Comparisons : 95 86 0

Failed Simulations are: 
None
Which comparisons are not bit-for-bit: 
None

@sayedmekhaimer
Copy link

sayedmekhaimer commented Jun 24, 2024

The modification to c_code.c, period.c, and rsl_lite.c solves the compilation error with GCC-14.1, but during the execution of wrf.exe using mpirun (dm) this modification causes an error (segmentation fault, reference to memory). The same configuration works well using GCC-13.3.

@islas
Copy link
Collaborator Author

islas commented Jun 24, 2024

@sayedmekhaimer Did you use commit 36d3c64 or later? The initial commit in this PR did have that issue which I thought I had fixed (tested locally with gcc14)

@sayedmekhaimer
Copy link

@sayedmekhaimer Did you use commit 36d3c64 or later? The initial commit in this PR did have that issue which I thought I had fixed (tested locally with gcc14)

I am sorry that I used previous modification, Now the model is compiled and run in parallel mode with no errors using the the commit 36d3c64 . Thank you so much

@mgduda
Copy link
Collaborator

mgduda commented Sep 12, 2024

I don't recall whether WRF always uses the PR description as the merge commit message, but if so, perhaps it would be better to include a description of the code changes here rather than simply pointing to #2047? That would make the git log more self-contained.

external/RSL_LITE/period.c Outdated Show resolved Hide resolved
@mgduda mgduda self-requested a review September 13, 2024 00:10
@weiwangncar
Copy link
Collaborator

@islas Maybe you could make a concise message when merging?

@islas
Copy link
Collaborator Author

islas commented Sep 16, 2024

@weiwangncar Do you mean instead of the PR description?

@islas islas merged commit 297b434 into wrf-model:release-v4.6.1 Sep 16, 2024
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants