forked from tada/pljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvocation.c
More file actions
311 lines (280 loc) · 6.57 KB
/
Copy pathInvocation.c
File metadata and controls
311 lines (280 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright (c) 2004, 2005, 2006 TADA AB - Taby Sweden
* Distributed under the terms shown in the file COPYRIGHT
* found in the root folder of this project or at
* http://eng.tada.se/osprojects/COPYRIGHT.html
*
* @author Thomas Hallgren
*/
#include <postgres.h>
#include <executor/spi.h>
#include "org_postgresql_pljava_jdbc_Invocation.h"
#include "pljava/Invocation.h"
#include "pljava/Function.h"
#include "pljava/PgObject.h"
#include "pljava/JNICalls.h"
#include "pljava/Backend.h"
#define LOCAL_FRAME_SIZE 128
struct CallLocal_
{
/**
* Pointer to the call local structure.
*/
void* pointer;
/**
* The invocation where this CallLocal was allocated
*/
Invocation* invocation;
/**
* Next CallLocal in a double linked list
*/
CallLocal* next;
/**
* Previous CallLocal in a double linked list
*/
CallLocal* prev;
};
static jmethodID s_Invocation_onExit;
static unsigned int s_callLevel = 0;
Invocation* currentInvocation;
extern void Invocation_initialize(void);
void Invocation_initialize(void)
{
jclass cls;
JNINativeMethod invocationMethods[] =
{
{
"_getCurrent",
"()Lorg/postgresql/pljava/jdbc/Invocation;",
Java_org_postgresql_pljava_jdbc_Invocation__1getCurrent
},
{
"_getNestingLevel",
"()I",
Java_org_postgresql_pljava_jdbc_Invocation__1getNestingLevel
},
{
"_clearErrorCondition",
"()V",
Java_org_postgresql_pljava_jdbc_Invocation__1clearErrorCondition
},
{
"_register",
"()V",
Java_org_postgresql_pljava_jdbc_Invocation__1register
},
{ 0, 0, 0 }
};
cls = PgObject_getJavaClass("org/postgresql/pljava/jdbc/Invocation");
PgObject_registerNatives2(cls, invocationMethods);
s_Invocation_onExit = PgObject_getJavaMethod(cls, "onExit", "()V");
JNI_deleteLocalRef(cls);
}
void Invocation_assertConnect(void)
{
if(!currentInvocation->hasConnected)
{
SPI_connect();
currentInvocation->hasConnected = true;
}
}
void Invocation_assertDisconnect(void)
{
if(currentInvocation->hasConnected)
{
SPI_finish();
currentInvocation->hasConnected = false;
}
}
jobject Invocation_getTypeMap(void)
{
Function f = currentInvocation->function;
return f == 0 ? 0 : Function_getTypeMap(f);
}
void Invocation_pushBootContext(Invocation* ctx)
{
ctx->invocation = 0;
ctx->function = 0;
ctx->trusted = false;
ctx->hasConnected = false;
ctx->upperContext = CurrentMemoryContext;
ctx->errorOccured = false;
ctx->inExprContextCB = false;
ctx->previous = 0;
ctx->callLocals = 0;
currentInvocation = ctx;
++s_callLevel;
}
void Invocation_popBootContext(void)
{
currentInvocation = 0;
--s_callLevel;
}
void Invocation_pushInvocation(Invocation* ctx, bool trusted)
{
JNI_pushLocalFrame(LOCAL_FRAME_SIZE);
ctx->invocation = 0;
ctx->function = 0;
ctx->trusted = trusted;
ctx->hasConnected = false;
ctx->upperContext = CurrentMemoryContext;
ctx->errorOccured = false;
ctx->inExprContextCB = false;
ctx->previous = currentInvocation;
ctx->callLocals = 0;
currentInvocation = ctx;
Backend_setJavaSecurity(trusted);
++s_callLevel;
}
void Invocation_popInvocation(bool wasException)
{
CallLocal* cl;
Invocation* ctx = currentInvocation->previous;
if(currentInvocation->invocation != 0)
{
if(!wasException)
JNI_callVoidMethod(currentInvocation->invocation, s_Invocation_onExit);
JNI_deleteGlobalRef(currentInvocation->invocation);
}
if(currentInvocation->hasConnected)
SPI_finish();
JNI_popLocalFrame(0);
if(ctx != 0)
{
PG_TRY();
{
Backend_setJavaSecurity(ctx->trusted);
}
PG_CATCH();
{
elog(FATAL, "Failed to reinstate untrusted security after a trusted call or vice versa");
}
PG_END_TRY();
MemoryContextSwitchTo(ctx->upperContext);
}
/**
* Reset all local wrappers that has been allocated during this call. Yank them
* from the double linked list but do *not* remove them.
*/
cl = currentInvocation->callLocals;
if(cl != 0)
{
CallLocal* first = cl;
do
{
cl->pointer = 0;
cl->invocation = 0;
cl = cl->next;
} while(cl != first);
}
currentInvocation = ctx;
--s_callLevel;
}
void Invocation_freeLocalWrapper(jlong wrapper)
{
Ptr2Long p2l;
Invocation* ctx;
CallLocal* cl;
CallLocal* prev;
p2l.longVal = wrapper;
cl = (CallLocal*)p2l.ptrVal;
prev = cl->prev;
if(prev != cl)
{
/* Disconnect
*/
CallLocal* next = cl->next;
prev->next = next;
next->prev = prev;
}
/* If this CallLocal is freed before its owning invocation was
* popped then there's a risk that this is the first CallLocal
* in the list.
*/
ctx = cl->invocation;
if(ctx != 0 && ctx->callLocals == cl)
{
if(prev == cl)
prev = 0;
ctx->callLocals = prev;
}
pfree(cl);
}
void* Invocation_getWrappedPointer(jlong wrapper)
{
Ptr2Long p2l;
p2l.longVal = wrapper;
return ((CallLocal*)p2l.ptrVal)->pointer;
}
jlong Invocation_createLocalWrapper(void* pointer)
{
/* Create a local wrapper for the pointer
*/
Ptr2Long p2l;
CallLocal* cl = (CallLocal*)MemoryContextAlloc(JavaMemoryContext, sizeof(CallLocal));
CallLocal* prev = currentInvocation->callLocals;
if(prev == 0)
{
currentInvocation->callLocals = cl;
cl->prev = cl;
cl->next = cl;
}
else
{
CallLocal* next = prev->next;
cl->prev = prev;
cl->next = next;
prev->next = cl;
next->prev = cl;
}
cl->pointer = pointer;
cl->invocation = currentInvocation;
p2l.longVal = 0L; /* ensure that the rest is zeroed out */
p2l.ptrVal = cl;
return p2l.longVal;
}
MemoryContext
Invocation_switchToUpperContext(void)
{
return MemoryContextSwitchTo(currentInvocation->upperContext);
}
/*
* Class: org_postgresql_pljava_jdbc_Invocation
* Method: _getNestingLevel
* Signature: ()I
*/
JNIEXPORT jint JNICALL
Java_org_postgresql_pljava_jdbc_Invocation__1getNestingLevel(JNIEnv* env, jclass cls)
{
return s_callLevel;
}
/*
* Class: org_postgresql_pljava_jdbc_Invocation
* Method: _getCurrent
* Signature: ()Lorg/postgresql/pljava/jdbc/Invocation;
*/
JNIEXPORT jobject JNICALL
Java_org_postgresql_pljava_jdbc_Invocation__1getCurrent(JNIEnv* env, jclass cls)
{
return currentInvocation->invocation;
}
/*
* Class: org_postgresql_pljava_jdbc_Invocation
* Method: _clearErrorCondition
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_org_postgresql_pljava_jdbc_Invocation__1clearErrorCondition(JNIEnv* env, jclass cls)
{
currentInvocation->errorOccured = false;
}
/*
* Class: org_postgresql_pljava_jdbc_Invocation
* Method: _register
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_org_postgresql_pljava_jdbc_Invocation__1register(JNIEnv* env, jobject _this)
{
currentInvocation->invocation = (*env)->NewGlobalRef(env, _this);
}