Skip to content

Commit 2a57657

Browse files
chandankdsSingh
andauthored
[OpenMP] [Flang] Resolved Issue #76121: Implemented Check for Unhandled Arguments in __kmpc_fork_call_if (#82221)
Root cause: Segmentation fault is caused by null pointer dereference inside the __kmpc_fork_call_if function at https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/z_Linux_asm.S#L1186 . __kmpc_fork_call_if is missing case to handle argc=0 . Fix: Added a check inside the __kmp_invoke_microtask function to handle the case when argc is 0. --------- Co-authored-by: Singh <[email protected]>
1 parent 666970c commit 2a57657

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

openmp/runtime/src/z_Linux_asm.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,9 @@ KMP_LABEL(kmp_invoke_pass_parms): // put 1st - 6th parms to pkfn in registers.
11501150
movq %rdi, %rbx // pkfn -> %rbx
11511151
leaq __gtid(%rbp), %rdi // &gtid -> %rdi (store 1st parm to pkfn)
11521152
leaq __tid(%rbp), %rsi // &tid -> %rsi (store 2nd parm to pkfn)
1153+
// Check if argc is 0
1154+
cmpq $0, %rax
1155+
je KMP_LABEL(kmp_no_args) // Jump ahead
11531156

11541157
movq %r8, %r11 // p_argv -> %r11
11551158

@@ -1195,6 +1198,7 @@ KMP_LABEL(kmp_1_exit):
11951198
cmovnsq (%r11), %rdx // p_argv[0] -> %rdx (store 3rd parm to pkfn)
11961199
#endif // KMP_MIC
11971200

1201+
KMP_LABEL(kmp_no_args):
11981202
call *%rbx // call (*pkfn)();
11991203
movq $1, %rax // move 1 into return register;
12001204

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %libomp-compile && %t | FileCheck %s
2+
3+
#include <stdio.h>
4+
#include <omp.h>
5+
6+
typedef int32_t kmp_int32;
7+
typedef void *ident_t;
8+
typedef void *kmpc_micro;
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
extern void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc,
14+
kmpc_micro microtask, kmp_int32 cond,
15+
void *args);
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
// Microtask function for parallel region
21+
void microtask(int *global_tid, int *bound_tid) {
22+
// CHECK: PASS
23+
if (omp_in_parallel()) {
24+
printf("FAIL\n");
25+
} else {
26+
printf("PASS\n");
27+
}
28+
}
29+
30+
int main() {
31+
// Condition for parallelization (false in this case)
32+
int cond = 0;
33+
// Call __kmpc_fork_call_if
34+
__kmpc_fork_call_if(NULL, 0, microtask, cond, NULL);
35+
return 0;
36+
}

0 commit comments

Comments
 (0)