Skip to content

[IR] Check that arguments of naked function are not used #104757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,8 @@ example:
attributes.
``naked``
This attribute disables prologue / epilogue emission for the
function. This can have very system-specific consequences.
function. This can have very system-specific consequences. The arguments of
a ``naked`` function can not be referenced through IR values.
``"no-inline-line-tables"``
When this attribute is set to true, the inliner discards source locations
when inlining code and instead uses the source location of the call site.
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,10 @@ void Verifier::visitFunction(const Function &F) {
Check(!Attrs.hasAttrSomewhere(Attribute::ElementType),
"Attribute 'elementtype' can only be applied to a callsite.", &F);

if (Attrs.hasFnAttr(Attribute::Naked))
for (const Argument &Arg : F.args())
Check(Arg.use_empty(), "cannot use argument of naked function", &Arg);

// Check that this function meets the restrictions on this calling convention.
// Sometimes varargs is used for perfectly forwarding thunks, so some of these
// restrictions can be lifted.
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Instrumentation/ThreadSanitizer/tsan_basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ define void @SwiftErrorCall(ptr swifterror) sanitize_thread {
ret void
}

; CHECK-LABEL: @NakedTest(ptr %a)
; CHECK-NEXT: call void @foo()
; CHECK-NEXT: %tmp1 = load i32, ptr %a, align 4
; CHECK-NEXT: ret i32 %tmp1
define i32 @NakedTest(ptr %a) naked sanitize_thread {
call void @foo()
; CHECK-LABEL: @NakedTest()
; CHECK-NEXT: %a = call ptr @foo()
; CHECK-NEXT: %tmp1 = load i32, ptr %a, align 4
; CHECK-NEXT: ret i32 %tmp1
define i32 @NakedTest() naked sanitize_thread {
%a = call ptr @foo()
%tmp1 = load i32, ptr %a, align 4
ret i32 %tmp1
}
Expand Down
2 changes: 0 additions & 2 deletions llvm/test/Transforms/Attributor/nonnull.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1048,10 +1048,8 @@ define internal void @naked(ptr dereferenceable(4) %a) naked {
; CHECK: Function Attrs: naked
; CHECK-LABEL: define {{[^@]+}}@naked
; CHECK-SAME: (ptr noundef nonnull dereferenceable(4) [[A:%.*]]) #[[ATTR11:[0-9]+]] {
; CHECK-NEXT: call void @use_i32_ptr(ptr nocapture nofree noundef nonnull [[A]])
; CHECK-NEXT: ret void
;
call void @use_i32_ptr(ptr %a)
ret void
}
; Avoid nonnull as we do not touch optnone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ attributes #0 = {

; attributes to drop
attributes #1 = {
alignstack=16 convergent inaccessiblememonly inaccessiblemem_or_argmemonly naked
alignstack=16 convergent inaccessiblememonly inaccessiblemem_or_argmemonly
noreturn readonly argmemonly returns_twice speculatable "thunk"
}
3 changes: 0 additions & 3 deletions llvm/test/Transforms/FunctionAttrs/nonnull.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1079,15 +1079,12 @@ define internal void @control(ptr dereferenceable(4) %a) {
define internal void @naked(ptr dereferenceable(4) %a) naked {
; FNATTRS-LABEL: define internal void @naked(
; FNATTRS-SAME: ptr dereferenceable(4) [[A:%.*]]) #[[ATTR10:[0-9]+]] {
; FNATTRS-NEXT: call void @use_i32_ptr(ptr [[A]])
; FNATTRS-NEXT: ret void
;
; ATTRIBUTOR-LABEL: define internal void @naked(
; ATTRIBUTOR-SAME: ptr nonnull dereferenceable(4) [[A:%.*]]) #[[ATTR11:[0-9]+]] {
; ATTRIBUTOR-NEXT: call void @use_i32_ptr(ptr [[A]])
; ATTRIBUTOR-NEXT: ret void
;
call void @use_i32_ptr(ptr %a)
ret void
}
; Avoid nonnull as we do not touch optnone
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Verifier/naked.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s

; CHECK: cannot use argument of naked function
define void @test(ptr %ptr) naked {
getelementptr i8, ptr %ptr, i64 1
call void @llvm.trap()
unreachable
}
Loading