mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 15:28:30 +00:00
Fix a bunch of Windows bugs reported on Discord
This change addresses everything from stack smashing to %SYSTEMROOT% breaking socket(). Issues relating to compile.com not reporting text printed to stderr has been resolved for Windows builds.
This commit is contained in:
parent
b0e3258709
commit
5018171fa5
10 changed files with 104 additions and 46 deletions
|
@ -18,11 +18,13 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/ntspawn.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/intrin/_getenv.internal.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/alloca.h"
|
||||
#include "libc/mem/arraylist2.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/str/thompike.h"
|
||||
#include "libc/str/utf16.h"
|
||||
|
@ -91,12 +93,15 @@ static textwindows void FixPath(char *path) {
|
|||
}
|
||||
|
||||
static textwindows void InsertString(char **a, size_t i, char *s,
|
||||
char buf[ARG_MAX], size_t *bufi) {
|
||||
char buf[ARG_MAX], size_t *bufi,
|
||||
bool *have_systemroot) {
|
||||
char *v;
|
||||
size_t j, k;
|
||||
|
||||
v = StrChr(s, '=');
|
||||
|
||||
// apply fixups to var=/c/...
|
||||
if ((v = StrChr(s, '=')) && v[1] == '/' && IsAlpha(v[2]) && v[3] == '/') {
|
||||
if (v && v[1] == '/' && IsAlpha(v[2]) && v[3] == '/') {
|
||||
v = buf + *bufi;
|
||||
for (k = 0; s[k]; ++k) {
|
||||
if (*bufi + 1 < ARG_MAX) {
|
||||
|
@ -136,11 +141,25 @@ textwindows int mkntenvblock(char16_t envvars[ARG_MAX / 2], char *const envp[],
|
|||
uint64_t w;
|
||||
char **vars;
|
||||
wint_t x, y;
|
||||
bool have_systemroot = false;
|
||||
size_t i, j, k, n, m, bufi = 0;
|
||||
for (n = 0; envp[n];) n++;
|
||||
vars = alloca((n + 1) * sizeof(char *));
|
||||
for (i = 0; i < n; ++i) InsertString(vars, i, envp[i], buf, &bufi);
|
||||
if (extravar) InsertString(vars, n++, extravar, buf, &bufi);
|
||||
for (i = 0; i < n; ++i) {
|
||||
InsertString(vars, i, envp[i], buf, &bufi, &have_systemroot);
|
||||
}
|
||||
if (extravar) {
|
||||
InsertString(vars, n++, extravar, buf, &bufi, &have_systemroot);
|
||||
}
|
||||
if (!have_systemroot && environ) {
|
||||
// https://jpassing.com/2009/12/28/the-hidden-danger-of-forgetting-to-specify-systemroot-in-a-custom-environment-block/
|
||||
struct Env systemroot;
|
||||
systemroot = _getenv(environ, "SYSTEMROOT");
|
||||
if (systemroot.s) {
|
||||
InsertString(vars, n++, environ[systemroot.i], buf, &bufi,
|
||||
&have_systemroot);
|
||||
}
|
||||
}
|
||||
for (k = i = 0; i < n; ++i) {
|
||||
j = 0;
|
||||
v = false;
|
||||
|
|
|
@ -31,17 +31,15 @@
|
|||
* @param shdr is from GetElfSectionHeaderAddress(), or null
|
||||
* @return pointer to section data within image, or null if
|
||||
* 1. `shdr` was null, or
|
||||
* 2. `sh_size` was zero, or
|
||||
* 3, `sh_type` was `SHT_NOBITS`, or
|
||||
* 4. content wasn't contained within `[elf,elf+mapsize)`, or
|
||||
* 5. an arithmetic overflow occurred
|
||||
* 2. content wasn't contained within `[elf,elf+mapsize)`, or
|
||||
* 3. an arithmetic overflow occurred
|
||||
*/
|
||||
void *GetElfSectionAddress(const Elf64_Ehdr *elf, // validated
|
||||
size_t mapsize, // validated
|
||||
const Elf64_Shdr *shdr) { // foreign
|
||||
Elf64_Off last;
|
||||
if (!shdr) return 0;
|
||||
if (shdr->sh_size <= 0) return 0;
|
||||
if (!shdr->sh_size) return elf;
|
||||
if (shdr->sh_type == SHT_NOBITS) return 0;
|
||||
if (ckd_add(&last, shdr->sh_offset, shdr->sh_size)) return 0;
|
||||
if (last > mapsize) return 0;
|
||||
|
|
|
@ -18,16 +18,24 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/enum/accessmask.h"
|
||||
#include "libc/nt/enum/fileflagandattributes.h"
|
||||
#include "libc/nt/enum/filemapflags.h"
|
||||
#include "libc/nt/ipc.h"
|
||||
|
||||
static const struct DescribeFlags kPipeOpenFlags[] = {
|
||||
{kNtPipeAccessDuplex, "Duplex"}, // 0x00000003
|
||||
{kNtPipeAccessOutbound, "Outbound"}, // 0x00000002
|
||||
{kNtPipeAccessInbound, "Inbound"}, // 0x00000001
|
||||
{kNtPipeAccessDuplex, "kNtPipeAccessDuplex"},
|
||||
{kNtPipeAccessOutbound, "kNtPipeAccessOutbound"},
|
||||
{kNtPipeAccessInbound, "kNtPipeAccessInbound"},
|
||||
{kNtFileFlagOverlapped, "kNtFileFlagOverlapped"},
|
||||
{kNtFileFlagFirstPipeInstance, "kNtFileFlagFirstPipeInstance"},
|
||||
{kNtFileFlagWriteThrough, "kNtFileFlagWriteThrough"},
|
||||
{kNtWriteDac, "kNtWriteDac"},
|
||||
{kNtWriteOwner, "kNtWriteOwner"},
|
||||
{kNtAccessSystemSecurity, "kNtAccessSystemSecurity"},
|
||||
};
|
||||
|
||||
const char *(DescribeNtPipeOpenFlags)(char buf[64], uint32_t x) {
|
||||
return DescribeFlags(buf, 64, kPipeOpenFlags, ARRAYLEN(kPipeOpenFlags),
|
||||
"kNtPipeAccess", x);
|
||||
return DescribeFlags(buf, 64, kPipeOpenFlags, ARRAYLEN(kPipeOpenFlags), "",
|
||||
x);
|
||||
}
|
||||
|
|
|
@ -17,10 +17,13 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/describeflags.internal.h"
|
||||
#include "libc/nt/struct/securityattributes.h"
|
||||
|
||||
const char *DescribeNtSecurityAttributes(struct NtSecurityAttributes *p) {
|
||||
const char *(DescribeNtSecurityAttributes)(char buf[32],
|
||||
struct NtSecurityAttributes *p) {
|
||||
if (p == &kNtIsInheritable) return "&kNtIsInheritable";
|
||||
return "0";
|
||||
FormatInt64(buf, (uintptr_t)p);
|
||||
return buf;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_NT_STRUCT_SECURITYATTRIBUTES_H_
|
||||
#define COSMOPOLITAN_LIBC_NT_STRUCT_SECURITYATTRIBUTES_H_
|
||||
#include "libc/mem/alloca.h"
|
||||
#include "libc/nt/struct/securitydescriptor.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
|
||||
|
@ -9,7 +10,10 @@ struct NtSecurityAttributes {
|
|||
bool32 bInheritHandle;
|
||||
};
|
||||
|
||||
const char *DescribeNtSecurityAttributes(struct NtSecurityAttributes *);
|
||||
const char *DescribeNtSecurityAttributes(char[32],
|
||||
struct NtSecurityAttributes *);
|
||||
#define DescribeNtSecurityAttributes(x) \
|
||||
DescribeNtSecurityAttributes(alloca(32), x)
|
||||
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_NT_STRUCT_SECURITYATTRIBUTES_H_ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue