mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 07:29:23 +00:00
Add some tests for execve()
This commit is contained in:
parent
2526a9b8c7
commit
daca5499b9
6 changed files with 81 additions and 14 deletions
|
@ -35,11 +35,9 @@
|
||||||
/**
|
/**
|
||||||
* Replaces current process with program.
|
* Replaces current process with program.
|
||||||
*
|
*
|
||||||
* Warning: Our implementation of argument escaping on Windows hasn't
|
* On Windows, `argv` and `envp` can't contain binary strings. They need
|
||||||
* been security reviewed for optimal handling of malicious arguments
|
* to be valid UTF-8 in order to round-trip the WIN32 API, without being
|
||||||
* when interoperating with commonly used software. We make an effort
|
* corrupted.
|
||||||
* to follow the same conventions as other FOSS software, e.g. PuTTY,
|
|
||||||
* however each WIN32 app is permitted to tokenize args how it wants.
|
|
||||||
*
|
*
|
||||||
* @param program will not be PATH searched, see commandv()
|
* @param program will not be PATH searched, see commandv()
|
||||||
* @param argv[0] is the name of the program to run
|
* @param argv[0] is the name of the program to run
|
||||||
|
|
|
@ -56,9 +56,9 @@ static bool have_getrandom;
|
||||||
*
|
*
|
||||||
* The following flags may be specified:
|
* The following flags may be specified:
|
||||||
*
|
*
|
||||||
* - GRND_RANDOM: Halt the entire system while I tap an entropy pool
|
* - `GRND_RANDOM`: Halt the entire system while I tap an entropy pool
|
||||||
* so small that it's hard to use statistics to test if it's random
|
* so small that it's hard to use statistics to test if it's random
|
||||||
* - GRND_NONBLOCK: Do not wait for i/o events or me to jiggle my
|
* - `GRND_NONBLOCK`: Do not wait for i/o events or me to jiggle my
|
||||||
* mouse, and instead return immediately the moment data isn't
|
* mouse, and instead return immediately the moment data isn't
|
||||||
* available, even if the result needs to be -1 w/ EAGAIN
|
* available, even if the result needs to be -1 w/ EAGAIN
|
||||||
*
|
*
|
||||||
|
@ -68,6 +68,8 @@ static bool have_getrandom;
|
||||||
* @note this function could block a nontrivial time on old computers
|
* @note this function could block a nontrivial time on old computers
|
||||||
* @note this function is indeed intended for cryptography
|
* @note this function is indeed intended for cryptography
|
||||||
* @note this function takes around 900 cycles
|
* @note this function takes around 900 cycles
|
||||||
|
* @raise EINVAL if `f` is invalid
|
||||||
|
* @raise ENOSYS on bare metal
|
||||||
* @asyncsignalsafe
|
* @asyncsignalsafe
|
||||||
* @restartable
|
* @restartable
|
||||||
* @vforksafe
|
* @vforksafe
|
||||||
|
@ -81,8 +83,10 @@ ssize_t getrandom(void *p, size_t n, unsigned f) {
|
||||||
const char *via;
|
const char *via;
|
||||||
sigset_t neu, old;
|
sigset_t neu, old;
|
||||||
if (n > 256) n = 256;
|
if (n > 256) n = 256;
|
||||||
if ((f & ~(GRND_RANDOM | GRND_NONBLOCK))) return einval();
|
if ((f & ~(GRND_RANDOM | GRND_NONBLOCK))) {
|
||||||
if (IsWindows()) {
|
rc = einval();
|
||||||
|
via = "n/a";
|
||||||
|
} else if (IsWindows()) {
|
||||||
via = "RtlGenRandom";
|
via = "RtlGenRandom";
|
||||||
if (RtlGenRandom(p, n)) {
|
if (RtlGenRandom(p, n)) {
|
||||||
rc = n;
|
rc = n;
|
||||||
|
|
60
test/libc/calls/execve_test.c
Normal file
60
test/libc/calls/execve_test.c
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
|
#include "libc/fmt/conv.h"
|
||||||
|
#include "libc/fmt/itoa.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
|
#include "libc/str/str.h"
|
||||||
|
#include "libc/testlib/subprocess.h"
|
||||||
|
#include "libc/testlib/testlib.h"
|
||||||
|
|
||||||
|
#define N 127
|
||||||
|
|
||||||
|
char *GenBuf(char buf[8], int x) {
|
||||||
|
int i;
|
||||||
|
bzero(buf, 8);
|
||||||
|
for (i = 0; i < 7; ++i) {
|
||||||
|
buf[i] = x & 127; // nt doesn't respect invalid unicode?
|
||||||
|
x >>= 1;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
__attribute__((__constructor__)) static void init(void) {
|
||||||
|
char buf[8];
|
||||||
|
if (__argc == 4 && !strcmp(__argv[1], "-")) {
|
||||||
|
ASSERT_STREQ(GenBuf(buf, atoi(__argv[2])), __argv[3]);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(execve, testArgPassing) {
|
||||||
|
int i;
|
||||||
|
char ibuf[12], buf[8];
|
||||||
|
for (i = 0; i < N; ++i) {
|
||||||
|
FormatInt32(ibuf, i);
|
||||||
|
GenBuf(buf, i);
|
||||||
|
SPAWN(vfork);
|
||||||
|
execve(GetProgramExecutableName(),
|
||||||
|
(char *const[]){GetProgramExecutableName(), "-", ibuf, buf, 0},
|
||||||
|
(char *const[]){0});
|
||||||
|
notpossible;
|
||||||
|
EXITS(0);
|
||||||
|
}
|
||||||
|
}
|
|
@ -100,6 +100,6 @@ TEST(mkntcmdline, fixAsBestAsWeCanForNow2) {
|
||||||
|
|
||||||
TEST(mkntcmdline, testWut) {
|
TEST(mkntcmdline, testWut) {
|
||||||
char *argv[] = {"redbean.com", "--strace", NULL};
|
char *argv[] = {"redbean.com", "--strace", NULL};
|
||||||
EXPECT_NE(-1, mkntcmdline(cmdline, "C:\\Users\\jart\\redbean.com", argv));
|
EXPECT_NE(-1, mkntcmdline(cmdline, "C:\\Users\\jart\\𝑟𝑒𝑑𝑏𝑒𝑎𝑛.com", argv));
|
||||||
EXPECT_STREQ(u"C:\\Users\\jart\\redbean.com --strace", cmdline);
|
EXPECT_STREQ(u"C:\\Users\\jart\\𝑟𝑒𝑑𝑏𝑒𝑎𝑛.com --strace", cmdline);
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,11 +175,11 @@ TEST(GetDosArgv, cmdToil) {
|
||||||
size_t size = ARG_MAX / 2;
|
size_t size = ARG_MAX / 2;
|
||||||
char *buf = malloc(size * sizeof(char));
|
char *buf = malloc(size * sizeof(char));
|
||||||
char **argv = malloc(max * sizeof(char *));
|
char **argv = malloc(max * sizeof(char *));
|
||||||
EXPECT_EQ(3, GetDosArgv(u"cmd.exe /C \"echo hi >\"\"\"foo bar.txt\"\"\"\"",
|
EXPECT_EQ(3, GetDosArgv(u"cmd.exe /C \"echo hi >\"\"\"𝑓𝑜𝑜 bar.txt\"\"\"\"",
|
||||||
buf, size, argv, max));
|
buf, size, argv, max));
|
||||||
EXPECT_STREQ("cmd.exe", argv[0]);
|
EXPECT_STREQ("cmd.exe", argv[0]);
|
||||||
EXPECT_STREQ("/C", argv[1]);
|
EXPECT_STREQ("/C", argv[1]);
|
||||||
EXPECT_STREQ("echo hi >\"foo bar.txt\"", argv[2]);
|
EXPECT_STREQ("echo hi >\"𝑓𝑜𝑜 bar.txt\"", argv[2]);
|
||||||
EXPECT_EQ(NULL, argv[3]);
|
EXPECT_EQ(NULL, argv[3]);
|
||||||
free(argv);
|
free(argv);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
|
|
@ -16,13 +16,14 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/errno.h"
|
||||||
#include "libc/intrin/bits.h"
|
#include "libc/intrin/bits.h"
|
||||||
#include "libc/log/check.h"
|
#include "libc/log/check.h"
|
||||||
#include "libc/math.h"
|
#include "libc/math.h"
|
||||||
#include "libc/nexgen32e/x86feature.h"
|
#include "libc/nexgen32e/x86feature.h"
|
||||||
|
#include "libc/runtime/runtime.h"
|
||||||
#include "libc/stdio/lcg.internal.h"
|
#include "libc/stdio/lcg.internal.h"
|
||||||
#include "libc/stdio/rand.h"
|
#include "libc/stdio/rand.h"
|
||||||
#include "libc/runtime/runtime.h"
|
|
||||||
#include "libc/stdio/stdio.h"
|
#include "libc/stdio/stdio.h"
|
||||||
#include "libc/sysv/consts/grnd.h"
|
#include "libc/sysv/consts/grnd.h"
|
||||||
#include "libc/testlib/ezbench.h"
|
#include "libc/testlib/ezbench.h"
|
||||||
|
@ -241,3 +242,7 @@ TEST(getrandom, sanityTest) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(getrandom, badflags_einval) {
|
||||||
|
ASSERT_SYS(EINVAL, -1, getrandom(0, 0, -1));
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue