authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-03-13 00:52:28+11:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-01 10:36:38-04:00
log7eb938c9096db54fe6e99148824252904c79c227
tree1eaf50073504d7005ed787ac798d5f56dd20bb57
parente535057364d33819001e55d34d104916cfab1b91
signaturelock-open Commit is signed but in an unrecognized format.

Use length field as passed in stage2 libc_installation instead of relying on zero termination


6 files changed, 115 insertions(+), 68 deletions(-)

src-self-hosted/libc_installation.zig+62-39
...@@ -14,11 +14,11 @@ usingnamespace @import("windows_sdk.zig");...@@ -14,11 +14,11 @@ usingnamespace @import("windows_sdk.zig");
1414
15/// See the render function implementation for documentation of the fields.15/// See the render function implementation for documentation of the fields.
16pub const LibCInstallation = struct {16pub const LibCInstallation = struct {
17 include_dir: ?[:0]const u8 = null,17 include_dir: ?[]const u8 = null,
18 sys_include_dir: ?[:0]const u8 = null,18 sys_include_dir: ?[]const u8 = null,
19 crt_dir: ?[:0]const u8 = null,19 crt_dir: ?[]const u8 = null,
20 msvc_lib_dir: ?[:0]const u8 = null,20 msvc_lib_dir: ?[]const u8 = null,
21 kernel32_lib_dir: ?[:0]const u8 = null,21 kernel32_lib_dir: ?[]const u8 = null,
2222
23 pub const FindError = error{23 pub const FindError = error{
24 OutOfMemory,24 OutOfMemory,
...@@ -327,15 +327,20 @@ pub const LibCInstallation = struct {...@@ -327,15 +327,20 @@ pub const LibCInstallation = struct {
327 var search_buf: [2]Search = undefined;327 var search_buf: [2]Search = undefined;
328 const searches = fillSearch(&search_buf, sdk);328 const searches = fillSearch(&search_buf, sdk);
329329
330 var result_buf = try std.Buffer.initSize(allocator, 0);
331 defer result_buf.deinit();
332
333 for (searches) |search| {330 for (searches) |search| {
334 result_buf.shrink(0);331 const dir_path = try fs.path.join(
335 const stream = result_buf.outStream();332 allocator,
336 try stream.print("{}\\Include\\{}\\ucrt", .{ search.path, search.version });333 &[_][]const u8{
337334 search.path,
338 var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) {335 "Include",
336 search.version,
337 "ucrt",
338 },
339 );
340 var found = false;
341 defer if (!found) allocator.free(dir_path);
342
343 var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
339 error.FileNotFound,344 error.FileNotFound,
340 error.NotDir,345 error.NotDir,
341 error.NoDevice,346 error.NoDevice,
...@@ -350,7 +355,8 @@ pub const LibCInstallation = struct {...@@ -350,7 +355,8 @@ pub const LibCInstallation = struct {
350 else => return error.FileSystem,355 else => return error.FileSystem,
351 };356 };
352357
353 self.include_dir = result_buf.toOwnedSlice();358 found = true;
359 self.include_dir = dir_path;
354 return;360 return;
355 }361 }
356362
...@@ -367,9 +373,6 @@ pub const LibCInstallation = struct {...@@ -367,9 +373,6 @@ pub const LibCInstallation = struct {
367 var search_buf: [2]Search = undefined;373 var search_buf: [2]Search = undefined;
368 const searches = fillSearch(&search_buf, sdk);374 const searches = fillSearch(&search_buf, sdk);
369375
370 var result_buf = try std.Buffer.initSize(allocator, 0);
371 defer result_buf.deinit();
372
373 const arch_sub_dir = switch (builtin.arch) {376 const arch_sub_dir = switch (builtin.arch) {
374 .i386 => "x86",377 .i386 => "x86",
375 .x86_64 => "x64",378 .x86_64 => "x64",
...@@ -378,11 +381,20 @@ pub const LibCInstallation = struct {...@@ -378,11 +381,20 @@ pub const LibCInstallation = struct {
378 };381 };
379382
380 for (searches) |search| {383 for (searches) |search| {
381 result_buf.shrink(0);384 const dir_path = try fs.path.join(
382 const stream = result_buf.outStream();385 allocator,
383 try stream.print("{}\\Lib\\{}\\ucrt\\{}", .{ search.path, search.version, arch_sub_dir });386 &[_][]const u8{
384387 search.path,
385 var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) {388 "Lib",
389 search.version,
390 "ucrt",
391 arch_sub_dir,
392 },
393 );
394 var found = false;
395 defer if (!found) allocator.free(dir_path);
396
397 var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
386 error.FileNotFound,398 error.FileNotFound,
387 error.NotDir,399 error.NotDir,
388 error.NoDevice,400 error.NoDevice,
...@@ -397,7 +409,8 @@ pub const LibCInstallation = struct {...@@ -397,7 +409,8 @@ pub const LibCInstallation = struct {
397 else => return error.FileSystem,409 else => return error.FileSystem,
398 };410 };
399411
400 self.crt_dir = result_buf.toOwnedSlice();412 found = true;
413 self.crt_dir = dir_path;
401 return;414 return;
402 }415 }
403 return error.LibCRuntimeNotFound;416 return error.LibCRuntimeNotFound;
...@@ -421,10 +434,6 @@ pub const LibCInstallation = struct {...@@ -421,10 +434,6 @@ pub const LibCInstallation = struct {
421434
422 var search_buf: [2]Search = undefined;435 var search_buf: [2]Search = undefined;
423 const searches = fillSearch(&search_buf, sdk);436 const searches = fillSearch(&search_buf, sdk);
424
425 var result_buf = try std.Buffer.initSize(allocator, 0);
426 defer result_buf.deinit();
427
428 const arch_sub_dir = switch (builtin.arch) {437 const arch_sub_dir = switch (builtin.arch) {
429 .i386 => "x86",438 .i386 => "x86",
430 .x86_64 => "x64",439 .x86_64 => "x64",
...@@ -433,11 +442,20 @@ pub const LibCInstallation = struct {...@@ -433,11 +442,20 @@ pub const LibCInstallation = struct {
433 };442 };
434443
435 for (searches) |search| {444 for (searches) |search| {
436 result_buf.shrink(0);445 const dir_path = try fs.path.join(
437 const stream = result_buf.outStream();446 allocator,
438 try stream.print("{}\\Lib\\{}\\um\\{}", .{ search.path, search.version, arch_sub_dir });447 &[_][]const u8{
439448 search.path,
440 var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) {449 "Lib",
450 search.version,
451 "um",
452 arch_sub_dir,
453 },
454 );
455 var found = false;
456 defer if (!found) allocator.free(dir_path);
457
458 var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
441 error.FileNotFound,459 error.FileNotFound,
442 error.NotDir,460 error.NotDir,
443 error.NoDevice,461 error.NoDevice,
...@@ -452,7 +470,8 @@ pub const LibCInstallation = struct {...@@ -452,7 +470,8 @@ pub const LibCInstallation = struct {
452 else => return error.FileSystem,470 else => return error.FileSystem,
453 };471 };
454472
455 self.kernel32_lib_dir = result_buf.toOwnedSlice();473 found = true;
474 self.kernel32_lib_dir = dir_path;
456 return;475 return;
457 }476 }
458 return error.LibCKernel32LibNotFound;477 return error.LibCKernel32LibNotFound;
...@@ -470,12 +489,16 @@ pub const LibCInstallation = struct {...@@ -470,12 +489,16 @@ pub const LibCInstallation = struct {
470 const up1 = fs.path.dirname(msvc_lib_dir) orelse return error.LibCStdLibHeaderNotFound;489 const up1 = fs.path.dirname(msvc_lib_dir) orelse return error.LibCStdLibHeaderNotFound;
471 const up2 = fs.path.dirname(up1) orelse return error.LibCStdLibHeaderNotFound;490 const up2 = fs.path.dirname(up1) orelse return error.LibCStdLibHeaderNotFound;
472491
473 var result_buf = try std.Buffer.init(allocator, up2);492 const dir_path = try fs.path.join(
474 defer result_buf.deinit();493 allocator,
475494 &[_][]const u8{
476 try result_buf.append("\\include");495 up2,
496 "include",
497 },
498 );
499 errdefer allocator.free(dir_path);
477500
478 var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) {501 var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
479 error.FileNotFound,502 error.FileNotFound,
480 error.NotDir,503 error.NotDir,
481 error.NoDevice,504 error.NoDevice,
...@@ -490,7 +513,7 @@ pub const LibCInstallation = struct {...@@ -490,7 +513,7 @@ pub const LibCInstallation = struct {
490 else => return error.FileSystem,513 else => return error.FileSystem,
491 };514 };
492515
493 self.sys_include_dir = result_buf.toOwnedSlice();516 self.sys_include_dir = dir_path;
494 }517 }
495518
496 fn findNativeMsvcLibDir(519 fn findNativeMsvcLibDir(
src-self-hosted/stage2.zig+10-10
...@@ -741,15 +741,15 @@ fn stage2TargetParse(...@@ -741,15 +741,15 @@ fn stage2TargetParse(
741741
742// ABI warning742// ABI warning
743const Stage2LibCInstallation = extern struct {743const Stage2LibCInstallation = extern struct {
744 include_dir: [*:0]const u8,744 include_dir: [*]const u8,
745 include_dir_len: usize,745 include_dir_len: usize,
746 sys_include_dir: [*:0]const u8,746 sys_include_dir: [*]const u8,
747 sys_include_dir_len: usize,747 sys_include_dir_len: usize,
748 crt_dir: [*:0]const u8,748 crt_dir: [*]const u8,
749 crt_dir_len: usize,749 crt_dir_len: usize,
750 msvc_lib_dir: [*:0]const u8,750 msvc_lib_dir: [*]const u8,
751 msvc_lib_dir_len: usize,751 msvc_lib_dir_len: usize,
752 kernel32_lib_dir: [*:0]const u8,752 kernel32_lib_dir: [*]const u8,
753 kernel32_lib_dir_len: usize,753 kernel32_lib_dir_len: usize,
754754
755 fn initFromStage2(self: *Stage2LibCInstallation, libc: LibCInstallation) void {755 fn initFromStage2(self: *Stage2LibCInstallation, libc: LibCInstallation) void {
...@@ -793,19 +793,19 @@ const Stage2LibCInstallation = extern struct {...@@ -793,19 +793,19 @@ const Stage2LibCInstallation = extern struct {
793 fn toStage2(self: Stage2LibCInstallation) LibCInstallation {793 fn toStage2(self: Stage2LibCInstallation) LibCInstallation {
794 var libc: LibCInstallation = .{};794 var libc: LibCInstallation = .{};
795 if (self.include_dir_len != 0) {795 if (self.include_dir_len != 0) {
796 libc.include_dir = self.include_dir[0..self.include_dir_len :0];796 libc.include_dir = self.include_dir[0..self.include_dir_len];
797 }797 }
798 if (self.sys_include_dir_len != 0) {798 if (self.sys_include_dir_len != 0) {
799 libc.sys_include_dir = self.sys_include_dir[0..self.sys_include_dir_len :0];799 libc.sys_include_dir = self.sys_include_dir[0..self.sys_include_dir_len];
800 }800 }
801 if (self.crt_dir_len != 0) {801 if (self.crt_dir_len != 0) {
802 libc.crt_dir = self.crt_dir[0..self.crt_dir_len :0];802 libc.crt_dir = self.crt_dir[0..self.crt_dir_len];
803 }803 }
804 if (self.msvc_lib_dir_len != 0) {804 if (self.msvc_lib_dir_len != 0) {
805 libc.msvc_lib_dir = self.msvc_lib_dir[0..self.msvc_lib_dir_len :0];805 libc.msvc_lib_dir = self.msvc_lib_dir[0..self.msvc_lib_dir_len];
806 }806 }
807 if (self.kernel32_lib_dir_len != 0) {807 if (self.kernel32_lib_dir_len != 0) {
808 libc.kernel32_lib_dir = self.kernel32_lib_dir[0..self.kernel32_lib_dir_len :0];808 libc.kernel32_lib_dir = self.kernel32_lib_dir[0..self.kernel32_lib_dir_len];
809 }809 }
810 return libc;810 return libc;
811 }811 }
src/cache_hash.cpp+7-1
...@@ -27,11 +27,17 @@ void cache_init(CacheHash *ch, Buf *manifest_dir) {...@@ -27,11 +27,17 @@ void cache_init(CacheHash *ch, Buf *manifest_dir) {
27void cache_mem(CacheHash *ch, const char *ptr, size_t len) {27void cache_mem(CacheHash *ch, const char *ptr, size_t len) {
28 assert(ch->manifest_file_path == nullptr);28 assert(ch->manifest_file_path == nullptr);
29 assert(ptr != nullptr);29 assert(ptr != nullptr);
30 // + 1 to include the null byte
31 blake2b_update(&ch->blake, ptr, len);30 blake2b_update(&ch->blake, ptr, len);
32}31}
3332
33void cache_slice(CacheHash *ch, Slice<const char> slice) {
34 // mix the length into the hash so that two juxtaposed cached slices can't collide
35 cache_usize(ch, slice.len);
36 cache_mem(ch, slice.ptr, slice.len);
37}
38
34void cache_str(CacheHash *ch, const char *ptr) {39void cache_str(CacheHash *ch, const char *ptr) {
40 // + 1 to include the null byte
35 cache_mem(ch, ptr, strlen(ptr) + 1);41 cache_mem(ch, ptr, strlen(ptr) + 1);
36}42}
3743
src/cache_hash.hpp+1
...@@ -36,6 +36,7 @@ void cache_init(CacheHash *ch, Buf *manifest_dir);...@@ -36,6 +36,7 @@ void cache_init(CacheHash *ch, Buf *manifest_dir);
3636
37// Next, use the hash population functions to add the initial parameters.37// Next, use the hash population functions to add the initial parameters.
38void cache_mem(CacheHash *ch, const char *ptr, size_t len);38void cache_mem(CacheHash *ch, const char *ptr, size_t len);
39void cache_slice(CacheHash *ch, Slice<const char> slice);
39void cache_str(CacheHash *ch, const char *ptr);40void cache_str(CacheHash *ch, const char *ptr);
40void cache_int(CacheHash *ch, int x);41void cache_int(CacheHash *ch, int x);
41void cache_bool(CacheHash *ch, bool x);42void cache_bool(CacheHash *ch, bool x);
src/codegen.cpp+16-11
...@@ -9123,21 +9123,26 @@ static void detect_libc(CodeGen *g) {...@@ -9123,21 +9123,26 @@ static void detect_libc(CodeGen *g) {
9123 g->libc_include_dir_len = 0;9123 g->libc_include_dir_len = 0;
9124 g->libc_include_dir_list = heap::c_allocator.allocate<const char *>(dir_count);9124 g->libc_include_dir_list = heap::c_allocator.allocate<const char *>(dir_count);
91259125
9126 g->libc_include_dir_list[g->libc_include_dir_len] = g->libc->include_dir;9126 g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem(g->libc->include_dir, g->libc->include_dir_len));
9127 g->libc_include_dir_len += 1;9127 g->libc_include_dir_len += 1;
91289128
9129 if (want_sys_dir) {9129 if (want_sys_dir) {
9130 g->libc_include_dir_list[g->libc_include_dir_len] = g->libc->sys_include_dir;9130 g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem(g->libc->sys_include_dir, g->libc->sys_include_dir_len));
9131 g->libc_include_dir_len += 1;9131 g->libc_include_dir_len += 1;
9132 }9132 }
91339133
9134 if (want_um_and_shared_dirs != 0) {9134 if (want_um_and_shared_dirs != 0) {
9135 g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_sprintf(9135 Buf *include_dir_parent = buf_alloc();
9136 "%s" OS_SEP ".." OS_SEP "um", g->libc->include_dir));9136 os_path_join(buf_create_from_mem(g->libc->include_dir, g->libc->include_dir_len), buf_create_from_str(".."), include_dir_parent);
9137
9138 Buf *buff1 = buf_alloc();
9139 os_path_join(include_dir_parent, buf_create_from_str("um"), buff1);
9140 g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buff1);
9137 g->libc_include_dir_len += 1;9141 g->libc_include_dir_len += 1;
91389142
9139 g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_sprintf(9143 Buf *buff2 = buf_alloc();
9140 "%s" OS_SEP ".." OS_SEP "shared", g->libc->include_dir));9144 os_path_join(include_dir_parent, buf_create_from_str("shared"), buff2);
9145 g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buff2);
9141 g->libc_include_dir_len += 1;9146 g->libc_include_dir_len += 1;
9142 }9147 }
9143 assert(g->libc_include_dir_len == dir_count);9148 assert(g->libc_include_dir_len == dir_count);
...@@ -10546,11 +10551,11 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -10546,11 +10551,11 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
10546 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);10551 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);
10547 cache_list_of_str(ch, g->framework_dirs.items, g->framework_dirs.length);10552 cache_list_of_str(ch, g->framework_dirs.items, g->framework_dirs.length);
10548 if (g->libc) {10553 if (g->libc) {
10549 cache_str(ch, g->libc->include_dir);10554 cache_slice(ch, Slice<const char>{g->libc->include_dir, g->libc->include_dir_len});
10550 cache_str(ch, g->libc->sys_include_dir);10555 cache_slice(ch, Slice<const char>{g->libc->sys_include_dir, g->libc->sys_include_dir_len});
10551 cache_str(ch, g->libc->crt_dir);10556 cache_slice(ch, Slice<const char>{g->libc->crt_dir, g->libc->crt_dir_len});
10552 cache_str(ch, g->libc->msvc_lib_dir);10557 cache_slice(ch, Slice<const char>{g->libc->msvc_lib_dir, g->libc->msvc_lib_dir_len});
10553 cache_str(ch, g->libc->kernel32_lib_dir);10558 cache_slice(ch, Slice<const char>{g->libc->kernel32_lib_dir, g->libc->kernel32_lib_dir_len});
10554 }10559 }
10555 cache_buf_opt(ch, g->version_script_path);10560 cache_buf_opt(ch, g->version_script_path);
10556 cache_buf_opt(ch, g->override_soname);10561 cache_buf_opt(ch, g->override_soname);
src/link.cpp+19-7
...@@ -1595,7 +1595,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr...@@ -1595,7 +1595,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
1595 } else {1595 } else {
1596 assert(parent->libc != nullptr);1596 assert(parent->libc != nullptr);
1597 Buf *out_buf = buf_alloc();1597 Buf *out_buf = buf_alloc();
1598 os_path_join(buf_create_from_str(parent->libc->crt_dir), buf_create_from_str(file), out_buf);1598 os_path_join(buf_create_from_mem(parent->libc->crt_dir, parent->libc->crt_dir_len), buf_create_from_str(file), out_buf);
1599 return buf_ptr(out_buf);1599 return buf_ptr(out_buf);
1600 }1600 }
1601}1601}
...@@ -1860,7 +1860,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -1860,7 +1860,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
1860 if (g->libc_link_lib != nullptr) {1860 if (g->libc_link_lib != nullptr) {
1861 if (g->libc != nullptr) {1861 if (g->libc != nullptr) {
1862 lj->args.append("-L");1862 lj->args.append("-L");
1863 lj->args.append(g->libc->crt_dir);1863 lj->args.append(buf_ptr(buf_create_from_mem(g->libc->crt_dir, g->libc->crt_dir_len)));
1864 }1864 }
18651865
1866 if (g->have_dynamic_link && (is_dyn_lib || g->out_type == OutTypeExe)) {1866 if (g->have_dynamic_link && (is_dyn_lib || g->out_type == OutTypeExe)) {
...@@ -2381,14 +2381,26 @@ static void construct_linker_job_coff(LinkJob *lj) {...@@ -2381,14 +2381,26 @@ static void construct_linker_job_coff(LinkJob *lj) {
2381 lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&g->bin_file_output_path))));2381 lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&g->bin_file_output_path))));
23822382
2383 if (g->libc_link_lib != nullptr && g->libc != nullptr) {2383 if (g->libc_link_lib != nullptr && g->libc != nullptr) {
2384 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", g->libc->crt_dir)));2384 Buf *buff0 = buf_create_from_str("-LIBPATH:");
2385 buf_append_mem(buff0, g->libc->crt_dir, g->libc->crt_dir_len);
2386 lj->args.append(buf_ptr(buff0));
23852387
2386 if (target_abi_is_gnu(g->zig_target->abi)) {2388 if (target_abi_is_gnu(g->zig_target->abi)) {
2387 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", g->libc->sys_include_dir)));2389 Buf *buff1 = buf_create_from_str("-LIBPATH:");
2388 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", g->libc->include_dir)));2390 buf_append_mem(buff1, g->libc->sys_include_dir, g->libc->sys_include_dir_len);
2391 lj->args.append(buf_ptr(buff1));
2392
2393 Buf *buff2 = buf_create_from_str("-LIBPATH:");
2394 buf_append_mem(buff2, g->libc->include_dir, g->libc->include_dir_len);
2395 lj->args.append(buf_ptr(buff2));
2389 } else {2396 } else {
2390 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", g->libc->msvc_lib_dir)));2397 Buf *buff1 = buf_create_from_str("-LIBPATH:");
2391 lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", g->libc->kernel32_lib_dir)));2398 buf_append_mem(buff1, g->libc->msvc_lib_dir, g->libc->msvc_lib_dir_len);
2399 lj->args.append(buf_ptr(buff1));
2400
2401 Buf *buff2 = buf_create_from_str("-LIBPATH:");
2402 buf_append_mem(buff2, g->libc->kernel32_lib_dir, g->libc->kernel32_lib_dir_len);
2403 lj->args.append(buf_ptr(buff2));
2392 }2404 }
2393 }2405 }
23942406