Skip to content

Commit

Permalink
Wrap rename and renameat
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 authored and topjohnwu committed Sep 2, 2023
1 parent 2dbb812 commit 3654e8a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion native/src/base/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LOCAL_MODULE := libcompat
LOCAL_SRC_FILES := compat/compat.cpp
# Fix static variables' ctor/dtor when using LTO
# See: https://github.com/android/ndk/issues/1461
LOCAL_EXPORT_LDFLAGS := -static -T src/lto_fix.lds
LOCAL_EXPORT_LDFLAGS := -static -T src/lto_fix.lds -Wl,--wrap=rename -Wl,--wrap=renameat
# For some reason, using the hacky libc.a with x86 will trigger stack protection violation
# when mixing Rust and C++ code. Disable stack protector to bypass this issue.
ifeq ($(TARGET_ARCH), x86)
Expand Down
36 changes: 36 additions & 0 deletions native/src/base/compat/compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ int ftruncate64(int fd, off64_t length) {
}
#endif

int __real_rename(const char *old_path, const char *new_path);

int __wrap_rename(const char *old_path, const char *new_path) {
return __real_rename(old_path, new_path);
}

int __real_renameat(int old_dir_fd, const char *old_path, int new_dir_fd, const char *new_path);

int __wrap_renameat(int old_dir_fd, const char *old_path, int new_dir_fd, const char *new_path) {
return __real_renameat(old_dir_fd, old_path, new_dir_fd, new_path);
}

[[gnu::weak]]
void android_set_abort_message(const char *) {}

Expand All @@ -112,4 +124,28 @@ extern FILE __sF[];
[[gnu::weak]] FILE* stderr = &__sF[2];

} // extern "C"
#else

#include <linux/fcntl.h>
#include <unistd.h>
#include <syscall.h>
#include <errno.h>

extern "C" {

[[maybe_unused]]
int __wrap_renameat(int old_dir_fd, const char *old_path, int new_dir_fd, const char *new_path) {
long out = syscall(__NR_renameat, old_dir_fd, old_path, new_dir_fd, new_path);
if (out == -1 && errno == ENOSYS) {
out = syscall(__NR_renameat2, old_dir_fd, old_path, new_dir_fd, new_path, 0);
}
return static_cast<int>(out);
}

[[maybe_unused]]
int __wrap_rename(const char *old_path, const char *new_path) {
return __wrap_renameat(AT_FDCWD, old_path, AT_FDCWD, new_path);
}
}

#endif

0 comments on commit 3654e8a

Please sign in to comment.