mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "liucheng (G)" <liucheng32@huawei.com>
To: "musl@lists.openwall.com" <musl@lists.openwall.com>
Cc: "liucheng (G)" <liucheng32@huawei.com>
Subject: [patch] return value of ulimit(UL_GETFSIZE) in X32 architecture
Date: Thu, 25 Apr 2019 13:44:24 +0000	[thread overview]
Message-ID: <869863DB5440B44FB22173F42FC3F3CE01CA3FC6@dggemm513-mbx.china.huawei.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2560 bytes --]

Dear ALL,

Return value of ulimit(UL_GETFSIZE) in X32 architecture seems to be wrong.

Here is the implementation of ulimit function in MUSL 1.1.22:
#include <sys/resource.h>
#include <ulimit.h>
#include <stdarg.h>

long ulimit(int cmd, ...)
{
                 struct rlimit rl;
                 getrlimit(RLIMIT_FSIZE, &rl);
                 if (cmd == UL_SETFSIZE) {
                         long val;
                         va_list ap;
                         va_start(ap, cmd);
                         val = va_arg(ap, long);
                         va_end(ap);
                         rl.rlim_cur = 512ULL * val;
                                     if (setrlimit(RLIMIT_FSIZE, &rl)) return -1;
                 }
                 return rl.rlim_cur / 512;
}

Make it simple in case of "cmd = UL_GETFSIZE", ulimit function becomes to be:
         long ulimit(int cmd, ...)
         {
                   struct rlimit rl;
                   getrlimit(RLIMIT_FSIZE, &rl);
                   return rl.rlim_cur / 512;
}

rl.rlim in ulimit function is the type of long long(8 Byte), however the return value in X32 architecture is the type of long(4 Byte).
So in that case, rl.rlim_cur / 512 would be larger than 0x7fffffff and ulimit function returns -1.


I also tried an experiment to improve my opinion as follows.

[benchmark]
$ cat ulimit_test.c
#include <stdio.h>
#include <ulimit.h>

int main()
{
        printf("ret:%d\n", ulimit(UL_GETFSIZE));

        return 0;
}

[testcase]
Environment: Linux 4.4.171 #1 SMP Thu Apr 25 00:39:22 UTC 2019 armv7l GNU/Linux

$ /tmp # ulimit -f
unlimited
$ /tmp # ./ulimit_test
ret:-1
$ /tmp # cp musl/libc.so /usr/lib/                                 // copy a new MUSL libc.so with my [patch]
$ /tmp # ./ulimit_test
ret:2147483647                                                               // 0x7fffffff

[patch]
---
diff --git a/src/legacy/ulimit.c b/src/legacy/ulimit.c
index 1f59e8e..d1620e6 100644
--- a/src/legacy/ulimit.c
+++ b/src/legacy/ulimit.c
@@ -1,6 +1,7 @@
#include <sys/resource.h>
#include <ulimit.h>
#include <stdarg.h>
+#include <limits.h>
 long ulimit(int cmd, ...)
{
@@ -15,5 +16,5 @@ long ulimit(int cmd, ...)
                rl.rlim_cur = 512ULL * val;
                if (setrlimit(RLIMIT_FSIZE, &rl)) return -1;
       }
-        return rl.rlim_cur / 512;
+       return rl.rlim_cur == RLIM_INFINITY ? LONG_MAX : rl.rlim_cur / 512;
}
---

Looking forward to your reply.
Best regards.
Cheng Liu


[-- Attachment #2: Type: text/html, Size: 13709 bytes --]

             reply	other threads:[~2019-04-25 13:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-25 13:44 liucheng (G) [this message]
2019-04-25 14:32 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=869863DB5440B44FB22173F42FC3F3CE01CA3FC6@dggemm513-mbx.china.huawei.com \
    --to=liucheng32@huawei.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).