blob: 7c31c2c8fff9250a92bd0447ec2368ae55fde554 [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.
Markus Armbruster2cbd15a2018-08-23 18:40:05 +020095 * - Interpolation, if enabled:
Markus Armbrustereddc0a72018-08-23 18:39:46 +020096 * 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,
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200126 IN_START_INTERP, /* must be IN_START + 1 */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600127};
128
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200129QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START_INTERP);
130QEMU_BUILD_BUG_ON(IN_START_INTERP != IN_START + 1);
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100131
Anthony Liguori5ab85582009-11-11 10:39:14 -0600132#define TERMINAL(state) [0 ... 0x7F] = (state)
133
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200134/* Return whether TERMINAL is a terminal state and the transition to it
135 from OLD_STATE required lookahead. This happens whenever the table
136 below uses the TERMINAL macro. */
137#define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200138 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200139
Anthony Liguori5ab85582009-11-11 10:39:14 -0600140static const uint8_t json_lexer[][256] = {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100141 /* Relies on default initialization to IN_ERROR! */
142
Anthony Liguori5ab85582009-11-11 10:39:14 -0600143 /* double quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600144 [IN_DQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200145 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600146 },
147 [IN_DQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200148 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600149 ['\\'] = IN_DQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200150 ['"'] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600151 },
152
153 /* single quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600154 [IN_SQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200155 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600156 },
157 [IN_SQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200158 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600159 ['\\'] = IN_SQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200160 ['\''] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600161 },
162
163 /* Zero */
164 [IN_ZERO] = {
165 TERMINAL(JSON_INTEGER),
Blue Swirl33d05392011-03-27 09:07:54 +0000166 ['0' ... '9'] = IN_ERROR,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600167 ['.'] = IN_MANTISSA,
168 },
169
170 /* Float */
171 [IN_DIGITS] = {
172 TERMINAL(JSON_FLOAT),
173 ['0' ... '9'] = IN_DIGITS,
174 },
175
176 [IN_DIGIT] = {
177 ['0' ... '9'] = IN_DIGITS,
178 },
179
180 [IN_EXP_E] = {
181 ['-'] = IN_DIGIT,
182 ['+'] = IN_DIGIT,
183 ['0' ... '9'] = IN_DIGITS,
184 },
185
186 [IN_MANTISSA_DIGITS] = {
187 TERMINAL(JSON_FLOAT),
188 ['0' ... '9'] = IN_MANTISSA_DIGITS,
189 ['e'] = IN_EXP_E,
190 ['E'] = IN_EXP_E,
191 },
192
193 [IN_MANTISSA] = {
194 ['0' ... '9'] = IN_MANTISSA_DIGITS,
195 },
196
197 /* Number */
198 [IN_NONZERO_NUMBER] = {
199 TERMINAL(JSON_INTEGER),
200 ['0' ... '9'] = IN_NONZERO_NUMBER,
201 ['e'] = IN_EXP_E,
202 ['E'] = IN_EXP_E,
203 ['.'] = IN_MANTISSA,
204 },
205
206 [IN_NEG_NONZERO_NUMBER] = {
207 ['0'] = IN_ZERO,
208 ['1' ... '9'] = IN_NONZERO_NUMBER,
209 },
210
211 /* keywords */
212 [IN_KEYWORD] = {
213 TERMINAL(JSON_KEYWORD),
214 ['a' ... 'z'] = IN_KEYWORD,
215 },
216
217 /* whitespace */
218 [IN_WHITESPACE] = {
219 TERMINAL(JSON_SKIP),
220 [' '] = IN_WHITESPACE,
221 ['\t'] = IN_WHITESPACE,
222 ['\r'] = IN_WHITESPACE,
223 ['\n'] = IN_WHITESPACE,
Eric Blakeff5394a2016-06-09 20:48:06 -0600224 },
Anthony Liguori5ab85582009-11-11 10:39:14 -0600225
Markus Armbruster61030282018-08-23 18:40:04 +0200226 /* interpolation */
227 [IN_INTERP_LL] = {
228 ['d'] = JSON_INTERP,
229 ['u'] = JSON_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600230 },
231
Markus Armbruster61030282018-08-23 18:40:04 +0200232 [IN_INTERP_L] = {
233 ['d'] = JSON_INTERP,
234 ['l'] = IN_INTERP_LL,
235 ['u'] = JSON_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600236 },
237
Markus Armbruster61030282018-08-23 18:40:04 +0200238 [IN_INTERP_I64] = {
239 ['d'] = JSON_INTERP,
240 ['u'] = JSON_INTERP,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800241 },
242
Markus Armbruster61030282018-08-23 18:40:04 +0200243 [IN_INTERP_I6] = {
244 ['4'] = IN_INTERP_I64,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800245 },
246
Markus Armbruster61030282018-08-23 18:40:04 +0200247 [IN_INTERP_I] = {
248 ['6'] = IN_INTERP_I6,
Roy Tam2c0d4b32010-02-04 10:30:30 +0800249 },
250
Markus Armbruster61030282018-08-23 18:40:04 +0200251 [IN_INTERP] = {
252 ['d'] = JSON_INTERP,
253 ['i'] = JSON_INTERP,
254 ['p'] = JSON_INTERP,
255 ['s'] = JSON_INTERP,
256 ['u'] = JSON_INTERP,
257 ['f'] = JSON_INTERP,
258 ['l'] = IN_INTERP_L,
259 ['I'] = IN_INTERP_I,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600260 },
261
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200262 /*
263 * Two start states:
264 * - IN_START recognizes JSON tokens with our string extensions
265 * - IN_START_INTERP additionally recognizes interpolation.
266 */
267 [IN_START ... IN_START_INTERP] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600268 ['"'] = IN_DQ_STRING,
269 ['\''] = IN_SQ_STRING,
270 ['0'] = IN_ZERO,
271 ['1' ... '9'] = IN_NONZERO_NUMBER,
272 ['-'] = IN_NEG_NONZERO_NUMBER,
Markus Armbrusterc5461662015-11-25 22:23:26 +0100273 ['{'] = JSON_LCURLY,
274 ['}'] = JSON_RCURLY,
275 ['['] = JSON_LSQUARE,
276 [']'] = JSON_RSQUARE,
277 [','] = JSON_COMMA,
278 [':'] = JSON_COLON,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600279 ['a' ... 'z'] = IN_KEYWORD,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600280 [' '] = IN_WHITESPACE,
281 ['\t'] = IN_WHITESPACE,
282 ['\r'] = IN_WHITESPACE,
283 ['\n'] = IN_WHITESPACE,
284 },
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200285 [IN_START_INTERP]['%'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600286};
287
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200288void json_lexer_init(JSONLexer *lexer, bool enable_interpolation)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600289{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200290 lexer->start_state = lexer->state = enable_interpolation
291 ? IN_START_INTERP : IN_START;
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100292 lexer->token = g_string_sized_new(3);
Luiz Capitulino03308f62010-05-17 17:50:01 -0300293 lexer->x = lexer->y = 0;
Anthony Liguori5ab85582009-11-11 10:39:14 -0600294}
295
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200296static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600297{
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200298 int char_consumed, new_state;
299
Anthony Liguori5ab85582009-11-11 10:39:14 -0600300 lexer->x++;
301 if (ch == '\n') {
302 lexer->x = 0;
303 lexer->y++;
304 }
305
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200306 do {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100307 assert(lexer->state <= ARRAY_SIZE(json_lexer));
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200308 new_state = json_lexer[lexer->state][(uint8_t)ch];
309 char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200310 if (char_consumed && !flush) {
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100311 g_string_append_c(lexer->token, ch);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200312 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600313
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200314 switch (new_state) {
Markus Armbrusterc5461662015-11-25 22:23:26 +0100315 case JSON_LCURLY:
316 case JSON_RCURLY:
317 case JSON_LSQUARE:
318 case JSON_RSQUARE:
319 case JSON_COLON:
320 case JSON_COMMA:
Markus Armbruster61030282018-08-23 18:40:04 +0200321 case JSON_INTERP:
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200322 case JSON_INTEGER:
323 case JSON_FLOAT:
324 case JSON_KEYWORD:
325 case JSON_STRING:
Markus Armbruster037f2442018-08-23 18:40:00 +0200326 json_message_process_token(lexer, lexer->token, new_state,
327 lexer->x, lexer->y);
Stefan Weil0b0404b2012-01-09 18:29:51 +0100328 /* fall through */
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200329 case JSON_SKIP:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100330 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200331 new_state = lexer->start_state;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200332 break;
Blue Swirl33d05392011-03-27 09:07:54 +0000333 case IN_ERROR:
Michael Rothb011f612011-06-01 12:14:58 -0500334 /* XXX: To avoid having previous bad input leaving the parser in an
335 * unresponsive state where we consume unpredictable amounts of
336 * subsequent "good" input, percolate this error state up to the
Markus Armbruster84a56f32018-08-23 18:40:06 +0200337 * parser by emitting a JSON_ERROR token, then reset lexer state.
Michael Rothb011f612011-06-01 12:14:58 -0500338 *
339 * Also note that this handling is required for reliable channel
340 * negotiation between QMP and the guest agent, since chr(0xFF)
341 * is placed at the beginning of certain events to ensure proper
342 * delivery when the channel is in an unknown state. chr(0xFF) is
343 * never a valid ASCII/UTF-8 sequence, so this should reliably
344 * induce an error/flush state.
345 */
Markus Armbruster037f2442018-08-23 18:40:00 +0200346 json_message_process_token(lexer, lexer->token, JSON_ERROR,
347 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100348 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200349 lexer->state = lexer->start_state;
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200350 return;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200351 default:
352 break;
353 }
354 lexer->state = new_state;
Michael Rothbd3924a2011-06-01 12:14:57 -0500355 } while (!char_consumed && !flush);
Anthony Liguori325601b2011-06-01 12:14:52 -0500356
357 /* Do not let a single token grow to an arbitrarily large size,
358 * this is a security consideration.
359 */
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100360 if (lexer->token->len > MAX_TOKEN_SIZE) {
Markus Armbruster037f2442018-08-23 18:40:00 +0200361 json_message_process_token(lexer, lexer->token, lexer->state,
362 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100363 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200364 lexer->state = lexer->start_state;
Anthony Liguori325601b2011-06-01 12:14:52 -0500365 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600366}
367
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200368void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600369{
370 size_t i;
371
372 for (i = 0; i < size; i++) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200373 json_lexer_feed_char(lexer, buffer[i], false);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600374 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600375}
376
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200377void json_lexer_flush(JSONLexer *lexer)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600378{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200379 if (lexer->state != lexer->start_state) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200380 json_lexer_feed_char(lexer, 0, true);
381 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600382}
383
384void json_lexer_destroy(JSONLexer *lexer)
385{
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100386 g_string_free(lexer->token, true);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600387}