/* Copies code from src to dest in a thread safe way, returns 1 on success,
 * returns 0 on error. This will likely assert on error to avoid partially
 * copied code or undefined state.
 */
static int NCCopyCode(uint8_t *dst, uint8_t *src, NaClPcAddress vbase,
                      size_t sz, NaClCopyInstructionFunc copy_func) {
  NCDecoderState dst_dstate;
  NCDecoderInst  dst_inst;
  NCDecoderState src_dstate;
  NCDecoderInst  src_inst;
  NCDecoderStatePair pair;
  int result = 0;

  NCDecoderStateConstruct(&dst_dstate, dst, vbase, sz, &dst_inst, 1);
  NCDecoderStateConstruct(&src_dstate, src, vbase, sz, &src_inst, 1);
  NCDecoderStatePairConstruct(&pair, &dst_dstate, &src_dstate, copy_func);
  pair.action_fn = CopyInstruction;
  if (NCDecoderStatePairDecode(&pair)) result = 1;
  NCDecoderStatePairDestruct(&pair);
  NCDecoderStateDestruct(&src_dstate);
  NCDecoderStateDestruct(&dst_dstate);

  return result;
}
void NCDecodeSegment(uint8_t* mbase, NaClPcAddress vbase,
                     NaClMemorySize size) {
  NCDecoderInst inst;
  NCDecoderState dstate;
  NCDecoderStateConstruct(&dstate, mbase, vbase, size, &inst, 1);
  NCDecoderStateSetErrorReporter(&dstate, &kNCVerboseErrorReporter);
  /* TODO(karl): Fix this so that we don't need to override the
   * action function.
   */
  dstate.action_fn = PrintInstLogGio;
  NCDecoderStateDecode(&dstate);
  NCDecoderStateDestruct(&dstate);
}
Exemple #3
0
/* Create the decoder state for the validator state, using the
 * given parameters.
 */
static void NCValidateDStateInit(NCValidatorState *vstate,
                                 uint8_t *mbase, NaClPcAddress vbase,
                                 NaClMemorySize sz) {
  NCDecoderState* dstate = &vstate->dstate;
  /* Note: Based on the current API, we must grab the error reporter
   * and reinstall it, after the call to NCValidateDStateInit, so that
   * we don't replace it with the default error reporter.
   */
  NaClErrorReporter* reporter = dstate->error_reporter;
  NCDecoderStateConstruct(dstate, mbase, vbase, sz,
                          vstate->inst_buffer, kNCValidatorInstBufferSize);
  dstate->action_fn = ValidateInst;
  dstate->new_segment_fn = (NCDecoderStateMethod) NCStatsNewSegment;
  dstate->segmentation_error_fn = (NCDecoderStateMethod) NCStatsSegFault;
  dstate->internal_error_fn = (NCDecoderStateMethod) NCStatsInternalError;
  NCDecoderStateSetErrorReporter(dstate, reporter);
}