Skip to content

Commit 78175cb

Browse files
author
Andrew Hsieh
committed
Don't use errno directly
errno in MIPS is macro for "*__errno)()". Use of errno inside WRAP(__errno)() cause infinite loop. This may happen in other wrapper functions which access errno. Change all error to *REAL(__error)() Change-Id: I1c09d84a58855bd7896fcd4e70f740b8a0f0b386
1 parent 6433ec2 commit 78175cb

File tree

10 files changed

+35
-31
lines changed

10 files changed

+35
-31
lines changed

ndk/sources/android/libportable/arch-mips/errno.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ volatile int* WRAP(__errno)()
285285
int save_errno;
286286

287287
/* pthread_* calls may modify errno so use a copy */
288-
save_errno = errno;
288+
save_errno = *REAL(__errno)();
289289

290290
p = errno_key_data();
291291

@@ -322,7 +322,7 @@ volatile int* WRAP(__errno)()
322322
ALOGV("%s: new save_errno:%d p:%p->{pshadow:%d, perrno:%d}", __func__,
323323
save_errno, p, p->pshadow, p->perrno);
324324

325-
errno = save_errno;
325+
*REAL(__errno)() = save_errno;
326326

327327
ALOGV("%s: return (&p->perrno):%p; }", __func__, &p->perrno);
328328

@@ -338,7 +338,7 @@ void WRAP(__set_errno)(int portable_errno)
338338
int save_errno;
339339

340340
/* pthread_* calls may modify errno so use a copy */
341-
save_errno = errno;
341+
save_errno = *REAL(__errno)();
342342

343343
p = errno_key_data();
344344

@@ -352,7 +352,7 @@ void WRAP(__set_errno)(int portable_errno)
352352
ALOGV("%s: new save_errno:%d, p:%p->{pshadow:%d, perrno:%d}", __func__,
353353
save_errno, p, p->pshadow, p->perrno);
354354

355-
errno = save_errno;
355+
*REAL(__errno)() = save_errno;
356356

357357
ALOGV("%s: return; }", __func__);
358358
}

ndk/sources/android/libportable/arch-mips/fcntl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdlib.h>
2222
#include <stdio.h>
2323
#include <errno.h>
24+
#include <errno_portable.h>
2425
#include <portability.h>
2526
#include <fcntl_portable.h>
2627
#include <filefd_portable.h>
@@ -317,7 +318,7 @@ int WRAP(fcntl)(int fd, int portable_cmd, ...)
317318

318319
if (invalid_pointer(flock_portable)) {
319320
ALOGE("%s: flock_portable:%p == {NULL||-1}", __func__, flock_portable);
320-
errno = EFAULT;
321+
*REAL(__errno)() = EFAULT;
321322
result = -1;
322323
goto done;
323324
}
@@ -350,7 +351,7 @@ int WRAP(fcntl)(int fd, int portable_cmd, ...)
350351

351352
if (invalid_pointer(flock_portable)) {
352353
ALOGE("%s: flock_portable:%p == {NULL||-1}", __func__, flock_portable);
353-
errno = EFAULT;
354+
*REAL(__errno)() = EFAULT;
354355
result = -1;
355356
goto done;
356357
}

ndk/sources/android/libportable/arch-mips/filefd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static int export_fd_env()
113113

114114
ALOGV("%s:() {", __func__);
115115

116-
saved_errno = errno;
116+
saved_errno = *REAL(__errno)();
117117

118118
type_env_allocated = malloc(max_env_size);
119119
fd_env_allocated = malloc(max_env_size);
@@ -216,7 +216,7 @@ static int export_fd_env()
216216
if (fd_env_allocated)
217217
free(fd_env_allocated);
218218

219-
errno = saved_errno;
219+
*REAL(__errno)() = saved_errno;
220220

221221
ALOGV("%s: return(rv:%d); }", __func__, rv);
222222
return rv;
@@ -239,7 +239,7 @@ static int import_fd_env(int verify)
239239

240240
ALOGV("%s:(verify:%d) {", __func__, verify);
241241

242-
saved_errno = errno;
242+
saved_errno = *REAL(__errno)();
243243

