@@ -8,6 +8,8 @@ const Coverage = std.debug.Coverage;
88const abi = std .Build .Fuzz .abi ;
99const log = std .log ;
1010const assert = std .debug .assert ;
11+ const Cache = std .Build .Cache ;
12+ const Path = Cache .Path ;
1113
1214const WebServer = @This ();
1315
@@ -31,6 +33,10 @@ coverage_mutex: std.Thread.Mutex,
3133/// Signaled when `coverage_files` changes.
3234coverage_condition : std.Thread.Condition ,
3335
36+ const fuzzer_bin_name = "fuzzer" ;
37+ const fuzzer_arch_os_abi = "wasm32-freestanding" ;
38+ const fuzzer_cpu_features = "baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext" ;
39+
3440const CoverageMap = struct {
3541 mapped_memory : []align (std .mem .page_size ) const u8 ,
3642 coverage : Coverage ,
@@ -181,9 +187,18 @@ fn serveWasm(
181187
182188 // Do the compilation every request, so that the user can edit the files
183189 // and see the changes without restarting the server.
184- const wasm_binary_path = try buildWasmBinary (ws , arena , optimize_mode );
190+ const wasm_base_path = try buildWasmBinary (ws , arena , optimize_mode );
191+ const bin_name = try std .zig .binNameAlloc (arena , .{
192+ .root_name = fuzzer_bin_name ,
193+ .target = std .zig .system .resolveTargetQuery (std .Build .parseTargetQuery (.{
194+ .arch_os_abi = fuzzer_arch_os_abi ,
195+ .cpu_features = fuzzer_cpu_features ,
196+ }) catch unreachable ) catch unreachable ,
197+ .output_mode = .Exe ,
198+ });
185199 // std.http.Server does not have a sendfile API yet.
186- const file_contents = try std .fs .cwd ().readFileAlloc (gpa , wasm_binary_path , 10 * 1024 * 1024 );
200+ const bin_path = try wasm_base_path .join (arena , bin_name );
201+ const file_contents = try bin_path .root_dir .handle .readFileAlloc (gpa , bin_path .sub_path , 10 * 1024 * 1024 );
187202 defer gpa .free (file_contents );
188203 try request .respond (file_contents , .{
189204 .extra_headers = &.{
@@ -197,7 +212,7 @@ fn buildWasmBinary(
197212 ws : * WebServer ,
198213 arena : Allocator ,
199214 optimize_mode : std.builtin.OptimizeMode ,
200- ) ! [] const u8 {
215+ ) ! Path {
201216 const gpa = ws .gpa ;
202217
203218 const main_src_path : Build.Cache.Path = .{
@@ -219,11 +234,11 @@ fn buildWasmBinary(
219234 ws .zig_exe_path , "build-exe" , //
220235 "-fno-entry" , //
221236 "-O" , @tagName (optimize_mode ), //
222- "-target" , "wasm32-freestanding" , //
223- "-mcpu" , "baseline+atomics+bulk_memory+multivalue+mutable_globals+nontrapping_fptoint+reference_types+sign_ext" , //
237+ "-target" , fuzzer_arch_os_abi , //
238+ "-mcpu" , fuzzer_cpu_features , //
224239 "--cache-dir" , ws .global_cache_directory .path orelse "." , //
225240 "--global-cache-dir" , ws .global_cache_directory .path orelse "." , //
226- "--name" , "fuzzer" , //
241+ "--name" , fuzzer_bin_name , //
227242 "-rdynamic" , //
228243 "-fsingle-threaded" , //
229244 "--dep" , "Walk" , //
@@ -251,7 +266,7 @@ fn buildWasmBinary(
251266 try sendMessage (child .stdin .? , .exit );
252267
253268 const Header = std .zig .Server .Message .Header ;
254- var result : ? [] const u8 = null ;
269+ var result : ? Path = null ;
255270 var result_error_bundle = std .zig .ErrorBundle .empty ;
256271
257272 const stdout = poller .fifo (.stdout );
@@ -288,13 +303,17 @@ fn buildWasmBinary(
288303 .extra = extra_array ,
289304 };
290305 },
291- .emit_bin_path = > {
292- const EbpHdr = std .zig .Server .Message .EmitBinPath ;
306+ .emit_digest = > {
307+ const EbpHdr = std .zig .Server .Message .EmitDigest ;
293308 const ebp_hdr = @as (* align (1 ) const EbpHdr , @ptrCast (body ));
294309 if (! ebp_hdr .flags .cache_hit ) {
295310 log .info ("source changes detected; rebuilt wasm component" , .{});
296311 }
297- result = try arena .dupe (u8 , body [@sizeOf (EbpHdr ).. ]);
312+ const digest = body [@sizeOf (EbpHdr ).. ][0.. Cache .bin_digest_len ];
313+ result = Path {
314+ .root_dir = ws .global_cache_directory ,
315+ .sub_path = try arena .dupe (u8 , "o" ++ std .fs .path .sep_str ++ Cache .binToHex (digest .* )),
316+ };
298317 },
299318 else = > {}, // ignore other messages
300319 }
@@ -568,10 +587,7 @@ fn prepareTables(
568587 };
569588 errdefer gop .value_ptr .coverage .deinit (gpa );
570589
571- const rebuilt_exe_path : Build.Cache.Path = .{
572- .root_dir = Build .Cache .Directory .cwd (),
573- .sub_path = run_step .rebuilt_executable .? ,
574- };
590+ const rebuilt_exe_path = run_step .rebuilt_executable .? ;
575591 var debug_info = std .debug .Info .load (gpa , rebuilt_exe_path , & gop .value_ptr .coverage ) catch | err | {
576592 log .err ("step '{s}': failed to load debug information for '{}': {s}" , .{
577593 run_step .step .name , rebuilt_exe_path , @errorName (err ),
0 commit comments