authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2023-06-25 11:24:54+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-06-25 11:24:54+02:00
loge7f872c9c6063e3d4e4f9bf46e998204b72fa84a
treeb8859c4b3ab5fa6b455ddbae6559fa12efd173cb
parentb11170294052585ae81b9946d770f860d163da8f
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

wasi-libc: compile emmalloc.c without strict aliasing (#16157)

emmalloc.c does a fair amount of type punning in order to access the size of memory regions and traverse them. Unfortunately, that can lead to unwanted optimizations. This simple test case currently triggers a memory fault: int main(void) { char * volatile p = malloc(1); p = realloc(p, 12); p = malloc(1); printf("%p\n", p); } Work around this by adding "-fno-strict-aliasing" when compiling that file.

1 files changed, 21 insertions(+), 12 deletions(-)

src/wasi_libc.zig+21-12
......@@ -72,7 +72,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
7272 switch (crt_file) {
7373 .crt1_reactor_o => {
7474 var args = std.ArrayList([]const u8).init(arena);
75 try addCCArgs(comp, arena, &args, false);
75 try addCCArgs(comp, arena, &args, .{});
7676 try addLibcBottomHalfIncludes(comp, arena, &args);
7777 return comp.build_crt_file("crt1-reactor", .Obj, .@"wasi crt1-reactor.o", prog_node, &.{
7878 .{
......@@ -85,7 +85,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
8585 },
8686 .crt1_command_o => {
8787 var args = std.ArrayList([]const u8).init(arena);
88 try addCCArgs(comp, arena, &args, false);
88 try addCCArgs(comp, arena, &args, .{});
8989 try addLibcBottomHalfIncludes(comp, arena, &args);
9090 return comp.build_crt_file("crt1-command", .Obj, .@"wasi crt1-command.o", prog_node, &.{
9191 .{
......@@ -102,7 +102,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
102102 {
103103 // Compile emmalloc.
104104 var args = std.ArrayList([]const u8).init(arena);
105 try addCCArgs(comp, arena, &args, true);
105 try addCCArgs(comp, arena, &args, .{ .want_O3 = true, .no_strict_aliasing = true });
106106 for (emmalloc_src_files) |file_path| {
107107 try libc_sources.append(.{
108108 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
......@@ -116,7 +116,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
116116 {
117117 // Compile libc-bottom-half.
118118 var args = std.ArrayList([]const u8).init(arena);
119 try addCCArgs(comp, arena, &args, true);
119 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
120120 try addLibcBottomHalfIncludes(comp, arena, &args);
121121
122122 for (libc_bottom_half_src_files) |file_path| {
......@@ -132,7 +132,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
132132 {
133133 // Compile libc-top-half.
134134 var args = std.ArrayList([]const u8).init(arena);
135 try addCCArgs(comp, arena, &args, true);
135 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
136136 try addLibcTopHalfIncludes(comp, arena, &args);
137137
138138 for (libc_top_half_src_files) |file_path| {
......@@ -149,7 +149,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
149149 },
150150 .libwasi_emulated_process_clocks_a => {
151151 var args = std.ArrayList([]const u8).init(arena);
152 try addCCArgs(comp, arena, &args, true);
152 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
153153 try addLibcBottomHalfIncludes(comp, arena, &args);
154154
155155 var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
......@@ -165,7 +165,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
165165 },
166166 .libwasi_emulated_getpid_a => {
167167 var args = std.ArrayList([]const u8).init(arena);
168 try addCCArgs(comp, arena, &args, true);
168 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
169169 try addLibcBottomHalfIncludes(comp, arena, &args);
170170
171171 var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
......@@ -181,7 +181,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
181181 },
182182 .libwasi_emulated_mman_a => {
183183 var args = std.ArrayList([]const u8).init(arena);
184 try addCCArgs(comp, arena, &args, true);
184 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
185185 try addLibcBottomHalfIncludes(comp, arena, &args);
186186
187187 var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
......@@ -200,7 +200,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
200200
201201 {
202202 var args = std.ArrayList([]const u8).init(arena);
203 try addCCArgs(comp, arena, &args, true);
203 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
204204
205205 for (emulated_signal_bottom_half_src_files) |file_path| {
206206 try emu_signal_sources.append(.{
......@@ -214,7 +214,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
214214
215215 {
216216 var args = std.ArrayList([]const u8).init(arena);
217 try addCCArgs(comp, arena, &args, true);
217 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
218218 try addLibcTopHalfIncludes(comp, arena, &args);
219219 try args.append("-D_WASI_EMULATED_SIGNAL");
220220
......@@ -249,17 +249,22 @@ fn sanitize(arena: Allocator, file_path: []const u8) ![]const u8 {
249249 return out_path;
250250}
251251
252const CCOptions = struct {
253 want_O3: bool = false,
254 no_strict_aliasing: bool = false,
255};
256
252257fn addCCArgs(
253258 comp: *Compilation,
254259 arena: Allocator,
255260 args: *std.ArrayList([]const u8),
256 want_O3: bool,
261 options: CCOptions,
257262) error{OutOfMemory}!void {
258263 const target = comp.getTarget();
259264 const arch_name = musl.archNameHeaders(target.cpu.arch);
260265 const os_name = @tagName(target.os.tag);
261266 const triple = try std.fmt.allocPrint(arena, "{s}-{s}-musl", .{ arch_name, os_name });
262 const o_arg = if (want_O3) "-O3" else "-Os";
267 const o_arg = if (options.want_O3) "-O3" else "-Os";
263268
264269 try args.appendSlice(&[_][]const u8{
265270 "-std=gnu17",
......@@ -280,6 +285,10 @@ fn addCCArgs(
280285
281286 "-DBULK_MEMORY_THRESHOLD=32",
282287 });
288
289 if (options.no_strict_aliasing) {
290 try args.appendSlice(&[_][]const u8{"-fno-strict-aliasing"});
291 }
283292}
284293
285294fn addLibcBottomHalfIncludes(