244244
/*
245245
* get file descriptor environment pointer and make a
@@ -328,7 +328,7 @@ static int import_fd_env(int verify)
328328
if (fd_env_allocated)
329329
free(fd_env_allocated);
330330

331-
errno = saved_errno;
331+
*REAL(__errno)() = saved_errno;
332332

333333
ALOGV("%s: return(rv:%d); }", __func__, rv);
334334
return rv;

ndk/sources/android/libportable/arch-mips/resource.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ static inline int mips_change_resource(int resource)
3939
return resource;
4040
}
4141

42-
extern int getrlimit(int resource, struct rlimit *rlp);
42+
extern int REAL(getrlimit)(int resource, struct rlimit *rlp);
4343
int WRAP(getrlimit)(int resource, struct rlimit *rlp)
4444
{
4545
return REAL(getrlimit)(mips_change_resource(resource), rlp);
4646
}
4747

48-
extern int setrlimit(int resource, const struct rlimit *rlp);
48+
extern int REAL(setrlimit)(int resource, const struct rlimit *rlp);
4949
int WRAP(setrlimit)(int resource, const struct rlimit *rlp)
5050
{
5151
return REAL(setrlimit)(mips_change_resource(resource), rlp);

ndk/sources/android/libportable/arch-mips/signal.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,6 @@ __hidden int read_signalfd_mapper(int fd, void *buf, size_t count)
13291329
return rv;
13301330
}
13311331

1332-
13331332
int WRAP(sigsuspend)(const sigset_portable_t *portable_sigmask)
13341333
{
13351334
int rv;
@@ -1338,7 +1337,7 @@ int WRAP(sigsuspend)(const sigset_portable_t *portable_sigmask)
13381337
ALOGV("%s(portable_sigmask:%p) {", __func__, portable_sigmask);
13391338

13401339
if (invalid_pointer((void *)portable_sigmask)) {
1341-
errno = EFAULT;
1340+
*REAL(__errno)() = EFAULT;
13421341
rv = -1;
13431342
} else {
13441343
sigset_pton((sigset_portable_t *)portable_sigmask, &mips_sigmask);
@@ -1359,7 +1358,7 @@ int WRAP(sigpending)(sigset_portable_t *portable_sigset)
13591358
portable_sigset);
13601359

13611360
if (invalid_pointer((void *)portable_sigset)) {
1362-
errno = EFAULT;
1361+
*REAL(__errno)() = EFAULT;
13631362
rv = -1;
13641363
} else {
13651364
rv = REAL(sigpending)(&mips_sigset);
@@ -1382,7 +1381,7 @@ int WRAP(sigwait)(const sigset_portable_t *portable_sigset, int *ptr_to_portable
13821381
portable_sigset, ptr_to_portable_sig);
13831382

13841383
if (invalid_pointer((void *)portable_sigset)) {
1385-
errno = EFAULT;
1384+
*REAL(__errno)() = EFAULT;
13861385
rv = -1;
13871386
} else {
13881387
sigset_pton((sigset_portable_t *)portable_sigset, &mips_sigset);
@@ -1508,7 +1507,7 @@ int WRAP(__rt_sigaction)(int portable_signum, const struct sigaction_portable *a
15081507

15091508
/* NOTE: ARM kernel is expecting sizeof(sigset_t) to be 8 bytes */
15101509
if (sigsetsize != (2* sizeof(long))) {
1511-
errno = EINVAL;
1510+
*REAL(__errno)() = EINVAL;
15121511
rv = -1;
15131512
goto done;
15141513
}
@@ -1533,7 +1532,7 @@ int WRAP(__rt_sigprocmask)(int portable_how,
15331532

15341533
/* NOTE: ARM kernel is expecting sizeof(sigset_t) to be 8 bytes */
15351534
if (sigsetsize != (2* sizeof(long))) {
1536-
errno = EINVAL;
1535+
*REAL(__errno)() = EINVAL;
15371536
rv = -1;
15381537
goto done;
15391538
}
@@ -1565,7 +1564,7 @@ int WRAP(__rt_sigtimedwait)(const sigset_portable_t *portable_sigset,
15651564

15661565
/* NOTE: ARM kernel is expecting sizeof(sigset_t) to be 8 bytes */
15671566
if (portable_sigsetsize != (2* sizeof(long))) {
1568-
errno = EINVAL;
1567+
*REAL(__errno)() = EINVAL;
15691568
rv = -1;
15701569
goto done;
15711570
}

ndk/sources/android/libportable/arch-mips/stat.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <portability.h>
1818
#include <errno.h>
19+
#include <errno_portable.h>
1920
#include <stat_portable.h>
2021

2122
/* Note: The Portable Header will define stat to stat_portable */
@@ -25,7 +26,7 @@ int WRAP(stat)(const char *path, struct stat_portable *s)
2526
int ret;
2627

2728
if (invalid_pointer(s)) {
28-
errno = EFAULT;
29+
*REAL(__errno)() = EFAULT;
2930
return -1;
3031
}
3132
ret = REAL(stat)(path, &mips_stat);
@@ -39,7 +40,7 @@ int WRAP(fstat)(int fd, struct stat_portable *s)
3940
int ret;
4041

4142
if (invalid_pointer(s)) {
42-
errno = EFAULT;
43+
*REAL(__errno)() = EFAULT;
4344
return -1;
4445
}
4546
ret = REAL(fstat)(fd, &mips_stat);
@@ -53,7 +54,7 @@ int WRAP(lstat)(const char *path, struct stat_portable *s)
5354
int ret;
5455

5556
if (invalid_pointer(s)) {
56-
errno = EFAULT;
57+
*REAL(__errno)() = EFAULT;
5758
return -1;
5859
}
5960
ret = REAL(lstat)(path, &mips_stat);
@@ -67,7 +68,7 @@ int WRAP(fstatat)(int dirfd, const char *path, struct stat_portable *s, int flag
6768
int ret;
6869

6970
if (invalid_pointer(s)) {
70-
errno = EFAULT;
71+
*REAL(__errno)() = EFAULT;
7172
return -1;
7273
}
7374
ret = REAL(fstatat)(dirfd, path, &mips_stat, flags);

ndk/sources/android/libportable/arch-mips/statfs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <portability.h>
1818
#include <string.h>
1919
#include <errno.h>
20+
#include <errno_portable.h>
2021
#include <statfs_portable.h>
2122

2223
static inline void statfs_ntop(struct statfs *n_statfs, struct statfs_portable *p_statfs)
@@ -40,7 +41,7 @@ int WRAP(statfs)(const char* path, struct statfs_portable* stat)
4041
int ret;
4142

4243
if (invalid_pointer(stat)) {
43-
errno = EFAULT;
44+
*REAL(__errno)() = EFAULT;
4445
return -1;
4546
}
4647
ret = REAL(statfs)(path, &mips_stat);
@@ -54,7 +55,7 @@ int WRAP(fstatfs)(int fd, struct statfs_portable* stat)
5455
int ret;
5556

5657
if (invalid_pointer(stat)) {
57-
errno = EFAULT;
58+
*REAL(__errno)() = EFAULT;
5859
return -1;
5960
}
6061
ret = REAL(fstatfs)(fd, &mips_stat);

ndk/sources/android/libportable/arch-mips/syscall.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ int WRAP(syscall)(int portable_number, ...)
597597
ALOGV("%s: native_number:%d <= 0; ret = -1; [ERROR: FIX SYSTEM CALL]", __func__,
598598
native_number);
599599

600-
errno = ENOSYS;
600+
*REAL(__errno)() = ENOSYS;
601601
ret = -1;
602602
goto done;
603603
}
@@ -633,7 +633,7 @@ int WRAP(syscall)(int portable_number, ...)
633633

634634
done:
635635
if (ret == -1) {
636-
ALOGV("%s: ret == -1; errno:%d;", __func__, errno);
636+
ALOGV("%s: ret == -1; errno:%d;", __func__, *REAL(__errno)());
637637
}
638638
ALOGV("%s: return(ret:%d); }", __func__, ret);
639639
return ret;

ndk/sources/android/libportable/common/include/errno_portable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,6 @@
130130
extern __hidden int errno_ntop(int native_errno);
131131
extern __hidden int errno_pton(int native_errno);
132132

133+
extern volatile int* REAL(__errno)(void);
134+
133135
#endif /* _ERRNO_PORTABLE_H */

ndk/sources/android/libportable/common/include/log_portable.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,24 @@ static inline char *portable_tag() {
7575
* to preserve the value of errno while logging.
7676
*/
7777
#define LOG_PRI(priority, tag, ...) ({ \
78-
int _errno = errno; \
78+
int _errno = *REAL(__errno)(); \
7979
int _rv = android_printLog(priority, tag, __VA_ARGS__); \
80-
errno = _errno; \
80+
*REAL(__errno)() = _errno; \
8181
_rv; /* Returned to caller */ \
8282
})
8383

8484
#if !defined(__HOST__)
8585
#include <cutils/log.h>
8686

8787
# define PERROR(str) { \
88-
ALOGE("%s: PERROR('%s'): errno:%d:'%s'", __func__, str, errno, strerror(errno)); \
88+
ALOGE("%s: PERROR('%s'): errno:%d:'%s'", __func__, str, *REAL(__errno)(), strerror(errno)); \
8989
}
9090

9191
# define ASSERT(cond) ALOG_ASSERT(cond, "assertion failed:(%s), file: %s, line: %d:%s", \
9292
#cond, __FILE__, __LINE__, __func__);
9393
#else
9494
#include <assert.h>
95-
# define PERROR(str) fprintf(stderr, "%s: PERROR('%s'): errno:%d:'%s'", __func__, str, errno, strerror(errno))
95+
# define PERROR(str) fprintf(stderr, "%s: PERROR('%s'): errno:%d:'%s'", __func__, str, *REAL(__errno)(), strerror(*REAL(__errno)()))
9696
# define ASSERT(cond) assert(cond)
9797
# define ALOGV(a,...)
9898
# define ALOGW(a,...)

0 commit comments

Comments
 (0)