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

Fix ABI wrapping for readdir() #3640

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion starboard/shared/modular/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ if ((sb_is_modular || sb_is_evergreen_compatible) &&
current_toolchain == starboard_toolchain) {
source_set("starboard_layer_posix_abi_wrappers") {
sources = [
"starboard_layer_posix_directory_abi_wrappers.cc",
"starboard_layer_posix_directory_abi_wrappers.h",
"starboard_layer_posix_mmap_abi_wrappers.cc",
"starboard_layer_posix_mmap_abi_wrappers.h",
"starboard_layer_posix_pthread_abi_wrappers.cc",
Expand All @@ -34,14 +36,18 @@ if ((sb_is_modular || sb_is_evergreen_compatible) &&

configs += [ "//starboard/build/config:starboard_implementation" ]

deps = [ "//starboard:starboard_headers_only" ]
deps = [
"//starboard:starboard_headers_only",
"//starboard/common:common_headers_only",
]
}
}

if (sb_is_modular && !sb_is_evergreen &&
current_toolchain == cobalt_toolchain) {
source_set("cobalt_layer_posix_abi_wrappers") {
sources = [
"cobalt_layer_posix_directory_abi_wrappers.cc",
"cobalt_layer_posix_mmap_abi_wrappers.cc",
"cobalt_layer_posix_pthread_abi_wrappers.cc",
"cobalt_layer_posix_socket_abi_wrappers.cc",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#if SB_API_VERSION >= 16

#include <dirent.h>

extern "C" {

int __abi_wrap_readdir_r(DIR* dirp,
struct dirent* entry,
struct dirent** result);

int readdir_r(DIR* dirp, struct dirent* entry, struct dirent** result) {
return __abi_wrap_readdir_r(dirp, entry, result);
}
}

#endif // SB_API_VERSION >= 16
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "starboard/shared/modular/starboard_layer_posix_directory_abi_wrappers.h"
#include <string.h>
#include <algorithm>
#include "starboard/common/log.h"

int __abi_wrap_readdir_r(DIR* dirp,
struct musl_dirent* musl_entry,
struct musl_dirent** musl_result) {
// readdir_r segfaults if any of those parameters are missing.
SB_CHECK(dirp);
SB_CHECK(musl_entry);
SB_CHECK(musl_result);

struct dirent entry = {0}; // The type from platform toolchain.
struct dirent* result = nullptr;
int retval = readdir_r(dirp, &entry, &result);
if (retval != 0) {
return retval;
}
musl_entry->d_ino = entry.d_ino;
musl_entry->d_off = entry.d_off;
musl_entry->d_reclen = entry.d_reclen;
musl_entry->d_type = entry.d_type;

memset(musl_entry->d_name, 0, sizeof(musl_entry->d_name));
constexpr auto minlen =
std::min(sizeof(musl_entry->d_name), sizeof(entry.d_name));
memcpy(musl_entry->d_name, entry.d_name, minlen);
if (result == nullptr) {
*musl_result = nullptr;
} else {
*musl_result = musl_entry;
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_DIRECTORY_ABI_WRAPPERS_H_
#define STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_DIRECTORY_ABI_WRAPPERS_H_

#include <dirent.h>
#include <stdint.h>
#include <sys/types.h>

#include "starboard/export.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct musl_dirent {
int64_t /* ino_t */ d_ino;
kaidokert marked this conversation as resolved.
Show resolved Hide resolved
int64_t /* off_t */ d_off;
uint16_t /* short */ d_reclen;
unsigned char d_type;
char d_name[256];
} musl_dirent;

SB_EXPORT int __abi_wrap_readdir_r(DIR* dirp,
struct musl_dirent* entry,
struct musl_dirent** result);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // STARBOARD_SHARED_MODULAR_STARBOARD_LAYER_POSIX_DIRECTORY_ABI_WRAPPERS_H_
Loading