Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include <cstdlib>
#include <cstring>
#include <exception>
#include <new>
#include <thread>
 
#include "OrtJniUtil.h"
 
namespace {
 
struct ReadCallbackTestAllocator {
  OrtAllocator allocator{};
  const OrtMemoryInfo* memory_info = nullptr;
  jint mode = 0;
  size_t allocation_count = 0;
};
 
void* ORT_API_CALL Allocate(OrtAllocator* allocator, size_t size) {
  auto& test_allocator = *reinterpret_cast<ReadCallbackTestAllocator*>(allocator);
  ++test_allocator.allocation_count;
  if (test_allocator.mode == 1) {
    return nullptr;
  }
#ifndef ORT_NO_EXCEPTIONS
  if (test_allocator.mode == 2) {
    throw std::bad_alloc();
  }
#endif
  return std::malloc(size);
}
 
void ORT_API_CALL Free(OrtAllocator*, void* buffer) {
  std::free(buffer);
}
 
const OrtMemoryInfo* ORT_API_CALL GetInfo(const OrtAllocator* allocator) {
  return reinterpret_cast<const ReadCallbackTestAllocator*>(allocator)->memory_info;
}
 
struct HolderAllocationFailure {
  const JNINativeInterface_* original_functions;
  jclass out_of_memory_error;
  const OrtApi* api;
  jint counts[3]{};
};
 
thread_local HolderAllocationFailure* holder_failure = nullptr;
 
jobject JNICALL CountNewGlobalRef(JNIEnv* env, jobject object) {
  jobject result = holder_failure->original_functions->NewGlobalRef(env, object);
  if (result != nullptr) {
    ++holder_failure->counts[0];
  }
  return result;
}
 
void JNICALL CountDeleteGlobalRef(JNIEnv* env, jobject object) {
  ++holder_failure->counts[1];
  holder_failure->original_functions->DeleteGlobalRef(env, object);
}
 
jobject JNICALL FailNewObject(JNIEnv* env, jclass, jmethodID, ...) {
  holder_failure->original_functions->ThrowNew(
      env, holder_failure->out_of_memory_error, "Injected EPContext ownership holder allocation failure");
  return nullptr;
}
 
OrtStatus* ORT_API_CALL RejectRegistration(OrtSessionOptions*, OrtReadNamedBufferFunc, void*) noexcept {
  ++holder_failure->counts[2];
  return holder_failure->api->CreateStatus(
      ORT_FAIL, "Native registration attempted before ownership holder construction");
}
 
}  // namespace
 
extern "C" JNIEXPORT jboolean JNICALL
Java_ai_onnxruntime_EpContextDataCallbackTest_supportsAllocatorExceptions(JNIEnv*, jclass) {
#ifdef ORT_NO_EXCEPTIONS
  return JNI_FALSE;
#else
  return JNI_TRUE;
#endif
}
 
