Skip to content

Commit

Permalink
Fix ABI wrapping for readdir()
Browse files Browse the repository at this point in the history
Add missing ABI wrappers for readdir_r. The dirent struct size
is different on 64/32bit platforms.

b/302730696
  • Loading branch information
kaidokert committed Jun 22, 2024
1 parent c9cfaa8 commit 4c1e0e3
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 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 @@ -42,6 +44,7 @@ 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; // The type from platform toolchain.
struct dirent* result;
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 == NULL) {
*musl_result = NULL;
} 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 {
ino_t d_ino;
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_

0 comments on commit 4c1e0e3

Please sign in to comment.