|
| 1 | +use cranelift_codegen::ir::*; |
| 2 | +use cranelift_codegen::isa::{CallConv, OwnedTargetIsa}; |
| 3 | +use cranelift_codegen::settings::{self, Configurable}; |
| 4 | +use cranelift_codegen::{Context, ir::types::I32, ir::types::I64}; |
| 5 | +use cranelift_entity::EntityRef; |
| 6 | +use cranelift_frontend::*; |
| 7 | +use cranelift_jit::*; |
| 8 | +use cranelift_module::*; |
| 9 | + |
| 10 | +fn isa() -> Option<OwnedTargetIsa> { |
| 11 | + let mut flag_builder = settings::builder(); |
| 12 | + flag_builder.set("use_colocated_libcalls", "false").unwrap(); |
| 13 | + flag_builder.set("is_pic", "false").unwrap(); |
| 14 | + let isa_builder = cranelift_native::builder().ok()?; |
| 15 | + isa_builder.finish(settings::Flags::new(flag_builder)).ok() |
| 16 | +} |
| 17 | + |
| 18 | +#[test] |
| 19 | +fn issue_8852_struct_argument_panic() { |
| 20 | + let Some(isa) = isa() else { |
| 21 | + return; |
| 22 | + }; |
| 23 | + |
| 24 | + // Only run on aarch64 |
| 25 | + if isa.triple().architecture |
| 26 | + != target_lexicon::Architecture::Aarch64(target_lexicon::Aarch64Architecture::Aarch64) |
| 27 | + { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + let mut module = JITModule::new(JITBuilder::with_isa(isa, default_libcall_names())); |
| 32 | + |
| 33 | + // Define %foo(i64 sarg(16)) -> i32 |
| 34 | + let mut sig_foo = module.make_signature(); |
| 35 | + sig_foo |
| 36 | + .params |
| 37 | + .push(AbiParam::special(I64, ArgumentPurpose::StructArgument(16))); |
| 38 | + sig_foo.returns.push(AbiParam::new(I32)); |
| 39 | + |
| 40 | + let foo_id = module |
| 41 | + .declare_function("foo", Linkage::Local, &sig_foo) |
| 42 | + .unwrap(); |
| 43 | + |
| 44 | + let mut ctx = Context::new(); |
| 45 | + ctx.func = |
| 46 | + Function::with_name_signature(UserFuncName::user(0, foo_id.as_u32()), sig_foo.clone()); |
| 47 | + let mut func_ctx = FunctionBuilderContext::new(); |
| 48 | + { |
| 49 | + let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx); |
| 50 | + let block = bcx.create_block(); |
| 51 | + bcx.switch_to_block(block); |
| 52 | + bcx.append_block_params_for_function_params(block); |
| 53 | + let v0 = bcx.block_params(block)[0]; |
| 54 | + let v1 = bcx.ins().load(I32, MemFlags::trusted(), v0, 0); |
| 55 | + bcx.ins().return_(&[v1]); |
| 56 | + } |
| 57 | + module.define_function(foo_id, &mut ctx).unwrap(); |
| 58 | + module.clear_context(&mut ctx); |
| 59 | + |
| 60 | + // Define %main() -> i32 |
| 61 | + let mut sig_main = module.make_signature(); |
| 62 | + sig_main.returns.push(AbiParam::new(I32)); |
| 63 | + |
| 64 | + let main_id = module |
| 65 | + .declare_function("main", Linkage::Local, &sig_main) |
| 66 | + .unwrap(); |
| 67 | + |
| 68 | + ctx.func = Function::with_name_signature(UserFuncName::user(0, main_id.as_u32()), sig_main); |
| 69 | + let mut func_ctx = FunctionBuilderContext::new(); |
| 70 | + { |
| 71 | + let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx); |
| 72 | + let block = bcx.create_block(); |
| 73 | + bcx.switch_to_block(block); |
| 74 | + |
| 75 | + let ss0 = |
| 76 | + bcx.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4, 4)); |
| 77 | + let v0 = bcx.ins().iconst(I32, 42); |
| 78 | + bcx.ins().stack_store(v0, ss0, 0); |
| 79 | + let v1 = bcx.ins().stack_addr(I64, ss0, 0); |
| 80 | + |
| 81 | + let local_foo = module.declare_func_in_func(foo_id, &mut bcx.func); |
| 82 | + let call = bcx.ins().call(local_foo, &[v1]); |
| 83 | + let v2 = bcx.inst_results(call)[0]; |
| 84 | + bcx.ins().return_(&[v2]); |
| 85 | + } |
| 86 | + module.define_function(main_id, &mut ctx).unwrap(); |
| 87 | + |
| 88 | + // This should panic on AArch64 if the bug is present |
| 89 | + module.finalize_definitions().unwrap(); |
| 90 | + |
| 91 | + let code = module.get_finalized_function(main_id); |
| 92 | + let ptr = code as *const _; |
| 93 | + let code_fn: extern "C" fn() -> i32 = unsafe { std::mem::transmute(ptr) }; |
| 94 | + let res = code_fn(); |
| 95 | + assert_eq!(res, 42); |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn issue_8852_plt_verification() { |
| 100 | + let Some(isa) = isa() else { |
| 101 | + return; |
| 102 | + }; |
| 103 | + |
| 104 | + // Only run on aarch64 |
| 105 | + if isa.triple().architecture |
| 106 | + != target_lexicon::Architecture::Aarch64(target_lexicon::Aarch64Architecture::Aarch64) |
| 107 | + { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + let mut module = JITModule::new(JITBuilder::with_isa(isa, default_libcall_names())); |
| 112 | + |
| 113 | + // Define %foo() -> i32 |
| 114 | + let mut sig_foo = module.make_signature(); |
| 115 | + sig_foo.returns.push(AbiParam::new(I32)); |
| 116 | + let foo_id = module |
| 117 | + .declare_function("foo", Linkage::Local, &sig_foo) |
| 118 | + .unwrap(); |
| 119 | + |
| 120 | + let mut ctx = Context::new(); |
| 121 | + ctx.func = |
| 122 | + Function::with_name_signature(UserFuncName::user(0, foo_id.as_u32()), sig_foo.clone()); |
| 123 | + let mut func_ctx = FunctionBuilderContext::new(); |
| 124 | + { |
| 125 | + let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx); |
| 126 | + let block = bcx.create_block(); |
| 127 | + bcx.switch_to_block(block); |
| 128 | + let v = bcx.ins().iconst(I32, 42); |
| 129 | + bcx.ins().return_(&[v]); |
| 130 | + } |
| 131 | + module.define_function(foo_id, &mut ctx).unwrap(); |
| 132 | + module.clear_context(&mut ctx); |
| 133 | + |
| 134 | + // Define large data object to force distance > 128MB |
| 135 | + let data_id = module |
| 136 | + .declare_data("large_gap", Linkage::Local, false, false) |
| 137 | + .unwrap(); |
| 138 | + let mut data_desc = DataDescription::new(); |
| 139 | + data_desc.define_zeroinit(130 * 1024 * 1024); // 130 MB |
| 140 | + module.define_data(data_id, &data_desc).unwrap(); |
| 141 | + |
| 142 | + // Define %main() -> i32 calling %foo |
| 143 | + let mut sig_main = module.make_signature(); |
| 144 | + sig_main.returns.push(AbiParam::new(I32)); |
| 145 | + let main_id = module |
| 146 | + .declare_function("main", Linkage::Local, &sig_main) |
| 147 | + .unwrap(); |
| 148 | + |
| 149 | + ctx.func = Function::with_name_signature(UserFuncName::user(0, main_id.as_u32()), sig_main); |
| 150 | + let mut func_ctx = FunctionBuilderContext::new(); |
| 151 | + { |
| 152 | + let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx); |
| 153 | + let block = bcx.create_block(); |
| 154 | + bcx.switch_to_block(block); |
| 155 | + |
| 156 | + let local_foo = module.declare_func_in_func(foo_id, &mut bcx.func); |
| 157 | + let call = bcx.ins().call(local_foo, &[]); |
| 158 | + let v = bcx.inst_results(call)[0]; |
| 159 | + bcx.ins().return_(&[v]); |
| 160 | + } |
| 161 | + module.define_function(main_id, &mut ctx).unwrap(); |
| 162 | + |
| 163 | + module.finalize_definitions().unwrap(); |
| 164 | + |
| 165 | + let code = module.get_finalized_function(main_id); |
| 166 | + let ptr = code as *const _; |
| 167 | + let code_fn: extern "C" fn() -> i32 = unsafe { std::mem::transmute(ptr) }; |
| 168 | + let res = code_fn(); |
| 169 | + assert_eq!(res, 42); |
| 170 | +} |
0 commit comments