blob: 674dfe6e854db70e849ce95067efbdd46c62c6df [file] [log] [blame]
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -06001/*
2 * JSON streaming support
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 Liguorid7ff3ac2009-11-11 10:38:59 -060015#include "qemu-common.h"
Markus Armbruster84a56f32018-08-23 18:40:06 +020016#include "qapi/error.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/json-lexer.h"
Markus Armbruster62815d82018-08-23 18:40:01 +020018#include "qapi/qmp/json-parser.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010019#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060020
Anthony Liguori29c75dd2011-06-01 12:14:53 -050021#define MAX_TOKEN_SIZE (64ULL << 20)
Markus Armbrusterdf649832015-11-25 22:23:33 +010022#define MAX_TOKEN_COUNT (2ULL << 20)
Anthony Liguori29c75dd2011-06-01 12:14:53 -050023#define MAX_NESTING (1ULL << 10)
24
Eric Blakeba4dba52016-05-18 15:46:52 -060025static void json_message_free_token(void *token, void *opaque)
26{
27 g_free(token);
28}
29
Paolo Bonzini95385fe2015-11-25 22:23:31 +010030static void json_message_free_tokens(JSONMessageParser *parser)
31{
32 if (parser->tokens) {
Eric Blakeba4dba52016-05-18 15:46:52 -060033 g_queue_foreach(parser->tokens, json_message_free_token, NULL);
Paolo Bonzini95385fe2015-11-25 22:23:31 +010034 g_queue_free(parser->tokens);
35 parser->tokens = NULL;
36 }
37}
38
Markus Armbruster037f2442018-08-23 18:40:00 +020039void json_message_process_token(JSONLexer *lexer, GString *input,
40 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060041{
42 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
Markus Armbrusterff281a22018-08-23 18:40:02 +020043 QObject *json = NULL;
Markus Armbruster62815d82018-08-23 18:40:01 +020044 Error *err = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +010045 JSONToken *token;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060046
Markus Armbrusterc5461662015-11-25 22:23:26 +010047 switch (type) {
48 case JSON_LCURLY:
49 parser->brace_count++;
50 break;
51 case JSON_RCURLY:
52 parser->brace_count--;
53 break;
54 case JSON_LSQUARE:
55 parser->bracket_count++;
56 break;
57 case JSON_RSQUARE:
58 parser->bracket_count--;
59 break;
Markus Armbruster269e57a2018-08-23 18:40:03 +020060 case JSON_ERROR:
Markus Armbruster84a56f32018-08-23 18:40:06 +020061 error_setg(&err, "JSON parse error, stray '%s'", input->str);
Markus Armbruster269e57a2018-08-23 18:40:03 +020062 goto out_emit;
Markus Armbrusterf9277912018-08-23 18:40:12 +020063 case JSON_END_OF_INPUT:
64 if (g_queue_is_empty(parser->tokens)) {
65 return;
66 }
67 json = json_parser_parse(parser->tokens, parser->ap, &err);
68 parser->tokens = NULL;
69 goto out_emit;
Markus Armbrusterc5461662015-11-25 22:23:26 +010070 default:
71 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060072 }
73
Paolo Bonzini9bada892015-11-25 22:23:32 +010074 token = g_malloc(sizeof(JSONToken) + input->len + 1);
75 token->type = type;
76 memcpy(token->str, input->str, input->len);
77 token->str[input->len] = 0;
78 token->x = x;
79 token->y = y;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060080
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010081 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050082
Paolo Bonzini9bada892015-11-25 22:23:32 +010083 g_queue_push_tail(parser->tokens, token);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060084
Markus Armbrusterff281a22018-08-23 18:40:02 +020085 if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050086 parser->bracket_count < 0 ||
87 (parser->brace_count == 0 &&
88 parser->bracket_count == 0)) {
Markus Armbrusterff281a22018-08-23 18:40:02 +020089 json = json_parser_parse(parser->tokens, parser->ap, &err);
90 parser->tokens = NULL;
Michael Roth5e2dafe2011-06-01 12:14:59 -050091 goto out_emit;
Markus Armbrusterff281a22018-08-23 18:40:02 +020092 }
93
Markus Armbruster84a56f32018-08-23 18:40:06 +020094 /*
95 * Security consideration, we limit total memory allocated per object
96 * and the maximum recursion depth that a message can force.
97 */
98 if (parser->token_size > MAX_TOKEN_SIZE) {
99 error_setg(&err, "JSON token size limit exceeded");
100 goto out_emit;
101 }
102 if (g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT) {
103 error_setg(&err, "JSON token count limit exceeded");
104 goto out_emit;
105 }
106 if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
107 error_setg(&err, "JSON nesting depth limit exceeded");
Markus Armbrusterff281a22018-08-23 18:40:02 +0200108 goto out_emit;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600109 }
Michael Roth5e2dafe2011-06-01 12:14:59 -0500110
111 return;
112
Michael Roth5e2dafe2011-06-01 12:14:59 -0500113out_emit:
Michael Roth5e2dafe2011-06-01 12:14:59 -0500114 parser->brace_count = 0;
115 parser->bracket_count = 0;
Markus Armbrusterff281a22018-08-23 18:40:02 +0200116 json_message_free_tokens(parser);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100117 parser->tokens = g_queue_new();
Michael Roth5e2dafe2011-06-01 12:14:59 -0500118 parser->token_size = 0;
Markus Armbruster62815d82018-08-23 18:40:01 +0200119 parser->emit(parser->opaque, json, err);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600120}
121
122void json_message_parser_init(JSONMessageParser *parser,
Markus Armbruster62815d82018-08-23 18:40:01 +0200123 void (*emit)(void *opaque, QObject *json,
124 Error *err),
125 void *opaque, va_list *ap)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600126{
Markus Armbruster62815d82018-08-23 18:40:01 +0200127 parser->emit = emit;
128 parser->opaque = opaque;
129 parser->ap = ap;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600130 parser->brace_count = 0;
131 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100132 parser->tokens = g_queue_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500133 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600134
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200135 json_lexer_init(&parser->lexer, !!ap);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600136}
137
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200138void json_message_parser_feed(JSONMessageParser *parser,
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600139 const char *buffer, size_t size)
140{
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200141 json_lexer_feed(&parser->lexer, buffer, size);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600142}
143
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200144void json_message_parser_flush(JSONMessageParser *parser)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600145{
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200146 json_lexer_flush(&parser->lexer);
Markus Armbrusterf9277912018-08-23 18:40:12 +0200147 assert(g_queue_is_empty(parser->tokens));
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600148}
149
150void json_message_parser_destroy(JSONMessageParser *parser)
151{
152 json_lexer_destroy(&parser->lexer);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100153 json_message_free_tokens(parser);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600154}