extern "C" JNIEXPORT void JNICALL
Java_ai_onnxruntime_EpContextDataCallbackTest_exerciseReadCallbackAllocator(
    JNIEnv* env, jclass, jlong api_handle, jobject callback, jint allocation_mode) {
  const auto* api = reinterpret_cast<const OrtApi*>(api_handle);
  OrtAllocator* default_allocator = nullptr;
  if (checkOrtStatus(env, api, api->GetAllocatorWithDefaultOptions(&default_allocator)) != ORT_OK) {
    return;
  }
 
  ReadCallbackTestAllocator allocator;
  allocator.allocator.version = ORT_API_VERSION;
  allocator.allocator.Alloc = Allocate;
  allocator.allocator.Free = Free;
  allocator.allocator.Info = GetInfo;
  allocator.mode = allocation_mode;
  if (checkOrtStatus(env, api, api->AllocatorGetInfo(default_allocator, &allocator.memory_info)) != ORT_OK) {
    return;
  }
 
  EpContextDataCallbackState* state = createEpContextDataCallbackState(
      env, api, callback, "read", "(Ljava/lang/String;)[B", 1024);
  if (state == nullptr) {
    return;
  }
 
  JavaVM* jvm = nullptr;
  if (env->GetJavaVM(&jvm) != JNI_OK) {
    releaseEpContextDataCallbackState(env, state);
    throwOrtException(env, ORT_FAIL, "Failed to obtain the JVM for the native worker test");
    return;
  }
 
  const char* failure = nullptr;
#ifndef ORT_NO_EXCEPTIONS
  try {
#endif
    std::thread worker([&]() {
      void* worker_env = nullptr;
      if (jvm->GetEnv(&worker_env, JNI_VERSION_1_6) != JNI_EDETACHED) {
        failure = "The native worker was already attached before the callback";
      }
 
      void* output = nullptr;
      size_t output_size = 0;
      OrtStatus* status = nullptr;
#ifndef ORT_NO_EXCEPTIONS
      try {
#endif
        status = javaEpContextDataReadCallback(state, "allocation-test", &allocator.allocator, &output, &output_size);
#ifndef ORT_NO_EXCEPTIONS
      } catch (const std::bad_alloc&) {
        failure = "An allocator exception escaped the EPContext read callback";
      }
#endif
 
      const jint attachment_status = jvm->GetEnv(&worker_env, JNI_VERSION_1_6);
      if (attachment_status != JNI_EDETACHED) {
        failure = "The EPContext read callback did not detach its native worker";
        if (attachment_status == JNI_OK) {
          jvm->DetachCurrentThread();
        }
      }
 
      const unsigned char expected[] = {1, 2, 3};
      if (allocation_mode == 0) {
        if (status != nullptr || output == nullptr || output_size != sizeof(expected) ||
            std::memcmp(output, expected, sizeof(expected)) != 0) {
          failure = "The successful EPContext read callback did not return the expected payload";
        }
      } else if (status == nullptr || output != nullptr || output_size != 0) {
        failure = "The failed EPContext read allocation did not return a status with empty outputs";
      }
      if (allocator.allocation_count != 1) {
        failure = "The EPContext read callback did not exercise the injected allocator";
      }
      if (output != nullptr) {
        allocator.allocator.Free(&allocator.allocator, output);
      }
      if (status != nullptr) {
        api->ReleaseStatus(status);
      }
    });
    worker.join();
#ifndef ORT_NO_EXCEPTIONS
  } catch (const std::exception& exception) {
    releaseEpContextDataCallbackState(env, state);
    throwOrtException(env, ORT_FAIL, exception.what());
    return;
  }
#endif
  releaseEpContextDataCallbackState(env, state);
  if (failure != nullptr) {
    throwOrtException(env, ORT_FAIL, failure);
  }
}
 
extern "C" JNIEXPORT void JNICALL
Java_ai_onnxruntime_EpContextDataCallbackTest_failReadCallbackHolderAllocation(
    JNIEnv* env, jclass, jlong api_handle, jlong options_handle, jobject callback, jintArray counts) {
  const auto* api = reinterpret_cast<const OrtApi*>(api_handle);
  jclass out_of_memory_error = env->FindClass("java/lang/OutOfMemoryError");
  if (out_of_memory_error == nullptr) {
    return;
  }
  jclass options_class = env->FindClass("ai/onnxruntime/OrtSession$SessionOptions");
  if (options_class == nullptr) {
    env->DeleteLocalRef(out_of_memory_error);
    return;
  }
  jmethodID set_callback = env->GetStaticMethodID(
      options_class, "setEpContextDataReadCallback",
      "(JJLai/onnxruntime/OrtSession$SessionOptions$EpContextDataReadCallback;J)"
      "Lai/onnxruntime/OrtSession$SessionOptions$EpContextDataReadCallbackRegistration;");
  if (set_callback == nullptr) {
    env->DeleteLocalRef(options_class);
    env->DeleteLocalRef(out_of_memory_error);
    return;
  }
 
  HolderAllocationFailure failure{env->functions, out_of_memory_error, api, {}};
  JNINativeInterface_ injected_functions = *env->functions;
  injected_functions.NewGlobalRef = CountNewGlobalRef;
  injected_functions.DeleteGlobalRef = CountDeleteGlobalRef;
  injected_functions.NewObject = FailNewObject;
  OrtApi injected_api = *api;
  injected_api.SessionOptionsSetEpContextDataReadFunc = RejectRegistration;
 
  // Replace only this calling thread's JNI table; other JVM threads are unaffected.
  holder_failure = &failure;
  env->functions = &injected_functions;
  jobject registration = env->CallStaticObjectMethod(
      options_class, set_callback, reinterpret_cast<jlong>(&injected_api), options_handle, callback, jlong{1024});
  env->functions = failure.original_functions;
  holder_failure = nullptr;
 
  jthrowable exception = env->ExceptionOccurred();
  env->ExceptionClear();
  env->SetIntArrayRegion(counts, 0, 3, failure.counts);
  env->DeleteLocalRef(options_class);
  env->DeleteLocalRef(out_of_memory_error);
  if (registration != nullptr) {
    env->DeleteLocalRef(registration);
  }
  if (exception != nullptr) {
    env->Throw(exception);
    env->DeleteLocalRef(exception);
  } else {
    throwOrtException(env, ORT_FAIL, "The injected ownership holder allocation did not fail");
  }
}