Skip to content

Commit fe2a16d

Browse files
committed
llvmjit: Work around bug in LLVM 3.9 causing crashes after 7255943.
Unfortunately in LLVM 3.9 LLVMGetAttributeCountAtIndex(func, index) crashes when called with an index that has 0 attributes. Since there's no way to work around this in the C API, add a small C++ wrapper doing so. The only reason this didn't fail before 7255943 is that there always are function attributes... Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20201016001254.w2nfj7gd74jmb5in@alap3.anarazel.de Backpatch: 11-, like 7255943
1 parent 536de14 commit fe2a16d

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/backend/jit/llvm/llvmjit.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,14 @@ llvm_copy_attributes_at_index(LLVMValueRef v_from, LLVMValueRef v_to, uint32 ind
333333
int num_attributes;
334334
LLVMAttributeRef *attrs;
335335

336-
num_attributes = LLVMGetAttributeCountAtIndex(v_from, index);
336+
num_attributes = LLVMGetAttributeCountAtIndexPG(v_from, index);
337+
338+
/*
339+
* Not just for efficiency: LLVM <= 3.9 crashes when
340+
* LLVMGetAttributesAtIndex() is called for an index with 0 attributes.
341+
*/
342+
if (num_attributes == 0)
343+
return;
337344

338345
attrs = palloc(sizeof(LLVMAttributeRef) * num_attributes);
339346
LLVMGetAttributesAtIndex(v_from, index, attrs);

src/backend/jit/llvm/llvmjit_wrap.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ extern "C"
1616
#include "postgres.h"
1717
}
1818

19+
#include <llvm-c/Core.h>
20+
21+
/* Avoid macro clash with LLVM's C++ headers */
22+
#undef Min
23+
24+
#include <llvm/IR/Attributes.h>
25+
#include <llvm/IR/Function.h>
1926
#include <llvm/MC/SubtargetFeature.h>
2027
#include <llvm/Support/Host.h>
2128

@@ -44,3 +51,28 @@ char *LLVMGetHostCPUFeatures(void) {
4451
return strdup(Features.getString().c_str());
4552
}
4653
#endif
54+
55+
/*
56+
* Like LLVM's LLVMGetAttributeCountAtIndex(), works around a bug in LLVM 3.9.
57+
*
58+
* In LLVM <= 3.9, LLVMGetAttributeCountAtIndex() segfaults if there are no
59+
* attributes at an index (fixed in LLVM commit ce9bb1097dc2).
60+
*/
61+
unsigned
62+
LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx)
63+
{
64+
/*
65+
* This is more expensive, so only do when using a problematic LLVM
66+
* version.
67+
*/
68+
#if LLVM_VERSION_MAJOR < 4
69+
if (!llvm::unwrap<llvm::Function>(F)->getAttributes().hasAttributes(Idx))
70+
return 0;
71+
#endif
72+
73+
/*
74+
* There is no nice public API to determine the count nicely, so just
75+
* always fall back to LLVM's C API.
76+
*/
77+
return LLVMGetAttributeCountAtIndex(F, Idx);
78+
}

src/include/jit/llvmjit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ extern char *LLVMGetHostCPUName(void);
129129
extern char *LLVMGetHostCPUFeatures(void);
130130
#endif
131131

132+
extern unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx);
133+
132134
#ifdef __cplusplus
133135
} /* extern "C" */
134136
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy