Skip to content

Commit 896a2f0

Browse files
authored
Fix memory corruption in getrusage (#24705)
The existing code we have was broken by an upstream musl change: https://git.musl-libc.org/cgit/musl/commit/?id=5850546e9669f793aab61dfc7c4f2c1ff35c4b29 Fixes: #18083
1 parent baca81a commit 896a2f0

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

system/lib/libc/emscripten_syscall_stubs.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static mode_t g_umask = S_IWGRP | S_IWOTH;
3535
#define REPORT(name)
3636
#else
3737
#define REPORT(name) \
38-
emscripten_err("warning: unsupported syscall: __syscall_" #name "\n");
38+
emscripten_err("warning: unsupported syscall: __syscall_" #name);
3939
#endif
4040

4141
#define UNIMPLEMENTED(name, args) \
@@ -127,14 +127,20 @@ weak int __syscall_umask(int mask) {
127127
return old;
128128
}
129129

130+
struct kusage {
131+
long utime_tv_sec;
132+
long utime_tv_usec;
133+
long stime_tv_sec;
134+
long stime_tv_usec;
135+
};
136+
130137
weak int __syscall_getrusage(int who, intptr_t usage) {
131138
REPORT(getrusage);
132-
struct rusage *u = (struct rusage *)usage;
133-
memset(u, 0, sizeof(*u));
134-
u->ru_utime.tv_sec = 1;
135-
u->ru_utime.tv_usec = 2;
136-
u->ru_stime.tv_sec = 3;
137-
u->ru_stime.tv_usec = 4;
139+
struct kusage *u = (struct kusage*)usage;
140+
u->utime_tv_sec = 1;
141+
u->utime_tv_usec = 2;
142+
u->stime_tv_sec = 3;
143+
u->stime_tv_usec = 4;
138144
return 0;
139145
}
140146

test/other/test_getrusage.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
#include <stdio.h>
12
#include <sys/time.h>
23
#include <sys/resource.h>
34

45
int main() {
56
struct rusage u;
6-
getrusage(0, &u);
7+
getrusage(RUSAGE_SELF, &u);
8+
printf("ru_utime.tv_sec: %lld\n", u.ru_utime.tv_sec);
9+
printf("ru_utime.tv_usec: %d\n", u.ru_utime.tv_usec);
10+
printf("ru_stime.tv_sec: %lld\n", u.ru_stime.tv_sec);
11+
printf("ru_stime.tv_usec: %d\n", u.ru_stime.tv_usec);
712
return 0;
813
}

test/other/test_getrusage.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
warning: unsupported syscall: __syscall_getrusage
2+
ru_utime.tv_sec: 1
3+
ru_utime.tv_usec: 2
4+
ru_stime.tv_sec: 3
5+
ru_stime.tv_usec: 4

test/test_other.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13381,8 +13381,9 @@ def test_missing_malloc_export(self):
1338113381
'malloc() called but not included in the build - add `_malloc` to EXPORTED_FUNCTIONS',
1338213382
'free() called but not included in the build - add `_free` to EXPORTED_FUNCTIONS'), assert_all=True)
1338313383

13384+
@also_with_asan
1338413385
def test_getrusage(self):
13385-
self.do_runf('other/test_getrusage.c')
13386+
self.do_other_test('test_getrusage.c')
1338613387

1338713388
@with_env_modify({'EMMAKEN_COMPILER': shared.CLANG_CC})
1338813389
def test_emmaken_compiler(self):

0 commit comments

Comments
 (0)