Hi! We weren't ignoring the ubsan variables created by c-ubsan.c before gimplification (others are added later). One way to fix this would be to introduce further UBSAN_ internal functions and lower it later (sanopt pass) like other ifns, this patch instead recognizes those magic vars by name/name of type and DECL_ARTIFICIAL and TYPE_ARTIFICIAL.
Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk. 2021-10-21 Jakub Jelinek <ja...@redhat.com> PR middle-end/64888 gcc/c-family/ * c-omp.c (c_omp_predefined_variable): Return true also for ubsan_create_data created artificial variables. gcc/testsuite/ * c-c++-common/ubsan/pr64888.c: New test. --- gcc/c-family/c-omp.c.jj 2021-10-15 11:59:15.753689459 +0200 +++ gcc/c-family/c-omp.c 2021-10-20 12:25:44.557498097 +0200 @@ -2860,13 +2860,44 @@ c_omp_predefined_variable (tree decl) { if (VAR_P (decl) && DECL_ARTIFICIAL (decl) - && TREE_READONLY (decl) && TREE_STATIC (decl) - && DECL_NAME (decl) - && (DECL_NAME (decl) == ridpointers[RID_C99_FUNCTION_NAME] - || DECL_NAME (decl) == ridpointers[RID_FUNCTION_NAME] - || DECL_NAME (decl) == ridpointers[RID_PRETTY_FUNCTION_NAME])) - return true; + && DECL_NAME (decl)) + { + if (TREE_READONLY (decl) + && (DECL_NAME (decl) == ridpointers[RID_C99_FUNCTION_NAME] + || DECL_NAME (decl) == ridpointers[RID_FUNCTION_NAME] + || DECL_NAME (decl) == ridpointers[RID_PRETTY_FUNCTION_NAME])) + return true; + /* For UBSan handle the same also ubsan_create_data created + variables. There is no magic flag for those, but user variables + shouldn't be DECL_ARTIFICIAL or have TYPE_ARTIFICIAL type with + such names. */ + if ((flag_sanitize & (SANITIZE_UNDEFINED + | SANITIZE_UNDEFINED_NONDEFAULT)) != 0 + && DECL_IGNORED_P (decl) + && !TREE_READONLY (decl) + && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE + && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE + && TYPE_ARTIFICIAL (TREE_TYPE (decl)) + && TYPE_NAME (TREE_TYPE (decl)) + && TREE_CODE (TYPE_NAME (TREE_TYPE (decl))) == TYPE_DECL + && DECL_NAME (TYPE_NAME (TREE_TYPE (decl))) + && (TREE_CODE (DECL_NAME (TYPE_NAME (TREE_TYPE (decl)))) + == IDENTIFIER_NODE)) + { + tree id1 = DECL_NAME (decl); + tree id2 = DECL_NAME (TYPE_NAME (TREE_TYPE (decl))); + if (IDENTIFIER_LENGTH (id1) >= sizeof ("ubsan_data") - 1 + && IDENTIFIER_LENGTH (id2) >= sizeof ("__ubsan__data") + && !memcmp (IDENTIFIER_POINTER (id2), "__ubsan_", + sizeof ("__ubsan_") - 1) + && !memcmp (IDENTIFIER_POINTER (id2) + IDENTIFIER_LENGTH (id2) + - sizeof ("_data") + 1, "_data", + sizeof ("_data") - 1) + && strstr (IDENTIFIER_POINTER (id1), "ubsan_data")) + return true; + } + } return false; } --- gcc/testsuite/c-c++-common/ubsan/pr64888.c.jj 2021-10-20 11:48:18.715240008 +0200 +++ gcc/testsuite/c-c++-common/ubsan/pr64888.c 2021-10-20 11:47:57.670539595 +0200 @@ -0,0 +1,27 @@ +/* PR middle-end/64888 */ +/* { dg-do compile { target fopenmp } } */ +/* { dg-options "-fopenmp -fsanitize=undefined" } */ + +int a, b; + +void +foo () +{ + int c; +#pragma omp parallel default (none) shared (a, b) private (c) + { + c = a / b; /* { dg-bogus "not specified in enclosing" } */ + (void) c; + } +#pragma omp task default (none) shared (a, b) private (c) + { + c = a << b; /* { dg-bogus "not specified in enclosing" } */ + (void) c; + } +#pragma omp teams default (none) shared (a, b) + { + int d[a]; /* { dg-bogus "not specified in enclosing" } */ + d[0] = 0; + (void) d[0]; + } +} Jakub