Skip to content

[libc++] use copy_file_range for fs::copy #109211

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

Merged
merged 8 commits into from
Jan 6, 2025
Merged

Conversation

Jannik2099
Copy link
Contributor

Hi,

this optimizes std::filesystem::copy_file to use the copy_file_range syscall (Linux and FreeBSD) when available. It allows for reflinks on filesystems such as btrfs, zfs and xfs, and server-side copy for network filesystems such as NFS.

This behaviour is in line with Boost.Filesystem and libstdc++ (where I implemented this some time ago). It's also the default behaviour of the standard file copy operations in .NET Core, Java, and (hopefully) soon python, and in the cp program from GNU coreutils.

I don't have the opportunity to test this on FreeBSD, but I think the FreeBSD folks will want a look at this. Sadly I don't know any by name, perhaps someone could CC the relevant person(s).

As for the LIBCXX_USE_COPY_FILE_RANGE cmake check, I was unsure where to put it, and this is probably not the desired location.

Lastly I am not sure if the largefile handling (see usage of off_t) is correct.

@Jannik2099 Jannik2099 requested a review from a team as a code owner September 18, 2024 22:36
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2024

@llvm/pr-subscribers-libcxx

Author: Jannik Glückert (Jannik2099)

Changes

Hi,

this optimizes std::filesystem::copy_file to use the copy_file_range syscall (Linux and FreeBSD) when available. It allows for reflinks on filesystems such as btrfs, zfs and xfs, and server-side copy for network filesystems such as NFS.

This behaviour is in line with Boost.Filesystem and libstdc++ (where I implemented this some time ago). It's also the default behaviour of the standard file copy operations in .NET Core, Java, and (hopefully) soon python, and in the cp program from GNU coreutils.

I don't have the opportunity to test this on FreeBSD, but I think the FreeBSD folks will want a look at this. Sadly I don't know any by name, perhaps someone could CC the relevant person(s).

As for the LIBCXX_USE_COPY_FILE_RANGE cmake check, I was unsure where to put it, and this is probably not the desired location.

Lastly I am not sure if the largefile handling (see usage of off_t) is correct.


Full diff: https://github.com/llvm/llvm-project/pull/109211.diff

2 Files Affected:

  • (modified) libcxx/src/CMakeLists.txt (+6)
  • (modified) libcxx/src/filesystem/operations.cpp (+59-1)
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index 48c5111a0acbf6..3f97d3e730a42c 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -173,6 +173,12 @@ if (APPLE AND LLVM_USE_SANITIZER)
   endif()
 endif()
 
+include(CheckCXXSymbolExists)
+check_cxx_symbol_exists("copy_file_range" "unistd.h" LIBCXX_USE_COPY_FILE_RANGE)
+if(LIBCXX_USE_COPY_FILE_RANGE)
+  list(APPEND LIBCXX_COMPILE_FLAGS "-D_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE")
+endif()
+
 split_list(LIBCXX_COMPILE_FLAGS)
 split_list(LIBCXX_LINK_FLAGS)
 
diff --git a/libcxx/src/filesystem/operations.cpp b/libcxx/src/filesystem/operations.cpp
index d771f200973528..42d16c0546ae10 100644
--- a/libcxx/src/filesystem/operations.cpp
+++ b/libcxx/src/filesystem/operations.cpp
@@ -32,6 +32,7 @@
 #  include <dirent.h>
 #  include <sys/stat.h>
 #  include <sys/statvfs.h>
+#  include <sys/types.h>
 #  include <unistd.h>
 #endif
 #include <fcntl.h> /* values for fchmodat */
