Do not allocate tiny cap_user_header/data structures, place them on stack.
This allows us to avoid having code to malloc them, and code to check
for malloc failure. Resulting code decrease:
text data bss dec hex filename
10175 0 16 10191 27cf system.o.old
9797 0 0 9797 2645 system.o
* system.c (sys_capget): Put cap_user_header_t and cap_user_data_t
on stack, rather than allocating them in heap. These structures
are very small (a few integer fields), stack is a better place
for them.
(sys_capset): Likewise.
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
diff --git a/system.c b/system.c
index 9176697..e35c117 100644
--- a/system.c
+++ b/system.c
@@ -1572,25 +1572,17 @@
int
sys_capget(struct tcb *tcp)
{
- static cap_user_header_t arg0 = NULL;
- static cap_user_data_t arg1 = NULL;
+ /* cap_user_ types are _pointers_ to (small) structs. */
+ /* Structs themselves have no names defined. */
+ /* Have to use ugly hack to place them on stack. */
+ cap_user_header_t arg0;
+ cap_user_data_t arg1;
+ long a0[sizeof(*arg0) / sizeof(long) + 1];
+ long a1[sizeof(*arg1) / sizeof(long) + 1];
+ arg0 = (cap_user_header_t*) &a0;
+ arg1 = (cap_user_data_t *) &a1;
if (!entering(tcp)) {
- if (!arg0) {
- if ((arg0 = malloc(sizeof(*arg0))) == NULL) {
- fprintf(stderr, "out of memory\n");
- tprintf("%#lx, %#lx", tcp->u_arg[0], tcp->u_arg[1]);
- return -1;
- }
- }
- if (!arg1) {
- if ((arg1 = malloc(sizeof(*arg1))) == NULL) {
- fprintf(stderr, "out of memory\n");
- tprintf("%#lx, %#lx", tcp->u_arg[0], tcp->u_arg[1]);
- return -1;
- }
- }
-
if (!tcp->u_arg[0])
tprintf("NULL");
else if (!verbose(tcp))
@@ -1623,25 +1615,14 @@
int
sys_capset(struct tcb *tcp)
{
- static cap_user_header_t arg0 = NULL;
- static cap_user_data_t arg1 = NULL;
+ cap_user_header_t arg0;
+ cap_user_data_t arg1;
+ long a0[sizeof(*arg0) / sizeof(long) + 1];
+ long a1[sizeof(*arg1) / sizeof(long) + 1];
+ arg0 = (cap_user_header_t*) &a0;
+ arg1 = (cap_user_data_t *) &a1;
if (entering(tcp)) {
- if (!arg0) {
- if ((arg0 = malloc(sizeof(*arg0))) == NULL) {
- fprintf(stderr, "out of memory\n");
- tprintf("%#lx, %#lx", tcp->u_arg[0], tcp->u_arg[1]);
- return -1;
- }
- }
- if (!arg1) {
- if ((arg1 = malloc(sizeof(*arg1))) == NULL) {
- fprintf(stderr, "out of memory\n");
- tprintf("%#lx, %#lx", tcp->u_arg[0], tcp->u_arg[1]);
- return -1;
- }
- }
-
if (!tcp->u_arg[0])
tprintf("NULL");
else if (!verbose(tcp))