blob: 5436809be65a281adfbeed1e323aba9d3a8fb62e [file] [log] [blame]
Anthony Liguori5ab85582009-11-11 10:39:14 -06001/*
2 * JSON lexer
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
Peter Maydellf2ad72b2016-01-29 17:50:01 +000014#include "qemu/osdep.h"
Anthony Liguori5ab85582009-11-11 10:39:14 -060015#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/qmp/json-lexer.h"
Markus Armbruster037f2442018-08-23 18:40:00 +020017#include "qapi/qmp/json-streamer.h"
Anthony Liguori5ab85582009-11-11 10:39:14 -060018
Anthony Liguori325601b2011-06-01 12:14:52 -050019#define MAX_TOKEN_SIZE (64ULL << 20)
20
Anthony Liguori5ab85582009-11-11 10:39:14 -060021/*
Markus Armbrustereddc0a72018-08-23 18:39:46 +020022 * From RFC 8259 "The JavaScript Object Notation (JSON) Data
23 * Interchange Format", with [comments in brackets]:
Eric Blakeff5394a2016-06-09 20:48:06 -060024 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020025 * The set of tokens includes six structural characters, strings,
26 * numbers, and three literal names.
Eric Blakeff5394a2016-06-09 20:48:06 -060027 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020028 * These are the six structural characters:
Eric Blakeff5394a2016-06-09 20:48:06 -060029 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020030 * begin-array = ws %x5B ws ; [ left square bracket
31 * begin-object = ws %x7B ws ; { left curly bracket
32 * end-array = ws %x5D ws ; ] right square bracket
33 * end-object = ws %x7D ws ; } right curly bracket
34 * name-separator = ws %x3A ws ; : colon
35 * value-separator = ws %x2C ws ; , comma
Eric Blakeff5394a2016-06-09 20:48:06 -060036 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020037 * Insignificant whitespace is allowed before or after any of the six
38 * structural characters.
39 * [This lexer accepts it before or after any token, which is actually
40 * the same, as the grammar always has structural characters between
41 * other tokens.]
Eric Blakeff5394a2016-06-09 20:48:06 -060042 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020043 * ws = *(
44 * %x20 / ; Space
45 * %x09 / ; Horizontal tab
46 * %x0A / ; Line feed or New line
47 * %x0D ) ; Carriage return
Anthony Liguori5ab85582009-11-11 10:39:14 -060048 *
Markus Armbrustereddc0a72018-08-23 18:39:46 +020049 * [...] three literal names:
50 * false null true
51 * [This lexer accepts [a-z]+, and leaves rejecting unknown literal
52 * names to the parser.]
53 *
54 * [Numbers:]
55 *
56 * number = [ minus ] int [ frac ] [ exp ]
57 * decimal-point = %x2E ; .
58 * digit1-9 = %x31-39 ; 1-9
59 * e = %x65 / %x45 ; e E
60 * exp = e [ minus / plus ] 1*DIGIT
61 * frac = decimal-point 1*DIGIT
62 * int = zero / ( digit1-9 *DIGIT )
63 * minus = %x2D ; -
64 * plus = %x2B ; +
65 * zero = %x30 ; 0
66 *
67 * [Strings:]
68 * string = quotation-mark *char quotation-mark
69 *
70 * char = unescaped /
71 * escape (
72 * %x22 / ; " quotation mark U+0022
73 * %x5C / ; \ reverse solidus U+005C
74 * %x2F / ; / solidus U+002F
75 * %x62 / ; b backspace U+0008
76 * %x66 / ; f form feed U+000C
77 * %x6E / ; n line feed U+000A
78 * %x72 / ; r carriage return U+000D
79 * %x74 / ; t tab U+0009
80 * %x75 4HEXDIG ) ; uXXXX U+XXXX
81 * escape = %x5C ; \
82 * quotation-mark = %x22 ; "
83 * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
Markus Armbrusterb2da4a42018-08-23 18:39:53 +020084 * [This lexer accepts any non-control character after escape, and
85 * leaves rejecting invalid ones to the parser.]
Markus Armbrustereddc0a72018-08-23 18:39:46 +020086 *
87 *
88 * Extensions over RFC 8259:
89 * - Extra escape sequence in strings:
90 * 0x27 (apostrophe) is recognized after escape, too
91 * - Single-quoted strings:
92 * Like double-quoted strings, except they're delimited by %x27
93 * (apostrophe) instead of %x22 (quotation mark), and can't contain
94 * unescaped apostrophe, but can contain unescaped quotation mark.
95 * - Interpolation:
96 * interpolation = %((l|ll|I64)[du]|[ipsf])
97 *
98 * Note:
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +020099 * - Input must be encoded in modified UTF-8.
Markus Armbrustereddc0a72018-08-23 18:39:46 +0200100 * - Decoding and validating is left to the parser.
Anthony Liguori5ab85582009-11-11 10:39:14 -0600101 */
102
103enum json_lexer_state {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100104 IN_ERROR = 0, /* must really be 0, see json_lexer[] */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600105 IN_DQ_STRING_ESCAPE,
106 IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600107 IN_SQ_STRING_ESCAPE,
108 IN_SQ_STRING,
109 IN_ZERO,
110 IN_DIGITS,
111 IN_DIGIT,
112 IN_EXP_E,
113 IN_MANTISSA,
114 IN_MANTISSA_DIGITS,
115 IN_NONZERO_NUMBER,
116 IN_NEG_NONZERO_NUMBER,
117 IN_KEYWORD,
Markus Armbruster61030282018-08-23 18:40:04 +0200118 IN_INTERP,
119 IN_INTERP_L,
120 IN_INTERP_LL,
121 IN_INTERP_I,
122 IN_INTERP_I6,
123 IN_INTERP_I64,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600124 IN_WHITESPACE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600125 IN_START,
126};
127
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100128QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START);
129
Anthony Liguori5ab85582009-11-11 10:39:14 -0600130#define TERMINAL(state) [0 ... 0x7F] = (state)
131
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200132/* Return whether TERMINAL is a terminal state and the transition to it
133 from OLD_STATE required lookahead. This happens whenever the table
134 below uses the TERMINAL macro. */
135#define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200136 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200137
Anthony Liguori5ab85582009-11-11 10:39:14 -0600138static const uint8_t json_lexer[][256] = {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100139 /* Relies on default initialization to IN_ERROR! */
140
Anthony Liguori5ab85582009-11-11 10:39:14 -0600141 /* double quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600142 [IN_DQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200143 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600144 },
145 [IN_DQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200146 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600147 ['\\'] = IN_DQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200148 ['"'] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600149 },
150
151 /* single quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600152 [IN_SQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200153 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600154 },
155 [IN_SQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200156 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600157 ['\\'] = IN_SQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200158 ['\''] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600159 },
160
161 /* Zero */
162 [IN_ZERO] = {
163 TERMINAL(JSON_INTEGER),
Blue Swirl33d05392011-03-27 09:07:54 +0000164 ['0' ... '9'] = IN_ERROR,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600165 ['.'] = IN_MANTISSA,
166 },
167
168 /* Float */
169 [IN_DIGITS] = {
170 TERMINAL(JSON_FLOAT),
171 ['0' ... '9'] = IN_DIGITS,
172 },
173
174 [IN_DIGIT] = {
175 ['0' ... '9'] = IN_DIGITS,
176 },
177
178 [IN_EXP_E] = {
179 ['-'] = IN_DIGIT,
180 ['+'] = IN_DIGIT,
181 ['0' ... '9'] = IN_DIGITS,
182 },
183
184 [IN_MANTISSA_DIGITS] = {
185 TERMINAL(JSON_FLOAT),
186 ['0' ... '9'] = IN_MANTISSA_DIGITS,
187 ['e'] = IN_EXP_E,
188 ['E'] = IN_EXP_E,
189 },
190
191 [IN_MANTISSA] = {
192 ['0' ... '9'] = IN_MANTISSA_DIGITS,
193 },
194
195 /* Number */
196 [IN_NONZERO_NUMBER] = {
197 TERMINAL(JSON_INTEGER),
198 ['0' ... '9'] = IN_NONZERO_NUMBER,
199 ['e'] = IN_EXP_E,
200 ['E'] = IN_EXP_E,
201 ['.'] = IN_MANTISSA,
202 },
203
204 [IN_NEG_NONZERO_NUMBER] = {
205 ['0'] = IN_ZERO,
206 ['1' ... '9'] = IN_NONZERO_NUMBER,
207 },
208
209 /* keywords */
210 [IN_KEYWORD] = {
211 TERMINAL(JSON_KEYWORD),
212 ['a' ... 'z'] = IN_KEYWORD,
213 },
214
215 /* whitespace */
216 [IN_WHITESPACE] = {
217 TERMINAL(JSON_SKIP),
218 [' '] = IN_WHITESPACE,
219 ['\t'] = IN_WHITESPACE,
220 ['\r'] = IN_WHITESPACE,
221 ['\n'] = IN_WHITESPACE,
Eric Blakeff5394a2016-06-09 20:48:06 -0600222 },
Anthony Liguori5ab85582009-11-11 10:39:14 -0600223
Markus Armbruster61030282018-08-23 18:40:04 +0200224 /* interpolation */
225 [IN_INTERP_LL] = {
226 ['d'] = JSON_INTERP,
227 ['u'] = JSON_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600228 },
229
Markus Armbruster61030282018-08-23 18:40:04 +0200230 [IN_INTERP_L] = {
231 ['d'] = JSON_INTERP,
232 ['l'] = IN_INTERP_LL,
233 ['u'] = JSON_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600234 },
235
Markus Armbruster61030282018-08-23 18:40:04 +0200236 [IN_INTERP_I64] = {
237 ['d'] = JSON_INTERP,
238 ['u'] = JSON_INTERP,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800239 },
240
Markus Armbruster61030282018-08-23 18:40:04 +0200241 [IN_INTERP_I6] = {
242 ['4'] = IN_INTERP_I64,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800243 },
244
Markus Armbruster61030282018-08-23 18:40:04 +0200245 [IN_INTERP_I] = {
246 ['6'] = IN_INTERP_I6,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800247 },
248
Markus Armbruster61030282018-08-23 18:40:04 +0200249 [IN_INTERP] = {
250 ['d'] = JSON_INTERP,
251 ['i'] = JSON_INTERP,
252 ['p'] = JSON_INTERP,
253 ['s'] = JSON_INTERP,
254 ['u'] = JSON_INTERP,
255 ['f'] = JSON_INTERP,
256 ['l'] = IN_INTERP_L,
257 ['I'] = IN_INTERP_I,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600258 },
259
260 /* top level rule */
261 [IN_START] = {
262 ['"'] = IN_DQ_STRING,
263 ['\''] = IN_SQ_STRING,
264 ['0'] = IN_ZERO,
265 ['1' ... '9'] = IN_NONZERO_NUMBER,
266 ['-'] = IN_NEG_NONZERO_NUMBER,
Markus Armbrusterc5461662015-11-25 22:23:26 +0100267 ['{'] = JSON_LCURLY,
268 ['}'] = JSON_RCURLY,
269 ['['] = JSON_LSQUARE,
270 [']'] = JSON_RSQUARE,
271 [','] = JSON_COMMA,
272 [':'] = JSON_COLON,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600273 ['a' ... 'z'] = IN_KEYWORD,
Markus Armbruster61030282018-08-23 18:40:04 +0200274 ['%'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600275 [' '] = IN_WHITESPACE,
276 ['\t'] = IN_WHITESPACE,
277 ['\r'] = IN_WHITESPACE,
278 ['\n'] = IN_WHITESPACE,
279 },
280};
281
Markus Armbruster037f2442018-08-23 18:40:00 +0200282void json_lexer_init(JSONLexer *lexer)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600283{
Anthony Liguori5ab85582009-11-11 10:39:14 -0600284 lexer->state = IN_START;
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100285 lexer->token = g_string_sized_new(3);
Luiz Capitulino03308f62010-05-17 17:50:01 -0300286 lexer->x = lexer->y = 0;
Anthony Liguori5ab85582009-11-11 10:39:14 -0600287}
288
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200289static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600290{
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200291 int char_consumed, new_state;
292
Anthony Liguori5ab85582009-11-11 10:39:14 -0600293 lexer->x++;
294 if (ch == '\n') {
295 lexer->x = 0;
296 lexer->y++;
297 }
298
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200299 do {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100300 assert(lexer->state <= ARRAY_SIZE(json_lexer));
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200301 new_state = json_lexer[lexer->state][(uint8_t)ch];
302 char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200303 if (char_consumed && !flush) {
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100304 g_string_append_c(lexer->token, ch);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200305 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600306
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200307 switch (new_state) {
Markus Armbrusterc5461662015-11-25 22:23:26 +0100308 case JSON_LCURLY:
309 case JSON_RCURLY:
310 case JSON_LSQUARE:
311 case JSON_RSQUARE:
312 case JSON_COLON:
313 case JSON_COMMA:
Markus Armbruster61030282018-08-23 18:40:04 +0200314 case JSON_INTERP:
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200315 case JSON_INTEGER:
316 case JSON_FLOAT:
317 case JSON_KEYWORD:
318 case JSON_STRING:
Markus Armbruster037f2442018-08-23 18:40:00 +0200319 json_message_process_token(lexer, lexer->token, new_state,
320 lexer->x, lexer->y);
Stefan Weil0b0404b2012-01-09 18:29:51 +0100321 /* fall through */
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200322 case JSON_SKIP:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100323 g_string_truncate(lexer->token, 0);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200324 new_state = IN_START;
325 break;
Blue Swirl33d05392011-03-27 09:07:54 +0000326 case IN_ERROR:
Michael Rothb011f612011-06-01 12:14:58 -0500327 /* XXX: To avoid having previous bad input leaving the parser in an
328 * unresponsive state where we consume unpredictable amounts of
329 * subsequent "good" input, percolate this error state up to the
330 * tokenizer/parser by forcing a NULL object to be emitted, then
331 * reset state.
332 *
333 * Also note that this handling is required for reliable channel
334 * negotiation between QMP and the guest agent, since chr(0xFF)
335 * is placed at the beginning of certain events to ensure proper
336 * delivery when the channel is in an unknown state. chr(0xFF) is
337 * never a valid ASCII/UTF-8 sequence, so this should reliably
338 * induce an error/flush state.
339 */
Markus Armbruster037f2442018-08-23 18:40:00 +0200340 json_message_process_token(lexer, lexer->token, JSON_ERROR,
341 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100342 g_string_truncate(lexer->token, 0);
Anthony Liguori529a0ef2011-06-01 12:14:56 -0500343 new_state = IN_START;
Michael Rothb011f612011-06-01 12:14:58 -0500344 lexer->state = new_state;
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200345 return;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200346 default:
347 break;
348 }
349 lexer->state = new_state;
Michael Rothbd3924a2011-06-01 12:14:57 -0500350 } while (!char_consumed && !flush);
Anthony Liguori325601b2011-06-01 12:14:52 -0500351
352 /* Do not let a single token grow to an arbitrarily large size,
353 * this is a security consideration.
354 */
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100355 if (lexer->token->len > MAX_TOKEN_SIZE) {
Markus Armbruster037f2442018-08-23 18:40:00 +0200356 json_message_process_token(lexer, lexer->token, lexer->state,
357 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100358 g_string_truncate(lexer->token, 0);
Anthony Liguori325601b2011-06-01 12:14:52 -0500359 lexer->state = IN_START;
360 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600361}
362
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200363void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600364{
365 size_t i;
366
367 for (i = 0; i < size; i++) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200368 json_lexer_feed_char(lexer, buffer[i], false);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600369 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600370}
371
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200372void json_lexer_flush(JSONLexer *lexer)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600373{
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200374 if (lexer->state != IN_START) {
375 json_lexer_feed_char(lexer, 0, true);
376 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600377}
378
379void json_lexer_destroy(JSONLexer *lexer)
380{
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100381 g_string_free(lexer->token, true);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600382}