Skip to content

Commit c835c7f

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 0ab7ca9 commit c835c7f

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
@@ -339,7 +339,14 @@ llvm_copy_attributes_at_index(LLVMValueRef v_from, LLVMValueRef v_to, uint32 ind
339339
int num_attributes;
340340
LLVMAttributeRef *attrs;
341341

342-
num_attributes = LLVMGetAttributeCountAtIndex(v_from, index);
342+
num_attributes = LLVMGetAttributeCountAtIndexPG(v_from, index);
343+
344+
/*
345+
* Not just for efficiency: LLVM <= 3.9 crashes when
346+
* LLVMGetAttributesAtIndex() is called for an index with 0 attributes.
347+
*/
348+
if (num_attributes == 0)
349+
return;
343350

344351
attrs = palloc(sizeof(LLVMAttributeRef) * num_attributes);
345352
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
@@ -138,6 +138,8 @@ extern char *LLVMGetHostCPUName(void);
138138
extern char *LLVMGetHostCPUFeatures(void);
139139
#endif
140140

141+
extern unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx);
142+
141143
#ifdef __cplusplus
142144
} /* extern "C" */
143145
#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