authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-21 23:50:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-22 13:07:02-07:00
log25198810c88d474c63ff537e3647f12e7df6297c
treef92e3b6dcf75aa653df9786fb6c17360291a8e4c
parent7930efc60becd7624471419b8fd49800512d4e10

add new builtin: `@disableInstrumentation`

This is needed to ensure that start code does not try to access thread local storage before it has set up thread local storage.

8 files changed, 72 insertions(+), 15 deletions(-)

lib/std/zig/AstGen.zig+8-6
......@@ -2817,6 +2817,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
28172817
28182818 .extended => switch (gz.astgen.instructions.items(.data)[@intFromEnum(inst)].extended.opcode) {
28192819 .breakpoint,
2820 .disable_instrumentation,
28202821 .fence,
28212822 .set_float_mode,
28222823 .set_align_stack,
......@@ -9305,12 +9306,13 @@ fn builtinCall(
93059306 },
93069307
93079308 // zig fmt: off
9308 .This => return rvalue(gz, ri, try gz.addNodeExtended(.this, node), node),
9309 .return_address => return rvalue(gz, ri, try gz.addNodeExtended(.ret_addr, node), node),
9310 .error_return_trace => return rvalue(gz, ri, try gz.addNodeExtended(.error_return_trace, node), node),
9311 .frame => return rvalue(gz, ri, try gz.addNodeExtended(.frame, node), node),
9312 .frame_address => return rvalue(gz, ri, try gz.addNodeExtended(.frame_address, node), node),
9313 .breakpoint => return rvalue(gz, ri, try gz.addNodeExtended(.breakpoint, node), node),
9309 .This => return rvalue(gz, ri, try gz.addNodeExtended(.this, node), node),
9310 .return_address => return rvalue(gz, ri, try gz.addNodeExtended(.ret_addr, node), node),
9311 .error_return_trace => return rvalue(gz, ri, try gz.addNodeExtended(.error_return_trace, node), node),
9312 .frame => return rvalue(gz, ri, try gz.addNodeExtended(.frame, node), node),
9313 .frame_address => return rvalue(gz, ri, try gz.addNodeExtended(.frame_address, node), node),
9314 .breakpoint => return rvalue(gz, ri, try gz.addNodeExtended(.breakpoint, node), node),
9315 .disable_instrumentation => return rvalue(gz, ri, try gz.addNodeExtended(.disable_instrumentation, node), node),
93149316
93159317 .type_info => return simpleUnOpType(gz, scope, ri, node, params[0], .type_info),
93169318 .size_of => return simpleUnOpType(gz, scope, ri, node, params[0], .size_of),
lib/std/zig/AstRlAnnotate.zig+1
......@@ -877,6 +877,7 @@ fn builtinCall(astrl: *AstRlAnnotate, block: ?*Block, ri: ResultInfo, node: Ast.
877877 .error_return_trace,
878878 .frame,
879879 .breakpoint,
880 .disable_instrumentation,
880881 .in_comptime,
881882 .panic,
882883 .trap,
lib/std/zig/BuiltinFn.zig+9
......@@ -15,6 +15,7 @@ pub const Tag = enum {
1515 int_from_bool,
1616 bit_size_of,
1717 breakpoint,
18 disable_instrumentation,
1819 mul_add,
1920 byte_swap,
2021 bit_reverse,
......@@ -263,6 +264,14 @@ pub const list = list: {
263264 .illegal_outside_function = true,
264265 },
265266 },
267 .{
268 "@disableInstrumentation",
269 .{
270 .tag = .disable_instrumentation,
271 .param_count = 0,
272 .illegal_outside_function = true,
273 },
274 },
266275 .{
267276 "@mulAdd",
268277 .{
lib/std/zig/Zir.zig+3-1
......@@ -1553,7 +1553,7 @@ pub const Inst = struct {
15531553 => false,
15541554
15551555 .extended => switch (data.extended.opcode) {
1556 .fence, .set_cold, .breakpoint => true,
1556 .fence, .set_cold, .breakpoint, .disable_instrumentation => true,
15571557 else => false,
15581558 },
15591559 };
......@@ -1973,6 +1973,8 @@ pub const Inst = struct {
19731973 /// Implements `@breakpoint`.
19741974 /// `operand` is `src_node: i32`.
19751975 breakpoint,
1976 /// Implement builtin `@disableInstrumentation`. `operand` is `src_node: i32`.
1977 disable_instrumentation,
19761978 /// Implements the `@select` builtin.
19771979 /// `operand` is payload index to `Select`.
19781980 select,
src/InternPool.zig+18-2
......@@ -5184,11 +5184,11 @@ pub const FuncAnalysis = packed struct(u32) {
51845184 is_noinline: bool,
51855185 calls_or_awaits_errorable_fn: bool,
51865186 stack_alignment: Alignment,
5187
51885187 /// True if this function has an inferred error set.
51895188 inferred_error_set: bool,
5189 disable_instrumentation: bool,
51905190
5191 _: u14 = 0,
5191 _: u13 = 0,
51925192
51935193 pub const State = enum(u8) {
51945194 /// This function has not yet undergone analysis, because we have not
......@@ -8111,6 +8111,7 @@ pub fn getFuncDecl(
81118111 .calls_or_awaits_errorable_fn = false,
81128112 .stack_alignment = .none,
81138113 .inferred_error_set = false,
8114 .disable_instrumentation = false,
81148115 },
81158116 .owner_decl = key.owner_decl,
81168117 .ty = key.ty,
......@@ -8214,6 +8215,7 @@ pub fn getFuncDeclIes(
82148215 .calls_or_awaits_errorable_fn = false,
82158216 .stack_alignment = .none,
82168217 .inferred_error_set = true,
8218 .disable_instrumentation = false,
82178219 },
82188220 .owner_decl = key.owner_decl,
82198221 .ty = func_ty,
......@@ -8405,6 +8407,7 @@ pub fn getFuncInstance(
84058407 .calls_or_awaits_errorable_fn = false,
84068408 .stack_alignment = .none,
84078409 .inferred_error_set = false,
8410 .disable_instrumentation = false,
84088411 },
84098412 // This is populated after we create the Decl below. It is not read
84108413 // by equality or hashing functions.
......@@ -8504,6 +8507,7 @@ pub fn getFuncInstanceIes(
85048507 .calls_or_awaits_errorable_fn = false,
85058508 .stack_alignment = .none,
85068509 .inferred_error_set = true,
8510 .disable_instrumentation = false,
85078511 },
85088512 // This is populated after we create the Decl below. It is not read
85098513 // by equality or hashing functions.
......@@ -11225,6 +11229,18 @@ pub fn funcSetCallsOrAwaitsErrorableFn(ip: *InternPool, func: Index) void {
1122511229 @atomicStore(FuncAnalysis, analysis_ptr, analysis, .release);
1122611230}
1122711231
11232pub fn funcSetDisableInstrumentation(ip: *InternPool, func: Index) void {
11233 const unwrapped_func = func.unwrap(ip);
11234 const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;
11235 extra_mutex.lock();
11236 defer extra_mutex.unlock();
11237
11238 const analysis_ptr = ip.funcAnalysisPtr(func);
11239 var analysis = analysis_ptr.*;
11240 analysis.disable_instrumentation = true;
11241 @atomicStore(FuncAnalysis, analysis_ptr, analysis, .release);
11242}
11243
1122811244pub fn funcSetCold(ip: *InternPool, func: Index, is_cold: bool) void {
1122911245 const unwrapped_func = func.unwrap(ip);
1123011246 const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;
src/Sema.zig+13
......@@ -1316,6 +1316,11 @@ fn analyzeBodyInner(
13161316 i += 1;
13171317 continue;
13181318 },
1319 .disable_instrumentation => {
1320 try sema.zirDisableInstrumentation();
1321 i += 1;
1322 continue;
1323 },
13191324 .restore_err_ret_index => {
13201325 try sema.zirRestoreErrRetIndex(block, extended);
13211326 i += 1;
......@@ -6576,6 +6581,14 @@ fn zirSetCold(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData)
65766581 ip.funcSetCold(sema.func_index, is_cold);
65776582}
65786583
6584fn zirDisableInstrumentation(sema: *Sema) CompileError!void {
6585 const pt = sema.pt;
6586 const mod = pt.zcu;
6587 const ip = &mod.intern_pool;
6588 if (sema.func_index == .none) return; // does nothing outside a function
6589 ip.funcSetDisableInstrumentation(sema.func_index);
6590}
6591
65796592fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
65806593 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
65816594 const src = block.builtinCallArgSrc(extra.node, 0);
src/codegen/llvm.zig+19-6
......@@ -1383,6 +1383,25 @@ pub const Object = struct {
13831383 _ = try attributes.removeFnAttr(.cold);
13841384 }
13851385
1386 if (owner_mod.sanitize_thread and !func_analysis.disable_instrumentation) {
1387 try attributes.addFnAttr(.sanitize_thread, &o.builder);
1388 } else {
1389 _ = try attributes.removeFnAttr(.sanitize_thread);
1390 }
1391 if (owner_mod.fuzz and !func_analysis.disable_instrumentation) {
1392 try attributes.addFnAttr(.optforfuzzing, &o.builder);
1393 if (comp.config.any_fuzz) {
1394 _ = try attributes.removeFnAttr(.skipprofile);
1395 _ = try attributes.removeFnAttr(.nosanitize_coverage);
1396 }
1397 } else {
1398 _ = try attributes.removeFnAttr(.optforfuzzing);
1399 if (comp.config.any_fuzz) {
1400 try attributes.addFnAttr(.skipprofile, &o.builder);
1401 try attributes.addFnAttr(.nosanitize_coverage, &o.builder);
1402 }
1403 }
1404
13861405 // TODO: disable this if safety is off for the function scope
13871406 const ssp_buf_size = owner_mod.stack_protector;
13881407 if (ssp_buf_size != 0) {
......@@ -2982,12 +3001,6 @@ pub const Object = struct {
29823001 try attributes.addFnAttr(.minsize, &o.builder);
29833002 try attributes.addFnAttr(.optsize, &o.builder);
29843003 }
2985 if (owner_mod.sanitize_thread) {
2986 try attributes.addFnAttr(.sanitize_thread, &o.builder);
2987 }
2988 if (owner_mod.fuzz) {
2989 try attributes.addFnAttr(.optforfuzzing, &o.builder);
2990 }
29913004 const target = owner_mod.resolved_target.result;
29923005 if (target.cpu.model.llvm_name) |s| {
29933006 try attributes.addFnAttr(.{ .string = .{
src/print_zir.zig+1
......@@ -524,6 +524,7 @@ const Writer = struct {
524524 .frame,
525525 .frame_address,
526526 .breakpoint,
527 .disable_instrumentation,
527528 .c_va_start,
528529 .in_comptime,
529530 .value_placeholder,