blob: f1a4b5a4302d65eaad0eeb025089c01cb7448084 [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 Armbrusterf7617d42018-08-23 18:40:07 +020096 * The lexer accepts %[A-Za-z0-9]*, and leaves rejecting invalid
97 * ones to the parser.
Markus Armbrustereddc0a72018-08-23 18:39:46 +020098 *
99 * Note:
Markus Armbruster4b1c0cd2018-08-23 18:39:52 +0200100 * - Input must be encoded in modified UTF-8.
Markus Armbrustereddc0a72018-08-23 18:39:46 +0200101 * - Decoding and validating is left to the parser.
Anthony Liguori5ab85582009-11-11 10:39:14 -0600102 */
103
104enum json_lexer_state {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100105 IN_ERROR = 0, /* must really be 0, see json_lexer[] */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600106 IN_DQ_STRING_ESCAPE,
107 IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600108 IN_SQ_STRING_ESCAPE,
109 IN_SQ_STRING,
110 IN_ZERO,
111 IN_DIGITS,
112 IN_DIGIT,
113 IN_EXP_E,
114 IN_MANTISSA,
115 IN_MANTISSA_DIGITS,
116 IN_NONZERO_NUMBER,
117 IN_NEG_NONZERO_NUMBER,
118 IN_KEYWORD,
Markus Armbruster61030282018-08-23 18:40:04 +0200119 IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600120 IN_WHITESPACE,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600121 IN_START,
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200122 IN_START_INTERP, /* must be IN_START + 1 */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600123};
124
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200125QEMU_BUILD_BUG_ON((int)JSON_MIN <= (int)IN_START_INTERP);
126QEMU_BUILD_BUG_ON(IN_START_INTERP != IN_START + 1);
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100127
Anthony Liguori5ab85582009-11-11 10:39:14 -0600128#define TERMINAL(state) [0 ... 0x7F] = (state)
129
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200130/* Return whether TERMINAL is a terminal state and the transition to it
131 from OLD_STATE required lookahead. This happens whenever the table
132 below uses the TERMINAL macro. */
133#define TERMINAL_NEEDED_LOOKAHEAD(old_state, terminal) \
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200134 (terminal != IN_ERROR && json_lexer[(old_state)][0] == (terminal))
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200135
Anthony Liguori5ab85582009-11-11 10:39:14 -0600136static const uint8_t json_lexer[][256] = {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100137 /* Relies on default initialization to IN_ERROR! */
138
Anthony Liguori5ab85582009-11-11 10:39:14 -0600139 /* double quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600140 [IN_DQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200141 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600142 },
143 [IN_DQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200144 [0x20 ... 0xFD] = IN_DQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600145 ['\\'] = IN_DQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200146 ['"'] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600147 },
148
149 /* single quote string */
Anthony Liguori5ab85582009-11-11 10:39:14 -0600150 [IN_SQ_STRING_ESCAPE] = {
Markus Armbrusterb2da4a42018-08-23 18:39:53 +0200151 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600152 },
153 [IN_SQ_STRING] = {
Markus Armbrusterde930f42018-08-23 18:39:51 +0200154 [0x20 ... 0xFD] = IN_SQ_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600155 ['\\'] = IN_SQ_STRING_ESCAPE,
Paolo Bonzini28e91a62010-05-24 09:39:53 +0200156 ['\''] = JSON_STRING,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600157 },
158
159 /* Zero */
160 [IN_ZERO] = {
161 TERMINAL(JSON_INTEGER),
Blue Swirl33d05392011-03-27 09:07:54 +0000162 ['0' ... '9'] = IN_ERROR,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600163 ['.'] = IN_MANTISSA,
164 },
165
166 /* Float */
167 [IN_DIGITS] = {
168 TERMINAL(JSON_FLOAT),
169 ['0' ... '9'] = IN_DIGITS,
170 },
171
172 [IN_DIGIT] = {
173 ['0' ... '9'] = IN_DIGITS,
174 },
175
176 [IN_EXP_E] = {
177 ['-'] = IN_DIGIT,
178 ['+'] = IN_DIGIT,
179 ['0' ... '9'] = IN_DIGITS,
180 },
181
182 [IN_MANTISSA_DIGITS] = {
183 TERMINAL(JSON_FLOAT),
184 ['0' ... '9'] = IN_MANTISSA_DIGITS,
185 ['e'] = IN_EXP_E,
186 ['E'] = IN_EXP_E,
187 },
188
189 [IN_MANTISSA] = {
190 ['0' ... '9'] = IN_MANTISSA_DIGITS,
191 },
192
193 /* Number */
194 [IN_NONZERO_NUMBER] = {
195 TERMINAL(JSON_INTEGER),
196 ['0' ... '9'] = IN_NONZERO_NUMBER,
197 ['e'] = IN_EXP_E,
198 ['E'] = IN_EXP_E,
199 ['.'] = IN_MANTISSA,
200 },
201
202 [IN_NEG_NONZERO_NUMBER] = {
203 ['0'] = IN_ZERO,
204 ['1' ... '9'] = IN_NONZERO_NUMBER,
205 },
206
207 /* keywords */
208 [IN_KEYWORD] = {
209 TERMINAL(JSON_KEYWORD),
210 ['a' ... 'z'] = IN_KEYWORD,
211 },
212
213 /* whitespace */
214 [IN_WHITESPACE] = {
215 TERMINAL(JSON_SKIP),
216 [' '] = IN_WHITESPACE,
217 ['\t'] = IN_WHITESPACE,
218 ['\r'] = IN_WHITESPACE,
219 ['\n'] = IN_WHITESPACE,
Eric Blakeff5394a2016-06-09 20:48:06 -0600220 },
Anthony Liguori5ab85582009-11-11 10:39:14 -0600221
Markus Armbruster61030282018-08-23 18:40:04 +0200222 /* interpolation */
Markus Armbruster61030282018-08-23 18:40:04 +0200223 [IN_INTERP] = {
Markus Armbrusterf7617d42018-08-23 18:40:07 +0200224 TERMINAL(JSON_INTERP),
225 ['A' ... 'Z'] = IN_INTERP,
226 ['a' ... 'z'] = IN_INTERP,
227 ['0' ... '9'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600228 },
229
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200230 /*
231 * Two start states:
232 * - IN_START recognizes JSON tokens with our string extensions
233 * - IN_START_INTERP additionally recognizes interpolation.
234 */
235 [IN_START ... IN_START_INTERP] = {
Anthony Liguori5ab85582009-11-11 10:39:14 -0600236 ['"'] = IN_DQ_STRING,
237 ['\''] = IN_SQ_STRING,
238 ['0'] = IN_ZERO,
239 ['1' ... '9'] = IN_NONZERO_NUMBER,
240 ['-'] = IN_NEG_NONZERO_NUMBER,
Markus Armbrusterc5461662015-11-25 22:23:26 +0100241 ['{'] = JSON_LCURLY,
242 ['}'] = JSON_RCURLY,
243 ['['] = JSON_LSQUARE,
244 [']'] = JSON_RSQUARE,
245 [','] = JSON_COMMA,
246 [':'] = JSON_COLON,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600247 ['a' ... 'z'] = IN_KEYWORD,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600248 [' '] = IN_WHITESPACE,
249 ['\t'] = IN_WHITESPACE,
250 ['\r'] = IN_WHITESPACE,
251 ['\n'] = IN_WHITESPACE,
252 },
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200253 [IN_START_INTERP]['%'] = IN_INTERP,
Anthony Liguori5ab85582009-11-11 10:39:14 -0600254};
255
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200256void json_lexer_init(JSONLexer *lexer, bool enable_interpolation)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600257{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200258 lexer->start_state = lexer->state = enable_interpolation
259 ? IN_START_INTERP : IN_START;
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100260 lexer->token = g_string_sized_new(3);
Luiz Capitulino03308f62010-05-17 17:50:01 -0300261 lexer->x = lexer->y = 0;
Anthony Liguori5ab85582009-11-11 10:39:14 -0600262}
263
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200264static void json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600265{
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200266 int char_consumed, new_state;
267
Anthony Liguori5ab85582009-11-11 10:39:14 -0600268 lexer->x++;
269 if (ch == '\n') {
270 lexer->x = 0;
271 lexer->y++;
272 }
273
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200274 do {
Markus Armbrusterb8d3b1d2015-11-25 22:23:25 +0100275 assert(lexer->state <= ARRAY_SIZE(json_lexer));
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200276 new_state = json_lexer[lexer->state][(uint8_t)ch];
277 char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
Markus Armbrustera2ec6be2018-08-23 18:39:44 +0200278 if (char_consumed && !flush) {
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100279 g_string_append_c(lexer->token, ch);
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200280 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600281
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200282 switch (new_state) {
Markus Armbrusterc5461662015-11-25 22:23:26 +0100283 case JSON_LCURLY:
284 case JSON_RCURLY:
285 case JSON_LSQUARE:
286 case JSON_RSQUARE:
287 case JSON_COLON:
288 case JSON_COMMA:
Markus Armbruster61030282018-08-23 18:40:04 +0200289 case JSON_INTERP:
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200290 case JSON_INTEGER:
291 case JSON_FLOAT:
292 case JSON_KEYWORD:
293 case JSON_STRING:
Markus Armbruster037f2442018-08-23 18:40:00 +0200294 json_message_process_token(lexer, lexer->token, new_state,
295 lexer->x, lexer->y);
Stefan Weil0b0404b2012-01-09 18:29:51 +0100296 /* fall through */
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200297 case JSON_SKIP:
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100298 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200299 new_state = lexer->start_state;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200300 break;
Blue Swirl33d05392011-03-27 09:07:54 +0000301 case IN_ERROR:
Michael Rothb011f612011-06-01 12:14:58 -0500302 /* XXX: To avoid having previous bad input leaving the parser in an
303 * unresponsive state where we consume unpredictable amounts of
304 * subsequent "good" input, percolate this error state up to the
Markus Armbruster84a56f32018-08-23 18:40:06 +0200305 * parser by emitting a JSON_ERROR token, then reset lexer state.
Michael Rothb011f612011-06-01 12:14:58 -0500306 *
307 * Also note that this handling is required for reliable channel
308 * negotiation between QMP and the guest agent, since chr(0xFF)
309 * is placed at the beginning of certain events to ensure proper
310 * delivery when the channel is in an unknown state. chr(0xFF) is
311 * never a valid ASCII/UTF-8 sequence, so this should reliably
312 * induce an error/flush state.
313 */
Markus Armbruster037f2442018-08-23 18:40:00 +0200314 json_message_process_token(lexer, lexer->token, JSON_ERROR,
315 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100316 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200317 lexer->state = lexer->start_state;
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200318 return;
Paolo Bonzinif7c05272010-05-24 09:39:52 +0200319 default:
320 break;
321 }
322 lexer->state = new_state;
Michael Rothbd3924a2011-06-01 12:14:57 -0500323 } while (!char_consumed && !flush);
Anthony Liguori325601b2011-06-01 12:14:52 -0500324
325 /* Do not let a single token grow to an arbitrarily large size,
326 * this is a security consideration.
327 */
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100328 if (lexer->token->len > MAX_TOKEN_SIZE) {
Markus Armbruster037f2442018-08-23 18:40:00 +0200329 json_message_process_token(lexer, lexer->token, lexer->state,
330 lexer->x, lexer->y);
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100331 g_string_truncate(lexer->token, 0);
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200332 lexer->state = lexer->start_state;
Anthony Liguori325601b2011-06-01 12:14:52 -0500333 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600334}
335
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200336void json_lexer_feed(JSONLexer *lexer, const char *buffer, size_t size)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600337{
338 size_t i;
339
340 for (i = 0; i < size; i++) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200341 json_lexer_feed_char(lexer, buffer[i], false);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600342 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600343}
344
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200345void json_lexer_flush(JSONLexer *lexer)
Anthony Liguori5ab85582009-11-11 10:39:14 -0600346{
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200347 if (lexer->state != lexer->start_state) {
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200348 json_lexer_feed_char(lexer, 0, true);
349 }
Anthony Liguori5ab85582009-11-11 10:39:14 -0600350}
351
352void json_lexer_destroy(JSONLexer *lexer)
353{
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +0100354 g_string_free(lexer->token, true);
Anthony Liguori5ab85582009-11-11 10:39:14 -0600355}