2 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
7 #include "native_client/src/trusted/validator/x86/halt_trim.h"
10 #include "native_client/src/trusted/validator/x86/ncinstbuffer.h"
12 /* Safety buffer size of halts we must keep, so that we guarantee
13 * that we don't trim the last legal instruction in the
14 * code segment. Note: we add 1 to the maximum instruction length just
17 static const NaClMemorySize kMinHaltKeepLength = MAX_INST_LENGTH + 1;
20 static const uint8_t kNaClHalt = 0xf4;
22 NaClMemorySize NCHaltTrimSize(uint8_t *mbase, NaClMemorySize sz,
25 NaClMemorySize num_halts;
26 for (i = sz - 1; i > 0; --i) {
27 if (kNaClHalt != mbase[i]) break;
29 num_halts = sz - (i + 1);
30 if (num_halts > kMinHaltKeepLength) {
31 /* May be able to trim off trailing halts. */
32 NaClMemorySize new_size;
33 NaClMemorySize block_overflow;
34 new_size = (sz - num_halts) + kMinHaltKeepLength;
35 /* Round to nearest block alignment. */
36 block_overflow = new_size % alignment;
38 new_size = new_size - block_overflow + alignment;
40 /* Never increase the segment size. */
45 /* No trim performed. */