| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2008 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2008 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * NaCl simple RPC over IMC mechanism. | 8 * NaCl simple RPC over IMC mechanism. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 #define BASIC_TYPE_IO_DECLARE(name, impl_type, field, format) \ | 344 #define BASIC_TYPE_IO_DECLARE(name, impl_type, field, format) \ |
| 345 static int name##Get(NaClSrpcImcBuffer* buffer, \ | 345 static int name##Get(NaClSrpcImcBuffer* buffer, \ |
| 346 int allocate_memory, \ | 346 int allocate_memory, \ |
| 347 int read_value, \ | 347 int read_value, \ |
| 348 NaClSrpcArg* arg) { \ | 348 NaClSrpcArg* arg) { \ |
| 349 UNREFERENCED_PARAMETER(allocate_memory); \ | 349 UNREFERENCED_PARAMETER(allocate_memory); \ |
| 350 if (read_value && \ | 350 if (read_value && \ |
| 351 1 != __NaClSrpcImcRead(buffer, \ | 351 1 != __NaClSrpcImcRead(buffer, \ |
| 352 sizeof(impl_type), \ | 352 sizeof(impl_type), \ |
| 353 1, \ | 353 1, \ |
| 354 &arg->u.field)) { \ | 354 &arg->field)) { \ |
| 355 return 0; \ | 355 return 0; \ |
| 356 } \ | 356 } \ |
| 357 return 1; \ | 357 return 1; \ |
| 358 } \ | 358 } \ |
| 359 \ | 359 \ |
| 360 static int name##Put(const NaClSrpcArg* arg, \ | 360 static int name##Put(const NaClSrpcArg* arg, \ |
| 361 int write_value, \ | 361 int write_value, \ |
| 362 NaClSrpcImcBuffer* buffer) { \ | 362 NaClSrpcImcBuffer* buffer) { \ |
| 363 if (write_value) { \ | 363 if (write_value) { \ |
| 364 return 1 == __NaClSrpcImcWrite(&arg->u.field, \ | 364 return 1 == __NaClSrpcImcWrite(&arg->field, \ |
| 365 sizeof(impl_type), \ | 365 sizeof(impl_type), \ |
| 366 1, \ | 366 1, \ |
| 367 buffer); \ | 367 buffer); \ |
| 368 } \ | 368 } \ |
| 369 return 1; \ | 369 return 1; \ |
| 370 } \ | 370 } \ |
| 371 \ | 371 \ |
| 372 static void name##Print(const NaClSrpcArg* arg) { \ | 372 static void name##Print(const NaClSrpcArg* arg) { \ |
| 373 dprintf(("%"format"", arg->u.field)); \ | 373 dprintf(("%"format"", arg->field)); \ |
| 374 } \ | 374 } \ |
| 375 \ | 375 \ |
| 376 static uint32_t name##Length(const NaClSrpcArg* arg, \ | 376 static uint32_t name##Length(const NaClSrpcArg* arg, \ |
| 377 int write_value, \ | 377 int write_value, \ |
| 378 int* handles) { \ | 378 int* handles) { \ |
| 379 UNREFERENCED_PARAMETER(arg); \ | 379 UNREFERENCED_PARAMETER(arg); \ |
| 380 *handles = 0; \ | 380 *handles = 0; \ |
| 381 if (write_value) { \ | 381 if (write_value) { \ |
| 382 return sizeof(impl_type); \ | 382 return sizeof(impl_type); \ |
| 383 } else { \ | 383 } else { \ |
| 384 return 0; \ | 384 return 0; \ |
| 385 } \ | 385 } \ |
| 386 } \ | 386 } \ |
| 387 \ | 387 \ |
| 388 static void name##Free(NaClSrpcArg* arg) { \ | 388 static void name##Free(NaClSrpcArg* arg) { \ |
| 389 UNREFERENCED_PARAMETER(arg); \ | 389 UNREFERENCED_PARAMETER(arg); \ |
| 390 } \ | 390 } \ |
| 391 \ | 391 \ |
| 392 static const ArgEltInterface k##name##IoInterface = { \ | 392 static const ArgEltInterface k##name##IoInterface = { \ |
| 393 name##Get, name##Put, name##Print, name##Length, name##Free \ | 393 name##Get, name##Put, name##Print, name##Length, name##Free \ |
| 394 }; | 394 }; |
| 395 | 395 |
| 396 /* | 396 /* |
| 397 * The basic parameter types. | 397 * The basic parameter types. |
| 398 */ | 398 */ |
| 399 BASIC_TYPE_IO_DECLARE(Bool, char, bval, "1d") | 399 BASIC_TYPE_IO_DECLARE(Bool, char, u.bval, "1d") |
| 400 BASIC_TYPE_IO_DECLARE(Double, double, dval, "f") | 400 BASIC_TYPE_IO_DECLARE(Double, double, u.dval, "f") |
| 401 BASIC_TYPE_IO_DECLARE(Int, int32_t, ival, NACL_PRId32) | 401 BASIC_TYPE_IO_DECLARE(Int, int32_t, u.ival, NACL_PRId32) |
| 402 BASIC_TYPE_IO_DECLARE(Long, int64_t, lval, NACL_PRId64) | 402 BASIC_TYPE_IO_DECLARE(Long, int64_t, u.lval, NACL_PRId64) |
| 403 | 403 |
| 404 #define ARRAY_TYPE_IO_DECLARE(name, impl_type, field, array) \ | 404 #define ARRAY_TYPE_IO_DECLARE(name, impl_type, array) \ |
| 405 static int name##ArrGet(NaClSrpcImcBuffer* buffer, \ | 405 static int name##ArrGet(NaClSrpcImcBuffer* buffer, \ |
| 406 int allocate_memory, \ | 406 int allocate_memory, \ |
| 407 int read_value, \ | 407 int read_value, \ |
| 408 NaClSrpcArg* arg) { \ | 408 NaClSrpcArg* arg) { \ |
| 409 nacl_abi_size_t dim; \ | 409 nacl_abi_size_t dim; \ |
| 410 \ | 410 \ |
| 411 if (1 != __NaClSrpcImcRead(buffer, sizeof(dim), 1, &dim)) { \ | 411 if (1 != __NaClSrpcImcRead(buffer, sizeof(dim), 1, &dim)) { \ |
| 412 return 0; \ | 412 return 0; \ |
| 413 } \ | 413 } \ |
| 414 if (allocate_memory) { \ | 414 if (allocate_memory) { \ |
| 415 if (dim >= NACL_ABI_SIZE_T_MAX / sizeof(*arg->u.field.array)) { \ | 415 if (dim >= NACL_ABI_SIZE_T_MAX / sizeof(*arg->array)) { \ |
| 416 return 0; \ | 416 return 0; \ |
| 417 } \ | 417 } \ |
| 418 arg->u.field.array = \ | 418 arg->array = \ |
| 419 (impl_type*) malloc(dim * sizeof(*arg->u.field.array)); \ | 419 (impl_type*) malloc(dim * sizeof(*arg->array)); \ |
| 420 if (NULL == arg->u.field.array) { \ | 420 if (NULL == arg->array) { \ |
| 421 return 0; \ | 421 return 0; \ |
| 422 } \ | 422 } \ |
| 423 arg->u.field.count = dim; \ | 423 arg->u.count = dim; \ |
| 424 } else if (arg->u.field.count < dim) { \ | 424 } else if (arg->u.count < dim) { \ |
| 425 return 0; \ | 425 return 0; \ |
| 426 } \ | 426 } \ |
| 427 if (read_value && \ | 427 if (read_value && \ |
| 428 dim != __NaClSrpcImcRead(buffer, \ | 428 dim != __NaClSrpcImcRead(buffer, \ |
| 429 sizeof(impl_type), \ | 429 sizeof(impl_type), \ |
| 430 dim, \ | 430 dim, \ |
| 431 arg->u.field.array)) { \ | 431 arg->array)) { \ |
| 432 return 0; \ | 432 return 0; \ |
| 433 } \ | 433 } \ |
| 434 return 1; \ | 434 return 1; \ |
| 435 } \ | 435 } \ |
| 436 \ | 436 \ |
| 437 static int name##ArrPut(const NaClSrpcArg* arg, \ | 437 static int name##ArrPut(const NaClSrpcArg* arg, \ |
| 438 int write_value, \ | 438 int write_value, \ |
| 439 NaClSrpcImcBuffer* buffer) { \ | 439 NaClSrpcImcBuffer* buffer) { \ |
| 440 if (1 != \ | 440 if (1 != \ |
| 441 __NaClSrpcImcWrite(&arg->u.field.count, sizeof(uint32_t), 1, buffer)) { \ | 441 __NaClSrpcImcWrite(&arg->u.count, sizeof(uint32_t), 1, buffer)) { \ |
| 442 return 0; \ | 442 return 0; \ |
| 443 } \ | 443 } \ |
| 444 if (write_value) { \ | 444 if (write_value) { \ |
| 445 return arg->u.field.count == \ | 445 return arg->u.count == \ |
| 446 __NaClSrpcImcWrite(arg->u.field.array, \ | 446 __NaClSrpcImcWrite(arg->array, \ |
| 447 sizeof(impl_type), \ | 447 sizeof(impl_type), \ |
| 448 arg->u.field.count, \ | 448 arg->u.count, \ |
| 449 buffer); \ | 449 buffer); \ |
| 450 } \ | 450 } \ |
| 451 return 1; \ | 451 return 1; \ |
| 452 } \ | 452 } \ |
| 453 \ | 453 \ |
| 454 static void name##ArrPrint(const NaClSrpcArg* arg) { \ | 454 static void name##ArrPrint(const NaClSrpcArg* arg) { \ |
| 455 dprintf(("[%"NACL_PRIu32"], array = %p", \ | 455 dprintf(("[%"NACL_PRIu32"], array = %p", \ |
| 456 arg->u.field.count, \ | 456 arg->u.count, \ |
| 457 (void*) arg->u.field.array)); \ | 457 (void*) arg->array)); \ |
| 458 } \ | 458 } \ |
| 459 \ | 459 \ |
| 460 static uint32_t name##ArrLength(const NaClSrpcArg* arg, \ | 460 static uint32_t name##ArrLength(const NaClSrpcArg* arg, \ |
| 461 int write_value, \ | 461 int write_value, \ |
| 462 int* handles) { \ | 462 int* handles) { \ |
| 463 *handles = 0; \ | 463 *handles = 0; \ |
| 464 if (write_value) { \ | 464 if (write_value) { \ |
| 465 return sizeof(uint32_t) + sizeof(impl_type) * arg->u.field.count; \ | 465 return sizeof(uint32_t) + sizeof(impl_type) * arg->u.count; \ |
| 466 } else { \ | 466 } else { \ |
| 467 return sizeof(uint32_t); \ | 467 return sizeof(uint32_t); \ |
| 468 } \ | 468 } \ |
| 469 } \ | 469 } \ |
| 470 \ | 470 \ |
| 471 static void name##ArrFree(NaClSrpcArg* arg) { \ | 471 static void name##ArrFree(NaClSrpcArg* arg) { \ |
| 472 free(arg->u.field.array); \ | 472 free(arg->array); \ |
| 473 arg->u.field.array = NULL; \ | 473 arg->array = NULL; \ |
| 474 } \ | 474 } \ |
| 475 \ | 475 \ |
| 476 static const ArgEltInterface k##name##ArrIoInterface = { \ | 476 static const ArgEltInterface k##name##ArrIoInterface = { \ |
| 477 name##ArrGet, name##ArrPut, name##ArrPrint, name##ArrLength, name##ArrFree \ | 477 name##ArrGet, name##ArrPut, name##ArrPrint, name##ArrLength, name##ArrFree \ |
| 478 }; | 478 }; |
| 479 | 479 |
| 480 /* | 480 /* |
| 481 * The three array parameter types. | 481 * The three array parameter types. |
| 482 */ | 482 */ |
| 483 ARRAY_TYPE_IO_DECLARE(Char, char, caval, carr) | 483 ARRAY_TYPE_IO_DECLARE(Char, char, arrays.carr) |
| 484 ARRAY_TYPE_IO_DECLARE(Double, double, daval, darr) | 484 ARRAY_TYPE_IO_DECLARE(Double, double, arrays.darr) |
| 485 ARRAY_TYPE_IO_DECLARE(Int, int32_t, iaval, iarr) | 485 ARRAY_TYPE_IO_DECLARE(Int, int32_t, arrays.iarr) |
| 486 ARRAY_TYPE_IO_DECLARE(Long, int64_t, laval, larr) | 486 ARRAY_TYPE_IO_DECLARE(Long, int64_t, arrays.larr) |
| 487 | 487 |
| 488 /* | 488 /* |
| 489 * Handle (descriptor) type I/O support. | 489 * Handle (descriptor) type I/O support. |
| 490 */ | 490 */ |
| 491 static int HandleGet(NaClSrpcImcBuffer* buffer, | 491 static int HandleGet(NaClSrpcImcBuffer* buffer, |
| 492 int allocate_memory, | 492 int allocate_memory, |
| 493 int read_value, | 493 int read_value, |
| 494 NaClSrpcArg* arg) { | 494 NaClSrpcArg* arg) { |
| 495 UNREFERENCED_PARAMETER(allocate_memory); | 495 UNREFERENCED_PARAMETER(allocate_memory); |
| 496 if (read_value) { | 496 if (read_value) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 if (1 != __NaClSrpcImcRead(buffer, sizeof(dim), 1, &dim)) { | 552 if (1 != __NaClSrpcImcRead(buffer, sizeof(dim), 1, &dim)) { |
| 553 return 0; | 553 return 0; |
| 554 } | 554 } |
| 555 /* | 555 /* |
| 556 * check if dim + 1 (in the malloc below) will result in an | 556 * check if dim + 1 (in the malloc below) will result in an |
| 557 * integer overflow | 557 * integer overflow |
| 558 */ | 558 */ |
| 559 if (dim >= NACL_ABI_SIZE_T_MAX) { | 559 if (dim >= NACL_ABI_SIZE_T_MAX) { |
| 560 return 0; | 560 return 0; |
| 561 } | 561 } |
| 562 arg->u.sval.str = (char*) malloc(dim + 1); | 562 arg->arrays.str = (char*) malloc(dim + 1); |
| 563 if (NULL == arg->u.sval.str) { | 563 if (NULL == arg->arrays.str) { |
| 564 return 0; | 564 return 0; |
| 565 } | 565 } |
| 566 if (dim != __NaClSrpcImcRead(buffer, sizeof(char), dim, arg->u.sval.str)) { | 566 if (dim != __NaClSrpcImcRead(buffer, sizeof(char), dim, arg->arrays.str)) { |
| 567 return 0; | 567 return 0; |
| 568 } | 568 } |
| 569 arg->u.sval.str[dim] = '\0'; | 569 arg->arrays.str[dim] = '\0'; |
| 570 } | 570 } |
| 571 return 1; | 571 return 1; |
| 572 } | 572 } |
| 573 | 573 |
| 574 static int StringPut(const NaClSrpcArg* arg, | 574 static int StringPut(const NaClSrpcArg* arg, |
| 575 int write_value, | 575 int write_value, |
| 576 NaClSrpcImcBuffer* buffer) { | 576 NaClSrpcImcBuffer* buffer) { |
| 577 if (write_value) { | 577 if (write_value) { |
| 578 uint32_t slen = (uint32_t) strlen(arg->u.sval.str); | 578 uint32_t slen = (uint32_t) strlen(arg->arrays.str); |
| 579 if (1 != __NaClSrpcImcWrite(&slen, sizeof(slen), 1, buffer) || | 579 if (1 != __NaClSrpcImcWrite(&slen, sizeof(slen), 1, buffer) || |
| 580 slen != __NaClSrpcImcWrite(arg->u.sval.str, 1, | 580 slen != __NaClSrpcImcWrite(arg->arrays.str, 1, |
| 581 (nacl_abi_size_t) slen, buffer)) { | 581 (nacl_abi_size_t) slen, buffer)) { |
| 582 return 0; | 582 return 0; |
| 583 } | 583 } |
| 584 } | 584 } |
| 585 return 1; | 585 return 1; |
| 586 } | 586 } |
| 587 | 587 |
| 588 static void StringPrint(const NaClSrpcArg* arg) { | 588 static void StringPrint(const NaClSrpcArg* arg) { |
| 589 dprintf((", strlen %u, '%s'", (unsigned) strlen(arg->u.sval.str), | 589 dprintf((", strlen %u, '%s'", (unsigned) strlen(arg->arrays.str), |
| 590 arg->u.sval.str)); | 590 arg->arrays.str)); |
| 591 } | 591 } |
| 592 | 592 |
| 593 static uint32_t StringLength(const NaClSrpcArg* arg, | 593 static uint32_t StringLength(const NaClSrpcArg* arg, |
| 594 int write_value, | 594 int write_value, |
| 595 int* handles) { | 595 int* handles) { |
| 596 *handles = 0; | 596 *handles = 0; |
| 597 if (write_value) { | 597 if (write_value) { |
| 598 uint32_t size = nacl_abi_size_t_saturate(sizeof(uint32_t) | 598 uint32_t size = nacl_abi_size_t_saturate(sizeof(uint32_t) |
| 599 + strlen(arg->u.sval.str)); | 599 + strlen(arg->arrays.str)); |
| 600 return size; | 600 return size; |
| 601 } else { | 601 } else { |
| 602 return 0; | 602 return 0; |
| 603 } | 603 } |
| 604 } | 604 } |
| 605 | 605 |
| 606 static void StringFree(NaClSrpcArg* arg) { | 606 static void StringFree(NaClSrpcArg* arg) { |
| 607 free(arg->u.sval.str); | 607 free(arg->arrays.str); |
| 608 arg->u.sval.str = NULL; | 608 arg->arrays.str = NULL; |
| 609 } | 609 } |
| 610 | 610 |
| 611 static const ArgEltInterface kStringIoInterface = { | 611 static const ArgEltInterface kStringIoInterface = { |
| 612 StringGet, StringPut, StringPrint, StringLength, StringFree | 612 StringGet, StringPut, StringPrint, StringLength, StringFree |
| 613 }; | 613 }; |
| 614 | 614 |
| 615 /* | 615 /* |
| 616 * Invalid type I/O support. | 616 * Invalid type I/O support. |
| 617 */ | 617 */ |
| 618 static int InvalidGet(NaClSrpcImcBuffer* buffer, | 618 static int InvalidGet(NaClSrpcImcBuffer* buffer, |
| (...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1204 rpc->is_request = 0; | 1204 rpc->is_request = 0; |
| 1205 if (!RpcWrite(buffer, rpc)) { | 1205 if (!RpcWrite(buffer, rpc)) { |
| 1206 dprintf((SIDE "SRPC: ResponseWrite: flush failed twice -- giving up\n")); | 1206 dprintf((SIDE "SRPC: ResponseWrite: flush failed twice -- giving up\n")); |
| 1207 return; | 1207 return; |
| 1208 } | 1208 } |
| 1209 __NaClSrpcImcFlush(buffer, channel); | 1209 __NaClSrpcImcFlush(buffer, channel); |
| 1210 } | 1210 } |
| 1211 dprintf((SIDE "SRPC: ResponseWrite: sent\n")); | 1211 dprintf((SIDE "SRPC: ResponseWrite: sent\n")); |
| 1212 rpc->ret_send_succeeded = 1; | 1212 rpc->ret_send_succeeded = 1; |
| 1213 } | 1213 } |
| OLD | NEW |