forked from pwntester/codeql.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryserver.lua
More file actions
393 lines (359 loc) · 11 KB
/
Copy pathqueryserver.lua
File metadata and controls
393 lines (359 loc) · 11 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
local util = require "codeql.util"
local loader = require "codeql.loader"
local config = require "codeql.config"
local rpc = require "vim.lsp.rpc"
local protocol = require "vim.lsp.protocol"
local vim = vim
local client_index = 0
local progress_id = -1
local last_rpc_msg_id = -1
local lsp_client_id
local lsp_progress_handler
local progress_stage
local function next_client_id()
client_index = client_index + 1
return client_index
end
local function next_progress_id()
progress_id = progress_id + 1
return progress_id
end
local M = {}
M.client = nil
function M.start_client(opts)
local client_id = next_client_id()
local callbacks = opts.callbacks or {}
local name = opts.name or tostring(client_id)
local log_prefix = string.format("QueryServer[%s]", name)
local handlers = {}
local function resolve_callback(method)
return callbacks[method] -- or default_callbacks[method]
end
function handlers.notification(method, params)
local callback = resolve_callback(method)
if callback then
-- Method name is provided here for convenience.
callback(method, params, client_id)
end
end
function handlers.server_request(method, params)
local callback = resolve_callback(method)
if callback then
return callback(method, params, client_id)
end
return nil, rpc.rpc_response_error(protocol.ErrorCodes.MethodNotFound)
end
function handlers.on_error(code, err)
util.err_message(string.format("%s: Error %s: %s", log_prefix, rpc.client_errors[code], vim.inspect(err)))
if opts.on_error then
local status, usererr = pcall(opts.on_error, code, err)
if not status then
util.err_message(string.format("%s user on_error failed: ", log_prefix, tostring(usererr)))
end
end
end
function handlers.on_exit(code, signal)
if opts.on_exit then
pcall(opts.on_exit, code, signal)
end
end
-- Start the RPC client.
local client = rpc.start(opts.cmd, handlers, {
env = opts.cmd_env,
})
client.id = client_id
return client
end
function M.get_lsp_client_id()
if lsp_client_id then
return lsp_client_id
else
local lsp_clients = vim.lsp.get_active_clients()
for _, lsp_client in ipairs(lsp_clients) do
if lsp_client.name == "codeqlls" then
lsp_client_id = lsp_client.id
return lsp_client_id
end
end
end
end
function M.get_lsp_handler()
if lsp_progress_handler then
return lsp_progress_handler
end
lsp_progress_handler = vim.lsp.handlers["$/progress"]
return lsp_progress_handler
end
function M.start_server()
if M.client then
return M.client
end
util.message "Starting CodeQL Query Server"
local cmd = {
"codeql",
"execute",
"query-server2",
"--debug",
"--tuple-counting",
"--threads=0",
"--evaluator-log-level",
"5",
"-v",
"--log-to-stderr",
--"--additional-packs", "/Users/pwntester/src/github.com/github/securitylab-codeql",
}
local conf = config.values
vim.list_extend(cmd, conf.ram_opts)
local opts = {
cmd = cmd,
offset_encoding = { "utf-8", "utf-16" },
callbacks = {
-- progress update
["ql/progressUpdated"] = function(_, params, _)
local client_id = M.get_lsp_client_id()
local lsp_handler = M.get_lsp_handler()
local message = params.message
local progress = (100 * params.step) / params.maxStep
if progress == 0 and not progress_stage then
progress_stage = "begin"
elseif progress == 0 then
return
elseif params.step >= params.maxStep - 1 then
progress_stage = "end"
end
local handler
if client_id and lsp_handler and type(lsp_handler) == "function" then
-- use LSP progress handler
handler = function(result)
-- https://github.com/neovim/neovim/blob/b8ad1bfe8bc23ed5ffbfe43df5fda3501f1d2802/runtime/lua/vim/lsp/handlers.lua#L24
local ctx = {
client_id = M.get_lsp_client_id(),
}
--vim.lsp.handlers["$/progress"](nil, {value = { message = "foo"}, token = "foo"}, { client_id = vim.lsp.get_active_clients()[1].id })
--print(vim.inspect(result), vim.inspect(ctx))
lsp_handler(nil, result, ctx)
end
else
-- Use vim.notify
handler = function(result)
if result.message then
util.message("Query execution progress: " .. result.value.message)
end
end
end
if progress_stage == "begin" then
handler {
token = "CodeQLToken",
value = {
kind = progress_stage,
title = "CodeQL",
percentage = 0,
},
}
progress_stage = "report"
elseif progress_stage == "report" then
handler {
token = "CodeQLToken",
value = {
kind = progress_stage,
message = message,
percentage = progress,
},
}
elseif progress_stage == "end" then
handler {
token = "CodeQLToken",
value = {
kind = progress_stage,
percentage = 100,
},
}
progress_stage = nil
end
end,
},
}
return M.start_client(opts)
end
function M.run_query(opts)
local dbPath = config.database.path
if not dbPath then
--util.err_message "Cannot find dataset folder. Did you `:QL db set`?"
return
end
if not M.client then
M.client = M.start_server()
end
local bufnr = opts.bufnr
local queryPath = opts.query
local bqrsPath = string.format(vim.fn.tempname(), ".bqrs")
local additionalPacks = util.get_additional_packs()
local runQuery_params = {
body = {
db = dbPath,
additionalPacks = {
additionalPacks or "",
},
externalInputs = {},
singletonExternalInputs = opts.templateValues or {},
outputPath = bqrsPath,
queryPath = queryPath,
-- do we want Datalog Intermediary Language dumps?
-- https://codeql.github.com/docs/codeql-overview/codeql-glossary/#dil
-- dilPath = "",
-- logPath = "",
target = opts.quick_eval and {
quickEval = {
quickEvalPos = {
fileName = queryPath,
line = opts.startLine,
column = opts.startColumn,
endLine = opts.endLine,
endColumn = opts.endColumn,
},
},
} or {
query = { xx = "" },
},
},
progressId = next_progress_id(),
}
local runQuery_callback = function(err, resp)
if err then
util.err_message("ERROR: " .. vim.inspect(err))
end
if not resp then
-- we may have got an RPC error, so print it
-- this is possible if the language server crashed, etc
return
end
if resp["resultType"] == 0 then
if util.is_file(bqrsPath) then
util.bqrs_info({
bqrs_path = bqrsPath,
bufnr = bufnr,
db_path = dbPath,
query_path = queryPath,
query_kind = opts.metadata["kind"],
query_id = opts.metadata["id"],
save_bqrs = true,
}, loader.process_results)
end
elseif resp["resultType"] == 1 then
util.err_message("ERROR: Other: " .. resp["message"])
elseif resp["resultType"] == 2 then
util.err_message("ERROR: Compilation Error: " .. resp["message"])
elseif resp["resultType"] == 3 then
util.err_message("ERROR: OOM Error: " .. resp["message"])
elseif resp["resultType"] == 4 then
util.err_message("ERROR: Query cancelled: " .. resp["message"])
elseif resp["resultType"] == 5 then
util.err_message("ERROR: DB Scheme mismatch: " .. resp["message"])
elseif resp["resultType"] == 6 then
util.err_message("ERROR: DB Scheme no upgrade found: " .. resp["message"])
else
util.err_message "Query run failed. Database may be locked by a different Query Server"
end
end
-- run query
--util.message(string.format("Running query %s", queryPath))
_, last_rpc_msg_id = M.client.request("evaluation/runQuery", runQuery_params, runQuery_callback)
end
function M.register_database(database)
if not M.client then
M.client = M.start_server()
end
config.database = database
local lang = config.database.languages[1]
-- https://github.com/github/vscode-codeql/blob/0e2c03f572226a85ef4691621ca07424c06a2162/extensions/ql-vscode/src/common/query-language.ts#L48-L57
local langTodbScheme = {
javascript = "semmlecode.javascript.dbscheme",
cpp = "semmlecode.cpp.dbscheme",
java = "semmlecode.dbscheme",
python = "semmlecode.python.dbscheme",
csharp = "semmlecode.csharp.dbscheme",
go = "go.dbscheme",
ruby = "ruby.dbscheme",
ql = "ql.dbscheme",
swift = "swift.dbscheme",
yaml = "yaml.dbscheme",
}
local dbschemePath = config.database.datasetFolder .. "/" .. langTodbScheme[lang]
if not vim.fn.filereadable(dbschemePath) then
util.err_message "Cannot find dbscheme file"
return
else
util.message(string.format("Using dbscheme file %s", dbschemePath))
end
local resp = util.database_upgrades(dbschemePath)
if resp ~= vim.NIL and #resp.scripts > 0 then
util.database_upgrade(config.database.path)
end
util.message(string.format("Registering database %s", config.database.path))
local params = {
body = {
databases = {
config.database.path,
},
progressId = next_progress_id(),
},
}
M.client.request("evaluation/registerDatabases", params, function(err, result)
if err then
util.err_message(string.format("Error registering database %s", vim.inspect(err)))
else
util.message(string.format("Successfully registered %s", result.registeredDatabases[1]))
-- TODO: add option to open the drawer automatically
--require 'codeql.explorer'.draw()
end
end)
end
function M.unregister_database(cb)
if util.is_blank(config.database) then
vim.notify "No database registered"
return
end
if not M.client then
M.client = M.start_server()
end
util.message(string.format("Deregistering database %s", config.database.path))
local params = {
body = {
databases = {
config.database.path,
},
progressId = next_progress_id(),
},
}
M.client.request("evaluation/deregisterDatabases", params, function(err, result)
if err then
util.err_message(string.format("Error registering database %s", vim.inspect(err)))
elseif #result.registeredDatabases == 0 then
util.message(string.format("Successfully deregistered %s", config.database.path))
config.database = nil
end
-- call the callback
if cb then
cb()
end
end)
end
function M.cancel_query()
if last_rpc_msg_id < 0 then
return
end
if not M.client then
M.client = M.start_server()
end
util.message(M.client.notify("$/cancelRequest", {
id = last_rpc_msg_id,
}))
end
function M.stop_server()
if M.client then
local handle = M.client.handle
handle:kill()
M.client = nil
end
end
return M