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...@@ -72,7 +72,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
72 switch (crt_file) {72 switch (crt_file) {
73 .crt1_reactor_o => {73 .crt1_reactor_o => {
74 var args = std.ArrayList([]const u8).init(arena);74 var args = std.ArrayList([]const u8).init(arena);
75 try addCCArgs(comp, arena, &args, false);75 try addCCArgs(comp, arena, &args, .{});
76 try addLibcBottomHalfIncludes(comp, arena, &args);76 try addLibcBottomHalfIncludes(comp, arena, &args);
77 return comp.build_crt_file("crt1-reactor", .Obj, .@"wasi crt1-reactor.o", prog_node, &.{77 return comp.build_crt_file("crt1-reactor", .Obj, .@"wasi crt1-reactor.o", prog_node, &.{
78 .{78 .{
...@@ -85,7 +85,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr...@@ -85,7 +85,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
85 },85 },
86 .crt1_command_o => {86 .crt1_command_o => {
87 var args = std.ArrayList([]const u8).init(arena);87 var args = std.ArrayList([]const u8).init(arena);
88 try addCCArgs(comp, arena, &args, false);88 try addCCArgs(comp, arena, &args, .{});
89 try addLibcBottomHalfIncludes(comp, arena, &args);89 try addLibcBottomHalfIncludes(comp, arena, &args);
90 return comp.build_crt_file("crt1-command", .Obj, .@"wasi crt1-command.o", prog_node, &.{90 return comp.build_crt_file("crt1-command", .Obj, .@"wasi crt1-command.o", prog_node, &.{
91 .{91 .{
...@@ -102,7 +102,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr...@@ -102,7 +102,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
102 {102 {
103 // Compile emmalloc.103 // Compile emmalloc.
104 var args = std.ArrayList([]const u8).init(arena);104 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 });
106 for (emmalloc_src_files) |file_path| {106 for (emmalloc_src_files) |file_path| {
107 try libc_sources.append(.{107 try libc_sources.append(.{
108 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{108 .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...@@ -116,7 +116,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
116 {116 {
117 // Compile libc-bottom-half.117 // Compile libc-bottom-half.
118 var args = std.ArrayList([]const u8).init(arena);118 var args = std.ArrayList([]const u8).init(arena);
119 try addCCArgs(comp, arena, &args, true);119 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
120 try addLibcBottomHalfIncludes(comp, arena, &args);120 try addLibcBottomHalfIncludes(comp, arena, &args);
121121
122 for (libc_bottom_half_src_files) |file_path| {122 for (libc_bottom_half_src_files) |file_path| {
...@@ -132,7 +132,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr...@@ -132,7 +132,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
132 {132 {
133 // Compile libc-top-half.133 // Compile libc-top-half.
134 var args = std.ArrayList([]const u8).init(arena);134 var args = std.ArrayList([]const u8).init(arena);
135 try addCCArgs(comp, arena, &args, true);135 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
136 try addLibcTopHalfIncludes(comp, arena, &args);136 try addLibcTopHalfIncludes(comp, arena, &args);
137137
138 for (libc_top_half_src_files) |file_path| {138 for (libc_top_half_src_files) |file_path| {
...@@ -149,7 +149,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr...@@ -149,7 +149,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
149 },149 },
150 .libwasi_emulated_process_clocks_a => {150 .libwasi_emulated_process_clocks_a => {
151 var args = std.ArrayList([]const u8).init(arena);151 var args = std.ArrayList([]const u8).init(arena);
152 try addCCArgs(comp, arena, &args, true);152 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
153 try addLibcBottomHalfIncludes(comp, arena, &args);153 try addLibcBottomHalfIncludes(comp, arena, &args);
154154
155 var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena);155 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...@@ -165,7 +165,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
165 },165 },
166 .libwasi_emulated_getpid_a => {166 .libwasi_emulated_getpid_a => {
167 var args = std.ArrayList([]const u8).init(arena);167 var args = std.ArrayList([]const u8).init(arena);
168 try addCCArgs(comp, arena, &args, true);168 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
169 try addLibcBottomHalfIncludes(comp, arena, &args);169 try addLibcBottomHalfIncludes(comp, arena, &args);
170170
171 var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena);171 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...@@ -181,7 +181,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
181 },181 },
182 .libwasi_emulated_mman_a => {182 .libwasi_emulated_mman_a => {
183 var args = std.ArrayList([]const u8).init(arena);183 var args = std.ArrayList([]const u8).init(arena);
184 try addCCArgs(comp, arena, &args, true);184 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
185 try addLibcBottomHalfIncludes(comp, arena, &args);185 try addLibcBottomHalfIncludes(comp, arena, &args);
186186
187 var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena);187 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...@@ -200,7 +200,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
200200
201 {201 {
202 var args = std.ArrayList([]const u8).init(arena);202 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
205 for (emulated_signal_bottom_half_src_files) |file_path| {205 for (emulated_signal_bottom_half_src_files) |file_path| {
206 try emu_signal_sources.append(.{206 try emu_signal_sources.append(.{
...@@ -214,7 +214,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr...@@ -214,7 +214,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
214214
215 {215 {
216 var args = std.ArrayList([]const u8).init(arena);216 var args = std.ArrayList([]const u8).init(arena);
217 try addCCArgs(comp, arena, &args, true);217 try addCCArgs(comp, arena, &args, .{ .want_O3 = true });
218 try addLibcTopHalfIncludes(comp, arena, &args);218 try addLibcTopHalfIncludes(comp, arena, &args);
219 try args.append("-D_WASI_EMULATED_SIGNAL");219 try args.append("-D_WASI_EMULATED_SIGNAL");
220220
...@@ -249,17 +249,22 @@ fn sanitize(arena: Allocator, file_path: []const u8) ![]const u8 {...@@ -249,17 +249,22 @@ fn sanitize(arena: Allocator, file_path: []const u8) ![]const u8 {
249 return out_path;249 return out_path;
250}250}
251251
252const CCOptions = struct {
253 want_O3: bool = false,
254 no_strict_aliasing: bool = false,
255};
256
252fn addCCArgs(257fn addCCArgs(
253 comp: *Compilation,258 comp: *Compilation,
254 arena: Allocator,259 arena: Allocator,
255 args: *std.ArrayList([]const u8),260 args: *std.ArrayList([]const u8),
256 want_O3: bool,261 options: CCOptions,
257) error{OutOfMemory}!void {262) error{OutOfMemory}!void {
258 const target = comp.getTarget();263 const target = comp.getTarget();
259 const arch_name = musl.archNameHeaders(target.cpu.arch);264 const arch_name = musl.archNameHeaders(target.cpu.arch);
260 const os_name = @tagName(target.os.tag);265 const os_name = @tagName(target.os.tag);
261 const triple = try std.fmt.allocPrint(arena, "{s}-{s}-musl", .{ arch_name, os_name });266 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
264 try args.appendSlice(&[_][]const u8{269 try args.appendSlice(&[_][]const u8{
265 "-std=gnu17",270 "-std=gnu17",
...@@ -280,6 +285,10 @@ fn addCCArgs(...@@ -280,6 +285,10 @@ fn addCCArgs(
280285
281 "-DBULK_MEMORY_THRESHOLD=32",286 "-DBULK_MEMORY_THRESHOLD=32",
282 });287 });
288
289 if (options.no_strict_aliasing) {
290 try args.appendSlice(&[_][]const u8{"-fno-strict-aliasing"});
291 }
283}292}
284293
285fn addLibcBottomHalfIncludes(294fn addLibcBottomHalfIncludes(