forked from duckdb/duckdb-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duckdb.patch
183 lines (178 loc) · 6.18 KB
/
duckdb.patch
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
diff --git a/src/function/table/system/test_all_types.cpp b/src/function/table/system/test_all_types.cpp
index a0b856266c..753fd6323b 100644
--- a/src/function/table/system/test_all_types.cpp
+++ b/src/function/table/system/test_all_types.cpp
@@ -204,13 +204,14 @@ vector<TestType> TestAllTypesFun::GetTestTypes(bool use_large_enum) {
auto max_map_value = Value::MAP(ListType::GetChildType(map_type), map_values);
result.emplace_back(map_type, "map", std::move(min_map_value), std::move(max_map_value));
+#if 0
// union
child_list_t<LogicalType> members = {{"name", LogicalType::VARCHAR}, {"age", LogicalType::SMALLINT}};
auto union_type = LogicalType::UNION(members);
const Value &min = Value::UNION(members, 0, Value("Frank"));
const Value &max = Value::UNION(members, 1, Value::SMALLINT(5));
result.emplace_back(union_type, "union", min, max);
-
+#endif
return result;
}
diff --git a/src/main/extension/extension_load.cpp b/src/main/extension/extension_load.cpp
index 80d24c2982..e34dadf813 100644
--- a/src/main/extension/extension_load.cpp
+++ b/src/main/extension/extension_load.cpp
@@ -109,6 +109,7 @@ bool ExtensionHelper::TryInitialLoad(DBConfig &config, FileSystem &fs, const str
filename = fs.JoinPath(local_path, extension_name + ".duckdb_extension");
#endif
}
+#ifndef WASM_LOADABLE_EXTENSIONS
if (!fs.FileExists(filename)) {
string message;
bool exact_match = ExtensionHelper::CreateSuggestions(extension, message);
@@ -118,6 +119,124 @@ bool ExtensionHelper::TryInitialLoad(DBConfig &config, FileSystem &fs, const str
error = StringUtil::Format("Extension \"%s\" not found.\n%s", filename, message);
return false;
}
+#endif
+#ifdef WASM_LOADABLE_EXTENSIONS
+ auto basename = fs.ExtractBaseName(filename);
+ char *exe = NULL;
+ exe = (char *)EM_ASM_PTR(
+ {
+ // Next few lines should argubly in separate JavaScript-land function call
+ // TODO: move them out / have them configurable
+ const xhr = new XMLHttpRequest();
+ xhr.open("GET", UTF8ToString($0), false);
+ xhr.responseType = "arraybuffer";
+ xhr.send(null);
+ if (xhr.status != 200)
+ return 0;
+ var uInt8Array = xhr.response;
+ var valid = WebAssembly.validate(uInt8Array);
+ var len = uInt8Array.byteLength;
+ var fileOnWasmHeap = _malloc(len + 4);
+
+ var properArray = new Uint8Array(uInt8Array);
+
+ for (var iii = 0; iii < len; iii++) {
+ Module.HEAPU8[iii + fileOnWasmHeap + 4] = properArray[iii];
+ }
+ var LEN123 = new Uint8Array(4);
+ LEN123[0] = len % 256;
+ len -= LEN123[0];
+ len /= 256;
+ LEN123[1] = len % 256;
+ len -= LEN123[1];
+ len /= 256;
+ LEN123[2] = len % 256;
+ len -= LEN123[2];
+ len /= 256;
+ LEN123[3] = len % 256;
+ len -= LEN123[3];
+ len /= 256;
+ Module.HEAPU8.set(LEN123, fileOnWasmHeap);
+ //console.log(LEN123);
+ //console.log(properArray);
+ //console.log(new Uint8Array(Module.HEAPU8, fileOnWasmHeap, len+4));
+ console.log('Loading extension ', UTF8ToString($1));
+
+ // Here we add the uInt8Array to Emscripten's filesystem, for it to be found by dlopen
+ FS.writeFile(UTF8ToString($1), new Uint8Array(uInt8Array));
+ return fileOnWasmHeap;
+ },
+ filename.c_str(), basename.c_str());
+ if (!exe) {
+ throw IOException("Extension %s is not available", filename);
+ }
+
+ auto dopen_from = basename;
+ if (!config.options.allow_unsigned_extensions) {
+ // signature is the last 256 bytes of the file
+
+ string signature;
+ signature.resize(256);
+
+ D_ASSERT(exe);
+ uint64_t LEN = 0;
+ LEN *= 256;
+ LEN += ((uint8_t *)exe)[3];
+ LEN *= 256;
+ LEN += ((uint8_t *)exe)[2];
+ LEN *= 256;
+ LEN += ((uint8_t *)exe)[1];
+ LEN *= 256;
+ LEN += ((uint8_t *)exe)[0];
+ auto signature_offset = LEN - signature.size();
+
+ const idx_t maxLenChunks = 1024ULL * 1024ULL;
+ const idx_t numChunks = (signature_offset + maxLenChunks - 1) / maxLenChunks;
+ std::vector<std::string> hash_chunks(numChunks);
+ std::vector<idx_t> splits(numChunks + 1);
+
+ for (idx_t i = 0; i < numChunks; i++) {
+ splits[i] = maxLenChunks * i;
+ }
+ splits.back() = signature_offset;
+
+ for (idx_t i = 0; i < numChunks; i++) {
+ string x;
+ x.resize(splits[i + 1] - splits[i]);
+ for (idx_t j = 0; j < x.size(); j++) {
+ x[j] = ((uint8_t *)exe)[j + 4 + splits[i]];
+ }
+ ComputeSHA256String(x, &hash_chunks[i]);
+ }
+
+ string hash_concatenation;
+ hash_concatenation.reserve(32 * numChunks); // 256 bits -> 32 bytes per chunk
+
+ for (auto &hash_chunk : hash_chunks) {
+ hash_concatenation += hash_chunk;
+ }
+
+ string two_level_hash;
+ ComputeSHA256String(hash_concatenation, &two_level_hash);
+
+ for (idx_t j = 0; j < signature.size(); j++) {
+ signature[j] = ((uint8_t *)exe)[4 + signature_offset + j];
+ }
+ bool any_valid = false;
+ for (auto &key : ExtensionHelper::GetPublicKeys()) {
+ if (duckdb_mbedtls::MbedTlsWrapper::IsValidSha256Signature(key, signature, two_level_hash)) {
+ any_valid = true;
+ break;
+ }
+ }
+ if (!any_valid) {
+ throw IOException(config.error_manager->FormatException(ErrorType::UNSIGNED_EXTENSION, filename));
+ }
+ }
+ if (exe) {
+ free(exe);
+ }
+#else
if (!config.options.allow_unsigned_extensions) {
auto handle = fs.OpenFile(filename, FileFlags::FILE_FLAGS_READ);
@@ -180,25 +299,6 @@ bool ExtensionHelper::TryInitialLoad(DBConfig &config, FileSystem &fs, const str
}
auto basename = fs.ExtractBaseName(filename);
-#ifdef WASM_LOADABLE_EXTENSIONS
- EM_ASM(
- {
- // Next few lines should argubly in separate JavaScript-land function call
- // TODO: move them out / have them configurable
- const xhr = new XMLHttpRequest();
- xhr.open("GET", UTF8ToString($0), false);
- xhr.responseType = "arraybuffer";
- xhr.send(null);
- var uInt8Array = xhr.response;
- WebAssembly.validate(uInt8Array);
- console.log('Loading extension ', UTF8ToString($1));
-
- // Here we add the uInt8Array to Emscripten's filesystem, for it to be found by dlopen
- FS.writeFile(UTF8ToString($1), new Uint8Array(uInt8Array));
- },
- filename.c_str(), basename.c_str());
- auto dopen_from = basename;
-#else
auto dopen_from = filename;
#endif