This is an archive of the discontinued LLVM Phabricator instance.

[C11] Allow initialization of an atomic-qualified pointer from a null pointer constant
ClosedPublic

Authored by aaron.ballman on Apr 19 2023, 10:47 AM.

Details

Summary

The relevant language rule from C11 is 6.5.16.1p1: "the left operand is an atomic, qualified, or unqualified pointer, and the right is a null pointer constant; or". We correctly handled qualified or unqualified pointer types, but failed to handle atomic-qualified pointer types. Now we look through the atomic qualification before testing the constraint requirements.

Fixes https://github.com/llvm/llvm-project/issues/49563

Diff Detail

Event Timeline

aaron.ballman created this revision.Apr 19 2023, 10:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 19 2023, 10:47 AM
aaron.ballman requested review of this revision.Apr 19 2023, 10:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 19 2023, 10:47 AM
aaron.ballman updated this revision to Diff 515021.EditedApr 19 2023, 10:55 AM

Added an additional test case (assignment of a null pointer constant to an atomic function pointer) that was mentioned in a comment on the original bug report).

Added a -pedantic run line and used (void *)0 as a null pointer constant for the last test.

erichkeane added inline comments.
clang/lib/Sema/SemaExpr.cpp
10307

Does the LHSType here need switching too? Or are we just going to use the 'normal' pointer conversion rules here?

aaron.ballman added inline comments.Apr 19 2023, 11:33 AM
clang/lib/Sema/SemaExpr.cpp
10307

The intent is to follow the normal conversion rules here -- this just allows us to check those rules properly when the LHS type is atomic as well as when it's non-atomic.

erichkeane accepted this revision.Apr 19 2023, 11:45 AM
This revision is now accepted and ready to land.Apr 19 2023, 11:45 AM
This revision was landed with ongoing or failed builds.Apr 20 2023, 5:02 AM
This revision was automatically updated to reflect the committed changes.
sbc100 added a subscriber: sbc100.Apr 24 2023, 4:33 PM

We bisected a build failure on the emscripten waterfall to this change:

https://ci.chromium.org/ui/p/emscripten-releases/builders/try/linux/b8782932845295577777/overview
https://logs.chromium.org/logs/emscripten-releases/buildbucket/cr-buildbucket/8782932845295577777/+/u/Build_Emscripten__upstream_/stdout

cache:INFO: generating system library: sysroot/lib/wasm32-emscripten/libc-mt.a... (this will be cached in "/b/s/w/ir/x/w/install/emscripten/cache/sysroot/lib/wasm32-emscripten/libc-mt.a" for subsequent builds)
/b/s/w/ir/x/w/install/emscripten/system/lib/pthread/emscripten_yield.c:10:26: error: cannot compile this static initializer yet
static _Atomic pthread_t crashed_thread_id = NULL;
                         ^
1 error generated.
emcc: error: '/b/s/w/ir/x/w/install/bin/clang -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -D__EMSCRIPTEN_SHARED_MEMORY__=1 -D__EMSCRIPTEN_WASM_WORKERS__=1 -Werror=implicit-function-declaration --sysroot=/b/s/w/ir/x/w/install/emscripten/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -O2 -Wall -Werror -fno-unroll-loops -std=c99 -D_XOPEN_SOURCE=700 -Wno-unused-result -Os -fno-inline-functions -fno-builtin -Wno-ignored-attributes -Wno-macro-redefined -Wno-shift-op-parentheses -Wno-string-plus-int -Wno-missing-braces -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-label -Wno-pointer-sign -g3 -I/b/s/w/ir/x/w/install/emscripten/system/lib/libc/musl/src/internal -I/b/s/w/ir/x/w/install/emscripten/system/lib/libc/musl/src/include -I/b/s/w/ir/x/w/install/emscripten/system/lib/libc -I/b/s/w/ir/x/w/install/emscripten/system/lib/pthread -pthread -DNDEBUG -ffile-prefix-map=/b/s/w/ir/x/w/install/emscripten=/emsdk/emscripten -fdebug-compilation-dir=/emsdk/emscripten -c -matomics -mbulk-memory /b/s/w/ir/x/w/install/emscripten/system/lib/pthread/emscripten_yield.c -o /b/s/w/ir/x/w/install/emscripten/cache/build/libc-mt-tmp/emscripten_yield.o' failed (returned 1)
embuilder: error: Subprocess 11/1107 failed (returned 1)! (cmdline: /b/s/w/ir/x/w/install/emscripten/emcc -O2 -Wall -Werror -fno-unroll-loops -std=c99 -D_XOPEN_SOURCE=700 -Wno-unused-result -Os -fno-inline-functions -fno-builtin -Wno-ignored-attributes -Wno-macro-redefined -Wno-shift-op-parentheses -Wno-string-plus-int -Wno-missing-braces -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-label -Wno-pointer-sign -g -sSTRICT -I/b/s/w/ir/x/w/install/emscripten/system/lib/libc/musl/src/internal -I/b/s/w/ir/x/w/install/emscripten/system/lib/libc/musl/src/include -I/b/s/w/ir/x/w/install/emscripten/system/lib/libc -I/b/s/w/ir/x/w/install/emscripten/system/lib/pthread -pthread -sWASM_WORKERS -DNDEBUG -ffile-prefix-map=/b/s/w/ir/x/w/install/emscripten=/emsdk/emscripten -fdebug-compilation-dir=/emsdk/emscripten -c /b/s/w/ir/x/w/install/emscripten/system/lib/pthread/emscripten_yield.c -o /b/s/w/ir/x/w/install/emscripten/cache/build/libc-mt-tmp/emscripten_yield.o)
Exception thrown in build step.

Prior to this change this code compiled fine.

bgraur added a subscriber: bgraur.Apr 24 2023, 11:16 PM

We see the same compilation error as the one reported by @sbc100 in several other C libraries.

Here's a short reproducer: https://godbolt.org/z/Y8eds47na

The tests imply that this should work. Please revert.

We see the same compilation error as the one reported by @sbc100 in several other C libraries.

Here's a short reproducer: https://godbolt.org/z/Y8eds47na

The tests imply that this should work. Please revert.

Thank you (both) for reporting this issue so quickly! I believe I've addressed it with 1395cde24b3641e284bb1daae7d56c189a2635e3, but if you're still having problems after that commit, let me know and I'll revert both changes to investigate further.

@aaron.ballman your patch (1395cde24b3641e284bb1daae7d56c189a2635e3) fixed all issues we encountered. Thanks!!

@aaron.ballman your patch (1395cde24b3641e284bb1daae7d56c189a2635e3) fixed all issues we encountered. Thanks!!

Excellent, thank you for the confirmation!