@@ -178,8 +179,35 @@ void __copy(const path& from, const path& to, copy_options options, error_code*
 namespace detail {
 namespace {
 
+#if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE)
+bool copy_file_impl_copy_file_range(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
+  size_t count = read_fd.get_stat().st_size;
+  // a zero-length file is either empty, or not copyable by this syscall
+  // return early to avoid the syscall cost
+  if (count == 0) {
+    ec = {EINVAL, generic_category()};
+    return false;
+  }
+  do {
+    ssize_t res;
+    // do not modify the fd positions as copy_file_impl_sendfile may be called after a partial copy
+    off_t off_in  = 0;
+    off_t off_out = 0;
+
+    if ((res = ::copy_file_range(read_fd.fd, &off_in, write_fd.fd, &off_out, count, 0)) == -1) {
+      ec = capture_errno();
+      return false;
+    }
+    count -= res;
+  } while (count > 0);
+
+  ec.clear();
+
+  return true;
+}
+#endif
 #if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)
-bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
+bool copy_file_impl_sendfile(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
   size_t count = read_fd.get_stat().st_size;
   do {
     ssize_t res;
@@ -194,6 +222,36 @@ bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_cod
 
   return true;
 }
+#endif
+
+#if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)
+bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
+  bool has_copied = false;
+#  if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE)
+  has_copied = copy_file_impl_copy_file_range(read_fd, write_fd, ec);
+  if (has_copied) {
+    return true;
+  }
+  // EINVAL: src and dst are the same file (this is not cheaply
+  // detectable from userspace)
+  // EINVAL: copy_file_range is unsupported for this file type by the
+  // underlying filesystem
+  // ENOTSUP: undocumented, can arise with old kernels and NFS
+  // EOPNOTSUPP: filesystem does not implement copy_file_range
+  // ETXTBSY: src or dst is an active swapfile (nonsensical, but allowed
+  // with normal copying)
+  // EXDEV: src and dst are on different filesystems that do not support
+  // cross-fs copy_file_range
+  // ENOENT: undocumented, can arise with CIFS
+  // ENOSYS: unsupported by kernel or blocked by seccomp
+  if (ec.value() != EINVAL && ec.value() != ENOTSUP && ec.value() != EOPNOTSUPP && ec.value() != ETXTBSY &&
+      ec.value() != EXDEV && ec.value() != ENOENT && ec.value() != ENOSYS) {
+    return false;
+  }
+  ec.clear();
+#  endif
+  return copy_file_impl_sendfile(read_fd, write_fd, ec);
+}
 #elif defined(_LIBCPP_FILESYSTEM_USE_COPYFILE)
 bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
   struct CopyFileState {

@mstorsjo
Copy link
Member

I don't have the opportunity to test this on FreeBSD, but I think the FreeBSD folks will want a look at this. Sadly I don't know any by name, perhaps someone could CC the relevant person(s).

CC @emaste

@Jannik2099 Note that the CI environment does contain a FreeBSD setup, which shows a failure (a somewhat boring one):

/home/buildkite/.buildkite-agent/builds/freebsd-test-1/llvm-project/libcxx-ci/libcxx/src/filesystem/operations.cpp:183:6: error: unused function 'copy_file_impl_copy_file_range' [-Werror,-Wunused-function]
  183 | bool copy_file_impl_copy_file_range(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

(There are also a couple other similar failing cases in the same CI run on buildkite.)

@Jannik2099
Copy link
Contributor Author

Thanks!

Yeah, I forgot to slap on -Werror yesterday, will fix later.

Another note, how TOCTOU-safe should this be? Right now I fstat both in the copy_file_range and in the sendfile impl. If the file size changes inbetween and copy_file_range fails halfway through, this may lead to inconsistent output.

@Jannik2099
Copy link
Contributor Author

So I looked at the FreeBSD failure again...

FreeBSD does have sendfile, but it does NOT have sys/sendfile.h, which means libc++ has been doing userspace copies on FreeBSD all this time!

I'll think of a more robust check.

@Jannik2099
Copy link
Contributor Author

FreeBSD does have sendfile, but it does NOT have sys/sendfile.h, which means libc++ has been doing userspace copies on FreeBSD all this time!

so turns out that was on purpose, because FreeBSD sendfile cannot send to files - I had assumed it was one of those "ancient unix syscalls that made it's way into every OS".

I'll have to restructure this a bit then.

Unrelated, I also took the opportunity to support copying special zero-length files on linux (such as those in /proc). Funnily enough, we discovered the same deficiency in libstdc++ when I was adding copy_file_range support there.

@Jannik2099
Copy link
Contributor Author

Ok, I think this is ready for review now.

To summarize:

Add copy_file_range support on Linux and FreeBSD. fs::copy_file will first try copy_file_range, if unavailable and on linux sendfile, and finally the generic fstream.

This covers the following scenarios:

  • copy_file_range is not implemented by the filesystem or the OS
  • copy_file_range and/or sendfile are unable to copy to/from the source/dest in question
  • fs::copy_file is used on a special linux zero-length file, such as those in /proc - this was previously unsupported.

I have also added a test for the /proc case, there are no such files on FreeBSD to my rudimentary knowledge, so the test is linux exclusive.

I am not super happy with how copy_file_impl looks now - it's stuck halfway between doing #ifdef on (effectively) the target OS, and halfway between doing it on the available syscalls. Feel free to tear apart.

@ldionne
Copy link
Member

ldionne commented Sep 25, 2024

Please rebase onto main to re-trigger CI. The CI instability should be resolved now.

@ldionne
Copy link
Member

ldionne commented Sep 25, 2024

(I'll get to this review in my queue, for now just trying to get CI back on track)

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch and sorry for the delay! I have some comments and questions, but overall this patch looks quite good and this is definitely a nice optimization.

@Jannik2099
Copy link
Contributor Author

The CI failure in https://github.com/llvm/llvm-project/actions/runs/11822050795/job/32940244678?pr=109211 got me a tad confused. Is some special magic required to get iostreams working in the generic-no-localization case?

@Jannik2099
Copy link
Contributor Author

@ldionne ping

I think all the remaining CI failures are unrelated, except for the one mentioned above. Though I don't know how to fix this.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with minor comment. I've pushed to your branch to reorganize the way I wanted since it was only a minor deviation from what you did -- please LMK if what I did is not OK with you.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once CI is green.

@Jannik2099
Copy link
Contributor Author

LGTM once CI is green.

NACK, see my review above

@ldionne
Copy link
Member

ldionne commented Dec 5, 2024

LGTM once CI is green.

NACK, see my review above

Sorry, I don't understand. Are you saying that something still needs to be addressed in this PR?

@Jannik2099
Copy link
Contributor Author

LGTM once CI is green.

NACK, see my review above

Sorry, I don't understand. Are you saying that something still needs to be addressed in this PR?

I am saying that your commit 8f314b9 completely changes the semantics.

You changed it to two definitions of copy_file_impl that attempt copy_file_range / sendfile first, and fstream second. The definition is chosen at compile time.

The intended behaviour is for one copy_file_impl that attempts copy_file_range (if available for the platform), then sendfile (if available for the platform), and finally fstream

@ldionne
Copy link
Member

ldionne commented Dec 5, 2024

Oh, geez, now I understand the intent. That's subtle. I assumed the copy-file-range implementation and the sendfile implementations were disjoint.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed something that should fix my mistake.

Can you double-check and confirm that this does what you originally intended? I think we're pretty much back to your patch without my edits, except I added a comment.

If that patch is fine with you, it's fine with me in the current state. Sorry for the mixup, I was trying to help expedite this patch by doing what I thought were NFC changes (but were not).

@Jannik2099
Copy link
Contributor Author

Sorry for the mixup

No worries!

LGTM

Jannik2099 and others added 4 commits December 10, 2024 21:57
opportunistically use copy_file_range (Linux, FreeBSD) where possible.
This allows for fast copies via reflinks,
and server side copies for network filesystems.
Fall back to sendfile if not supported.

Signed-off-by: Jannik Glückert <[email protected]>
Virtual linux filesystems such as /proc and /sys
may contain files that appear as zero length,
but do contain data.
These require a traditional userspace read + write loop.

Signed-off-by: Jannik Glückert <[email protected]>
@Jannik2099
Copy link
Contributor Author

Ugh, I forgot that Bionic does not provide a copy_file_range syscall wrapper. I guess checking for availability of the function at configure time like I did initially is the best option?

Also, there's still the issue that the no-locale build now fails due to use of fstream. Does libc++ have an internal locale-independent fstream I could use here? It'd be silly to drop fs::copy_file support from the no-locale build, but so is duplicating the old implementation which doesn't support /proc files

@ldionne
Copy link
Member

ldionne commented Dec 12, 2024

Ugh, I forgot that Bionic does not provide a copy_file_range syscall wrapper. I guess checking for availability of the function at configure time like I did initially is the best option?

Can't you just check for #ifdef __BIONIC__? I'd like to avoid introducing any CMake coupling just to check that property.

Also, there's still the issue that the no-locale build now fails due to use of fstream. Does libc++ have an internal locale-independent fstream I could use here? It'd be silly to drop fs::copy_file support from the no-locale build, but so is duplicating the old implementation which doesn't support /proc files

No, we don't have such a thing. Instead, I'd disable it for the no-localization build for now. I'm working on changes that should make fstream available even when localization is disabled, so that problem would be solved then.

Signed-off-by: Jannik Glückert <[email protected]>
@ldionne ldionne merged commit cb1c156 into llvm:main Jan 6, 2025
62 checks passed
@ldionne
Copy link
Member

ldionne commented Jan 6, 2025

Thanks for the patch!

Copy link

github-actions bot commented Jan 6, 2025

@Jannik2099 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@Jannik2099 Jannik2099 deleted the copy_file_range branch January 6, 2025 22:18
@Jannik2099 Jannik2099 restored the copy_file_range branch January 6, 2025 23:13
@vitalybuka
Copy link
Collaborator

We have this error now
https://lab.llvm.org/buildbot/#/builders/66/builds/8314

/home/b/sanitizer-x86_64-linux/build/build_default/bin/clang++ -DLIBCXX_BUILDING_LIBCXXABI -DLIBC_NAMESPACE=__llvm_libc_common_utils -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_LINK_PTHREAD_LIB -D_LIBCPP_LINK_RT_LIB -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/b/sanitizer-x86_64-linux/build/llvm-project/libcxx/src -I/home/b/sanitizer-x86_64-linux/build/build_default/runtimes/runtimes-bins/compiler-rt/lib/sanitizer_common/symbolizer/RTSanitizerCommonSymbolizerInternal.i386/symbolizer/libcxx/include/c++/v1 -I/home/b/sanitizer-x86_64-linux/build/llvm-project/libcxxabi/include -I/home/b/sanitizer-x86_64-linux/build/llvm-project/runtimes/cmake/Modules/../../../libc -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -m32 -U_FILE_OFFSET_BITS -fPIC -flto -Oz -g0 -DNDEBUG -target i386-unknown-linux-gnu -Wno-unused-command-line-argument -include /home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/../sanitizer_redefine_builtins.h -DSANITIZER_COMMON_REDEFINE_BUILTINS_IN_STD -Wno-language-extension-token -Wno-macro-redefined -std=c++23 -faligned-allocation -nostdinc++ -fvisibility-inlines-hidden -fvisibility=hidden -fsized-deallocation -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wzero-length-array -Wdeprecated-redundant-constexpr-static-def -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -fno-exceptions -fno-rtti -MD -MT libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o -MF libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o.d -o libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o -c /home/b/sanitizer-x86_64-linux/build/llvm-project/libcxx/src/filesystem/operations.cpp
/home/b/sanitizer-x86_64-linux/build/llvm-project/libcxx/src/filesystem/operations.cpp:246:46: error: cannot initialize a parameter of type '__off64_t *' (aka 'long long *') with an rvalue of type 'off_t *' (aka 'long *')
  246 |     if ((res = ::copy_file_range(read_fd.fd, &off_in, write_fd.fd, &off_out, count, 0)) == -1) {
      |                                              ^~~~~~~
/usr/lib/gcc-cross/i686-linux-gnu/14/../../../../i686-linux-gnu/include/unistd.h:1142:49: note: passing argument to parameter '__pinoff' here
 1142 | ssize_t copy_file_range (int __infd, __off64_t *__pinoff,
      |                                                 ^
1 error generated.

@vitalybuka
Copy link
Collaborator

Looks like on linux this code must compile with _FILE_OFFSET_BITS 64

@Jannik2099
Copy link
Contributor Author

Looks like on linux this code must compile with _FILE_OFFSET_BITS 64

largefile handling isn't that simple on linux sadly, especially once multiple libcs are involved.

I've changed it to just use the linux-specific loff_t, which is also what we did for libstdc++. See the linked PR

@vvereschaka
Copy link
Contributor

We got the following build error for the cross builds (win to armv7 linux)

https://lab.llvm.org/buildbot/#/builders/38/builds/1661

AILED: libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o 
C:\buildbot\as-builder-1\x-armv7l\build\.\bin\clang++.exe --target=armv7-unknown-linux-gnueabihf -DLIBCXX_BUILDING_LIBCXXABI -DLIBC_NAMESPACE=__llvm_libc_common_utils -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_LINK_PTHREAD_LIB -D_LIBCPP_LINK_RT_LIB -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -IC:/buildbot/as-builder-1/x-armv7l/llvm-project/libcxx/src -IC:/buildbot/as-builder-1/x-armv7l/build/include/armv7-unknown-linux-gnueabihf/c++/v1 -IC:/buildbot/as-builder-1/x-armv7l/build/include/c++/v1 -IC:/buildbot/as-builder-1/x-armv7l/llvm-project/libcxxabi/include -IC:/buildbot/as-builder-1/x-armv7l/llvm-project/runtimes/cmake/Modules/../../../libc -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -UNDEBUG -faligned-allocation -nostdinc++ -fvisibility-inlines-hidden -fvisibility=hidden -fsized-deallocation -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wzero-length-array -Wdeprecated-redundant-constexpr-static-def -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -std=c++2b -MD -MT libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o -MF libcxx\src\CMakeFiles\cxx_static.dir\filesystem\operations.cpp.o.d -o libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o -c C:/buildbot/as-builder-1/x-armv7l/llvm-project/libcxx/src/filesystem/operations.cpp
C:/buildbot/as-builder-1/x-armv7l/llvm-project/libcxx/src/filesystem/operations.cpp:246:46: error: cannot initialize a parameter of type '__off64_t *' (aka 'long long *') with an rvalue of type 'off_t *' (aka 'long *')
  246 |     if ((res = ::copy_file_range(read_fd.fd, &off_in, write_fd.fd, &off_out, count, 0)) == -1) {
      |                                              ^~~~~~~
c:/buildbot/fs/jetson-tk1-arm-ubuntu/usr/include\unistd.h:1110:49: note: passing argument to parameter '__pinoff' here
 1110 | ssize_t copy_file_range (int __infd, __off64_t *__pinoff,
      |                                                 ^
1 error generated.

@kongy
Copy link
Collaborator

kongy commented Jan 7, 2025

copy_file_range was introduced in glibc 2.27. This change breaks environments with older glibc than that.

libcxx/src/filesystem/operations.cpp:246:18: error: no member named 'copy_file_range' in the global namespace
  246 |     if ((res = ::copy_file_range(read_fd.fd, &off_in, write_fd.fd, &off_out, count, 0)) == -1) {
      |                ~~^
1 error generated.

@vitalybuka
Copy link
Collaborator

We got the following build error for the cross builds (win to armv7 linux)

The is #121855 for that

kongy added a commit to kongy/llvm-project that referenced this pull request Jan 7, 2025
PR llvm#109211 introduced a build break on systems with glibc < 2.27, since
copy_file_range was only introduced after that version. A version check
is added to prevent this breakage.
@fmayer
Copy link
Contributor

fmayer commented Jan 7, 2025

We got another error related to this on the sanitizer buildbot (https://lab.llvm.org/buildbot/#/builders/66/builds/8314)

FAILED: libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o 
/home/b/sanitizer-x86_64-linux/build/build_default/bin/clang++ -DLIBCXX_BUILDING_LIBCXXABI -DLIBC_NAMESPACE=__llvm_libc_common_utils -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_LINK_PTHREAD_LIB -D_LIBCPP_LINK_RT_LIB -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/b/sanitizer-x86_64-linux/build/llvm-project/libcxx/src -I/home/b/sanitizer-x86_64-linux/build/build_default/runtimes/runtimes-bins/compiler-rt/lib/sanitizer_common/symbolizer/RTSanitizerCommonSymbolizerInternal.i386/symbolizer/libcxx/include/c++/v1 -I/home/b/sanitizer-x86_64-linux/build/llvm-project/libcxxabi/include -I/home/b/sanitizer-x86_64-linux/build/llvm-project/runtimes/cmake/Modules/../../../libc -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -m32 -U_FILE_OFFSET_BITS -fPIC -flto -Oz -g0 -DNDEBUG -target i386-unknown-linux-gnu -Wno-unused-command-line-argument -include /home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/../sanitizer_redefine_builtins.h -DSANITIZER_COMMON_REDEFINE_BUILTINS_IN_STD -Wno-language-extension-token -Wno-macro-redefined -std=c++23 -faligned-allocation -nostdinc++ -fvisibility-inlines-hidden -fvisibility=hidden -fsized-deallocation -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wzero-length-array -Wdeprecated-redundant-constexpr-static-def -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -fno-exceptions -fno-rtti -MD -MT libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o -MF libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o.d -o libcxx/src/CMakeFiles/cxx_static.dir/filesystem/operations.cpp.o -c /home/b/sanitizer-x86_64-linux/build/llvm-project/libcxx/src/filesystem/operations.cpp
/home/b/sanitizer-x86_64-linux/build/llvm-project/libcxx/src/filesystem/operations.cpp:246:46: error: cannot initialize a parameter of type '__off64_t *' (aka 'long long *') with an rvalue of type 'off_t *' (aka 'long *')
  246 |     if ((res = ::copy_file_range(read_fd.fd, &off_in, write_fd.fd, &off_out, count, 0)) == -1) {
      |                                              ^~~~~~~
/usr/lib/gcc-cross/i686-linux-gnu/14/../../../../i686-linux-gnu/include/unistd.h:1142:49: note: passing argument to parameter '__pinoff' here
 1142 | ssize_t copy_file_range (int __infd, __off64_t *__pinoff,
      |                                                 ^
1 error generated.

Seems like this doesn't build on 32bit

@Jannik2099
Copy link
Contributor Author

We got another error related to this on the sanitizer buildbot (https://lab.llvm.org/buildbot/#/builders/66/builds/8314)

sorry, this is also fixed in the pending #121855

ldionne pushed a commit that referenced this pull request Jan 7, 2025
@ldionne
Copy link
Member

ldionne commented Jan 7, 2025

I merged #121855, thanks @Jannik2099 for the quick fix!

vitalybuka pushed a commit that referenced this pull request Jan 7, 2025
PR #109211 introduced a build break on systems with glibc < 2.27, since
copy_file_range was only introduced after that version. A version check
is added to prevent this breakage.
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants