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:
Justine Tunney 2023-07-28 06:17:34 -07:00
parent b0e3258709
commit 5018171fa5
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
10 changed files with 104 additions and 46 deletions

View file

@ -201,17 +201,18 @@ char buf[PAGESIZE];
char tmpout[PATH_MAX];
const char *const kSafeEnv[] = {
"ADDR2LINE", // needed by GetAddr2linePath
"HOME", // needed by ~/.runit.psk
"HOMEDRIVE", // needed by ~/.runit.psk
"HOMEPATH", // needed by ~/.runit.psk
"MAKEFLAGS", // needed by IsRunningUnderMake
"MODE", // needed by test scripts
"PATH", // needed by clang
"PWD", // just seems plain needed
"STRACE", // useful for troubleshooting
"TERM", // needed to detect colors
"TMPDIR", // needed by compiler
"ADDR2LINE", // needed by GetAddr2linePath
"HOME", // needed by ~/.runit.psk
"HOMEDRIVE", // needed by ~/.runit.psk
"HOMEPATH", // needed by ~/.runit.psk
"MAKEFLAGS", // needed by IsRunningUnderMake
"MODE", // needed by test scripts
"PATH", // needed by clang
"PWD", // just seems plain needed
"STRACE", // useful for troubleshooting
"TERM", // needed to detect colors
"TMPDIR", // needed by compiler
"SYSTEMROOT", // needed by socket()
};
const char *const kGccOnlyFlags[] = {
@ -373,7 +374,11 @@ bool IsSafeEnv(const char *s) {
r = ARRAYLEN(kSafeEnv) - 1;
while (l <= r) {
m = (l & r) + ((l ^ r) >> 1); // floor((a+b)/2)
x = strncmp(s, kSafeEnv[m], n);
if (IsWindows()) {
x = strncasecmp(s, kSafeEnv[m], n);
} else {
x = strncmp(s, kSafeEnv[m], n);
}
if (x < 0) {
r = m - 1;
} else if (x > 0) {
@ -643,10 +648,6 @@ int Launch(void) {
close(pipefds[1]);
for (;;) {
if (gotchld) {
rc = 0;
break;
}
if (gotalrm) {
PrintRed();
appends(&output, "\n\n`");

View file

@ -176,13 +176,13 @@ static void RewriteTlsCode(void) {
uint32_t *p, *pe;
for (i = 0; i < elf->e_shnum; ++i) {
if (!(shdr = GetElfSectionHeaderAddress(elf, esize, i))) {
Die("elf header overflow");
Die("elf header overflow #1");
}
if (shdr->sh_type == SHT_PROGBITS && //
(shdr->sh_flags & SHF_ALLOC) && //
(shdr->sh_flags & SHF_EXECINSTR)) {
if (!(p = GetElfSectionAddress(elf, esize, shdr))) {
Die("elf header overflow");
Die("elf header overflow #2");
}
for (pe = p + shdr->sh_size / 4; p <= pe; ++p) {
if ((*p & -32) == MRS_TPIDR_EL0) {
@ -213,11 +213,11 @@ static void OptimizePatchableFunctionEntries(void) {
if (!syms[i].st_size) continue;
if (ELF64_ST_TYPE(syms[i].st_info) != STT_FUNC) continue;
if (!(shdr = GetElfSectionHeaderAddress(elf, esize, syms[i].st_shndx))) {
Die("elf header overflow");
Die("elf header overflow #3");
}
if (shdr->sh_type != SHT_PROGBITS) continue;
if (!(p = GetElfSectionAddress(elf, esize, shdr))) {
Die("elf header overflow");
Die("elf header overflow #4");
}
if (syms[i].st_value < shdr->sh_addr) {
Die("elf symbol beneath section");

View file

@ -651,9 +651,9 @@ int main(int argc, char *argv[]) {
if (argc == 2 && !strcmp(argv[1], "-n")) {
exit(0);
}
if (!IsOptimized()) {
ShowCrashReports();
}
#ifndef NDEBUG
ShowCrashReports();
#endif
bzero(&pkg, sizeof(pkg));
bzero(&deps, sizeof(deps));
Package(argc, argv, &pkg, &deps);

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/elf/def.h"
@ -139,16 +140,27 @@ void ProcessFile(struct ElfWriter *elf, const char *path) {
size_t pathlen;
struct stat st;
const char *name;
CHECK_NE(-1, (fd = open(path, O_RDONLY)));
CHECK_NE(-1, fstat(fd, &st));
if (stat(path, &st)) {
perror(path);
exit(1);
}
if (S_ISDIR(st.st_mode)) {
if ((fd = open(path, O_RDONLY | O_DIRECTORY)) == -1) {
perror(path);
exit(1);
}
map = "";
st.st_size = 0;
} else if (st.st_size) {
CHECK_NE(MAP_FAILED,
(map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)));
if ((fd = open(path, O_RDONLY)) == -1 ||
(map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) ==
MAP_FAILED) {
perror(path);
exit(1);
}
} else {
map = NULL;
fd = -1;
map = 0;
}
if (name_) {
name = name_;
@ -166,7 +178,9 @@ void ProcessFile(struct ElfWriter *elf, const char *path) {
}
elfwriter_zip(elf, name, name, strlen(name), map, st.st_size, st.st_mode,
timestamp, timestamp, timestamp, nocompress_);
if (st.st_size) CHECK_NE(-1, munmap(map, st.st_size));
if (st.st_size) {
unassert(!munmap(map, st.st_size));
}
close(fd);
}